source: trunk/abcl/src/org/armedbear/lisp/StringInputStream.java

Last change on this file was 14857, checked in by Mark Evenson, 8 years ago

[PATCH 5/5] Support FILE-POSITION on string streams.
From cb667c106187443ff2d00bace14f0ee0686fe2fd Mon Sep 17 00:00:00 2001
Adds a custom, seekable writer to be able to go back in the written
output for STRING-OUTPUT-STREAM - the input case is slightly less
complex.
---

build.xml | 1 +
src/org/armedbear/lisp/SeekableStringWriter.java | 140 +++++++++++++++++++++
src/org/armedbear/lisp/StringInputStream.java | 43 ++++++-
src/org/armedbear/lisp/StringOutputStream.java | 35 +++++-
test/lisp/abcl/misc-tests.lisp | 11 +-
.../armedbear/lisp/SeekableStringWriterTest.java | 19 +++
6 files changed, 242 insertions(+), 7 deletions(-)
create mode 100644 src/org/armedbear/lisp/SeekableStringWriter.java
create mode 100644 test/src/org/armedbear/lisp/SeekableStringWriterTest.java

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/*
2 * StringInputStream.java
3 *
4 * Copyright (C) 2003-2004 Peter Graves
5 * $Id: StringInputStream.java 14857 2016-09-04 07:01:02Z mevenson $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38import java.io.IOException;
39import java.io.StringReader;
40
41public final class StringInputStream extends Stream
42{
43    private final StringReader stringReader;
44    private final int start;
45    private final String subString;
46   
47    public StringInputStream(String s)
48    {
49        this(s, 0, s.length());
50    }
51
52    public StringInputStream(String s, int start)
53    {
54        this(s, start, s.length());
55    }
56
57    public StringInputStream(String s, int start, int end)
58    {
59        super(Symbol.STRING_INPUT_STREAM);
60        elementType = Symbol.CHARACTER;
61        setExternalFormat(keywordDefault);
62        eolStyle = EolStyle.RAW;
63
64        this.start = start;
65
66        subString = s.substring(start, end);
67        stringReader = new StringReader(subString);
68        initAsCharacterInputStream(stringReader);
69    }
70
71    @Override
72    public LispObject typeOf()
73    {
74        return Symbol.STRING_INPUT_STREAM;
75    }
76
77    @Override
78    public LispObject classOf()
79    {
80        return BuiltInClass.STRING_INPUT_STREAM;
81    }
82
83    @Override
84    public LispObject typep(LispObject type)
85    {
86        if (type == Symbol.STRING_INPUT_STREAM)
87            return T;
88        if (type == Symbol.STRING_STREAM)
89            return T;
90        if (type == BuiltInClass.STRING_INPUT_STREAM)
91            return T;
92        if (type == BuiltInClass.STRING_STREAM)
93            return T;
94        return super.typep(type);
95    }
96
97    @Override
98    public int getOffset() {
99        return start + offset;
100    }
101
102    @Override
103    protected long _getFilePosition() {
104        return getOffset();
105    }
106
107    @Override
108    protected boolean _setFilePosition(LispObject arg) {
109        try {
110            int offset;
111
112            if (arg == Keyword.START)
113                offset = 0;
114            else if (arg == Keyword.END)
115                offset = subString.length();
116            else {
117                long n = Fixnum.getValue(arg);
118                if (n < 0 || n > subString.length())
119                    error(new StreamError(this, "FILE-POSITION got out of bounds argument."));
120                offset = (int) n; // FIXME arg might be a bignum
121            }
122
123            stringReader.reset();
124            stringReader.skip(offset);
125            initAsCharacterInputStream(stringReader);
126
127            this.offset = offset;
128        }
129        catch (IOException e) {
130            error(new StreamError(this, e));
131        }
132
133        return true;
134    }
135   
136    // ### make-string-input-stream
137    // make-string-input-stream string &optional start end => string-stream
138    private static final Primitive MAKE_STRING_INPUT_STREAM =
139        new Primitive("make-string-input-stream", "string &optional start end")
140    {
141        @Override
142        public LispObject execute(LispObject arg)
143        {
144            return new StringInputStream(arg.getStringValue());
145        }
146
147        @Override
148        public LispObject execute(LispObject first, LispObject second)
149
150        {
151            String s = first.getStringValue();
152            int start = Fixnum.getValue(second);
153            return new StringInputStream(s, start);
154        }
155
156        @Override
157        public LispObject execute(LispObject first, LispObject second,
158                                  LispObject third)
159
160        {
161            String s = first.getStringValue();
162            int start = Fixnum.getValue(second);
163            if (third == NIL)
164                return new StringInputStream(s, start);
165            int end = Fixnum.getValue(third);
166            return new StringInputStream(s, start, end);
167        }
168    };
169
170    // ### string-input-stream-current
171    private static final Primitive STRING_INPUT_STREAM_CURRENT =
172        new Primitive("string-input-stream-current", PACKAGE_EXT, true, "stream")
173    {
174        @Override
175        public LispObject execute(LispObject arg)
176        {
177            if (arg instanceof StringInputStream)
178                return Fixnum.getInstance(((StringInputStream)arg).getOffset());
179            return error(new TypeError(String.valueOf(arg) +
180                                        " is not a string input stream."));
181        }
182    };
183}
Note: See TracBrowser for help on using the repository browser.