author | Claus Gittinger <cg@exept.de> |
Thu, 05 Mar 1998 13:46:28 +0100 | |
changeset 3316 | ba0a4305f900 |
parent 3112 | ccf7121ddae8 |
child 4271 | d1878c9dc428 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
202 | 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 |
PositionableStream subclass:#ReadStream |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
14 |
instanceVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
15 |
classVariableNames:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
16 |
poolDictionaries:'' |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
17 |
category:'Streams' |
1 | 18 |
! |
19 |
||
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
20 |
!ReadStream class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
202 | 25 |
All Rights Reserved |
1 | 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 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
ReadStream defines protocol for reading streamwise over collections. |
|
1295 | 39 |
|
40 |
[author:] |
|
41 |
Claus Gittinger |
|
42 |
||
88 | 43 |
" |
44 |
! ! |
|
1 | 45 |
|
202 | 46 |
!ReadStream methodsFor:'converting'! |
47 |
||
48 |
readStream |
|
49 |
"return a readStream from the receiver. Since this is already |
|
50 |
a readStream, return self." |
|
51 |
||
52 |
^ self |
|
53 |
! ! |
|
54 |
||
1400 | 55 |
!ReadStream methodsFor:'emphasis'! |
56 |
||
57 |
emphasis |
|
58 |
"return the emphasis of the current (i.e. next returned by #next) |
|
59 |
element. Streams on a string will return nil for all elements. |
|
60 |
Streams on collections which nothing at all about emphasises, |
|
61 |
will report an error." |
|
62 |
||
63 |
^ collection emphasisAt:position. |
|
64 |
||
65 |
" |
|
66 |
|t s| |
|
67 |
||
68 |
t := 'hello world' asText |
|
69 |
emphasizeFrom:1 to:5 with:#bold; |
|
70 |
emphasizeFrom:7 to:11 with:#italic. |
|
71 |
||
72 |
s := t readStream. |
|
73 |
[s atEnd] whileFalse:[ |
|
74 |
Transcript show:(s emphasis); show:' '. |
|
1422 | 75 |
Transcript show:''''; show:(s next); showCR:''''. |
1400 | 76 |
]. |
77 |
" |
|
78 |
||
79 |
"Modified: 15.5.1996 / 17:30:33 / cg" |
|
80 |
! ! |
|
81 |
||
345 | 82 |
!ReadStream methodsFor:'queries'! |
83 |
||
84 |
isReadable |
|
2997 | 85 |
"return true, if reading is supported by the recevier. |
86 |
Here, true is always returned." |
|
87 |
||
345 | 88 |
^ true |
2997 | 89 |
|
90 |
"Modified: 4.10.1997 / 17:59:21 / cg" |
|
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
91 |
! |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
92 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
93 |
size |
2997 | 94 |
"return the number of elements in the streamed collection." |
95 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
96 |
^ collection size |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
97 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
98 |
"Created: 13.9.1996 / 18:14:35 / cg" |
2997 | 99 |
"Modified: 4.10.1997 / 17:59:45 / cg" |
345 | 100 |
! ! |
101 |
||
59 | 102 |
!ReadStream methodsFor:'reading'! |
1 | 103 |
|
104 |
next |
|
105 |
"return the next element; advance read pointer. |
|
2 | 106 |
return nil, if there is no next element. |
345 | 107 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
1 | 108 |
|
109 |
|ret| |
|
110 |
||
111 |
%{ /* NOCONTEXT */ |
|
112 |
||
113 |
REGISTER int pos; |
|
114 |
unsigned ch; |
|
252 | 115 |
OBJ coll, p, l; |
1 | 116 |
|
1133 | 117 |
coll = __INST(collection); |
118 |
p = __INST(position); |
|
119 |
l = __INST(readLimit); |
|
1 | 120 |
|
252 | 121 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
122 |
||
1133 | 123 |
pos = __intVal(p); |
124 |
if (pos > 0 && pos <= __intVal(l)) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
125 |
OBJ cls; |
59 | 126 |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
127 |
cls = __qClass(coll); |
1133 | 128 |
if (cls == @global(String)) { |
129 |
if (pos <= __stringSize(coll)) { |
|
130 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
131 |
ch = __stringVal(coll)[pos-1]; |
|
132 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
133 |
} |
1133 | 134 |
} else if (cls == @global(ByteArray)) { |
135 |
if (pos <= __byteArraySize(coll)) { |
|
136 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
137 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
138 |
RETURN ( __MKSMALLINT(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
139 |
} |
1133 | 140 |
} else if (cls == @global(Array)) { |
141 |
if (pos <= __arraySize(coll)) { |
|
142 |
__INST(position) = __MKSMALLINT(pos + 1); |
|
143 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
144 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
145 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
146 |
} |
1 | 147 |
} |
148 |
%} |
|
149 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
150 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
1 | 151 |
ret := collection at:position. |
152 |
position := position + 1. |
|
153 |
^ ret |
|
154 |
! |
|
155 |
||
252 | 156 |
nextAlphaNumericWord |
1 | 157 |
"read the next word (i.e. up to non letter-or-digit). |
158 |
return a string containing those characters. |
|
3112
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
159 |
Skips any non-alphanumeric chars first. |
59 | 160 |
- tuned for speed on String-Streams for faster scanning" |
1 | 161 |
%{ |
162 |
/* speedup, if collection is a string */ |
|
163 |
||
164 |
int pos, limit, sz; |
|
165 |
int len; |
|
13 | 166 |
char buffer[256]; |
167 |
REGISTER unsigned char *cp; |
|
168 |
REGISTER unsigned ch; |
|
252 | 169 |
OBJ coll, p, l; |
1 | 170 |
|
1133 | 171 |
coll = __INST(collection); |
172 |
p = __INST(position); |
|
173 |
l = __INST(readLimit); |
|
252 | 174 |
|
175 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
176 |
||
1133 | 177 |
pos = __intVal(p); |
178 |
limit = __intVal(l); |
|
329 | 179 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 180 |
if (sz < limit) |
181 |
limit = sz; |
|
1133 | 182 |
cp = __stringVal(coll) + pos - 1; |
1 | 183 |
|
202 | 184 |
for (;;) { |
185 |
if (pos > limit) break; |
|
186 |
ch = *cp; |
|
1 | 187 |
|
3112
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
188 |
if (((ch >= 'a') && (ch <= 'z')) || |
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
189 |
((ch >= 'A') && (ch <= 'Z')) || |
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
190 |
((ch >= '0') && (ch <= '9'))) |
ccf7121ddae8
nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents:
2997
diff
changeset
|
191 |
break; |
202 | 192 |
cp++; |
193 |
pos++; |
|
194 |
} |
|
1 | 195 |
|
202 | 196 |
len = 0; |
197 |
for (;;) { |
|
198 |
if (pos > limit) break; |
|
199 |
ch = *cp & 0xFF; |
|
1 | 200 |
|
202 | 201 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
202 |
((ch >= 'A') && (ch <= 'Z')) || |
|
203 |
((ch >= '0') && (ch <= '9')))) |
|
204 |
break; |
|
205 |
buffer[len++] = ch; |
|
206 |
if (len >= (sizeof(buffer)-1)) { |
|
207 |
/* emergency */ |
|
208 |
break; |
|
209 |
} |
|
210 |
pos++; |
|
211 |
cp++; |
|
212 |
} |
|
1 | 213 |
|
1133 | 214 |
__INST(position) = __MKSMALLINT(pos); |
202 | 215 |
buffer[len] = '\0'; |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1665
diff
changeset
|
216 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil ); |
1 | 217 |
} |
218 |
%} |
|
219 |
. |
|
252 | 220 |
^ super nextAlphaNumericWord |
1 | 221 |
! |
222 |
||
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
223 |
nextByte |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
224 |
"return the next element as a byteValued integer" |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
225 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
226 |
|ret| |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
227 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
228 |
ret := self next. |
2446 | 229 |
ret notNil ifTrue:[ |
230 |
((ret < 0) or:[ret > 255]) ifTrue:[ |
|
231 |
self error:'oops - not a byte value in stream'. |
|
232 |
^ nil |
|
233 |
] |
|
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
234 |
]. |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
235 |
^ ret |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
236 |
|
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
237 |
"Created: 13.9.1996 / 18:10:38 / cg" |
2446 | 238 |
"Modified: 5.3.1997 / 21:16:12 / cg" |
1665
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
239 |
! |
928e9a308cea
added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents:
1488
diff
changeset
|
240 |
|
611 | 241 |
nextDecimalInteger |
242 |
"read the next integer in radix 10. dont skip whitespace. |
|
243 |
- tuned for speed on String-Streams for faster scanning" |
|
244 |
||
245 |
|value nextOne| |
|
246 |
%{ |
|
247 |
int pos, limit, sz; |
|
248 |
REGISTER unsigned char *cp; |
|
249 |
REGISTER unsigned ch; |
|
250 |
int val = 0; |
|
251 |
OBJ coll, p, l; |
|
252 |
||
1133 | 253 |
coll = __INST(collection); |
254 |
p = __INST(position); |
|
255 |
l = __INST(readLimit); |
|
611 | 256 |
|
257 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
258 |
||
1133 | 259 |
pos = __intVal(p); |
260 |
limit = __intVal(l); |
|
611 | 261 |
sz = __qSize(coll) - OHDR_SIZE; |
262 |
if (sz < limit) |
|
263 |
limit = sz; |
|
1133 | 264 |
cp = __stringVal(coll) + pos - 1; |
611 | 265 |
|
266 |
for (;;) { |
|
267 |
if (pos > limit) break; |
|
268 |
ch = *cp; |
|
269 |
||
270 |
if ((ch < '0') || (ch > '9')) break; |
|
271 |
val = val * 10 + (ch - '0'); |
|
272 |
pos++; |
|
273 |
if (val > (_MAX_INT / 10)) goto oops; |
|
274 |
cp++; |
|
275 |
} |
|
1133 | 276 |
__INST(position) = __MKSMALLINT(pos); |
277 |
return __MKSMALLINT(val); |
|
611 | 278 |
} |
279 |
oops: |
|
1133 | 280 |
value = __MKSMALLINT(val); |
611 | 281 |
%} |
282 |
. |
|
283 |
"fall-back for non-string streams - we have to continue where |
|
284 |
above primitive left off, in case of a large integer ... |
|
285 |
(instead of doing a super nextDecimalInteger)" |
|
286 |
||
287 |
nextOne := self peek. |
|
288 |
[nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[ |
|
289 |
value = (value * 10) + nextOne digitValue. |
|
290 |
nextOne := self nextPeek |
|
291 |
]. |
|
292 |
^ value |
|
293 |
! |
|
294 |
||
295 |
nextPeek |
|
296 |
"advance read pointer return the peek element. |
|
297 |
this is equivalent to (self next; peek). |
|
298 |
- tuned for speed on String-Streams for faster scanning" |
|
299 |
||
300 |
%{ /* NOCONTEXT */ |
|
301 |
OBJ coll, l, p; |
|
302 |
||
1133 | 303 |
coll = __INST(collection); |
304 |
p = __INST(position); |
|
305 |
l = __INST(readLimit); |
|
611 | 306 |
|
307 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
308 |
REGISTER int pos; |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
309 |
unsigned ch; |
611 | 310 |
|
1133 | 311 |
pos = __intVal(p); |
312 |
if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
313 |
pos += 1; |
1133 | 314 |
__INST(position) = __MKSMALLINT(pos); |
315 |
ch = __stringVal(coll)[pos-1]; |
|
316 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
317 |
} |
611 | 318 |
} |
319 |
%} |
|
320 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
321 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 322 |
position := position + 1. |
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
323 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 324 |
^ collection at:position |
325 |
! |
|
326 |
||
1 | 327 |
nextSymbol |
328 |
"read the next selector-symbol (i.e. up to non letter-or-digit). |
|
329 |
return a string containing those characters. |
|
59 | 330 |
- tuned for speed on String-Streams for faster scanning" |
1 | 331 |
%{ |
332 |
int pos, limit, sz; |
|
333 |
int len; |
|
13 | 334 |
char buffer[256]; |
335 |
REGISTER unsigned char *cp; |
|
336 |
REGISTER unsigned ch; |
|
252 | 337 |
OBJ coll, p, l; |
1 | 338 |
|
1133 | 339 |
coll = __INST(collection); |
340 |
p = __INST(position); |
|
341 |
l = __INST(readLimit); |
|
252 | 342 |
|
343 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
344 |
||
1133 | 345 |
pos = __intVal(p); |
346 |
limit = __intVal(l); |
|
329 | 347 |
sz = __qSize(coll) - OHDR_SIZE; |
202 | 348 |
if (sz < limit) |
349 |
limit = sz; |
|
1133 | 350 |
cp = __stringVal(coll) + pos - 1; |
1 | 351 |
|
202 | 352 |
len = 0; |
353 |
for (;;) { |
|
354 |
if (pos > limit) break; |
|
355 |
ch = *cp; |
|
1 | 356 |
|
202 | 357 |
if (! (((ch >= 'a') && (ch <= 'z')) || |
358 |
((ch >= 'A') && (ch <= 'Z')) || |
|
359 |
((ch >= '0') && (ch <= '9')) || |
|
360 |
(ch == ':'))) |
|
361 |
break; |
|
362 |
buffer[len++] = ch; |
|
363 |
if (len >= (sizeof(buffer)-1)) { |
|
364 |
/* emergency */ |
|
365 |
break; |
|
366 |
} |
|
367 |
pos++; |
|
368 |
cp++; |
|
369 |
} |
|
1 | 370 |
|
1133 | 371 |
__INST(position) = __MKSMALLINT(pos); |
202 | 372 |
buffer[len] = '\0'; |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1665
diff
changeset
|
373 |
RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil ); |
1 | 374 |
} |
375 |
%} |
|
376 |
. |
|
377 |
^ super nextSymbol |
|
378 |
! |
|
379 |
||
611 | 380 |
peek |
381 |
"return the next element; do NOT advance read pointer. |
|
382 |
return nil, if there is no next element. |
|
383 |
- tuned for a bit more speed on String/ByteArray/Array-Streams" |
|
1 | 384 |
|
385 |
%{ /* NOCONTEXT */ |
|
611 | 386 |
|
387 |
REGISTER int pos; |
|
388 |
unsigned ch; |
|
389 |
OBJ coll; |
|
390 |
OBJ cls, p, l; |
|
1 | 391 |
|
1133 | 392 |
coll = __INST(collection); |
393 |
p = __INST(position); |
|
394 |
l = __INST(readLimit); |
|
252 | 395 |
|
611 | 396 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
1 | 397 |
|
1133 | 398 |
pos = __intVal(p); |
399 |
if (pos <= __intVal(l) && pos > 0) { |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
400 |
cls = __qClass(coll); |
1133 | 401 |
if (cls == @global(String)) { |
402 |
if (pos <= __stringSize(coll)) { |
|
403 |
ch = __stringVal(coll)[pos-1]; |
|
404 |
RETURN ( __MKCHARACTER(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
405 |
} |
1133 | 406 |
} else if (cls == @global(ByteArray)) { |
407 |
if (pos <= __byteArraySize(coll)) { |
|
408 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
409 |
RETURN ( __MKSMALLINT(ch) ); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
410 |
} |
1133 | 411 |
} else if (cls == @global(Array)) { |
412 |
if (pos <= __arraySize(coll)) { |
|
413 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
414 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
415 |
} |
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
416 |
} |
1 | 417 |
} |
418 |
%} |
|
419 |
. |
|
970
3b59c9b38dbb
ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents:
829
diff
changeset
|
420 |
(position > readLimit) ifTrue:[^ self pastEnd]. |
611 | 421 |
^ collection at:position |
1 | 422 |
! |
423 |
||
3316 | 424 |
peekOrNil |
425 |
"return the next element; do NOT advance read pointer. |
|
426 |
return nil, if there is no next element. |
|
427 |
This is much like #peek - |
|
428 |
However, unlike #peek, this does not raise an atEnd-query signal - even |
|
429 |
if handled. Instead, nil is returned immediately." |
|
430 |
||
431 |
%{ /* NOCONTEXT */ |
|
432 |
||
433 |
REGISTER int pos; |
|
434 |
unsigned ch; |
|
435 |
OBJ coll; |
|
436 |
OBJ cls, p, l; |
|
437 |
||
438 |
coll = __INST(collection); |
|
439 |
p = __INST(position); |
|
440 |
l = __INST(readLimit); |
|
441 |
||
442 |
if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) { |
|
443 |
||
444 |
pos = __intVal(p); |
|
445 |
if (pos <= __intVal(l) && pos > 0) { |
|
446 |
cls = __qClass(coll); |
|
447 |
if (cls == @global(String)) { |
|
448 |
if (pos <= __stringSize(coll)) { |
|
449 |
ch = __stringVal(coll)[pos-1]; |
|
450 |
RETURN ( __MKCHARACTER(ch) ); |
|
451 |
} |
|
452 |
RETURN ( nil ); |
|
453 |
} else if (cls == @global(ByteArray)) { |
|
454 |
if (pos <= __byteArraySize(coll)) { |
|
455 |
ch = __ByteArrayInstPtr(coll)->ba_element[pos-1]; |
|
456 |
RETURN ( __MKSMALLINT(ch) ); |
|
457 |
} |
|
458 |
RETURN ( nil ); |
|
459 |
} else if (cls == @global(Array)) { |
|
460 |
if (pos <= __arraySize(coll)) { |
|
461 |
RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]); |
|
462 |
} |
|
463 |
RETURN ( nil ); |
|
464 |
} |
|
465 |
} |
|
466 |
} |
|
467 |
%} |
|
468 |
. |
|
469 |
(position > readLimit) ifTrue:[^ nil]. |
|
470 |
^ collection at:position |
|
471 |
! |
|
472 |
||
1 | 473 |
skipSeparators |
474 |
"skip all whitespace; next will return next non-white-space element. |
|
59 | 475 |
Return the peeked character or nil, if the end-of-stream was reached. |
2 | 476 |
- reimplemented for speed on String-Streams for faster scanning" |
1 | 477 |
|
478 |
%{ /* NOCONTEXT */ |
|
252 | 479 |
OBJ coll, p, l; |
1 | 480 |
|
1133 | 481 |
coll = __INST(collection); |
482 |
p = __INST(position); |
|
483 |
l = __INST(readLimit); |
|
252 | 484 |
|
485 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
1406 | 486 |
REGISTER unsigned char *chars; |
487 |
REGISTER unsigned ch; |
|
488 |
REGISTER int pos; |
|
489 |
int limit; |
|
490 |
int sz; |
|
1 | 491 |
|
1406 | 492 |
pos = __intVal(p); |
493 |
if (pos <= 0) { |
|
494 |
RETURN ( nil ); |
|
495 |
} |
|
1 | 496 |
|
1406 | 497 |
limit = __intVal(l); |
498 |
sz = __qSize(coll) - OHDR_SIZE; |
|
499 |
if (limit > sz) { |
|
500 |
limit = sz; |
|
501 |
} |
|
1 | 502 |
|
1406 | 503 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
504 |
while (pos <= limit) { |
|
505 |
pos++; |
|
506 |
ch = *chars++; |
|
507 |
if ((ch > 0x20) |
|
508 |
|| ((ch != ' ') |
|
509 |
&& (ch != '\t') |
|
510 |
&& (ch != '\r') |
|
511 |
&& (ch != '\n') |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
512 |
&& (ch != '\f') |
1406 | 513 |
&& (ch != 0x0B))) { |
514 |
__INST(position) = __MKSMALLINT(pos-1); |
|
515 |
RETURN ( __MKCHARACTER(ch) ); |
|
516 |
} |
|
517 |
} |
|
518 |
__INST(position) = __MKSMALLINT(pos); |
|
519 |
RETURN ( nil ); |
|
1 | 520 |
} |
1406 | 521 |
%}. |
1 | 522 |
^ super skipSeparators |
1406 | 523 |
|
524 |
" |
|
525 |
|s| |
|
526 |
||
527 |
s := ' hello world ' readStream. |
|
528 |
s skipSeparators. |
|
529 |
s next. |
|
530 |
" |
|
1 | 531 |
! |
532 |
||
13 | 533 |
skipSeparatorsExceptCR |
534 |
"skip all whitespace except newlines; |
|
202 | 535 |
next will return next non-white-space element. |
536 |
- reimplemented for speed on String-Streams for faster scanning" |
|
537 |
||
13 | 538 |
%{ /* NOCONTEXT */ |
539 |
||
252 | 540 |
OBJ coll, p, l; |
541 |
||
1133 | 542 |
coll = __INST(collection); |
543 |
p = __INST(position); |
|
544 |
l = __INST(readLimit); |
|
252 | 545 |
|
546 |
if (__isString(coll) && __bothSmallInteger(p, l)) { |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
547 |
REGISTER unsigned char *chars; |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
548 |
REGISTER unsigned ch; |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
549 |
REGISTER int pos; |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
550 |
int limit; |
13 | 551 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
552 |
pos = __intVal(p); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
553 |
if (pos <= 0) { |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
554 |
RETURN ( nil ); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
555 |
} |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
556 |
|
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
557 |
limit = __intVal(l); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
558 |
if (limit > (__qSize(coll) - OHDR_SIZE)) |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
559 |
limit = __qSize(coll) - OHDR_SIZE; |
13 | 560 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
561 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
562 |
while (pos <= limit) { |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
563 |
ch = *chars++; |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
564 |
if (((int)ch > 0x20) |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
565 |
|| ( |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
566 |
(ch != ' ') |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
567 |
&& (ch != '\t') |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
568 |
&& (ch != '\f') |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
569 |
&& (ch != '\b') |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
570 |
&& (ch != 0x0B))) { |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
571 |
__INST(position) = __MKSMALLINT(pos); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
572 |
RETURN ( __MKCHARACTER(ch) ); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
573 |
} |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
574 |
pos++; |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
575 |
} |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
576 |
__INST(position) = __MKSMALLINT(pos); |
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
577 |
RETURN ( nil ); |
13 | 578 |
} |
579 |
%} |
|
580 |
. |
|
581 |
^ super skipSeparatorsExceptCR |
|
582 |
! |
|
583 |
||
611 | 584 |
skipThrough:anObject |
585 |
"skip all objects up-to and including anObject, return anObject on success, |
|
586 |
nil if end-of-stream is reached before. |
|
587 |
On success, the next read operation will return the element after anObject. |
|
588 |
- reimplemented for speed on String-Streams for faster scanning" |
|
589 |
||
590 |
%{ /* NOCONTEXT */ |
|
591 |
OBJ coll, p, l; |
|
592 |
||
1133 | 593 |
coll = __INST(collection); |
594 |
p = __INST(position); |
|
595 |
l = __INST(readLimit); |
|
611 | 596 |
|
597 |
if (__isString(coll) |
|
598 |
&& __isCharacter(anObject) |
|
599 |
&& __bothSmallInteger(p, l)) { |
|
600 |
REGISTER unsigned char *chars; |
|
601 |
REGISTER int pos, limit; |
|
602 |
unsigned ch; |
|
603 |
int sz; |
|
604 |
||
1133 | 605 |
pos = __intVal(p); |
611 | 606 |
if (pos <= 0) { |
607 |
RETURN ( nil ); |
|
608 |
} |
|
609 |
||
1133 | 610 |
limit = __intVal(l); |
611 |
sz = __stringSize(coll); |
|
611 | 612 |
if (limit > sz) limit = sz; |
613 |
||
1133 | 614 |
chars = (unsigned char *)(__stringVal(coll) + pos - 1); |
615 |
ch = __intVal(_characterVal(anObject)) & 0xFF; |
|
611 | 616 |
while (pos < limit) { |
617 |
if (*chars == ch) { |
|
618 |
ch = *++chars; |
|
619 |
pos++; |
|
1133 | 620 |
__INST(position) = __MKSMALLINT(pos); |
611 | 621 |
RETURN ( anObject ); |
622 |
} |
|
623 |
chars++; |
|
624 |
pos++; |
|
625 |
} |
|
626 |
RETURN ( nil ); |
|
627 |
} |
|
628 |
%} |
|
629 |
. |
|
630 |
^ super skipThrough:anObject |
|
1 | 631 |
! ! |
632 |
||
59 | 633 |
!ReadStream methodsFor:'writing'! |
1 | 634 |
|
635 |
nextPut:anElement |
|
10 | 636 |
"catch write access to readstreams - report an error" |
637 |
||
638 |
self shouldNotImplement |
|
1 | 639 |
! ! |
611 | 640 |
|
2162
7e1601f633b6
ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
641 |
!ReadStream class methodsFor:'documentation'! |
701 | 642 |
|
643 |
version |
|
3316 | 644 |
^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.37 1998-03-05 12:46:28 cg Exp $' |
701 | 645 |
! ! |