author | Claus Gittinger <cg@exept.de> |
Mon, 27 Nov 1995 21:33:02 +0100 | |
changeset 657 | 449935f15b9e |
parent 611 | 80bb0f1a7bab |
child 671 | 53de87163ad5 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
216 | 3 |
All Rights Reserved |
1 | 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 |
|
611 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'StreamErrorSignal PositionErrorSignal' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!Stream class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
216 | 25 |
All Rights Reserved |
88 | 26 |
|
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 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
An abstract class defining common behavior for all stream-like objects. |
|
39 |
See concrete subclasses for more detail. |
|
345 | 40 |
|
41 |
Subclasses should (at least) implement: |
|
42 |
#next (if readable) |
|
43 |
#nextPut: (if writable) |
|
44 |
#contents |
|
45 |
#atEnd |
|
46 |
#isReadable |
|
47 |
#isWritable |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
48 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
49 |
Class variables: |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
50 |
StreamErrorSignal <Signal> parent of all stream errors |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
51 |
PositionErrorSignal <Signal> position attemted on a stream |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
52 |
which does not support positioning |
88 | 53 |
" |
611 | 54 |
! |
55 |
||
56 |
version |
|
657 | 57 |
^ '$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.27 1995-11-27 20:33:02 cg Exp $' |
88 | 58 |
! ! |
1 | 59 |
|
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
60 |
!Stream class methodsFor:'initialization'! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
61 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
62 |
initialize |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
63 |
StreamErrorSignal isNil ifTrue:[ |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
64 |
StreamErrorSignal := ErrorSignal newSignalMayProceed:false. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
65 |
StreamErrorSignal nameClass:self message:#streamErrorSignal. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
66 |
StreamErrorSignal notifierString:'Stream error'. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
67 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
68 |
PositionErrorSignal := StreamErrorSignal newSignalMayProceed:true. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
69 |
PositionErrorSignal nameClass:self message:#positionErrorSignal. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
70 |
PositionErrorSignal notifierString:'stream as no concept of a position'. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
71 |
] |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
72 |
! ! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
73 |
|
611 | 74 |
!Stream class methodsFor:'instance creation'! |
75 |
||
76 |
new |
|
77 |
"report an error - Streams are created using on:-messages" |
|
78 |
||
79 |
^ self error:'Streams cannot be created with new' |
|
80 |
! ! |
|
81 |
||
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
82 |
!Stream class methodsFor:'Signal constants'! |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
83 |
|
611 | 84 |
positionErrorSignal |
85 |
"return the signal raised if positioning is requested for |
|
86 |
a stream which does not support that kind of operation" |
|
87 |
||
88 |
^ PositionErrorSignal |
|
89 |
! |
|
90 |
||
593
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
91 |
streamErrorSignal |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
92 |
"return the parent of all stream errors; |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
93 |
handling this one also handles all other errors. |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
94 |
Also, this one may be raised for errors not related to read/write |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
95 |
operations, such as failed ioctls in externalStream etc." |
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
96 |
|
19d568779cf7
moved StreamErrorSignal into Stream;
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
97 |
^ StreamErrorSignal |
1 | 98 |
! ! |
99 |
||
100 |
!Stream methodsFor:'accessing'! |
|
101 |
||
102 |
contents |
|
62 | 103 |
"return the entire contents of the stream |
1 | 104 |
- we do not know here how to do it, it must be redefined in subclass" |
105 |
||
106 |
^ self subclassResponsibility |
|
107 |
! ! |
|
108 |
||
611 | 109 |
!Stream methodsFor:'closing'! |
110 |
||
111 |
close |
|
112 |
"close the stream - nothing done here. |
|
113 |
Added for compatibility with external streams." |
|
114 |
||
115 |
^ self |
|
116 |
! ! |
|
117 |
||
118 |
!Stream methodsFor:'enumerating'! |
|
119 |
||
120 |
do:aBlock |
|
121 |
"evaluate the argument, aBlock for all remaining elements, |
|
122 |
up to the end of the stream" |
|
123 |
||
124 |
[self atEnd] whileFalse:[ |
|
125 |
aBlock value:(self next) |
|
126 |
] |
|
127 |
||
128 |
" |
|
129 |
|s| |
|
130 |
s := ReadStream on:#(1 2 3 4 5 6 7 8 9). |
|
131 |
s next. |
|
132 |
s next. |
|
133 |
s do:[:element | Transcript showCr:element] |
|
134 |
" |
|
135 |
! ! |
|
136 |
||
137 |
!Stream methodsFor:'misc'! |
|
138 |
||
139 |
binary |
|
140 |
"switch to binary mode. ignored here, but added to make |
|
141 |
internalStreams protocol compatible with externStreams." |
|
142 |
||
143 |
! ! |
|
144 |
||
145 |
!Stream methodsFor:'non homogenous reading'! |
|
146 |
||
147 |
nextLine |
|
148 |
"for compatibility with externalStreams" |
|
149 |
||
150 |
^ self upTo:Character cr. |
|
151 |
! |
|
152 |
||
153 |
nextLongMSB:msbFlag |
|
154 |
"return a signed long (4 bytes) from the stream. |
|
155 |
The receiver must support reading of binary bytes." |
|
156 |
||
157 |
|high uval "{ Class: SmallInteger }"| |
|
158 |
||
159 |
msbFlag ifTrue:[ |
|
160 |
"most significant first" |
|
161 |
high := self next. |
|
162 |
uval := self next bitShift:16. |
|
163 |
uval := uval bitOr:(self next bitShift:8). |
|
164 |
uval := uval bitOr:(self next). |
|
165 |
uval := uval bitOr:(high bitShift:24). |
|
166 |
] ifFalse:[ |
|
167 |
"least significant first" |
|
168 |
uval := self next. |
|
169 |
uval := uval bitOr:(self next bitShift:8). |
|
170 |
uval := uval bitOr:(self next bitShift:16). |
|
171 |
uval := uval bitOr:(self next bitShift:24). |
|
172 |
]. |
|
173 |
"change from unsigned 0..FFFFFFFF to signed -80000000..7FFFFFFF" |
|
174 |
||
175 |
"sorry- for now, stc cannot compile LargeInteger constants |
|
176 |
uval >= 16r80000000 ifTrue:[ |
|
177 |
^ uval - 16r100000000 |
|
178 |
]. |
|
179 |
the following kludges around this" |
|
180 |
(uval bitShift:-16) >= 16r8000 ifTrue:[ |
|
181 |
^ uval - (16r10000000 bitShift:8) |
|
182 |
]. |
|
183 |
^ uval |
|
184 |
! |
|
185 |
||
186 |
nextNumber:n |
|
187 |
"Return the next n bytes as a positive Integer; bytes are taken msb-first." |
|
188 |
||
189 |
|s |
|
190 |
val "{ Class: SmallInteger }" |
|
191 |
rep "{ Class: SmallInteger }"| |
|
192 |
||
193 |
"claus: this method is central in binaryStorage - |
|
194 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
195 |
||
196 |
n == 1 ifTrue:[ |
|
197 |
^ self next |
|
198 |
]. |
|
199 |
n == 2 ifTrue:[ |
|
200 |
val := self next. |
|
201 |
val := (val bitShift:8) + self next. |
|
202 |
^ val |
|
203 |
]. |
|
204 |
n == 3 ifTrue:[ |
|
205 |
val := self next. |
|
206 |
val := (val bitShift:8) + self next. |
|
207 |
val := (val bitShift:8) + self next. |
|
208 |
^ val |
|
209 |
]. |
|
210 |
n == 4 ifTrue:[ |
|
211 |
val := self next. |
|
212 |
val <= 16r3F ifTrue:[ |
|
213 |
val := (val bitShift:8) + self next. |
|
214 |
val := (val bitShift:8) + self next. |
|
215 |
val := (val bitShift:8) + self next. |
|
216 |
^ val |
|
217 |
]. |
|
218 |
"sorry, but need a largeInteger" |
|
219 |
s := (val bitShift:8) + self next. |
|
220 |
s := (s bitShift:8) + self next. |
|
221 |
"the multiplication below is a temporary workaround for the |
|
222 |
inability of smallintegers to handle shifts out of the 32bit range ..." |
|
223 |
s := (s * 256) + self next. |
|
224 |
^ s |
|
225 |
]. |
|
226 |
||
227 |
" |
|
228 |
arbitrary long |
|
229 |
" |
|
230 |
s := 0. |
|
231 |
rep := n. |
|
232 |
rep timesRepeat:[ |
|
233 |
s := s * 256 + self next |
|
234 |
]. |
|
235 |
^ s truncated |
|
236 |
! |
|
237 |
||
238 |
nextShortMSB:msbFlag |
|
239 |
"return a signed short (2 bytes) from the stream. |
|
240 |
The receiver must support reading of binary bytes." |
|
241 |
||
242 |
|uval "{ Class: SmallInteger }"| |
|
243 |
||
244 |
msbFlag ifTrue:[ |
|
245 |
"most significant first" |
|
246 |
uval := self next bitShift:8. |
|
247 |
uval := uval bitOr:(self next). |
|
248 |
] ifFalse:[ |
|
249 |
"least significant first" |
|
250 |
uval := self next. |
|
251 |
uval := uval bitOr:(self next bitShift:8). |
|
252 |
]. |
|
253 |
"change from unsigned 0..FFFF to signed -8000..7FFF" |
|
254 |
uval >= 16r8000 ifTrue:[ |
|
255 |
^ uval - 16r10000 |
|
256 |
]. |
|
257 |
^ uval |
|
258 |
! |
|
259 |
||
260 |
nextUnsignedLongMSB:msbFlag |
|
261 |
"return an unsigned long (4 bytes) from the stream. |
|
262 |
The receiver must support reading of binary bytes." |
|
263 |
||
264 |
|val| |
|
265 |
||
266 |
msbFlag ifTrue:[ |
|
267 |
val := self next. |
|
268 |
val := (val bitShift:8) bitOr:(self next). |
|
269 |
val := (val bitShift:8) bitOr:(self next). |
|
270 |
val := (val * 256) + (self next). |
|
271 |
^ val |
|
272 |
]. |
|
273 |
val := self next. |
|
274 |
^ (val bitShift:24) |
|
275 |
+ (self next bitShift:16) |
|
276 |
+ (self next bitShift:8) |
|
277 |
+ self next |
|
278 |
! |
|
279 |
||
280 |
nextUnsignedShortMSB:msbFlag |
|
281 |
"return an unsigned short (2 bytes) from the stream. |
|
282 |
The receiver must support reading of binary bytes." |
|
283 |
||
284 |
msbFlag ifTrue:[ |
|
285 |
^ (self next bitShift:8) bitOr:(self next) |
|
286 |
]. |
|
287 |
^ (self next) bitOr:(self next bitShift:8) |
|
288 |
! |
|
289 |
||
290 |
nextWord |
|
291 |
^ self nextAlphaNumericWord |
|
292 |
! ! |
|
293 |
||
294 |
!Stream methodsFor:'non homogenous writing'! |
|
295 |
||
296 |
nextLongPut:aNumber |
|
297 |
"for ST-80 compatibility" |
|
298 |
||
299 |
^ self nextPutLong:aNumber MSB:true |
|
300 |
! |
|
301 |
||
302 |
nextNumber:n put:v |
|
303 |
"Append to the receiver the argument, v, which is a positive Integer, |
|
304 |
as the next n bytes. Bytes are written msb first. |
|
305 |
Possibly pad with leading zeros. |
|
306 |
The receiver must support writing of binary bytes." |
|
307 |
||
308 |
|vlen i| |
|
309 |
||
310 |
"claus: this method is central in binaryStorage - |
|
311 |
therefore it has been tuned a bit (and needs even more tuning)" |
|
312 |
||
313 |
v class == SmallInteger ifTrue:[ "- this is a hint to stc" |
|
314 |
n == 1 ifTrue:[ |
|
315 |
(v between:0 and:16rFF) ifTrue:[ |
|
316 |
self nextPut:v. |
|
317 |
^ self |
|
318 |
]. |
|
319 |
]. |
|
320 |
n == 2 ifTrue:[ |
|
321 |
(v between:0 and:16rFFFF) ifTrue:[ |
|
322 |
self nextPut:(v bitShift:-8). |
|
323 |
self nextPut:(v bitAnd:16rFF). |
|
324 |
^ self |
|
325 |
]. |
|
326 |
]. |
|
327 |
n == 3 ifTrue:[ |
|
328 |
(v between:0 and:16rFFFFFF) ifTrue:[ |
|
329 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
330 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
331 |
self nextPut:(v bitAnd:16rFF). |
|
332 |
^ self |
|
333 |
]. |
|
334 |
]. |
|
335 |
n == 4 ifTrue:[ |
|
336 |
(v >= 0) ifTrue:[ |
|
337 |
self nextPut:((v bitShift:-24) bitAnd:16rFF). |
|
338 |
self nextPut:((v bitShift:-16) bitAnd:16rFF). |
|
339 |
self nextPut:((v bitShift:-8) bitAnd:16rFF). |
|
340 |
self nextPut:(v bitAnd:16rFF). |
|
341 |
^ self |
|
342 |
]. |
|
343 |
]. |
|
344 |
]. |
|
345 |
||
346 |
" |
|
347 |
arbitrary long |
|
348 |
" |
|
349 |
n < (vlen := v digitLength) ifTrue: [ |
|
350 |
" |
|
351 |
the number is too big to be repesented in n bytes |
|
352 |
" |
|
353 |
self error: 'number too big' |
|
354 |
]. |
|
355 |
||
356 |
"pad with leading zeros" |
|
357 |
i := n. |
|
358 |
[i > vlen] whileTrue: [ |
|
359 |
self nextPut: 0. |
|
360 |
i := i - 1 |
|
361 |
]. |
|
362 |
i = 1 ifTrue: [ |
|
363 |
^ self nextPut: v |
|
364 |
]. |
|
365 |
[i > 0] whileTrue: [ |
|
366 |
self nextPut: (v digitAt: i). |
|
367 |
i := i - 1 |
|
368 |
] |
|
369 |
! |
|
370 |
||
371 |
nextPutLong:aNumber MSB:msbFlag |
|
372 |
"Write the argument, aNumber as a long (four bytes). If msbFlag is |
|
373 |
true, data is written most-significant byte first; otherwise least |
|
374 |
first. Returns the receiver on ok, nil on error. |
|
375 |
The receiver must support writing of binary bytes." |
|
376 |
||
377 |
msbFlag ifTrue:[ |
|
378 |
"high word first" |
|
379 |
self nextShortPut:(aNumber // 16r10000) MSB:true. |
|
380 |
^ self nextShortPut:(aNumber \\ 16r10000) MSB:true |
|
381 |
]. |
|
382 |
"low word first" |
|
383 |
self nextShortPut:(aNumber \\ 16r10000) MSB:false. |
|
384 |
^ self nextShortPut:(aNumber // 16r10000) MSB:false. |
|
385 |
! |
|
386 |
||
387 |
nextPutShort:aNumber MSB:msbFlag |
|
388 |
"Write the argument, aNumber as a short (two bytes). If msbFlag is |
|
389 |
true, data is written most-significant byte first; otherwise least |
|
390 |
first. Returns the receiver on ok, nil on error. |
|
391 |
The receiver must support writing of binary bytes." |
|
392 |
||
393 |
msbFlag ifTrue:[ |
|
394 |
"most significant first" |
|
395 |
self nextPut:(aNumber bitShift:-8). |
|
396 |
^ self nextPut:(aNumber bitAnd:16rFF). |
|
397 |
]. |
|
398 |
"least significant first" |
|
399 |
self nextPut:(aNumber bitAnd:16rFF). |
|
400 |
self nextPut:(aNumber bitShift:-8). |
|
401 |
! |
|
402 |
||
403 |
nextPutWord:aNumber |
|
404 |
"write the argument, aNumber as a signed short (two bytes); |
|
405 |
write msb-first for compatibility with other smalltalks. |
|
406 |
The receiver must support writing of binary bytes. |
|
407 |
I dont know if it should be named nextPutWord: or nextWordPut:; |
|
408 |
one of them will vanish ..." |
|
409 |
||
410 |
^ self nextPutShort:aNumber MSB:true |
|
411 |
! |
|
412 |
||
413 |
nextShortPut:aNumber MSB:msbFlag |
|
414 |
"for compatibility - this will vanish" |
|
415 |
||
416 |
^ self nextPutShort:aNumber MSB:msbFlag |
|
417 |
! |
|
418 |
||
419 |
nextWordPut:aNumber |
|
420 |
"for ST-80 compatibility" |
|
421 |
||
422 |
^ self nextPutShort:aNumber MSB:true |
|
423 |
! ! |
|
424 |
||
425 |
!Stream methodsFor:'private'! |
|
426 |
||
427 |
contentsSpecies |
|
428 |
"this should return the class of which an instance is |
|
429 |
returned by the #contents method. It is redefinable in subclasses" |
|
430 |
||
431 |
"return Array here - since the abstract Stream has no idea |
|
432 |
of the underlying collection class" |
|
433 |
||
434 |
^ Array |
|
435 |
! ! |
|
436 |
||
437 |
!Stream methodsFor:'queries'! |
|
438 |
||
439 |
isBinary |
|
440 |
"return true, if in binary mode. Returns false here, |
|
441 |
to make internalStreams protocol compatible with externStreams." |
|
442 |
||
443 |
^ false |
|
444 |
! |
|
445 |
||
446 |
isPositionable |
|
447 |
"return true, if the stream supports positioning (some do not)" |
|
448 |
||
449 |
^ false |
|
450 |
! |
|
451 |
||
452 |
isReadable |
|
453 |
"return true, if reading is supported by the recevier. |
|
454 |
Assume yes here." |
|
455 |
||
456 |
^ self subclassResponsibility |
|
457 |
! |
|
458 |
||
459 |
isStream |
|
460 |
"return true, if the receiver is some kind of Stream." |
|
461 |
||
462 |
^ true |
|
463 |
! |
|
464 |
||
465 |
isWritable |
|
466 |
"return true, if writing is supported by the recevier. |
|
467 |
Assume yes here." |
|
468 |
||
469 |
^ self subclassResponsibility |
|
470 |
! |
|
471 |
||
472 |
lineLength |
|
473 |
"this is just a layout hint for prettyprinting functions |
|
474 |
- for compatibility with TextCollectors" |
|
475 |
||
476 |
^ 80 |
|
477 |
! ! |
|
478 |
||
62 | 479 |
!Stream methodsFor:'reading'! |
1 | 480 |
|
481 |
next |
|
482 |
"return the next element of the stream |
|
483 |
- we do not know here how to do it, it must be redefined in subclass" |
|
484 |
||
485 |
^ self subclassResponsibility |
|
486 |
! |
|
487 |
||
488 |
next:count |
|
62 | 489 |
"return the next count elements of the stream as a Collection." |
1 | 490 |
|
329 | 491 |
|answerStream |
285 | 492 |
cnt "{ Class: SmallInteger }" | |
1 | 493 |
|
329 | 494 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 495 |
cnt := count. |
496 |
1 to:cnt do:[:index | |
|
329 | 497 |
answerStream nextPut:(self next) |
1 | 498 |
]. |
329 | 499 |
^ answerStream contents |
1 | 500 |
|
275 | 501 |
" |
502 |
(ReadStream on:#(1 2 3 4 5)) next:3 |
|
503 |
(ReadStream on:'hello') next:3 |
|
504 |
" |
|
1 | 505 |
! |
506 |
||
285 | 507 |
nextAvailable:count |
508 |
"return the next count elements of the stream as a Collection. |
|
509 |
If the stream reaches the end before count elements have been read, |
|
510 |
return what is available. (i.e. a shorter collection)" |
|
511 |
||
345 | 512 |
|answerStream |
285 | 513 |
cnt "{ Class: SmallInteger }"| |
514 |
||
329 | 515 |
answerStream := WriteStream on:(self contentsSpecies new). |
285 | 516 |
cnt := count. |
517 |
1 to:cnt do:[:index | |
|
518 |
self atEnd ifTrue:[ |
|
329 | 519 |
^ answerStream contents |
285 | 520 |
]. |
329 | 521 |
answerStream nextPut:(self next) |
285 | 522 |
]. |
329 | 523 |
^ answerStream contents |
285 | 524 |
|
525 |
" |
|
526 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:3 |
|
527 |
(ReadStream on:#(1 2 3 4 5)) nextAvailable:10 |
|
528 |
(ReadStream on:'hello') nextAvailable:3 |
|
529 |
(ReadStream on:'hello') nextAvailable:10 |
|
530 |
" |
|
531 |
! |
|
532 |
||
1 | 533 |
nextMatchFor:anObject |
62 | 534 |
"read an element from the receiver, return true if it was equal to |
535 |
the argument, anObject" |
|
536 |
||
537 |
^ (self next = anObject) |
|
538 |
||
539 |
" |
|
540 |
|s| |
|
541 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
542 |
s nextMatchFor:2 |
|
543 |
" |
|
544 |
" |
|
545 |
|s| |
|
546 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
547 |
s nextMatchFor:2. |
|
548 |
s nextMatchFor:2 |
|
549 |
" |
|
550 |
! |
|
551 |
||
552 |
skip:count |
|
553 |
"skip count objects, return the receiver" |
|
554 |
||
345 | 555 |
|n "{ Class: SmallInteger }"| |
556 |
||
557 |
n := count. |
|
558 |
||
62 | 559 |
"dont know how to unread ..." |
345 | 560 |
n < 0 ifTrue:[ |
216 | 561 |
^ self error:'stream is not positionable' |
62 | 562 |
]. |
345 | 563 |
n timesRepeat:self next |
62 | 564 |
|
565 |
" |
|
566 |
|s| |
|
567 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
568 |
s skip:4. |
|
569 |
s next |
|
570 |
" |
|
571 |
! |
|
572 |
||
573 |
skipFor:anObject |
|
574 |
"skip all objects up-to and including anObject; return the element after anObject." |
|
575 |
||
576 |
(self skipThrough:anObject) notNil ifTrue:[ |
|
216 | 577 |
^ self next |
62 | 578 |
]. |
579 |
^ nil |
|
580 |
||
581 |
" |
|
582 |
|s| |
|
583 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
584 |
s skipFor:4. |
|
585 |
" |
|
586 |
! |
|
587 |
||
588 |
skipThrough:anObject |
|
77 | 589 |
"skip all objects up-to and including anObject. Return the receiver if |
590 |
skip was successfull, otherwise (i.e. if not found) return nil. |
|
62 | 591 |
The next read operation will return the element after anObject." |
1 | 592 |
|
593 |
|nextElement| |
|
594 |
||
595 |
[self atEnd] whileFalse:[ |
|
216 | 596 |
nextElement := self next. |
597 |
(nextElement = anObject) ifTrue: [ ^ self ] |
|
1 | 598 |
]. |
599 |
^ nil |
|
2 | 600 |
|
62 | 601 |
" |
602 |
|s| |
|
603 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
604 |
s skipThrough:4. |
|
605 |
s next |
|
606 |
" |
|
77 | 607 |
! |
608 |
||
611 | 609 |
through:anObject |
610 |
"read a collection of all objects up-to anObject and return these |
|
611 |
elements, including anObject. |
|
612 |
The next read operation will return the element after anObject. |
|
613 |
Compare this with #upTo: which also reads up to some object |
|
614 |
and also positions behind it, but does not include it in the returned |
|
615 |
value." |
|
616 |
||
617 |
|answerStream element| |
|
618 |
||
619 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
620 |
[self atEnd] whileFalse:[ |
|
621 |
element := self next. |
|
622 |
answerStream nextPut:element. |
|
623 |
(element = anObject) ifTrue: [ |
|
624 |
^ answerStream contents |
|
625 |
] |
|
626 |
]. |
|
627 |
^ answerStream contents |
|
628 |
||
629 |
" |
|
630 |
|s| |
|
631 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
632 |
Transcript showCr:(s through:4). |
|
633 |
Transcript showCr:s next |
|
634 |
" |
|
635 |
" |
|
636 |
|s| |
|
637 |
s := ReadStream on:'hello world'. |
|
638 |
Transcript showCr:(s through:Character space). |
|
639 |
Transcript showCr:(s upToEnd) |
|
640 |
" |
|
641 |
! |
|
642 |
||
427 | 643 |
upTo:anObject |
644 |
"read a collection of all objects up-to anObject and return these |
|
645 |
elements, but excluding anObject. |
|
646 |
The next read operation will return the element after anObject. |
|
647 |
Compare this with #through: which also reads up to some object |
|
648 |
and also positions behind it, but DOES include it in the returned |
|
649 |
value." |
|
650 |
||
651 |
|answerStream element| |
|
652 |
||
653 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
654 |
[self atEnd] whileFalse:[ |
|
655 |
element := self next. |
|
656 |
(element = anObject) ifTrue: [ |
|
657 |
^ answerStream contents |
|
658 |
]. |
|
659 |
answerStream nextPut: element. |
|
660 |
]. |
|
661 |
^ answerStream contents |
|
662 |
||
663 |
" |
|
664 |
|s| |
|
665 |
s := ReadStream on:#(1 2 3 4 5 6 7 8). |
|
666 |
Transcript showCr:(s upTo:4). |
|
667 |
Transcript showCr:s next |
|
668 |
||
669 |
|s| |
|
670 |
s := ReadStream on:'hello world'. |
|
671 |
Transcript showCr:(s upTo:Character space). |
|
672 |
Transcript showCr:(s upToEnd) |
|
673 |
||
674 |
(ReadStream on:'12345678905') upTo:$5; upTo:$5 |
|
675 |
||
676 |
(ReadStream on:'123456') upTo:$7 |
|
677 |
||
678 |
(ReadStream on:#(1 2 3 4 5 6)) upTo:4 |
|
679 |
||
680 |
(ReadStream on:'line 1 |
|
681 |
line 2') upTo:Character cr |
|
682 |
||
683 |
'Makefile' asFilename readStream upTo:Character cr;upTo:Character cr |
|
684 |
" |
|
685 |
! |
|
686 |
||
77 | 687 |
upToEnd |
688 |
"return a collection of the elements up-to the end. |
|
689 |
Return nil if the stream-end is reached before." |
|
690 |
||
691 |
|answerStream| |
|
692 |
||
693 |
answerStream := WriteStream on:(self contentsSpecies new). |
|
694 |
[self atEnd] whileFalse:[ |
|
216 | 695 |
answerStream nextPut:(self next) |
77 | 696 |
]. |
697 |
^ answerStream contents |
|
698 |
||
275 | 699 |
" |
700 |
(ReadStream on:'1234567890') upToEnd |
|
701 |
('123456' readStream) next; next; upToEnd |
|
427 | 702 |
('1 23456' readStream) upTo:Character space; upToEnd |
275 | 703 |
" |
611 | 704 |
! ! |
77 | 705 |
|
611 | 706 |
!Stream methodsFor:'testing'! |
77 | 707 |
|
611 | 708 |
atEnd |
709 |
"return true if the end of the stream has been reached; |
|
710 |
- we do not know here how to do it, it must be redefined in subclass" |
|
77 | 711 |
|
611 | 712 |
^ self subclassResponsibility |
1 | 713 |
! ! |
714 |
||
62 | 715 |
!Stream methodsFor:'writing'! |
1 | 716 |
|
657 | 717 |
flush |
718 |
"write out all unbuffered data - for ST-80 compatibility" |
|
719 |
||
720 |
self commit |
|
721 |
||
722 |
"Created: 27.11.1995 / 12:00:35 / cg" |
|
723 |
! |
|
724 |
||
339 | 725 |
commit |
726 |
"write out all unbuffered data - ignored here, but added |
|
727 |
to make internalStreams protocol compatible with externalStreams" |
|
728 |
||
729 |
! |
|
730 |
||
611 | 731 |
cr |
732 |
"append a carriage-return to the stream" |
|
733 |
||
734 |
self nextPut:(Character cr) |
|
735 |
! |
|
736 |
||
737 |
crTab |
|
738 |
"append a carriage-return followed by a tab to the stream. |
|
739 |
Same as crtab for ST/X backward compatibility" |
|
740 |
||
741 |
self obsoleteMethodWarning:'use crTab'. |
|
742 |
self crtab |
|
743 |
! |
|
744 |
||
745 |
crtab |
|
746 |
"append a carriage-return followed by a tab to the stream." |
|
1 | 747 |
|
611 | 748 |
self crtab:1 |
749 |
! |
|
750 |
||
751 |
crtab:n |
|
752 |
"append a carriage-return followed by n tabs to the stream." |
|
753 |
||
754 |
self nextPut:(Character cr). |
|
755 |
self next:n put:(Character tab) |
|
756 |
! |
|
757 |
||
758 |
endEntry |
|
759 |
"ignored here - for compatibility with Transcript" |
|
760 |
||
761 |
^ self |
|
762 |
! |
|
763 |
||
764 |
ff |
|
765 |
"append a form-feed (new-pagee) to the receiver-stream" |
|
766 |
||
767 |
self nextPut:(Character ff) |
|
1 | 768 |
! |
769 |
||
770 |
next:count put:anObject |
|
771 |
"put the argument, anObject count times onto the receiver" |
|
772 |
||
345 | 773 |
|n "{ Class: SmallInteger }"| |
774 |
||
775 |
n := count. |
|
776 |
n timesRepeat:[self nextPut:anObject]. |
|
1 | 777 |
^ anObject |
62 | 778 |
|
779 |
" |
|
780 |
|s| |
|
781 |
||
782 |
s := WriteStream on:#(). |
|
783 |
s nextPut:1. |
|
784 |
s next:5 put:2. |
|
785 |
s nextPut:3. |
|
786 |
s contents |
|
787 |
" |
|
1 | 788 |
! |
789 |
||
611 | 790 |
nextPut:anObject |
791 |
"put the argument, anObject onto the receiver |
|
792 |
- we do not know here how to do it, it must be redefined in subclass" |
|
793 |
||
794 |
^ self subclassResponsibility |
|
795 |
! |
|
796 |
||
1 | 797 |
nextPutAll:aCollection |
798 |
"put all elements of the argument, aCollection onto the receiver" |
|
799 |
||
800 |
aCollection do:[:element | |
|
216 | 801 |
self nextPut:element |
1 | 802 |
]. |
803 |
^ aCollection |
|
62 | 804 |
|
805 |
" |
|
806 |
|s| |
|
807 |
||
808 |
s := WriteStream on:#(). |
|
809 |
s nextPutAll:(1 to:5). |
|
810 |
s nextPutAll:#('one' 'two' 'three'). |
|
811 |
s contents |
|
812 |
" |
|
813 |
" |
|
814 |
|s| |
|
815 |
||
816 |
s := WriteStream on:(String new). |
|
817 |
s nextPutAll:($a to:$f). |
|
818 |
s nextPutAll:'one '; |
|
819 |
nextPutAll:'two '; |
|
820 |
nextPutAll:'three'. |
|
821 |
s contents |
|
822 |
" |
|
1 | 823 |
! |
824 |
||
825 |
nextPutAll:aCollection startingAt:first to:last |
|
826 |
"put the elements with index from first to last |
|
827 |
of the argument, aCollection onto the receiver" |
|
828 |
||
829 |
aCollection from:first to:last do:[:element | |
|
216 | 830 |
self nextPut:element |
1 | 831 |
]. |
832 |
^ aCollection |
|
62 | 833 |
|
834 |
" |
|
835 |
|s| |
|
836 |
||
837 |
s := WriteStream on:#(). |
|
838 |
s nextPutAll:#('one' 'two' 'three' 'four' 'five') startingAt:2 to:4. |
|
839 |
s contents |
|
840 |
" |
|
1 | 841 |
! |
842 |
||
611 | 843 |
print:anObject |
844 |
"append a printed representation of anObject to the receiver. |
|
845 |
Same as anObject printOn:self; For ST-80 compatibility." |
|
2 | 846 |
|
611 | 847 |
anObject printOn:self |
2 | 848 |
! |
849 |
||
1 | 850 |
show:aString |
851 |
"put all elements of the argument, aString onto the receiver; |
|
852 |
this makes streams somewhat compatible to TextCollectors and |
|
853 |
allows you to say: Smalltalk at:#Transcript put:Stdout" |
|
854 |
||
855 |
^ self nextPutAll:aString printString |
|
856 |
! |
|
857 |
||
858 |
showCr:aString |
|
859 |
"put all elements of the argument, aString onto the receiver; |
|
860 |
and append a newline. |
|
62 | 861 |
For compatibility with TextCollectors" |
1 | 862 |
|
863 |
self show:aString. |
|
864 |
self cr |
|
62 | 865 |
! |
866 |
||
867 |
space |
|
868 |
"append a space character to the receiver-stream" |
|
869 |
||
870 |
self nextPut:(Character space) |
|
871 |
! |
|
872 |
||
873 |
spaces:count |
|
874 |
"append count space-characters to the receiver-stream" |
|
875 |
||
876 |
self next:count put:(Character space) |
|
877 |
! |
|
878 |
||
611 | 879 |
store:anObject |
880 |
"append a printed representation of anObject to the receiver, |
|
881 |
from which the receiver can be reconstructed. |
|
882 |
Same as anObject storeOn:self; For ST-80 compatibility." |
|
77 | 883 |
|
611 | 884 |
anObject storeOn:self |
77 | 885 |
! |
886 |
||
611 | 887 |
tab |
888 |
"append a tab-character to the stream" |
|
77 | 889 |
|
611 | 890 |
self nextPut:(Character tab) |
77 | 891 |
! ! |
892 |
||
611 | 893 |
Stream initialize! |