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