1 | /* |
---|
2 | * SlimeInputStream.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Andras Simon, Peter Graves |
---|
5 | * $Id: SlimeInputStream.java 12254 2009-11-06 20:07:54Z ehuelsmann $ |
---|
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 | public class SlimeInputStream extends Stream |
---|
37 | { |
---|
38 | String s; |
---|
39 | int length; |
---|
40 | final Function f; |
---|
41 | final Stream ostream; |
---|
42 | |
---|
43 | public SlimeInputStream(Function f, Stream ostream) |
---|
44 | { |
---|
45 | elementType = Symbol.CHARACTER; |
---|
46 | isInputStream = true; |
---|
47 | isOutputStream = false; |
---|
48 | isCharacterStream = true; |
---|
49 | isBinaryStream = false; |
---|
50 | this.f = f; |
---|
51 | this.ostream = ostream; |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public LispObject typeOf() |
---|
56 | { |
---|
57 | return Symbol.SLIME_INPUT_STREAM; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public LispObject classOf() |
---|
62 | { |
---|
63 | return BuiltInClass.SLIME_INPUT_STREAM; |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public LispObject typep(LispObject type) |
---|
68 | { |
---|
69 | if (type == Symbol.SLIME_INPUT_STREAM) |
---|
70 | return T; |
---|
71 | if (type == Symbol.STRING_STREAM) |
---|
72 | return T; |
---|
73 | if (type == BuiltInClass.SLIME_INPUT_STREAM) |
---|
74 | return T; |
---|
75 | if (type == BuiltInClass.STRING_STREAM) |
---|
76 | return T; |
---|
77 | return super.typep(type); |
---|
78 | } |
---|
79 | |
---|
80 | @Override |
---|
81 | public LispObject close(LispObject abort) |
---|
82 | { |
---|
83 | setOpen(false); |
---|
84 | return T; |
---|
85 | } |
---|
86 | |
---|
87 | @Override |
---|
88 | public LispObject listen() |
---|
89 | { |
---|
90 | return offset < length ? T : NIL; |
---|
91 | } |
---|
92 | |
---|
93 | @Override |
---|
94 | protected int _readChar() |
---|
95 | { |
---|
96 | if (offset >= length) { |
---|
97 | try { |
---|
98 | ostream.finishOutput(); |
---|
99 | s = LispThread.currentThread().execute(f).getStringValue(); |
---|
100 | } |
---|
101 | catch (Throwable t) { |
---|
102 | return -1; |
---|
103 | } |
---|
104 | if (s.length() == 0) |
---|
105 | return -1; |
---|
106 | offset = 0; |
---|
107 | length = s.length(); |
---|
108 | } |
---|
109 | int n = s.charAt(offset); |
---|
110 | ++offset; |
---|
111 | if (n == '\n') |
---|
112 | ++lineNumber; |
---|
113 | return n; |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | protected void _unreadChar(int n) |
---|
118 | { |
---|
119 | if (offset > 0) { |
---|
120 | --offset; |
---|
121 | if (n == '\n') |
---|
122 | --lineNumber; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | @Override |
---|
127 | protected boolean _charReady() |
---|
128 | { |
---|
129 | return offset < length ? true : false; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | @Override |
---|
134 | public void _clearInput() |
---|
135 | { |
---|
136 | super._clearInput(); |
---|
137 | s = ""; |
---|
138 | offset = 0; |
---|
139 | length = 0; |
---|
140 | lineNumber = 0; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | @Override |
---|
145 | public String toString() |
---|
146 | { |
---|
147 | return unreadableString("SLIME-INPUT-STREAM"); |
---|
148 | } |
---|
149 | |
---|
150 | // ### make-slime-input-stream |
---|
151 | // make-slime-input-stream function output-stream => slime-input-stream |
---|
152 | private static final Primitive MAKE_SLIME_INPUT_STREAM = |
---|
153 | new Primitive("make-slime-input-stream", PACKAGE_EXT, true, |
---|
154 | "function output-stream") |
---|
155 | { |
---|
156 | @Override |
---|
157 | public LispObject execute(LispObject first, LispObject second) |
---|
158 | |
---|
159 | { |
---|
160 | final Function fun; |
---|
161 | final Stream os; |
---|
162 | if (first instanceof Symbol) |
---|
163 | fun = (Function)first.getSymbolFunction(); |
---|
164 | else |
---|
165 | fun = (Function)first; |
---|
166 | os = checkCharacterOutputStream(second); |
---|
167 | return new SlimeInputStream(fun, os); |
---|
168 | } |
---|
169 | }; |
---|
170 | } |
---|