1 package kawa.standard; 2 import gnu.text.Char; 3 import gnu.lists.*; 4 import java.io.Reader ; 5 import java.io.InputStream ; 6 import gnu.mapping.Procedure0or1; 7 import gnu.mapping.WrongType; 8 import gnu.mapping.InPort; 9 10 public class readchar extends Procedure0or1 11 { 12 public static final readchar readChar = new readchar(false); 13 public static final readchar peekChar = new readchar(true); 14 15 boolean peeking; 16 public readchar(boolean peeking) 17 { 18 super(peeking ? "peek-char" : "read-char"); 19 this.peeking = peeking; 20 } 21 22 final Object readChar (InPort port) 23 { 24 try 25 { 26 int ch = peeking ? port.peek() : port.read(); 27 if (ch < 0) 28 return Sequence.eofValue; 29 return Char.make (ch); 30 } 31 catch (java.io.IOException e) 32 { 33 throw new RuntimeException ("IO Exception caught"); 34 } 35 } 36 37 final Object readChar (Reader port) 38 { 39 try 40 { 41 int ch; 42 if (peeking) 43 { 44 port.mark(1); 45 ch = port.read(); 46 port.reset(); 47 } 48 else 49 ch = port.read(); 50 if (ch < 0) 51 return Sequence.eofValue; 52 return Char.make (ch); 53 } 54 catch (java.io.IOException e) 55 { 56 throw new RuntimeException ("IO Exception caught"); 57 } 58 } 59 60 final Object readChar (InputStream port) 61 { 62 try 63 { 64 int ch; 65 if (peeking) 66 { 67 port.mark(1); 68 ch = port.read(); 69 port.reset(); 70 } 71 else 72 ch = port.read(); 73 if (ch < 0) 74 return Sequence.eofValue; 75 return Char.make (ch); 76 } 77 catch (java.io.IOException e) 78 { 79 throw new RuntimeException ("IO Exception caught"); 80 } 81 } 82 83 public final Object apply0 () 84 { 85 return readChar (InPort.inDefault()); 86 } 87 88 public final Object apply1 (Object arg1) 89 { 90 if (arg1 instanceof InPort) 91 return readChar ((InPort) arg1); 92 if (arg1 instanceof Reader ) 93 return readChar ((Reader ) arg1); 94 if (arg1 instanceof InputStream ) 95 return readChar ((InputStream ) arg1); 96 throw new WrongType (this, 1, arg1, "<input-port>"); 97 } 98 } 99 | Popular Tags |