KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > StringInputStream


1 /*
2  * StringInputStream.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: StringInputStream.java,v 1.15 2004/03/11 11:29:04 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 Stream
25 {
26     final String JavaDoc s;
27     final int start;
28     final int end;
29
30     public StringInputStream(String JavaDoc s)
31     {
32         this(s, 0, s.length());
33     }
34
35     public StringInputStream(String JavaDoc s, int start)
36     {
37         this(s, start, s.length());
38     }
39
40     public StringInputStream(String JavaDoc 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 JavaDoc toString()
113     {
114         return unreadableString("STRING-INPUT-STREAM");
115     }
116
117     // ### make-string-input-stream
118
// make-string-input-stream string &optional start end => string-stream
119
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 JavaDoc 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 JavaDoc 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     // ### string-input-stream-current
149
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