Have FileInputStream ignore carriage returns by intercepting before returning to...
authorjjenista <jjenista>
Fri, 27 Feb 2009 23:52:00 +0000 (23:52 +0000)
committerjjenista <jjenista>
Fri, 27 Feb 2009 23:52:00 +0000 (23:52 +0000)
Robust/src/ClassLibrary/FileInputStream.java
Robust/src/Runtime/file.c
Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java
Robust/src/Tests/FileInputStreamTest/charmap.txt [new file with mode: 0644]

index da717979f9b7ca445f275f72289c3ed0940bef09..eeda05dd57fad945f1f83b8922188f03b83dda6e 100644 (file)
@@ -14,16 +14,28 @@ public class FileInputStream extends InputStream {
 
   private static native int nativeOpen(byte[] filename);
   private static native int nativeRead(int fd, byte[] array, int numBytes);
+  private static native int nativePeek(int fd);
   private static native void nativeClose(int fd);
 
   public int read() {
     byte b[]=new byte[1];
     int retval=read(b);
-    if (retval==-1)
+    if (retval==-1 || retval==0)
       return -1;
+
+    // if carriage return comes back, dump it
+    if( b[0] == 13 ) {
+      return read();
+    }
+
+    // otherwise return result
     return b[0];
   }
 
+  public int peek() {
+    return nativePeek(fd);
+  }
+
   public int read(byte[] b) {
     return nativeRead(fd, b, b.length);
   }
@@ -39,10 +51,20 @@ public class FileInputStream extends InputStream {
       return null;
     }
 
-    while( c != '\n' && c > 0 ) {
+    // ASCII 13 is carriage return, check for that also
+    while( c != '\n' && c != 13 && c > 0 ) {
       line += (char)c;
       c = read();
-    } 
+    }
+
+    // peek and consume characters that are carriage 
+    // returns or line feeds so the whole line is read
+    // and returned, and none of the line-ending chars
+    c = peek();
+    while( c == '\n' || c == 13 ) {
+      c = read();
+      c = peek();
+    }
 
     return line;
   }
index ae4366cf2ab003e7c78029658a61cfb797883a6e..7125335dee1327b3660b02d90e1cf93c0b1e82b4 100644 (file)
@@ -62,6 +62,18 @@ int CALL23(___FileInputStream______nativeRead____I__AR_B_I, int fd, int numBytes
   return status;
 }
 
+int CALL11(___FileInputStream______nativePeek____I, int fd, int fd) {
+  int status;
+  char string[1];
+  status=read(fd, string, 1);
+
+  if( status <= 0 ) {
+    return status;
+  }
+  lseek(fd, -1, SEEK_CUR);
+  return string[0];
+}
+
 long long CALL01(___File______nativeLength_____AR_B, struct ArrayObject * ___pathname___) {
   int length=VAR(___pathname___)->___length___;
   char* filename= (((char *)&VAR(___pathname___)->___length___)+sizeof(int));
index 7a32b575137b87c35ef87a42c2eaac77e49c75bc..aa901e2126bbae6ab6d55e236345aa6ba6a30025 100644 (file)
@@ -9,6 +9,38 @@ public class FileInputStreamTest {
       line = in.readLine();
     } 
     System.out.println( "#####################" );
+    System.out.println( "["+in.read()+"]" );
+    System.out.println( "["+in.read()+"]" );
+    in.close();
+
+    
+    in = new FileInputStream( "charmap.txt" );
+    System.out.println( "\n\n\n#####################" );
+    line = in.readLine();
+    while( line != null ) {
+      System.out.println( line );
+      for( int i = 0; i < line.length(); ++i ) {
+       System.out.print( line.charAt( i )+"." );
+      }
+      System.out.println( "" );
+      line = in.readLine();
+    } 
+    System.out.println( "#####################" );
+    System.out.println( "["+in.read()+"]" );
+    System.out.println( "["+in.read()+"]" );
+    in.close();
+
+
+    in = new FileInputStream( "test.txt" );
+    System.out.println( "\n\n\n#####################" );
+    int c = in.read();
+    while( c != -1 ) {
+      System.out.print( c+"." );
+      c = in.read();
+    } 
+    System.out.println( "#####################" );
+    System.out.println( "["+in.read()+"]" );
+    System.out.println( "["+in.read()+"]" );
     in.close();
   }
 }
\ No newline at end of file
diff --git a/Robust/src/Tests/FileInputStreamTest/charmap.txt b/Robust/src/Tests/FileInputStreamTest/charmap.txt
new file mode 100644 (file)
index 0000000..d37e8ec
--- /dev/null
@@ -0,0 +1,12 @@
+# basic characters\r
+<char:linebreak><index:22>\r
+\r
+# dots\r
+<char:cdot><index:22>\r
+\r
+# quotes\r
+<char:quote><index:22>\r
+<char:quoteleft><index:22>\r
+<char:quoteright><index:22>\r
+<char:quotedblleft><index:22>\r
+<char:quotedblright><index:22>\r