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