source: branches/0.17.x/abcl/src/org/armedbear/lisp/StringInputStream.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/*
2 * StringInputStream.java
3 *
4 * Copyright (C) 2003-2004 Peter Graves
5 * $Id: StringInputStream.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
34package org.armedbear.lisp;
35
36import java.io.StringReader;
37
38public final class StringInputStream extends Stream
39{
40    private final StringReader stringReader;
41    private final int start;
42   
43    public StringInputStream(String s)
44    {
45        this(s, 0, s.length());
46    }
47
48    public StringInputStream(String s, int start)
49    {
50        this(s, start, s.length());
51    }
52
53    public StringInputStream(String s, int start, int end)
54    {
55        elementType = Symbol.CHARACTER;
56        setExternalFormat(keywordDefault);
57        eolStyle = EolStyle.RAW;
58
59        this.start = start;
60       
61        stringReader = new StringReader(s.substring(start, end));
62        initAsCharacterInputStream(stringReader);
63    }
64
65    @Override
66    public LispObject typeOf()
67    {
68        return Symbol.STRING_INPUT_STREAM;
69    }
70
71    @Override
72    public LispObject classOf()
73    {
74        return BuiltInClass.STRING_INPUT_STREAM;
75    }
76
77    @Override
78    public LispObject typep(LispObject type)
79    {
80        if (type == Symbol.STRING_INPUT_STREAM)
81            return T;
82        if (type == Symbol.STRING_STREAM)
83            return T;
84        if (type == BuiltInClass.STRING_INPUT_STREAM)
85            return T;
86        if (type == BuiltInClass.STRING_STREAM)
87            return T;
88        return super.typep(type);
89    }
90
91    @Override
92    public String toString()
93    {
94        return unreadableString("STRING-INPUT-STREAM");
95    }
96
97    @Override
98    public int getOffset() {
99        return start + super.getOffset();
100    }
101   
102    // ### make-string-input-stream
103    // make-string-input-stream string &optional start end => string-stream
104    private static final Primitive MAKE_STRING_INPUT_STREAM =
105        new Primitive("make-string-input-stream", "string &optional start end")
106    {
107        @Override
108        public LispObject execute(LispObject arg)
109        {
110            return new StringInputStream(arg.getStringValue());
111        }
112
113        @Override
114        public LispObject execute(LispObject first, LispObject second)
115
116        {
117            String s = first.getStringValue();
118            int start = Fixnum.getValue(second);
119            return new StringInputStream(s, start);
120        }
121
122        @Override
123        public LispObject execute(LispObject first, LispObject second,
124                                  LispObject third)
125
126        {
127            String s = first.getStringValue();
128            int start = Fixnum.getValue(second);
129            if (third == NIL)
130                return new StringInputStream(s, start);
131            int end = Fixnum.getValue(third);
132            return new StringInputStream(s, start, end);
133        }
134    };
135
136    // ### string-input-stream-current
137    private static final Primitive STRING_INPUT_STREAM_CURRENT =
138        new Primitive("string-input-stream-current", PACKAGE_EXT, true, "stream")
139    {
140        @Override
141        public LispObject execute(LispObject arg)
142        {
143            if (arg instanceof StringInputStream)
144                return Fixnum.getInstance(((StringInputStream)arg).getOffset());
145            return error(new TypeError(String.valueOf(arg) +
146                                        " is not a string input stream."));
147        }
148    };
149}
Note: See TracBrowser for help on using the repository browser.