X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fchr%2FCharInput.java;h=b6a175d66f50865d663bf9ccd564e9478bae6b1b;hp=f86e85f68aa0b8a54317ddffa4448a9f3449196d;hb=62be1c1ae2ac13086b508e34eeffdb7e536d5b61;hpb=225993309e6183afa9a88fc13d39df56be54b992 diff --git a/src/edu/berkeley/sbp/chr/CharInput.java b/src/edu/berkeley/sbp/chr/CharInput.java index f86e85f..b6a175d 100644 --- a/src/edu/berkeley/sbp/chr/CharInput.java +++ b/src/edu/berkeley/sbp/chr/CharInput.java @@ -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,48 @@ public class CharInput extends Cartesian.Input { 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 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 (start < 0) start = 0; + if (end < start) end = start; + 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"); + } + }