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

Last change on this file was 15139, checked in by Mark Evenson, 4 years ago

Fix ANSI-TESTS FILE-POSITION.10
(Douglas Miles)

Addreses <https://github.com/armedbear/abcl/issues/101>.

From <https://github.com/TeamSPoon/abcl/commit/ff57ff5d4d13c6a31cb15b949d3b6be6f2b5f2e4>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/*
2 * StringOutputStream.java
3 *
4 * Copyright (C) 2002-2004 Peter Graves
5 * $Id: StringOutputStream.java 15139 2019-11-01 15:52:59Z 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.StringWriter;
40
41public final class StringOutputStream extends Stream
42{
43    private final SeekableStringWriter stringWriter;
44
45    public StringOutputStream()
46    {
47        this(Symbol.CHARACTER);
48    }
49
50    StringOutputStream(LispObject elementType)
51    {
52        super(Symbol.STRING_OUTPUT_STREAM);
53        this.elementType = elementType;
54        this.eolStyle = EolStyle.RAW;
55        initAsCharacterOutputStream(stringWriter = new SeekableStringWriter());
56    }
57
58    @Override
59    public LispObject typeOf()
60    {
61        return Symbol.STRING_OUTPUT_STREAM;
62    }
63
64    @Override
65    public LispObject classOf()
66    {
67        return BuiltInClass.STRING_OUTPUT_STREAM;
68    }
69
70    @Override
71    public LispObject typep(LispObject type)
72    {
73        if (type == Symbol.STRING_OUTPUT_STREAM)
74            return T;
75        if (type == Symbol.STRING_STREAM)
76            return T;
77        if (type == BuiltInClass.STRING_OUTPUT_STREAM)
78            return T;
79        if (type == BuiltInClass.STRING_STREAM)
80            return T;
81        return super.typep(type);
82    }
83
84    @Override
85    protected long _getFilePosition()
86    {
87        if (elementType == NIL)
88            return 0;
89        return stringWriter.getOffset();
90    }
91
92    @Override
93    protected boolean _setFilePosition(LispObject arg) {
94        if (elementType == NIL)
95            return false;
96
97        try {
98            int offset;
99
100            if (arg == Keyword.START)
101                offset = 0;
102            else if (arg == Keyword.END)
103                offset = stringWriter.getBuffer().length();
104            else {
105                long n = Fixnum.getValue(arg);
106                offset = (int) n; // FIXME arg might be a bignum
107            }
108
109            stringWriter.seek(offset);
110
111            // FixME super.offset needs to be maintained differently?
112            this.offset = offset;
113        }
114        catch (IllegalArgumentException e) {
115            error(new StreamError(this, e));
116        }
117
118        return true;
119    }
120
121
122    public LispObject getString() {
123      if (elementType == NIL) {
124        return new NilVector(0);
125      }
126
127      StringBuffer sb = stringWriter.getBuffer();
128      SimpleString s = new SimpleString(sb);
129      sb.setLength(0);
130      return s;
131    }
132
133    public LispObject getSimpleStringAndClear()
134    {
135        if (elementType == NIL)
136            return new NilVector(0);
137        String contents = stringWriter.toStringAndClear();
138       
139        return new SimpleString(contents);
140    }
141
142    // ### %make-string-output-stream
143    // %make-string-output-stream element-type => string-stream
144    private static final Primitive MAKE_STRING_OUTPUT_STREAM =
145        new Primitive("%make-string-output-stream", PACKAGE_SYS, false,
146                       "element-type")
147    {
148        @Override
149        public LispObject execute(LispObject arg)
150        {
151            return new StringOutputStream(arg);
152        }
153    };
154
155    // ### get-output-stream-string
156    // get-output-stream-string string-output-stream => string
157    private static final Primitive GET_OUTPUT_STREAM_STRING =
158        new Primitive("get-output-stream-string", "string-output-stream")
159    {
160        @Override
161        public LispObject execute(LispObject arg)
162        {
163          if (arg instanceof StringOutputStream) {
164            return ((StringOutputStream)arg).getSimpleStringAndClear();
165          }
166          return type_error(this, Symbol.STRING_OUTPUT_STREAM);
167        }
168    };
169}
Note: See TracBrowser for help on using the repository browser.