1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
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 |
Object subclass:#Stream
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
Stream comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
5
|
25 |
$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.4 1993-10-13 02:14:01 claus Exp $
|
1
|
26 |
'!
|
|
27 |
|
|
28 |
!Stream class methodsFor:'instance creation'!
|
|
29 |
|
|
30 |
new
|
|
31 |
"report an error - Streams are created using on:-messages"
|
|
32 |
|
|
33 |
^ self error:'Streams cannot be created with new'
|
|
34 |
! !
|
|
35 |
|
|
36 |
!Stream methodsFor:'accessing'!
|
|
37 |
|
|
38 |
contents
|
|
39 |
"return the contents of the stream
|
|
40 |
- we do not know here how to do it, it must be redefined in subclass"
|
|
41 |
|
|
42 |
^ self subclassResponsibility
|
|
43 |
! !
|
|
44 |
|
|
45 |
!Stream methodsFor:'accessing-reading'!
|
|
46 |
|
|
47 |
next
|
|
48 |
"return the next element of the stream
|
|
49 |
- we do not know here how to do it, it must be redefined in subclass"
|
|
50 |
|
|
51 |
^ self subclassResponsibility
|
|
52 |
!
|
|
53 |
|
|
54 |
next:count
|
|
55 |
"return the next count elements of the stream as a Collection"
|
|
56 |
|
|
57 |
|array|
|
|
58 |
|
|
59 |
array := Array new:count.
|
|
60 |
1 to:count do: [:index |
|
|
61 |
array at:index put:(self next)
|
|
62 |
].
|
|
63 |
^ array
|
|
64 |
!
|
|
65 |
|
|
66 |
nextPeek
|
|
67 |
"advance to next element and return the peeked element"
|
|
68 |
|
|
69 |
self next.
|
|
70 |
^ self peek
|
|
71 |
!
|
|
72 |
|
|
73 |
nextMatchFor:anObject
|
|
74 |
"read from the receiver, searching for the argument, anObject.
|
2
|
75 |
if the end is reached, return nil; otherwise return the argument,
|
|
76 |
anObject. The next read operation will return the element after anObject."
|
1
|
77 |
|
|
78 |
|nextElement|
|
|
79 |
|
|
80 |
[self atEnd] whileFalse:[
|
|
81 |
nextElement := self next.
|
|
82 |
(nextElement == anObject) ifTrue: [ ^ nextElement ]
|
|
83 |
].
|
|
84 |
^ nil
|
2
|
85 |
!
|
|
86 |
|
|
87 |
skipFor:anObject
|
|
88 |
"skip all objects up-to and including anObject; return the element after anObject."
|
|
89 |
|
|
90 |
(self nextMatchFor:anObject) notNil ifTrue:[
|
|
91 |
^ self next
|
|
92 |
].
|
|
93 |
^ nil
|
|
94 |
!
|
|
95 |
|
|
96 |
skipThrough:anObject
|
|
97 |
"skip all objects up-to and including anObject. The next read operation
|
|
98 |
will return the element after anObject."
|
|
99 |
|
|
100 |
^ self nextMatchFor:anObject
|
1
|
101 |
! !
|
|
102 |
|
|
103 |
!Stream methodsFor:'accessing-writing'!
|
|
104 |
|
|
105 |
nextPut:anObject
|
|
106 |
"put the argument, anObject onto the receiver
|
|
107 |
- we do not know here how to do it, it must be redefined in subclass"
|
|
108 |
|
|
109 |
^ self subclassResponsibility
|
|
110 |
!
|
|
111 |
|
|
112 |
next:count put:anObject
|
|
113 |
"put the argument, anObject count times onto the receiver"
|
|
114 |
|
|
115 |
count timesRepeat:[self nextPut:anObject].
|
|
116 |
^ anObject
|
|
117 |
!
|
|
118 |
|
|
119 |
nextPutAll:aCollection
|
|
120 |
"put all elements of the argument, aCollection onto the receiver"
|
|
121 |
|
|
122 |
aCollection do:[:element |
|
|
123 |
self nextPut:element
|
|
124 |
].
|
|
125 |
^ aCollection
|
|
126 |
!
|
|
127 |
|
|
128 |
nextPutAll:aCollection startingAt:first to:last
|
|
129 |
"put the elements with index from first to last
|
|
130 |
of the argument, aCollection onto the receiver"
|
|
131 |
|
|
132 |
aCollection from:first to:last do:[:element |
|
|
133 |
self nextPut:element
|
|
134 |
].
|
|
135 |
^ aCollection
|
|
136 |
!
|
|
137 |
|
2
|
138 |
endEntry
|
|
139 |
"for compatibility with Transcript"
|
|
140 |
|
|
141 |
^ self
|
|
142 |
!
|
|
143 |
|
1
|
144 |
show:aString
|
|
145 |
"put all elements of the argument, aString onto the receiver;
|
|
146 |
this makes streams somewhat compatible to TextCollectors and
|
|
147 |
allows you to say: Smalltalk at:#Transcript put:Stdout"
|
|
148 |
|
|
149 |
^ self nextPutAll:aString printString
|
|
150 |
!
|
|
151 |
|
|
152 |
showCr:aString
|
|
153 |
"put all elements of the argument, aString onto the receiver;
|
|
154 |
and append a newline.
|
|
155 |
for compatibility with TextCollectors"
|
|
156 |
|
|
157 |
self show:aString.
|
|
158 |
self cr
|
|
159 |
! !
|
|
160 |
|
|
161 |
!Stream methodsFor:'closing'!
|
|
162 |
|
|
163 |
close
|
|
164 |
"close the stream - nothing done here"
|
|
165 |
|
|
166 |
^ self
|
|
167 |
! !
|
|
168 |
|
|
169 |
!Stream methodsFor:'testing'!
|
|
170 |
|
|
171 |
atEnd
|
|
172 |
"return true if the end of the stream has been reached;
|
|
173 |
- we do not know here how to do it, it must be redefined in subclass"
|
|
174 |
|
|
175 |
^ self subclassResponsibility
|
|
176 |
! !
|
|
177 |
|
|
178 |
!Stream methodsFor:'enumerating'!
|
|
179 |
|
|
180 |
do:aBlock
|
|
181 |
"evaluate the argument, aBlock for every element up to the end of the
|
|
182 |
stream"
|
|
183 |
|
|
184 |
[self atEnd] whileFalse:[
|
|
185 |
aBlock value:(self next)
|
|
186 |
]
|
|
187 |
! !
|
|
188 |
|
|
189 |
!Stream methodsFor:'queries'!
|
|
190 |
|
|
191 |
isStream
|
|
192 |
"return true, if the receiver is some kind of Stream."
|
|
193 |
|
|
194 |
^ true
|
|
195 |
!
|
|
196 |
|
|
197 |
lineLength
|
|
198 |
"this is just a layout hint for prettyprinting functions
|
|
199 |
- for compatibility with TextCollectors"
|
|
200 |
|
|
201 |
^ 80
|
|
202 |
! !
|
2
|
203 |
|
|
204 |
!Stream methodsFor: 'nonhomogeneous accessing'!
|
|
205 |
|
|
206 |
nextNumber: n
|
|
207 |
"Answer the next n bytes as a positive Integer or LargePositiveInteger."
|
|
208 |
|
|
209 |
| s i |
|
|
210 |
n <= 4 ifTrue:[
|
|
211 |
s _ 0.
|
|
212 |
i _ 0.
|
|
213 |
[(i _ i + 1) <= n] whileTrue: [s _ ((s bitShift: 8) bitOr: self next)].
|
|
214 |
^s
|
|
215 |
].
|
|
216 |
s _ 0.
|
|
217 |
1 to: n do: [:j | s := s * 256 + self next].
|
|
218 |
"reverse order of significance"
|
|
219 |
^s truncated
|
|
220 |
!
|
|
221 |
|
|
222 |
nextNumber: n put: v
|
|
223 |
"Append to the receiver the argument, v, which is a positive SmallInteger or
|
|
224 |
a LargePositiveInteger, as the next n bytes. Possibly pad with leading zeros."
|
|
225 |
|
|
226 |
|
|
227 |
| vlen i |
|
|
228 |
n < (vlen _ v digitLength) ifTrue: [self error: 'number too big'].
|
|
229 |
|
|
230 |
"pad with leading zeros"
|
|
231 |
i _ n.
|
|
232 |
[i > vlen] whileTrue: [self nextPut: 0. i _ i - 1].
|
|
233 |
i = 1 ifTrue: [^self nextPut: v].
|
|
234 |
[i > 0] whileTrue: [self nextPut: (v digitAt: i). i _ i - 1]
|
|
235 |
! !
|