1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 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 |
PositionableStream subclass:#ReadStream
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
ReadStream comment:'
|
88
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
1
|
24 |
|
88
|
25 |
!ReadStream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
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 |
!
|
1
|
40 |
|
88
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.8 1994-06-02 16:21:32 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
documentation
|
|
48 |
"
|
|
49 |
ReadStream defines protocol for reading streamwise over collections.
|
|
50 |
Code here is specially tuned for streaming over strings.
|
|
51 |
"
|
|
52 |
! !
|
1
|
53 |
|
59
|
54 |
!ReadStream methodsFor:'reading'!
|
1
|
55 |
|
|
56 |
peek
|
|
57 |
"return the next element; do NOT advance read pointer.
|
2
|
58 |
return nil, if there is no next element.
|
59
|
59 |
- tuned for speed on String/ByteArray/Array-Streams"
|
1
|
60 |
|
|
61 |
%{ /* NOCONTEXT */
|
|
62 |
|
|
63 |
REGISTER int pos;
|
|
64 |
unsigned ch;
|
2
|
65 |
OBJ coll;
|
59
|
66 |
OBJ cls;
|
2
|
67 |
extern OBJ ByteArray;
|
1
|
68 |
|
2
|
69 |
coll = _INST(collection);
|
|
70 |
if (_isNonNilObject(coll)
|
1
|
71 |
&& _isSmallInteger(_INST(position))
|
|
72 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
73 |
|
|
74 |
pos = _intVal(_INST(position));
|
|
75 |
if (pos > _intVal(_INST(readLimit))) {
|
|
76 |
RETURN ( nil );
|
|
77 |
}
|
59
|
78 |
|
2
|
79 |
if (pos > 0) {
|
59
|
80 |
cls = _qClass(coll);
|
|
81 |
if (cls == String) {
|
|
82 |
if (pos <= _stringSize(coll)) {
|
2
|
83 |
ch = _stringVal(coll)[pos-1];
|
|
84 |
RETURN ( _MKCHARACTER(ch) );
|
|
85 |
}
|
59
|
86 |
} else if (cls == ByteArray) {
|
|
87 |
if (pos <= _byteArraySize(coll)) {
|
2
|
88 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
|
|
89 |
RETURN ( _MKSMALLINT(ch) );
|
|
90 |
}
|
59
|
91 |
} else if (cls == Array) {
|
|
92 |
if (pos <= _arraySize(coll)) {
|
|
93 |
RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]);
|
|
94 |
}
|
2
|
95 |
}
|
1
|
96 |
}
|
|
97 |
}
|
|
98 |
%}
|
|
99 |
.
|
|
100 |
(position > readLimit) ifTrue:[^ nil].
|
|
101 |
^ collection at:position
|
|
102 |
!
|
|
103 |
|
|
104 |
next
|
|
105 |
"return the next element; advance read pointer.
|
2
|
106 |
return nil, if there is no next element.
|
59
|
107 |
- tuned for speed on String/ByteArray/Array-Streams"
|
1
|
108 |
|
|
109 |
|ret|
|
|
110 |
|
|
111 |
%{ /* NOCONTEXT */
|
|
112 |
|
|
113 |
REGISTER int pos;
|
|
114 |
unsigned ch;
|
2
|
115 |
OBJ coll;
|
1
|
116 |
|
2
|
117 |
coll = _INST(collection);
|
|
118 |
if (_isNonNilObject(coll)
|
1
|
119 |
&& _isSmallInteger(_INST(position))
|
|
120 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
121 |
|
|
122 |
pos = _intVal(_INST(position));
|
|
123 |
if (pos > _intVal(_INST(readLimit))) {
|
|
124 |
RETURN ( nil );
|
|
125 |
}
|
2
|
126 |
if (pos > 0) {
|
59
|
127 |
OBJ cls;
|
|
128 |
|
|
129 |
cls = _qClass(coll);
|
|
130 |
if (cls == String) {
|
|
131 |
if (pos <= _stringSize(coll)) {
|
2
|
132 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
133 |
ch = _stringVal(coll)[pos-1];
|
|
134 |
RETURN ( _MKCHARACTER(ch) );
|
|
135 |
}
|
59
|
136 |
} else if (cls == ByteArray) {
|
|
137 |
if (pos <= _byteArraySize(coll)) {
|
2
|
138 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
139 |
ch = _ByteArrayInstPtr(coll)->ba_element[pos-1];
|
|
140 |
RETURN ( _MKSMALLINT(ch) );
|
|
141 |
}
|
59
|
142 |
} else if (cls == Array) {
|
|
143 |
if (pos <= _arraySize(coll)) {
|
|
144 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
145 |
RETURN ( _ArrayInstPtr(coll)->a_element[pos-1]);
|
|
146 |
}
|
2
|
147 |
}
|
1
|
148 |
}
|
|
149 |
}
|
|
150 |
%}
|
|
151 |
.
|
|
152 |
(position > readLimit) ifTrue:[^ nil].
|
|
153 |
ret := collection at:position.
|
|
154 |
position := position + 1.
|
|
155 |
^ ret
|
|
156 |
!
|
|
157 |
|
|
158 |
nextPeek
|
|
159 |
"advance read pointer return the peek element.
|
|
160 |
this is equivalent to (self next; peek).
|
59
|
161 |
- tuned for speed on String-Streams for faster scanning"
|
1
|
162 |
|
|
163 |
%{ /* NOCONTEXT */
|
59
|
164 |
OBJ coll;
|
1
|
165 |
|
59
|
166 |
coll = _INST(collection);
|
|
167 |
if (__isString(coll)
|
1
|
168 |
&& _isSmallInteger(_INST(position))
|
|
169 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
170 |
REGISTER int pos;
|
|
171 |
unsigned ch;
|
|
172 |
|
|
173 |
pos = _intVal(_INST(position));
|
|
174 |
if (pos > _intVal(_INST(readLimit))) {
|
|
175 |
RETURN ( nil );
|
|
176 |
}
|
59
|
177 |
if ((pos > 0) && (pos <= _stringSize(coll))) {
|
1
|
178 |
_INST(position) = _MKSMALLINT(pos + 1);
|
|
179 |
pos = pos + 1;
|
59
|
180 |
if (pos <= _stringSize(coll)) {
|
|
181 |
ch = _stringVal(coll)[pos-1];
|
1
|
182 |
RETURN ( _MKCHARACTER(ch) );
|
|
183 |
}
|
|
184 |
RETURN ( nil );
|
|
185 |
}
|
|
186 |
}
|
|
187 |
%}
|
|
188 |
.
|
|
189 |
(position > readLimit) ifTrue:[^ nil].
|
|
190 |
position := position + 1.
|
|
191 |
(position > readLimit) ifTrue:[^ nil].
|
|
192 |
^ collection at:position
|
|
193 |
!
|
|
194 |
|
|
195 |
nextDecimalInteger
|
|
196 |
"read the next integer in radix 10. dont skip whitespace.
|
59
|
197 |
- tuned for speed on String-Streams for faster scanning"
|
1
|
198 |
|
59
|
199 |
|value nextOne|
|
1
|
200 |
%{
|
|
201 |
int pos, limit, sz;
|
13
|
202 |
REGISTER unsigned char *cp;
|
|
203 |
REGISTER unsigned ch;
|
1
|
204 |
int val = 0;
|
|
205 |
|
59
|
206 |
if (__isString(_INST(collection))
|
1
|
207 |
&& _isSmallInteger(_INST(position))
|
|
208 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
209 |
pos = _intVal(_INST(position));
|
|
210 |
limit = _intVal(_INST(readLimit));
|
|
211 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
212 |
if (sz < limit)
|
|
213 |
limit = sz;
|
|
214 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
215 |
|
|
216 |
for (;;) {
|
|
217 |
if (pos > limit) break;
|
|
218 |
ch = *cp;
|
|
219 |
|
|
220 |
if ((ch < '0') || (ch > '9')) break;
|
|
221 |
val = val * 10 + (ch - '0');
|
|
222 |
pos++;
|
|
223 |
if (val > (_MAX_INT / 10)) goto oops;
|
|
224 |
cp++;
|
|
225 |
}
|
|
226 |
_INST(position) = _MKSMALLINT(pos);
|
|
227 |
return _MKSMALLINT(val);
|
|
228 |
}
|
|
229 |
oops:
|
|
230 |
value = _MKSMALLINT(val);
|
|
231 |
%}
|
|
232 |
.
|
59
|
233 |
"fall-back for non-string streams - we have to continue where
|
|
234 |
above primitive left off, in case of a large integer ...
|
|
235 |
(instead of doing a super nextDecimalInteger)"
|
13
|
236 |
|
59
|
237 |
nextOne := self peek.
|
|
238 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
|
|
239 |
value = (value * 10) + nextOne digitValue.
|
|
240 |
nextOne := self nextPeek
|
1
|
241 |
].
|
|
242 |
^ value
|
|
243 |
!
|
|
244 |
|
|
245 |
nextWord
|
|
246 |
"read the next word (i.e. up to non letter-or-digit).
|
|
247 |
return a string containing those characters.
|
59
|
248 |
- tuned for speed on String-Streams for faster scanning"
|
1
|
249 |
%{
|
|
250 |
/* speedup, if collection is a string */
|
|
251 |
|
|
252 |
int pos, limit, sz;
|
|
253 |
int len;
|
13
|
254 |
char buffer[256];
|
|
255 |
REGISTER unsigned char *cp;
|
|
256 |
REGISTER unsigned ch;
|
1
|
257 |
|
59
|
258 |
if (__isString(_INST(collection))
|
1
|
259 |
&& _isSmallInteger(_INST(position))
|
|
260 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
261 |
pos = _intVal(_INST(position));
|
|
262 |
limit = _intVal(_INST(readLimit));
|
|
263 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
264 |
if (sz < limit)
|
|
265 |
limit = sz;
|
|
266 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
267 |
|
|
268 |
for (;;) {
|
|
269 |
if (pos > limit) break;
|
|
270 |
ch = *cp;
|
|
271 |
|
|
272 |
if (ch > ' ') break;
|
|
273 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r')
|
|
274 |
&& (ch != '\n') && (ch != 0x0b)) break;
|
|
275 |
cp++;
|
|
276 |
pos++;
|
|
277 |
}
|
|
278 |
|
|
279 |
len = 0;
|
|
280 |
for (;;) {
|
|
281 |
if (pos > limit) break;
|
|
282 |
ch = *cp & 0xFF;
|
|
283 |
|
|
284 |
if (! (((ch >= 'a') && (ch <= 'z')) ||
|
|
285 |
((ch >= 'A') && (ch <= 'Z')) ||
|
|
286 |
((ch >= '0') && (ch <= '9'))))
|
|
287 |
break;
|
|
288 |
buffer[len++] = ch;
|
|
289 |
if (len >= (sizeof(buffer)-1)) {
|
|
290 |
/* emergency */
|
|
291 |
break;
|
|
292 |
}
|
|
293 |
pos++;
|
|
294 |
cp++;
|
|
295 |
}
|
|
296 |
|
|
297 |
_INST(position) = _MKSMALLINT(pos);
|
|
298 |
buffer[len] = '\0';
|
|
299 |
RETURN ( (len != 0) ? _MKSTRING(buffer COMMA_CON) : nil );
|
|
300 |
}
|
|
301 |
%}
|
|
302 |
.
|
|
303 |
^ super nextWord
|
|
304 |
!
|
|
305 |
|
|
306 |
nextSymbol
|
|
307 |
"read the next selector-symbol (i.e. up to non letter-or-digit).
|
|
308 |
return a string containing those characters.
|
59
|
309 |
- tuned for speed on String-Streams for faster scanning"
|
1
|
310 |
%{
|
|
311 |
int pos, limit, sz;
|
|
312 |
int len;
|
13
|
313 |
char buffer[256];
|
|
314 |
REGISTER unsigned char *cp;
|
|
315 |
REGISTER unsigned ch;
|
1
|
316 |
|
59
|
317 |
if (__isString(_INST(collection))
|
1
|
318 |
&& _isSmallInteger(_INST(position))
|
|
319 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
320 |
pos = _intVal(_INST(position));
|
|
321 |
limit = _intVal(_INST(readLimit));
|
|
322 |
sz = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
323 |
if (sz < limit)
|
|
324 |
limit = sz;
|
|
325 |
cp = _stringVal(_INST(collection)) + pos - 1;
|
|
326 |
|
|
327 |
len = 0;
|
|
328 |
for (;;) {
|
|
329 |
if (pos > limit) break;
|
|
330 |
ch = *cp;
|
|
331 |
|
|
332 |
if (! (((ch >= 'a') && (ch <= 'z')) ||
|
|
333 |
((ch >= 'A') && (ch <= 'Z')) ||
|
|
334 |
((ch >= '0') && (ch <= '9')) ||
|
|
335 |
(ch == ':')))
|
|
336 |
break;
|
|
337 |
buffer[len++] = ch;
|
|
338 |
if (len >= (sizeof(buffer)-1)) {
|
|
339 |
/* emergency */
|
|
340 |
break;
|
|
341 |
}
|
|
342 |
pos++;
|
|
343 |
cp++;
|
|
344 |
}
|
|
345 |
|
|
346 |
_INST(position) = _MKSMALLINT(pos);
|
|
347 |
buffer[len] = '\0';
|
|
348 |
RETURN ( (len != 0) ? _MKSTRING(buffer COMMA_CON) : nil );
|
|
349 |
}
|
|
350 |
%}
|
|
351 |
.
|
|
352 |
^ super nextSymbol
|
|
353 |
!
|
|
354 |
|
59
|
355 |
skipThrough:anObject
|
2
|
356 |
"skip all objects up-to and including anObject, return anObject on success,
|
59
|
357 |
nil if end-of-stream is reached before.
|
|
358 |
On success, the next read operation will return the element after anObject.
|
2
|
359 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
360 |
|
|
361 |
%{ /* NOCONTEXT */
|
59
|
362 |
OBJ coll;
|
1
|
363 |
|
59
|
364 |
coll = _INST(collection);
|
|
365 |
if (__isString(coll)
|
|
366 |
&& __isCharacter(anObject)
|
1
|
367 |
&& _isSmallInteger(_INST(position))
|
|
368 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
369 |
REGISTER unsigned char *chars;
|
|
370 |
REGISTER int pos, limit;
|
2
|
371 |
unsigned ch;
|
59
|
372 |
int sz;
|
1
|
373 |
|
|
374 |
pos = _intVal(_INST(position));
|
|
375 |
if (pos <= 0) {
|
|
376 |
RETURN ( nil );
|
|
377 |
}
|
|
378 |
|
|
379 |
limit = _intVal(_INST(readLimit));
|
59
|
380 |
sz = _stringSize(coll);
|
|
381 |
if (limit > sz) limit = sz;
|
1
|
382 |
|
59
|
383 |
chars = (unsigned char *)(_stringVal(coll) + pos - 1);
|
1
|
384 |
ch = _intVal(_characterVal(anObject)) & 0xFF;
|
|
385 |
while (pos < limit) {
|
|
386 |
if (*chars == ch) {
|
|
387 |
ch = *++chars;
|
|
388 |
pos++;
|
|
389 |
_INST(position) = _MKSMALLINT(pos);
|
2
|
390 |
RETURN ( anObject );
|
1
|
391 |
}
|
|
392 |
chars++;
|
|
393 |
pos++;
|
|
394 |
}
|
|
395 |
RETURN ( nil );
|
|
396 |
}
|
|
397 |
%}
|
|
398 |
.
|
59
|
399 |
^ super skipThrough:anObject
|
1
|
400 |
!
|
|
401 |
|
|
402 |
skipSeparators
|
|
403 |
"skip all whitespace; next will return next non-white-space element.
|
59
|
404 |
Return the peeked character or nil, if the end-of-stream was reached.
|
2
|
405 |
- reimplemented for speed on String-Streams for faster scanning"
|
1
|
406 |
|
|
407 |
%{ /* NOCONTEXT */
|
|
408 |
|
59
|
409 |
if (__isString(_INST(collection))
|
1
|
410 |
&& _isSmallInteger(_INST(position))
|
|
411 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
412 |
REGISTER unsigned char *chars;
|
|
413 |
REGISTER unsigned ch;
|
|
414 |
REGISTER int pos;
|
|
415 |
int limit;
|
|
416 |
|
|
417 |
pos = _intVal(_INST(position));
|
|
418 |
if (pos <= 0) {
|
|
419 |
RETURN ( nil );
|
|
420 |
}
|
|
421 |
|
|
422 |
limit = _intVal(_INST(readLimit));
|
|
423 |
if (limit > (_qSize(_INST(collection)) - OHDR_SIZE))
|
|
424 |
limit = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
425 |
|
|
426 |
chars = (unsigned char *)(_stringVal(_INST(collection)) + pos - 1);
|
|
427 |
while (pos <= limit) {
|
|
428 |
ch = *chars++;
|
|
429 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r')
|
|
430 |
&& (ch != '\n') && (ch != 0x0B)) {
|
|
431 |
_INST(position) = _MKSMALLINT(pos);
|
|
432 |
RETURN ( _MKCHARACTER(ch) );
|
|
433 |
}
|
|
434 |
pos++;
|
|
435 |
}
|
|
436 |
_INST(position) = _MKSMALLINT(pos);
|
|
437 |
RETURN ( nil );
|
|
438 |
}
|
|
439 |
%}
|
|
440 |
.
|
|
441 |
^ super skipSeparators
|
|
442 |
!
|
|
443 |
|
13
|
444 |
skipSeparatorsExceptCR
|
|
445 |
"skip all whitespace except newlines;
|
|
446 |
next will return next non-white-space element"
|
|
447 |
%{ /* NOCONTEXT */
|
|
448 |
|
59
|
449 |
if (__isString(_INST(collection))
|
13
|
450 |
&& _isSmallInteger(_INST(position))
|
|
451 |
&& _isSmallInteger(_INST(readLimit))) {
|
|
452 |
REGISTER unsigned char *chars;
|
|
453 |
REGISTER unsigned ch;
|
|
454 |
REGISTER int pos;
|
|
455 |
int limit;
|
|
456 |
|
|
457 |
pos = _intVal(_INST(position));
|
|
458 |
if (pos <= 0) {
|
|
459 |
RETURN ( nil );
|
|
460 |
}
|
|
461 |
|
|
462 |
limit = _intVal(_INST(readLimit));
|
|
463 |
if (limit > (_qSize(_INST(collection)) - OHDR_SIZE))
|
|
464 |
limit = _qSize(_INST(collection)) - OHDR_SIZE;
|
|
465 |
|
|
466 |
chars = (unsigned char *)(_stringVal(_INST(collection)) + pos - 1);
|
|
467 |
while (pos <= limit) {
|
|
468 |
ch = *chars++;
|
|
469 |
if ((ch != ' ') && (ch != '\t') && (ch != '\r') && (ch != 0x0B)) {
|
|
470 |
_INST(position) = _MKSMALLINT(pos);
|
|
471 |
RETURN ( _MKCHARACTER(ch) );
|
|
472 |
}
|
|
473 |
pos++;
|
|
474 |
}
|
|
475 |
_INST(position) = _MKSMALLINT(pos);
|
|
476 |
RETURN ( nil );
|
|
477 |
}
|
|
478 |
%}
|
|
479 |
.
|
|
480 |
^ super skipSeparatorsExceptCR
|
|
481 |
!
|
|
482 |
|
1
|
483 |
skipToAll:aCollection
|
|
484 |
"skip for the sequence given by the argument, aCollection;
|
|
485 |
return nil if not found, self otherwise. On a successful match, next read
|
|
486 |
will return elements of aCollection."
|
|
487 |
|
|
488 |
|oldPos buffer l first idx|
|
|
489 |
|
|
490 |
oldPos := self position.
|
|
491 |
l := aCollection size.
|
|
492 |
first := aCollection at:1.
|
|
493 |
[self atEnd] whileFalse:[
|
|
494 |
buffer := self next:l.
|
|
495 |
buffer = aCollection ifTrue:[
|
|
496 |
self position:(self position - l).
|
|
497 |
^ self
|
|
498 |
].
|
|
499 |
idx := buffer indexOf:first startingAt:2.
|
|
500 |
idx == 0 ifFalse:[
|
|
501 |
self position:(self position - l + idx - 1)
|
|
502 |
]
|
|
503 |
].
|
|
504 |
self position:oldPos.
|
|
505 |
^ nil
|
|
506 |
|
59
|
507 |
"
|
|
508 |
|s|
|
1
|
509 |
s := ReadStream on:'12345678901234567890'.
|
|
510 |
s skipToAll:'901'.
|
59
|
511 |
s next:4
|
|
512 |
"
|
1
|
513 |
! !
|
|
514 |
|
59
|
515 |
!ReadStream methodsFor:'writing'!
|
1
|
516 |
|
|
517 |
nextPut:anElement
|
10
|
518 |
"catch write access to readstreams - report an error"
|
|
519 |
|
|
520 |
self shouldNotImplement
|
1
|
521 |
! !
|