From: jjenista <jjenista>
Date: Fri, 27 Feb 2009 23:52:00 +0000 (+0000)
Subject: Have FileInputStream ignore carriage returns by intercepting before returning to... 
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=31016ad22bca142ed3a5f2acf95f0f72637a59b2;p=IRC.git

Have FileInputStream ignore carriage returns by intercepting before returning to user
---

diff --git a/Robust/src/ClassLibrary/FileInputStream.java b/Robust/src/ClassLibrary/FileInputStream.java
index da717979..eeda05dd 100644
--- a/Robust/src/ClassLibrary/FileInputStream.java
+++ b/Robust/src/ClassLibrary/FileInputStream.java
@@ -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;
   }
diff --git a/Robust/src/Runtime/file.c b/Robust/src/Runtime/file.c
index ae4366cf..7125335d 100644
--- a/Robust/src/Runtime/file.c
+++ b/Robust/src/Runtime/file.c
@@ -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));
diff --git a/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java b/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java
index 7a32b575..aa901e21 100644
--- a/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java
+++ b/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java
@@ -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
index 00000000..d37e8ec0
--- /dev/null
+++ b/Robust/src/Tests/FileInputStreamTest/charmap.txt
@@ -0,0 +1,12 @@
+# basic characters
+<char:linebreak><index:22>
+
+# dots
+<char:cdot><index:22>
+
+# quotes
+<char:quote><index:22>
+<char:quoteleft><index:22>
+<char:quoteright><index:22>
+<char:quotedblleft><index:22>
+<char:quotedblright><index:22>