58
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Stream subclass:#PeekableStream
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
PeekableStream comment:'
|
88
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
58
|
24 |
|
88
|
25 |
!PeekableStream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
58
|
30 |
All Rights Reserved
|
|
31 |
|
88
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
58
|
40 |
|
88
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/PeekableStream.st,v 1.4 1994-06-02 16:21:13 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
documentation
|
|
48 |
"
|
|
49 |
abstract superclass for all Stream which support read-ahead
|
|
50 |
(i.e. peeking) of one element.
|
|
51 |
Concrete subclasses must implement a peek method.
|
|
52 |
"
|
|
53 |
! !
|
58
|
54 |
|
|
55 |
!PeekableStream methodsFor:'reading'!
|
|
56 |
|
|
57 |
peek
|
|
58 |
"return the next element of the stream without advancing (i.e.
|
|
59 |
the following send of next will return this element again.)
|
|
60 |
- we do not know here how to do it, it must be redefined in subclass"
|
|
61 |
|
|
62 |
^ self subclassResponsibility
|
|
63 |
!
|
|
64 |
|
|
65 |
peekFor:anObject
|
|
66 |
"if the next-to-be-read object is equal to the argument, anObject, read
|
|
67 |
and return it. Otherwise, leave the receiver unaffected"
|
|
68 |
|
|
69 |
self peek = anObject ifTrue:[
|
|
70 |
self next.
|
|
71 |
^ anObject
|
|
72 |
].
|
|
73 |
^ false
|
|
74 |
!
|
|
75 |
|
|
76 |
nextPeek
|
|
77 |
"advance to next element and return the peeked element"
|
|
78 |
|
|
79 |
self next.
|
|
80 |
^ self peek
|
|
81 |
!
|
|
82 |
|
68
|
83 |
skipAny:skipCollection
|
|
84 |
"skip all characters included in the argument-set.
|
|
85 |
returns the next peeked element or nil, if the end-of-stream was reached."
|
|
86 |
|
|
87 |
|nextOne|
|
|
88 |
|
|
89 |
nextOne := self peek.
|
|
90 |
[nextOne notNil and:[skipCollection includes:nextOne]] whileTrue:[
|
|
91 |
self next.
|
|
92 |
nextOne := self peek
|
|
93 |
].
|
|
94 |
^ nextOne
|
|
95 |
|
|
96 |
"
|
|
97 |
|s skipChars|
|
|
98 |
|
|
99 |
s := ReadStream on:'some numbers1234with\in other99 stuff' withCRs.
|
|
100 |
skipChars := 'abcdefghijklmnopqrstuvwxyz\ ' withCRs.
|
|
101 |
s skipAny:skipChars.
|
|
102 |
Transcript showCr:(Integer readFrom:s).
|
|
103 |
s skipAny:skipChars.
|
|
104 |
Transcript showCr:(Integer readFrom:s).
|
|
105 |
"
|
|
106 |
!
|
|
107 |
|
|
108 |
skipSpaces
|
|
109 |
"skip all spaces; returns the next peeked element or
|
|
110 |
nil, if the end-of-stream was reached.
|
|
111 |
The streams elements should be characters.
|
|
112 |
Notice: this one skips only spaces (i.e. no cr, tabs etc)
|
|
113 |
usually, skipSeparators is what you want."
|
|
114 |
|
|
115 |
|nextOne|
|
|
116 |
|
|
117 |
nextOne := self peek.
|
|
118 |
[nextOne notNil and:[nextOne == Character space]] whileTrue:[
|
|
119 |
self next.
|
|
120 |
nextOne := self peek
|
|
121 |
].
|
|
122 |
^ nextOne
|
|
123 |
|
|
124 |
"
|
|
125 |
|s|
|
|
126 |
|
|
127 |
s := ReadStream on:'one two\three' withCRs.
|
|
128 |
s skipSpaces.
|
|
129 |
Transcript showCr:(s nextWord).
|
|
130 |
s skipSpaces.
|
|
131 |
Transcript showCr:(s nextWord).
|
|
132 |
s skipSpaces.
|
|
133 |
Transcript showCr:(s next displayString).
|
|
134 |
"
|
|
135 |
!
|
|
136 |
|
58
|
137 |
skipSeparators
|
|
138 |
"skip all whitespace; returns the next peeked element or
|
|
139 |
nil, if the end-of-stream was reached.
|
68
|
140 |
The streams elements should be characters.
|
|
141 |
Notice: compare this method to skipSpaces"
|
58
|
142 |
|
|
143 |
|nextOne|
|
|
144 |
|
|
145 |
nextOne := self peek.
|
|
146 |
[nextOne notNil and:[nextOne isSeparator]] whileTrue:[
|
|
147 |
self next.
|
|
148 |
nextOne := self peek
|
|
149 |
].
|
|
150 |
^ nextOne
|
68
|
151 |
"
|
|
152 |
|s|
|
|
153 |
|
|
154 |
s := ReadStream on:'one two\three' withCRs.
|
|
155 |
s skipSeparators.
|
|
156 |
Transcript showCr:(s nextWord).
|
|
157 |
s skipSeparators.
|
|
158 |
Transcript showCr:(s nextWord).
|
|
159 |
s skipSeparators.
|
|
160 |
Transcript showCr:(s next displayString).
|
|
161 |
"
|
58
|
162 |
!
|
|
163 |
|
|
164 |
skipSeparatorsExceptCR
|
|
165 |
"skip all whitespace except carriage return; returns the
|
|
166 |
next peeked element or nil, if the end-of-stream was reached.
|
68
|
167 |
The streams elements should be characters.
|
|
168 |
Notice: compare this method to skipSpaces and skipSeparators"
|
58
|
169 |
|
|
170 |
|nextOne|
|
|
171 |
|
|
172 |
nextOne := self peek.
|
|
173 |
[nextOne notNil
|
|
174 |
and:[nextOne isSeparator
|
|
175 |
and:[nextOne ~~ Character cr]]] whileTrue:[
|
|
176 |
self next.
|
|
177 |
nextOne := self peek
|
|
178 |
].
|
|
179 |
^ nextOne
|
|
180 |
!
|
|
181 |
|
68
|
182 |
upTo:anObject
|
|
183 |
"return a collection of the elements up-to
|
|
184 |
(but excluding) the argument, element.
|
77
|
185 |
The next read operation will return anObject.
|
68
|
186 |
Return everything up to the end, if the stream-end is reached before."
|
|
187 |
|
|
188 |
|answerStream element|
|
|
189 |
|
|
190 |
answerStream := WriteStream on:(self contentsSpecies new).
|
|
191 |
[self atEnd] whileFalse: [
|
77
|
192 |
element := self peek.
|
68
|
193 |
(element = anObject) ifTrue: [^answerStream contents].
|
|
194 |
answerStream nextPut: element.
|
77
|
195 |
self next
|
68
|
196 |
].
|
|
197 |
^ answerStream contents
|
|
198 |
|
|
199 |
"(ReadStream on:'1234567890') upTo:$5"
|
|
200 |
"(ReadStream on:'123456') upTo:$7"
|
|
201 |
"(ReadStream on:#(1 2 3 4 5 6)) upTo:4"
|
|
202 |
"(ReadStream on:'line 1
|
|
203 |
line 2') upTo:Character cr "
|
|
204 |
"'Makefile' asFilename readStream upTo:Character cr"
|
|
205 |
!
|
|
206 |
|
|
207 |
upToSeparator
|
|
208 |
"Return the next elements up to but not including the next separator.
|
|
209 |
The elements are supposed to understand isSeparator."
|
|
210 |
|
|
211 |
|answerStream element|
|
|
212 |
|
|
213 |
answerStream := WriteStream on:(self contentsSpecies new).
|
|
214 |
[self atEnd] whileFalse: [
|
77
|
215 |
element := self peek.
|
68
|
216 |
element isSeparator ifTrue: [^answerStream contents].
|
|
217 |
answerStream nextPut: element.
|
77
|
218 |
self next.
|
68
|
219 |
].
|
|
220 |
^ answerStream contents
|
|
221 |
!
|
|
222 |
|
58
|
223 |
nextDecimalInteger
|
|
224 |
"read the next integer in radix 10. dont skip whitespace.
|
68
|
225 |
The streams elements should be characters."
|
58
|
226 |
|
|
227 |
|nextOne value|
|
|
228 |
|
|
229 |
nextOne := self peek.
|
|
230 |
value := 0.
|
|
231 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
|
|
232 |
value := (value * 10) + nextOne digitValue.
|
|
233 |
nextOne := self nextPeek
|
|
234 |
].
|
|
235 |
^ value
|
|
236 |
! !
|