1 21 22 package org.armedbear.lisp; 23 24 public final class StringInputStream extends Stream 25 { 26 final String s; 27 final int start; 28 final int end; 29 30 public StringInputStream(String s) 31 { 32 this(s, 0, s.length()); 33 } 34 35 public StringInputStream(String s, int start) 36 { 37 this(s, start, s.length()); 38 } 39 40 public StringInputStream(String s, int start, int end) 41 { 42 elementType = Symbol.CHARACTER; 43 isInputStream = true; 44 isOutputStream = false; 45 isCharacterStream = true; 46 isBinaryStream = false; 47 this.s = s; 48 this.start = start; 49 this.end = end; 50 offset = start; 51 } 52 53 public LispObject typeOf() 54 { 55 return Symbol.STRING_INPUT_STREAM; 56 } 57 58 public LispClass classOf() 59 { 60 return BuiltInClass.STRING_INPUT_STREAM; 61 } 62 63 public LispObject typep(LispObject type) throws ConditionThrowable 64 { 65 if (type == Symbol.STRING_INPUT_STREAM) 66 return T; 67 if (type == Symbol.STRING_STREAM) 68 return T; 69 if (type == BuiltInClass.STRING_INPUT_STREAM) 70 return T; 71 if (type == BuiltInClass.STRING_STREAM) 72 return T; 73 return super.typep(type); 74 } 75 76 public LispObject close(LispObject abort) throws ConditionThrowable 77 { 78 setOpen(false); 79 return T; 80 } 81 82 public LispObject listen() 83 { 84 return offset < end ? T : NIL; 85 } 86 87 protected int _readChar() 88 { 89 if (offset >= end) 90 return -1; 91 int n = s.charAt(offset); 92 ++offset; 93 if (n == '\n') 94 ++lineNumber; 95 return n; 96 } 97 98 protected void _unreadChar(int n) 99 { 100 if (offset > start) { 101 --offset; 102 if (n == '\n') 103 --lineNumber; 104 } 105 } 106 107 protected boolean _charReady() 108 { 109 return true; 110 } 111 112 public String toString() 113 { 114 return unreadableString("STRING-INPUT-STREAM"); 115 } 116 117 private static final Primitive MAKE_STRING_INPUT_STREAM = 120 new Primitive("make-string-input-stream", "string &optional start end") 121 { 122 public LispObject execute(LispObject arg) throws ConditionThrowable 123 { 124 return new StringInputStream(arg.getStringValue()); 125 } 126 127 public LispObject execute(LispObject first, LispObject second) 128 throws ConditionThrowable 129 { 130 String s = first.getStringValue(); 131 int start = Fixnum.getValue(second); 132 return new StringInputStream(s, start); 133 } 134 135 public LispObject execute(LispObject first, LispObject second, 136 LispObject third) 137 throws ConditionThrowable 138 { 139 String s = first.getStringValue(); 140 int start = Fixnum.getValue(second); 141 if (third == NIL) 142 return new StringInputStream(s, start); 143 int end = Fixnum.getValue(third); 144 return new StringInputStream(s, start, end); 145 } 146 }; 147 148 private static final Primitive1 STRING_INPUT_STREAM_CURRENT = 150 new Primitive1("string-input-stream-current", PACKAGE_EXT, true, "stream") 151 { 152 public LispObject execute(LispObject arg) throws ConditionThrowable 153 { 154 if (arg instanceof StringInputStream) 155 return new Fixnum(((StringInputStream)arg).getOffset()); 156 return signal(new TypeError(String.valueOf(arg) + 157 " is not a string input stream.")); 158 } 159 }; 160 } 161 | Popular Tags |