01: /*
02: * StringInputStream.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: StringInputStream.java,v 1.6 2003/11/15 11:03:35 beedlem Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: public final class StringInputStream extends CharacterInputStream {
25: final String s;
26: final int start;
27: final int end;
28:
29: public StringInputStream(String s) {
30: this (s, 0, s.length());
31: }
32:
33: public StringInputStream(String s, int start) {
34: this (s, start, s.length());
35: }
36:
37: public StringInputStream(String s, int start, int end) {
38: this .s = s;
39: this .start = start;
40: this .end = end;
41: offset = start;
42: }
43:
44: public LispClass classOf() {
45: return BuiltInClass.STRING_STREAM;
46: }
47:
48: protected int read() {
49: if (offset >= end)
50: return -1;
51: int n = s.charAt(offset);
52: ++offset;
53: if (n == '\n')
54: ++lineNumber;
55: return n;
56: }
57:
58: protected void unread(int n) {
59: if (offset > start) {
60: --offset;
61: if (n == '\n')
62: --lineNumber;
63: }
64: }
65:
66: protected boolean ready() {
67: return true;
68: }
69:
70: // ### make-string-input-stream
71: // make-string-input-stream string &optional start end => string-stream
72: private static final Primitive MAKE_STRING_INPUT_STREAM = new Primitive(
73: "make-string-input-stream") {
74: public LispObject execute(LispObject arg)
75: throws ConditionThrowable {
76: return new StringInputStream(LispString.getValue(arg));
77: }
78:
79: public LispObject execute(LispObject first, LispObject second)
80: throws ConditionThrowable {
81: String s = LispString.getValue(first);
82: int start = Fixnum.getValue(second);
83: return new StringInputStream(s.substring(start));
84: }
85:
86: public LispObject execute(LispObject first, LispObject second,
87: LispObject third) throws ConditionThrowable {
88: String s = LispString.getValue(first);
89: int start = Fixnum.getValue(second);
90: if (third == NIL)
91: return new StringInputStream(s.substring(start));
92: int end = Fixnum.getValue(third);
93: return new StringInputStream(s.substring(start, end));
94: }
95: };
96: }
|