author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:42:15 +0200 | |
changeset 3903 | 5730e66fd49c |
parent 3719 | 3750518f5ada |
child 4084 | 14a4edd54856 |
permissions | -rw-r--r-- |
1463 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3539 | 3 |
"{ NameSpace: Smalltalk }" |
4 |
||
1463 | 5 |
Object subclass:#PrintfScanf |
6 |
instanceVariableNames:'' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
9 |
category:'System-Support' |
|
10 |
! |
|
11 |
||
12 |
!PrintfScanf class methodsFor:'documentation'! |
|
13 |
||
1465 | 14 |
documentation |
15 |
" |
|
16 |
Contributed by Jan Steinman donated to the |
|
17 |
community in 1989. |
|
18 |
||
19 |
Provided AS-IS - no warranty, use at your own risk. |
|
20 |
||
21 |
Original comment: |
|
22 |
||
23 |
NAME printf-scanf |
|
24 |
AUTHOR Jan Steinman <jans@tekgvs.labs.tek.com> |
|
25 |
FUNCTION printf and scanf for Smalltalk |
|
26 |
ST-VERSIONS Tek 2.2.2a 4.0 |
|
27 |
PREREQUISITES CharacterComparing |
|
28 |
CONFLICTS |
|
29 |
DISTRIBUTION world |
|
30 |
VERSION 1.1 |
|
31 |
DATE Apr 1989? |
|
32 |
SUMMARY |
|
33 |
||
34 |
The following methods implement printf and scanf functionality. They |
|
35 |
are intended to be used to ease porting between Smalltalk and C, and |
|
36 |
for facilitating machine-machine communication. They are not at all |
|
37 |
intended as replacements for Smalltalk's printOn: functionality. |
|
38 |
||
39 |
Jan Steinman - N7JDB |
|
40 |
Tektronix Electronic Systems Laboratory |
|
41 |
Box 500, MS 50-370, Beaverton, OR 97077 |
|
42 |
(w)503/627-5881 (h)503/657-7703 |
|
43 |
" |
|
44 |
! |
|
45 |
||
1463 | 46 |
examples |
47 |
" |
|
48 |
self new printf:'%#x %#X %03o%*.*s' arguments: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') |
|
49 |
||
3115 | 50 |
self new printf:'%- 10.4s%.2e' arguments: { 'abcdefghijkl' . Float pi } |
1463 | 51 |
|
3115 | 52 |
self new printf:'%8.3f' arguments: { 200 sqrt negated } |
1463 | 53 |
|
2889 | 54 |
self new printf:'%x' arguments: #(16r41) |
55 |
self new printf:'%#x' arguments: #(16r41) |
|
56 |
self new printf:'%d' arguments: #(16r41) |
|
57 |
self new printf:'%b' arguments: #(16r41) |
|
1464 | 58 |
self new printf:'%c' arguments: #(16r41) |
59 |
self new printf:'%c' arguments: #( $A ) |
|
60 |
self new printf:'%s' arguments: #( $A ) |
|
61 |
self new printf:'%s' arguments: #( 'hello' ) |
|
62 |
self new printf:'%4s' arguments: #( 'hello' ) |
|
63 |
self new printf:'%7s' arguments: #( 'hello' ) |
|
1463 | 64 |
|
3115 | 65 |
self new sscanf:'%f%2s%s%s%s' fromString: '237.0 this is a test' |
1463 | 66 |
|
1464 | 67 |
self new sscanf:'%d%f%s' fromString: '25 54.32e-01 monday' |
68 |
||
69 |
self new sscanf:'%f%*f %8[A-F0-9]%c%d 0x%x%f' fromString: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
1463 | 70 |
|
71 |
'%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') |
|
72 |
||
3115 | 73 |
'%- 10.4s%.2e' printf: { 'abcdefghijkl' . Float pi } |
1463 | 74 |
|
3115 | 75 |
'%8.3f' printf: { 200 sqrt negated } |
1463 | 76 |
|
77 |
'%c' printf: #(16r41) |
|
78 |
||
79 |
'%f%2s%s%s%s' sscanf: '237.0 this is a test' |
|
80 |
||
81 |
'%d%f%s' sscanf: '25 54.32e-01 monday' |
|
82 |
||
83 |
'%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
84 |
" |
|
85 |
! ! |
|
86 |
||
2875 | 87 |
!PrintfScanf class methodsFor:'printing'! |
1463 | 88 |
|
2875 | 89 |
printf:formatString arguments:args |
90 |
"Format and print the receiver with <args> formatted in C style, |
|
91 |
as described in the UTek manual page for printf(3)." |
|
1463 | 92 |
|
3115 | 93 |
^ self new printf:formatString arguments:args |
1463 | 94 |
! |
95 |
||
2875 | 96 |
printf:formatString on:outStream arguments: args |
97 |
"Format and print formatString on <outStream> with <args> |
|
1463 | 98 |
formatted in C style, as described in the UTek manual page for |
99 |
printf(3). This method is designed for producing output |
|
100 |
suitable for a machine." |
|
101 |
||
2875 | 102 |
^ self new printf:formatString on:outStream arguments: args |
1463 | 103 |
! ! |
104 |
||
2875 | 105 |
!PrintfScanf class methodsFor:'scanning'! |
106 |
||
107 |
scanf:formatString fromStream:dataStream |
|
108 |
"Return a Collection of objects found in the Character Stream |
|
109 |
<dataStream> as interpreted according to the receiver. The |
|
110 |
receiver is assumed to be a conversion control string as |
|
111 |
specified in the UTek manual page for scanf(3)." |
|
112 |
||
3115 | 113 |
^ self new scanf:formatString fromStream:dataStream |
2875 | 114 |
! |
115 |
||
116 |
sscanf:formatString fromString:aString |
|
117 |
"Return a Collection of objects found in <string> as |
|
118 |
interpreted according to the receiver. The receiver is |
|
119 |
assumed to be a conversion control string as specified in the |
|
120 |
UTek manual page for scanf(3)." |
|
121 |
||
3115 | 122 |
^ self new sscanf:formatString fromString:aString |
2875 | 123 |
! ! |
124 |
||
125 |
!PrintfScanf methodsFor:'helpers'! |
|
1463 | 126 |
|
127 |
absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
128 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits, using decimal notation." |
|
129 |
||
130 |
|exp x fuzz i| |
|
131 |
||
132 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
|
133 |
exp := aFloat abs < 1.0 ifTrue:[ |
|
134 |
(10.0 / aFloat abs) log floor negated |
|
135 |
] ifFalse:[ |
|
136 |
aFloat abs log floor |
|
137 |
]. |
|
138 |
x := aFloat abs / (10.0 raisedTo:exp). |
|
139 |
fuzz := 10.0 raisedTo:1 - digits. |
|
140 |
x := 0.5 * fuzz + x. |
|
141 |
x >= 10.0 ifTrue:[ |
|
142 |
"check if rounding has unnormalized x" |
|
143 |
x := x / 10.0. |
|
144 |
exp := exp + 1 |
|
145 |
]. |
|
146 |
exp < 0 ifTrue:[ |
|
147 |
1 to:1 - exp do:[:j | |
|
148 |
aStream nextPut:('0.000000000000' at:j) |
|
149 |
] |
|
150 |
]. |
|
151 |
[ x >= fuzz ] whileTrue:[ |
|
152 |
"use fuzz to track significance" |
|
153 |
i := x truncated. |
|
154 |
aStream nextPut:(48 + i) asCharacter. |
|
155 |
x := (x - i) * 10.0. |
|
156 |
fuzz := fuzz * 10.0. |
|
157 |
exp := exp - 1. |
|
158 |
exp = -1 ifTrue:[ |
|
159 |
aStream nextPut:$. |
|
160 |
] |
|
161 |
]. |
|
162 |
[ exp >= -1 ] whileTrue:[ |
|
163 |
aStream nextPut:$0. |
|
164 |
exp := exp - 1. |
|
165 |
exp = -1 ifTrue:[ |
|
166 |
aStream nextPut:$. |
|
167 |
] |
|
168 |
] |
|
169 |
! |
|
170 |
||
171 |
absPrintFloat:aFloat on:aStream digits:digits |
|
172 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits." |
|
173 |
||
174 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
175 |
self |
|
176 |
absDecimalPrintFloat:aFloat |
|
177 |
on:aStream |
|
178 |
digits:digits |
|
179 |
] ifFalse:[ |
|
180 |
aFloat |
|
181 |
absScientificPrintFloat:aFloat |
|
182 |
on:aStream |
|
183 |
digits:digits |
|
184 |
] |
|
185 |
! |
|
186 |
||
187 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
|
188 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits, using scientific notation." |
|
189 |
||
190 |
|exp fuzz x q i| |
|
191 |
||
192 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
|
193 |
exp := aFloat abs < 1.0 ifTrue:[ |
|
194 |
(10.0 / aFloat abs) log floor negated |
|
195 |
] ifFalse:[ |
|
196 |
aFloat abs log floor |
|
197 |
]. |
|
198 |
x := aFloat abs / (10.0 raisedTo:exp). |
|
199 |
fuzz := 10.0 raisedTo:1 - digits. |
|
200 |
x := 0.5 * fuzz + x. |
|
201 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
202 |
x := x / 10.0. |
|
203 |
exp := exp + 1 |
|
204 |
]. |
|
205 |
q := exp. |
|
206 |
exp := 0. |
|
207 |
[ x >= fuzz ] whileTrue:[ |
|
208 |
"use fuzz to track significance" |
|
209 |
i := x truncated. |
|
210 |
aStream nextPut:(48 + i) asCharacter. |
|
211 |
x := (x - i) * 10.0. |
|
212 |
fuzz := fuzz * 10.0. |
|
213 |
exp := exp - 1. |
|
214 |
exp = -1 ifTrue:[ |
|
215 |
aStream nextPut:$. |
|
216 |
] |
|
217 |
]. |
|
218 |
[ exp >= -1 ] whileTrue:[ |
|
219 |
aStream nextPut:$0. |
|
220 |
exp := exp - 1. |
|
221 |
exp = -1 ifTrue:[ |
|
222 |
aStream nextPut:$. |
|
223 |
] |
|
224 |
]. |
|
225 |
aStream nextPut:$e. |
|
226 |
q printOn:aStream |
|
227 |
! |
|
228 |
||
229 |
formatArgCountFor:aFormatString |
|
230 |
"Return the number of arguments required/produced, |
|
231 |
if the argument is interpreted as a printf/scanf format control string." |
|
232 |
||
233 |
|nonConsecutive count| |
|
234 |
||
235 |
nonConsecutive := true. |
|
236 |
count := 0. |
|
237 |
aFormatString do:[:c | |
|
238 |
c == $% ifTrue:[ |
|
239 |
nonConsecutive ifTrue:[ |
|
240 |
count := count + 1. |
|
241 |
nonConsecutive := false |
|
242 |
] ifFalse:[ |
|
243 |
count := count - 1. |
|
244 |
nonConsecutive := true |
|
245 |
] |
|
246 |
] ifFalse:[ |
|
247 |
nonConsecutive := true |
|
248 |
] |
|
249 |
]. |
|
250 |
^ count |
|
251 |
! ! |
|
252 |
||
2875 | 253 |
!PrintfScanf methodsFor:'printing'! |
254 |
||
255 |
printArgFrom:inStream to:outStream arguments:argStream |
|
256 |
"Interpret the required number of arguments from <argStream> |
|
257 |
according to the formatting information in <inStream>. Place |
|
258 |
the interpretation on <outStream>. The interpretation is C |
|
259 |
printf(3) style, as described in the UTek manual page for |
|
260 |
printf(3). <inStream> is assumed to be positioned just past |
|
261 |
$%, and a complete control string is assumed available. |
|
262 |
||
263 |
Return when the conversion control string is consumed. |
|
264 |
Leave <inStream> pointing past the last character in the conversion control string. |
|
265 |
||
266 |
This code assumes that <inStream> is formatted according to |
|
267 |
specification, and error checking is minimal. Unexpected |
|
268 |
results will be obtained by illegal control strings, or when |
|
269 |
argument types do not match conversion codes, but it probably |
|
270 |
won't dump core, like C does in such cases!!!!" |
|
271 |
||
272 |
| ljust plus pound width precision pad char arg argString | |
|
273 |
||
274 |
ljust := plus := pound := false. |
|
275 |
width := 0. |
|
276 |
precision := SmallInteger maxVal. |
|
277 |
pad := $ . |
|
278 |
char := inStream peek. |
|
279 |
||
280 |
char == $% ifTrue:[ |
|
281 |
^ outStream nextPut: inStream next |
|
282 |
]. |
|
283 |
||
284 |
char == $- ifTrue:[ |
|
285 |
ljust := true. |
|
286 |
inStream next. |
|
287 |
char := inStream peek |
|
288 |
]. |
|
289 |
||
290 |
char == $ ifTrue:[ |
|
291 |
outStream space. |
|
292 |
inStream next. |
|
293 |
char := inStream peek |
|
294 |
]. |
|
295 |
||
296 |
char == $+ ifTrue:[ |
|
297 |
plus := true. |
|
298 |
inStream next. |
|
299 |
char := inStream peek |
|
300 |
]. |
|
301 |
||
302 |
char == $# ifTrue:[ |
|
303 |
pound := true. |
|
304 |
inStream next. |
|
305 |
char := inStream peek |
|
306 |
]. |
|
307 |
||
308 |
char == $* ifTrue:[ |
|
309 |
width := argStream next. |
|
310 |
inStream next. |
|
311 |
char := inStream peek |
|
312 |
]. |
|
313 |
||
314 |
char isDigit ifTrue:[ |
|
2889 | 315 |
char == $0 ifTrue: [pad := $0]. |
2875 | 316 |
width := Integer readFrom: inStream. |
317 |
char := inStream peek |
|
318 |
]. |
|
319 |
||
320 |
char == $. ifTrue:[ |
|
2889 | 321 |
inStream next. char := inStream peek. |
2875 | 322 |
char == $* |
2889 | 323 |
ifTrue: [precision := argStream next. inStream next.] |
324 |
ifFalse: [precision := Integer readFrom: inStream.]. |
|
325 |
char := inStream peek |
|
2875 | 326 |
]. |
327 |
||
328 |
char == $l ifTrue:[ |
|
329 |
"Ignore long specifier." |
|
2889 | 330 |
inStream next. char := inStream peek |
2875 | 331 |
]. |
332 |
||
333 |
('feg' includes: char) ifTrue:[ |
|
2889 | 334 |
arg := argStream next asFloat. |
335 |
precision := precision min: 6. |
|
336 |
argString := WriteStream on: String new. |
|
2875 | 337 |
char == $g ifTrue: |
338 |
[self absPrintFloat:arg on: argString digits: precision + 1]. |
|
339 |
char == $f ifTrue: |
|
340 |
[self absDecimalPrintFloat:arg on: argString digits: precision + arg abs log + 1]. |
|
341 |
char == $e ifTrue: |
|
342 |
[self absScientificPrintFloat:arg on: argString digits: precision + 1]. |
|
2889 | 343 |
argString := argString contents. |
2875 | 344 |
arg < 0 |
2889 | 345 |
ifTrue: [argString := '-', argString] |
346 |
ifFalse: [plus ifTrue: [argString := '+', argString]]. |
|
2875 | 347 |
(precision = 0 and: [pound not]) ifTrue: |
348 |
[(argString includes: $e) |
|
349 |
ifTrue: ["self halt"] |
|
350 |
ifFalse: |
|
2889 | 351 |
[argString := arg truncated printString]]. |
2875 | 352 |
pound ifTrue: |
353 |
[(argString includes: $e) |
|
354 |
ifTrue: ["self halt"] |
|
355 |
ifFalse: |
|
356 |
[precision - (argString size - (argString indexOf: $.)) timesRepeat: |
|
2889 | 357 |
[argString := argString, '0']]]. |
2875 | 358 |
ljust ifTrue: [outStream nextPutAll: argString]. |
359 |
width - argString size timesRepeat: [outStream nextPut: pad]. |
|
360 |
ljust ifFalse: [outStream nextPutAll: argString]. |
|
361 |
^inStream next |
|
362 |
]. |
|
363 |
||
364 |
char == $c ifTrue:[ |
|
3539 | 365 |
arg := argStream next asCharacter asString |
2875 | 366 |
]. |
367 |
||
368 |
char == $s ifTrue:[ |
|
369 |
"Assume the arg is a String or Symbol." |
|
2889 | 370 |
arg := argStream next asString |
2875 | 371 |
]. |
372 |
||
373 |
char == $d ifTrue:[ |
|
2889 | 374 |
arg := argStream next asInteger printString. |
375 |
plus ifTrue: [arg := '+', arg] |
|
2875 | 376 |
]. |
377 |
||
378 |
char == $u ifTrue:[ |
|
2889 | 379 |
arg := argStream next asInteger abs printString |
2875 | 380 |
]. |
381 |
||
382 |
char == $o ifTrue:[ |
|
2889 | 383 |
arg := argStream next asInteger abs printStringRadix: 8. |
384 |
pound ifTrue: [arg := '0', arg] |
|
2875 | 385 |
]. |
386 |
||
387 |
('xX' includes: char) ifTrue:[ |
|
2889 | 388 |
arg := argStream next asInteger abs printStringRadix: 16. |
389 |
pound ifTrue: [arg := '0x', arg] |
|
390 |
]. |
|
391 |
('bB' includes: char) ifTrue:[ |
|
392 |
arg := argStream next asInteger abs printStringRadix: 2. |
|
393 |
pound ifTrue: [arg := '0b', arg] |
|
2875 | 394 |
]. |
395 |
||
396 |
char == $x ifTrue:[ |
|
397 |
1 to: arg size do: [:i | |
|
398 |
('ABCDEF' includes: (arg at: i)) ifTrue: |
|
3719 | 399 |
[arg at: i put: (arg at: i) asLowercase] |
400 |
] |
|
2875 | 401 |
]. |
402 |
||
2889 | 403 |
precision := precision min: arg size. |
2875 | 404 |
ljust ifTrue: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
405 |
width - precision timesRepeat: [outStream nextPut: pad]. |
|
406 |
ljust ifFalse: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
|
407 |
^ inStream next |
|
408 |
||
409 |
"Modified (format): / 24-07-2011 / 08:39:04 / cg" |
|
410 |
! |
|
411 |
||
412 |
printf:aString arguments:args |
|
413 |
"Format and print the receiver with <args> formatted in C style, |
|
414 |
as described in the UTek manual page for printf(3)." |
|
415 |
||
416 |
|aStream| |
|
417 |
||
3539 | 418 |
aStream := WriteStream on:(aString species new:100). |
2875 | 419 |
self |
420 |
printf:aString |
|
421 |
on:aStream |
|
422 |
arguments:args. |
|
423 |
^ aStream contents |
|
424 |
||
425 |
" |
|
426 |
self new printf:'%d %x' arguments:#(1234 45054) |
|
427 |
" |
|
428 |
! |
|
429 |
||
430 |
printf:aFormatString on:outStream arguments: args |
|
431 |
"Format and print aFormatString on <outStream> with <args> |
|
432 |
formatted in C style, as described in the UTek manual page for |
|
433 |
printf(3). This method is designed for producing output |
|
434 |
suitable for a machine." |
|
435 |
||
436 |
| argStream inStream char | |
|
437 |
||
438 |
argStream := ReadStream on: args. |
|
439 |
inStream := ReadStream on: aFormatString. |
|
440 |
[inStream atEnd] whileFalse:[ |
|
441 |
(char := inStream next) == $% ifFalse: [ |
|
442 |
outStream nextPut: char |
|
443 |
] ifTrue: [ |
|
444 |
self printArgFrom:inStream to:outStream arguments:argStream |
|
445 |
] |
|
446 |
] |
|
447 |
! ! |
|
448 |
||
1463 | 449 |
!PrintfScanf methodsFor:'scanning'! |
450 |
||
451 |
scanArgFrom:dataStream to:collection format:format |
|
452 |
"Add to <collection> an object who's representation is found |
|
453 |
in <dataStream> interpreted according to the conversion |
|
454 |
control string in the Stream <format>. <format> is assumed to |
|
455 |
be positioned just past a $%, and a complete control string is |
|
456 |
assumed available. |
|
457 |
||
458 |
Return when the conversion control string is consumed. Leave |
|
459 |
<format> pointing past the last character in the conversion |
|
460 |
control string, leave <dataStream> pointing past any width |
|
461 |
specified in <format>, or at the first character that doesn't |
|
462 |
make sense for the <format>." |
|
463 |
||
464 |
|final width char pos data scanset exclusive return last| |
|
465 |
||
466 |
final := [:retval | |
|
467 |
collection add:retval. |
|
468 |
data == dataStream ifFalse:[ |
|
469 |
dataStream position:dataStream position + data position |
|
470 |
]. |
|
471 |
^ self |
|
472 |
]. |
|
1464 | 473 |
|
1463 | 474 |
char := format peek. |
475 |
char == $% ifTrue:[ |
|
476 |
^ dataStream peekFor:char |
|
477 |
]. |
|
478 |
char == $* ifTrue:[ |
|
479 |
format next. |
|
480 |
char := format peek. |
|
481 |
final := [:retval | |
|
482 |
data == dataStream ifFalse:[ |
|
483 |
dataStream position:dataStream position + data position |
|
484 |
]. |
|
485 |
^ self |
|
486 |
] |
|
487 |
]. |
|
1464 | 488 |
width := 0. |
1463 | 489 |
char isDigit ifTrue:[ |
490 |
width := Integer readFrom:format. |
|
491 |
char := format peek |
|
492 |
]. |
|
493 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
494 |
dataStream skipSeparators |
|
495 |
]. |
|
496 |
width = 0 ifTrue:[ |
|
497 |
data := dataStream |
|
498 |
] ifFalse:[ |
|
499 |
pos := dataStream position. |
|
500 |
data := ReadStream on:(dataStream next:width). |
|
501 |
dataStream position:pos |
|
502 |
]. |
|
503 |
char == $s ifTrue:[ |
|
504 |
final value:(data upToSeparator) |
|
505 |
]. |
|
506 |
char == $c ifTrue:[ |
|
507 |
width = 0 ifTrue:[ |
|
508 |
final value:(String with:data next) |
|
509 |
] ifFalse:[ |
|
510 |
final value:data contents |
|
511 |
] |
|
512 |
]. |
|
513 |
char == $[ ifTrue:[ |
|
514 |
"What a mess!!!!" |
|
3539 | 515 |
return := CharacterWriteStream on:(String new:8). |
1463 | 516 |
scanset := IdentitySet new. |
517 |
format next. |
|
518 |
width = 0 ifTrue:[ |
|
519 |
width := SmallInteger maxVal |
|
520 |
]. |
|
521 |
exclusive := format peekFor:$^. |
|
522 |
[ |
|
523 |
last := char. |
|
524 |
char := format next. |
|
525 |
char == $] |
|
526 |
] whileFalse:[ |
|
527 |
char == $- ifFalse:[ |
|
528 |
scanset add:char |
|
529 |
] ifTrue:[ |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
530 |
last to:format next do:[:c | |
1463 | 531 |
scanset add:c |
532 |
] |
|
533 |
] |
|
534 |
]. |
|
535 |
[ |
|
536 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
537 |
] whileTrue:[ return nextPut:data next ]. |
|
538 |
final value:return contents |
|
539 |
]. |
|
540 |
('lh' includes:char) ifTrue:[ |
|
541 |
format next. |
|
542 |
char := format peek |
|
543 |
]. |
|
544 |
('DUdu' includes:char) ifTrue:[ |
|
545 |
final value:(Integer readFrom:data) |
|
546 |
]. |
|
547 |
('FEGfeg' includes:char) ifTrue:[ |
|
548 |
final value:(Float readFrom:data) |
|
549 |
]. |
|
550 |
('Oo' includes:char) ifTrue:[ |
|
551 |
final value:(Integer readFrom:data radix:8) |
|
552 |
]. |
|
553 |
('Xx' includes:char) ifTrue:[ |
|
554 |
final value:(Integer readFrom:data radix:16) |
|
2889 | 555 |
]. |
556 |
('Bb' includes:char) ifTrue:[ |
|
557 |
final value:(Integer readFrom:data radix:2) |
|
558 |
]. |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
559 |
|
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
560 |
"Modified: / 29-11-2011 / 11:55:32 / cg" |
1463 | 561 |
! |
562 |
||
563 |
scanf:formatString fromStream:dataStream |
|
564 |
"Return a Collection of objects found in the Character Stream |
|
565 |
<dataStream> as interpreted according to the receiver. The |
|
566 |
receiver is assumed to be a conversion control string as |
|
567 |
specified in the UTek manual page for scanf(3)." |
|
568 |
||
569 |
|results format char| |
|
570 |
||
571 |
results := OrderedCollection new. |
|
572 |
format := ReadStream on:formatString. |
|
573 |
[ format atEnd ] whileFalse:[ |
|
574 |
char := format next. |
|
575 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
576 |
dataStream skipSeparators. |
|
577 |
format skipSeparators |
|
578 |
]. |
|
579 |
char == $% ifTrue:[ |
|
580 |
self |
|
581 |
scanArgFrom:dataStream |
|
582 |
to:results |
|
583 |
format:format |
|
584 |
] ifFalse:[ |
|
585 |
dataStream peekFor:char |
|
586 |
] |
|
587 |
]. |
|
588 |
^ results |
|
589 |
! |
|
590 |
||
591 |
sscanf:formatString fromString:aString |
|
592 |
"Return a Collection of objects found in <string> as |
|
593 |
interpreted according to the receiver. The receiver is |
|
594 |
assumed to be a conversion control string as specified in the |
|
595 |
UTek manual page for scanf(3)." |
|
596 |
||
597 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
598 |
||
599 |
" |
|
600 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
601 |
" |
|
602 |
! ! |
|
603 |
||
604 |
!PrintfScanf class methodsFor:'documentation'! |
|
605 |
||
606 |
version |
|
3719 | 607 |
^ '$Header$' |
3115 | 608 |
! |
609 |
||
610 |
version_CVS |
|
3719 | 611 |
^ '$Header$' |
1463 | 612 |
! ! |
2875 | 613 |