01: package kawa.standard;
02:
03: import gnu.text.Char;
04: import gnu.lists.*;
05: import java.io.Reader;
06: import java.io.InputStream;
07: import gnu.mapping.Procedure0or1;
08: import gnu.mapping.WrongType;
09: import gnu.mapping.InPort;
10:
11: public class readchar extends Procedure0or1 {
12: public static final readchar readChar = new readchar(false);
13: public static final readchar peekChar = new readchar(true);
14:
15: boolean peeking;
16:
17: public readchar(boolean peeking) {
18: super (peeking ? "peek-char" : "read-char");
19: this .peeking = peeking;
20: }
21:
22: final Object readChar(InPort port) {
23: try {
24: int ch = peeking ? port.peek() : port.read();
25: if (ch < 0)
26: return Sequence.eofValue;
27: return Char.make(ch);
28: } catch (java.io.IOException e) {
29: throw new RuntimeException("IO Exception caught");
30: }
31: }
32:
33: final Object readChar(Reader port) {
34: try {
35: int ch;
36: if (peeking) {
37: port.mark(1);
38: ch = port.read();
39: port.reset();
40: } else
41: ch = port.read();
42: if (ch < 0)
43: return Sequence.eofValue;
44: return Char.make(ch);
45: } catch (java.io.IOException e) {
46: throw new RuntimeException("IO Exception caught");
47: }
48: }
49:
50: final Object readChar(InputStream port) {
51: try {
52: int ch;
53: if (peeking) {
54: port.mark(1);
55: ch = port.read();
56: port.reset();
57: } else
58: ch = port.read();
59: if (ch < 0)
60: return Sequence.eofValue;
61: return Char.make(ch);
62: } catch (java.io.IOException e) {
63: throw new RuntimeException("IO Exception caught");
64: }
65: }
66:
67: public final Object apply0() {
68: return readChar(InPort.inDefault());
69: }
70:
71: public final Object apply1(Object arg1) {
72: if (arg1 instanceof InPort)
73: return readChar((InPort) arg1);
74: if (arg1 instanceof Reader)
75: return readChar((Reader) arg1);
76: if (arg1 instanceof InputStream)
77: return readChar((InputStream) arg1);
78: throw new WrongType(this , 1, arg1, "<input-port>");
79: }
80: }
|