checkpoint
[sbp.git] / src / edu / berkeley / sbp / chr / CharInput.java
index f86e85f..65b2b3e 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp.chr;
 import java.io.*;
 import java.util.*;
@@ -13,20 +15,46 @@ public class CharInput extends Cartesian.Input<Character> {
     
     public CharInput(String s)                { this(new StringReader(s)); }
     public CharInput(Reader r)                { this(r, null); }
-    public CharInput(Reader r,      String s) { this.r = r; }
+    public CharInput(Reader r,      String s) { this.r = new BufferedReader(r); }
     public CharInput(InputStream i)           { this(i, null); }
     public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); }
     
     boolean cr = false;
     private int count = 0;
+    private StringBuilder cache = new StringBuilder();
+    
+    public void setCacheEnabled(boolean enabled) {
+        if (!enabled) cache = null;
+        else if (cache == null) cache = new StringBuilder();
+    }
+
     public boolean   isCR() { return cr; }
-    public Character next() throws IOException {
+    public Character _next() throws IOException {
         cr = false;
         int i = r.read();
-        if (i==-1) { System.err.print("\r...done       \r"); return null; }
+        if (i==-1) { /*System.err.print("\r...done       \r"); */return null; }
         char c = (char)i;
+        if (cache != null) cache.append(c);
         cr = c=='\n';
-        System.err.print("  " + (count++) + "\r");
+        /*
+        if ((count++) % 100 == 0)
+         System.err.print("  " + count + "\r");
+        */
         return c;
     }
+
+    public String showRegion(Region<Character> rc) {
+        if (cache == null) return null;
+        Cartesian.Region r = (Cartesian.Region)rc;
+        int start = r.getStart().getScalar()+1;
+        int end = r.getEnd().getScalar()+1;
+        if (end > cache.length()) end = cache.length();
+        String ret;
+        if (end-start < 60) ret = cache.substring(start, end);
+        else ret = cache.substring(start, start+25) +
+                 "..." +
+                 cache.substring(end-25, end);
+        return StringUtil.escapify(ret, "\n\r");
+    }
+
 }