| 1 | /* |
|---|
| 2 | * SlimeInputStream.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 Andras Simon, Peter Graves |
|---|
| 5 | * $Id: SlimeInputStream.java 14148 2012-09-02 18:11:30Z 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 | 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 | eolStyle = EolStyle.LF; |
|---|
| 54 | this.f = f; |
|---|
| 55 | this.ostream = ostream; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | @Override |
|---|
| 59 | public LispObject typeOf() |
|---|
| 60 | { |
|---|
| 61 | return Symbol.SLIME_INPUT_STREAM; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | @Override |
|---|
| 65 | public LispObject classOf() |
|---|
| 66 | { |
|---|
| 67 | return BuiltInClass.SLIME_INPUT_STREAM; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | @Override |
|---|
| 71 | public LispObject typep(LispObject type) |
|---|
| 72 | { |
|---|
| 73 | if (type == Symbol.SLIME_INPUT_STREAM) |
|---|
| 74 | return T; |
|---|
| 75 | if (type == Symbol.STRING_STREAM) |
|---|
| 76 | return T; |
|---|
| 77 | if (type == BuiltInClass.SLIME_INPUT_STREAM) |
|---|
| 78 | return T; |
|---|
| 79 | if (type == BuiltInClass.STRING_STREAM) |
|---|
| 80 | return T; |
|---|
| 81 | return super.typep(type); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | @Override |
|---|
| 85 | public LispObject close(LispObject abort) |
|---|
| 86 | { |
|---|
| 87 | setOpen(false); |
|---|
| 88 | return T; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | @Override |
|---|
| 92 | public LispObject listen() |
|---|
| 93 | { |
|---|
| 94 | return offset < length ? T : NIL; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | @Override |
|---|
| 98 | protected int _readChar() |
|---|
| 99 | { |
|---|
| 100 | if (offset >= length) { |
|---|
| 101 | ostream.finishOutput(); |
|---|
| 102 | s = LispThread.currentThread().execute(f).getStringValue(); |
|---|
| 103 | if (s.length() == 0) |
|---|
| 104 | return -1; |
|---|
| 105 | offset = 0; |
|---|
| 106 | length = s.length(); |
|---|
| 107 | } |
|---|
| 108 | int n = s.charAt(offset); |
|---|
| 109 | ++offset; |
|---|
| 110 | if (n == '\n') |
|---|
| 111 | ++lineNumber; |
|---|
| 112 | return n; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | @Override |
|---|
| 116 | protected void _unreadChar(int n) |
|---|
| 117 | { |
|---|
| 118 | if (offset > 0) { |
|---|
| 119 | --offset; |
|---|
| 120 | if (n == '\n') |
|---|
| 121 | --lineNumber; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | @Override |
|---|
| 126 | protected boolean _charReady() |
|---|
| 127 | { |
|---|
| 128 | return offset < length ? true : false; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | @Override |
|---|
| 133 | public void _clearInput() |
|---|
| 134 | { |
|---|
| 135 | super._clearInput(); |
|---|
| 136 | s = ""; |
|---|
| 137 | offset = 0; |
|---|
| 138 | length = 0; |
|---|
| 139 | lineNumber = 0; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | // ### make-slime-input-stream |
|---|
| 143 | // make-slime-input-stream function output-stream => slime-input-stream |
|---|
| 144 | private static final Primitive MAKE_SLIME_INPUT_STREAM = |
|---|
| 145 | new Primitive("make-slime-input-stream", PACKAGE_EXT, true, |
|---|
| 146 | "function output-stream") |
|---|
| 147 | { |
|---|
| 148 | @Override |
|---|
| 149 | public LispObject execute(LispObject first, LispObject second) |
|---|
| 150 | |
|---|
| 151 | { |
|---|
| 152 | final Function fun; |
|---|
| 153 | final Stream os; |
|---|
| 154 | if (first instanceof Symbol) |
|---|
| 155 | fun = (Function)first.getSymbolFunction(); |
|---|
| 156 | else |
|---|
| 157 | fun = (Function)first; |
|---|
| 158 | os = checkCharacterOutputStream(second); |
|---|
| 159 | return new SlimeInputStream(fun, os); |
|---|
| 160 | } |
|---|
| 161 | }; |
|---|
| 162 | } |
|---|