1 | /* |
---|
2 | * SlimeInputStream.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Andras Simon, Peter Graves |
---|
5 | * $Id: SlimeInputStream.java 12362 2010-01-11 20:03:29Z vvoutilainen $ |
---|
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 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public class SlimeInputStream extends Stream |
---|
39 | { |
---|
40 | String s; |
---|
41 | int length; |
---|
42 | final Function f; |
---|
43 | final Stream ostream; |
---|
44 | |
---|
45 | public SlimeInputStream(Function f, Stream ostream) |
---|
46 | { |
---|
47 | super(Symbol.SLIME_INPUT_STREAM); |
---|
48 | elementType = Symbol.CHARACTER; |
---|
49 | isInputStream = true; |
---|
50 | isOutputStream = false; |
---|
51 | isCharacterStream = true; |
---|
52 | isBinaryStream = false; |
---|
53 | this.f = f; |
---|
54 | this.ostream = ostream; |
---|
55 | } |
---|
56 | |
---|
57 | @Override |
---|
58 | public LispObject typeOf() |
---|
59 | { |
---|
60 | return Symbol.SLIME_INPUT_STREAM; |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public LispObject classOf() |
---|
65 | { |
---|
66 | return BuiltInClass.SLIME_INPUT_STREAM; |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public LispObject typep(LispObject type) |
---|
71 | { |
---|
72 | if (type == Symbol.SLIME_INPUT_STREAM) |
---|
73 | return T; |
---|
74 | if (type == Symbol.STRING_STREAM) |
---|
75 | return T; |
---|
76 | if (type == BuiltInClass.SLIME_INPUT_STREAM) |
---|
77 | return T; |
---|
78 | if (type == BuiltInClass.STRING_STREAM) |
---|
79 | return T; |
---|
80 | return super.typep(type); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public LispObject close(LispObject abort) |
---|
85 | { |
---|
86 | setOpen(false); |
---|
87 | return T; |
---|
88 | } |
---|
89 | |
---|
90 | @Override |
---|
91 | public LispObject listen() |
---|
92 | { |
---|
93 | return offset < length ? T : NIL; |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | protected int _readChar() |
---|
98 | { |
---|
99 | if (offset >= length) { |
---|
100 | ostream.finishOutput(); |
---|
101 | s = LispThread.currentThread().execute(f).getStringValue(); |
---|
102 | if (s.length() == 0) |
---|
103 | return -1; |
---|
104 | offset = 0; |
---|
105 | length = s.length(); |
---|
106 | } |
---|
107 | int n = s.charAt(offset); |
---|
108 | ++offset; |
---|
109 | if (n == '\n') |
---|
110 | ++lineNumber; |
---|
111 | return n; |
---|
112 | } |
---|
113 | |
---|
114 | @Override |
---|
115 | protected void _unreadChar(int n) |
---|
116 | { |
---|
117 | if (offset > 0) { |
---|
118 | --offset; |
---|
119 | if (n == '\n') |
---|
120 | --lineNumber; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | @Override |
---|
125 | protected boolean _charReady() |
---|
126 | { |
---|
127 | return offset < length ? true : false; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | @Override |
---|
132 | public void _clearInput() |
---|
133 | { |
---|
134 | super._clearInput(); |
---|
135 | s = ""; |
---|
136 | offset = 0; |
---|
137 | length = 0; |
---|
138 | lineNumber = 0; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | @Override |
---|
143 | public String toString() |
---|
144 | { |
---|
145 | return unreadableString("SLIME-INPUT-STREAM"); |
---|
146 | } |
---|
147 | |
---|
148 | // ### make-slime-input-stream |
---|
149 | // make-slime-input-stream function output-stream => slime-input-stream |
---|
150 | private static final Primitive MAKE_SLIME_INPUT_STREAM = |
---|
151 | new Primitive("make-slime-input-stream", PACKAGE_EXT, true, |
---|
152 | "function output-stream") |
---|
153 | { |
---|
154 | @Override |
---|
155 | public LispObject execute(LispObject first, LispObject second) |
---|
156 | |
---|
157 | { |
---|
158 | final Function fun; |
---|
159 | final Stream os; |
---|
160 | if (first instanceof Symbol) |
---|
161 | fun = (Function)first.getSymbolFunction(); |
---|
162 | else |
---|
163 | fun = (Function)first; |
---|
164 | os = checkCharacterOutputStream(second); |
---|
165 | return new SlimeInputStream(fun, os); |
---|
166 | } |
---|
167 | }; |
---|
168 | } |
---|