author | Claus Gittinger <cg@exept.de> |
Wed, 23 Jan 2013 11:01:22 +0100 | |
changeset 2875 | 17ad0f5c8f53 |
parent 2678 | a2f6f1e865e6 |
child 2889 | 7491ae1f59ac |
permissions | -rw-r--r-- |
1463 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3 |
Object subclass:#PrintfScanf |
|
4 |
instanceVariableNames:'' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'System-Support' |
|
8 |
! |
|
9 |
||
10 |
!PrintfScanf class methodsFor:'documentation'! |
|
11 |
||
1465 | 12 |
documentation |
13 |
" |
|
14 |
Contributed by Jan Steinman donated to the |
|
15 |
community in 1989. |
|
16 |
||
17 |
Provided AS-IS - no warranty, use at your own risk. |
|
18 |
||
19 |
Original comment: |
|
20 |
||
21 |
NAME printf-scanf |
|
22 |
AUTHOR Jan Steinman <jans@tekgvs.labs.tek.com> |
|
23 |
FUNCTION printf and scanf for Smalltalk |
|
24 |
ST-VERSIONS Tek 2.2.2a 4.0 |
|
25 |
PREREQUISITES CharacterComparing |
|
26 |
CONFLICTS |
|
27 |
DISTRIBUTION world |
|
28 |
VERSION 1.1 |
|
29 |
DATE Apr 1989? |
|
30 |
SUMMARY |
|
31 |
||
32 |
The following methods implement printf and scanf functionality. They |
|
33 |
are intended to be used to ease porting between Smalltalk and C, and |
|
34 |
for facilitating machine-machine communication. They are not at all |
|
35 |
intended as replacements for Smalltalk's printOn: functionality. |
|
36 |
||
37 |
Jan Steinman - N7JDB |
|
38 |
Tektronix Electronic Systems Laboratory |
|
39 |
Box 500, MS 50-370, Beaverton, OR 97077 |
|
40 |
(w)503/627-5881 (h)503/657-7703 |
|
41 |
" |
|
42 |
! |
|
43 |
||
1463 | 44 |
examples |
45 |
" |
|
46 |
self new printf:'%#x %#X %03o%*.*s' arguments: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') |
|
47 |
||
48 |
self new printf:'%- 10.4s%.2e' arguments: (Array with: 'abcdefghijkl' with: Float pi) |
|
49 |
||
50 |
self new printf:'%8.3f' arguments: (Array with: 200 sqrt negated) |
|
51 |
||
1464 | 52 |
self new printf:'%c' arguments: #(16r41) |
53 |
self new printf:'%c' arguments: #( $A ) |
|
54 |
self new printf:'%s' arguments: #( $A ) |
|
55 |
self new printf:'%s' arguments: #( 'hello' ) |
|
56 |
self new printf:'%4s' arguments: #( 'hello' ) |
|
57 |
self new printf:'%7s' arguments: #( 'hello' ) |
|
1463 | 58 |
|
59 |
self new sscanf:'%f%2s%s%s%s' string: '237.0 this is a test' |
|
60 |
||
61 |
||
1464 | 62 |
self new sscanf:'%d%f%s' fromString: '25 54.32e-01 monday' |
63 |
||
64 |
self new sscanf:'%f%*f %8[A-F0-9]%c%d 0x%x%f' fromString: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
1463 | 65 |
|
66 |
||
67 |
||
68 |
||
69 |
'%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') |
|
70 |
||
71 |
||
72 |
'%- 10.4s%.2e' printf: (Array with: 'abcdefghijkl' with: Float pi) |
|
73 |
||
74 |
'%8.3f' printf: (Array with: 200 sqrt negated) |
|
75 |
||
76 |
'%c' printf: #(16r41) |
|
77 |
||
78 |
'%f%2s%s%s%s' sscanf: '237.0 this is a test' |
|
79 |
||
80 |
'%d%f%s' sscanf: '25 54.32e-01 monday' |
|
81 |
||
82 |
'%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
83 |
" |
|
84 |
! ! |
|
85 |
||
2581 | 86 |
!PrintfScanf class methodsFor:'others'! |
87 |
||
88 |
version_CVS |
|
2875 | 89 |
^ '$Header: /cvs/stx/stx/libbasic2/PrintfScanf.st,v 1.6 2013-01-23 10:01:22 cg Exp $' |
2581 | 90 |
! ! |
91 |
||
2875 | 92 |
!PrintfScanf class methodsFor:'printing'! |
1463 | 93 |
|
2875 | 94 |
printf:formatString arguments:args |
95 |
"Format and print the receiver with <args> formatted in C style, |
|
96 |
as described in the UTek manual page for printf(3)." |
|
1463 | 97 |
|
2875 | 98 |
^ self new printf:formatString arguments:args |
1463 | 99 |
! |
100 |
||
2875 | 101 |
printf:formatString on:outStream arguments: args |
102 |
"Format and print formatString on <outStream> with <args> |
|
1463 | 103 |
formatted in C style, as described in the UTek manual page for |
104 |
printf(3). This method is designed for producing output |
|
105 |
suitable for a machine." |
|
106 |
||
2875 | 107 |
^ self new printf:formatString on:outStream arguments: args |
1463 | 108 |
! ! |
109 |
||
2875 | 110 |
!PrintfScanf class methodsFor:'scanning'! |
111 |
||
112 |
scanf:formatString fromStream:dataStream |
|
113 |
"Return a Collection of objects found in the Character Stream |
|
114 |
<dataStream> as interpreted according to the receiver. The |
|
115 |
receiver is assumed to be a conversion control string as |
|
116 |
specified in the UTek manual page for scanf(3)." |
|
117 |
||
118 |
^ self new scanf:formatString fromStream:dataStream |
|
119 |
! |
|
120 |
||
121 |
sscanf:formatString fromString:aString |
|
122 |
"Return a Collection of objects found in <string> as |
|
123 |
interpreted according to the receiver. The receiver is |
|
124 |
assumed to be a conversion control string as specified in the |
|
125 |
UTek manual page for scanf(3)." |
|
126 |
||
127 |
^ self new sscanf:formatString fromString:aString |
|
128 |
! ! |
|
129 |
||
130 |
!PrintfScanf methodsFor:'helpers'! |
|
1463 | 131 |
|
132 |
absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
133 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits, using decimal notation." |
|
134 |
||
135 |
|exp x fuzz i| |
|
136 |
||
137 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
|
138 |
exp := aFloat abs < 1.0 ifTrue:[ |
|
139 |
(10.0 / aFloat abs) log floor negated |
|
140 |
] ifFalse:[ |
|
141 |
aFloat abs log floor |
|
142 |
]. |
|
143 |
x := aFloat abs / (10.0 raisedTo:exp). |
|
144 |
fuzz := 10.0 raisedTo:1 - digits. |
|
145 |
x := 0.5 * fuzz + x. |
|
146 |
x >= 10.0 ifTrue:[ |
|
147 |
"check if rounding has unnormalized x" |
|
148 |
x := x / 10.0. |
|
149 |
exp := exp + 1 |
|
150 |
]. |
|
151 |
exp < 0 ifTrue:[ |
|
152 |
1 to:1 - exp do:[:j | |
|
153 |
aStream nextPut:('0.000000000000' at:j) |
|
154 |
] |
|
155 |
]. |
|
156 |
[ x >= fuzz ] whileTrue:[ |
|
157 |
"use fuzz to track significance" |
|
158 |
i := x truncated. |
|
159 |
aStream nextPut:(48 + i) asCharacter. |
|
160 |
x := (x - i) * 10.0. |
|
161 |
fuzz := fuzz * 10.0. |
|
162 |
exp := exp - 1. |
|
163 |
exp = -1 ifTrue:[ |
|
164 |
aStream nextPut:$. |
|
165 |
] |
|
166 |
]. |
|
167 |
[ exp >= -1 ] whileTrue:[ |
|
168 |
aStream nextPut:$0. |
|
169 |
exp := exp - 1. |
|
170 |
exp = -1 ifTrue:[ |
|
171 |
aStream nextPut:$. |
|
172 |
] |
|
173 |
] |
|
174 |
! |
|
175 |
||
176 |
absPrintFloat:aFloat on:aStream digits:digits |
|
177 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits." |
|
178 |
||
179 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
180 |
self |
|
181 |
absDecimalPrintFloat:aFloat |
|
182 |
on:aStream |
|
183 |
digits:digits |
|
184 |
] ifFalse:[ |
|
185 |
aFloat |
|
186 |
absScientificPrintFloat:aFloat |
|
187 |
on:aStream |
|
188 |
digits:digits |
|
189 |
] |
|
190 |
! |
|
191 |
||
192 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
|
193 |
"Place a string representation of the receiver on <aStream> using <digits> significant digits, using scientific notation." |
|
194 |
||
195 |
|exp fuzz x q i| |
|
196 |
||
197 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
|
198 |
exp := aFloat abs < 1.0 ifTrue:[ |
|
199 |
(10.0 / aFloat abs) log floor negated |
|
200 |
] ifFalse:[ |
|
201 |
aFloat abs log floor |
|
202 |
]. |
|
203 |
x := aFloat abs / (10.0 raisedTo:exp). |
|
204 |
fuzz := 10.0 raisedTo:1 - digits. |
|
205 |
x := 0.5 * fuzz + x. |
|
206 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
207 |
x := x / 10.0. |
|
208 |
exp := exp + 1 |
|
209 |
]. |
|
210 |
q := exp. |
|
211 |
exp := 0. |
|
212 |
[ x >= fuzz ] whileTrue:[ |
|
213 |
"use fuzz to track significance" |
|
214 |
i := x truncated. |
|
215 |
aStream nextPut:(48 + i) asCharacter. |
|
216 |
x := (x - i) * 10.0. |
|
217 |
fuzz := fuzz * 10.0. |
|
218 |
exp := exp - 1. |
|
219 |
exp = -1 ifTrue:[ |
|
220 |
aStream nextPut:$. |
|
221 |
] |
|
222 |
]. |
|
223 |
[ exp >= -1 ] whileTrue:[ |
|
224 |
aStream nextPut:$0. |
|
225 |
exp := exp - 1. |
|
226 |
exp = -1 ifTrue:[ |
|
227 |
aStream nextPut:$. |
|
228 |
] |
|
229 |
]. |
|
230 |
aStream nextPut:$e. |
|
231 |
q printOn:aStream |
|
232 |
! |
|
233 |
||
234 |
formatArgCountFor:aFormatString |
|
235 |
"Return the number of arguments required/produced, |
|
236 |
if the argument is interpreted as a printf/scanf format control string." |
|
237 |
||
238 |
|nonConsecutive count| |
|
239 |
||
240 |
nonConsecutive := true. |
|
241 |
count := 0. |
|
242 |
aFormatString do:[:c | |
|
243 |
c == $% ifTrue:[ |
|
244 |
nonConsecutive ifTrue:[ |
|
245 |
count := count + 1. |
|
246 |
nonConsecutive := false |
|
247 |
] ifFalse:[ |
|
248 |
count := count - 1. |
|
249 |
nonConsecutive := true |
|
250 |
] |
|
251 |
] ifFalse:[ |
|
252 |
nonConsecutive := true |
|
253 |
] |
|
254 |
]. |
|
255 |
^ count |
|
256 |
! ! |
|
257 |
||
2875 | 258 |
!PrintfScanf methodsFor:'printing'! |
259 |
||
260 |
printArgFrom:inStream to:outStream arguments:argStream |
|
261 |
"Interpret the required number of arguments from <argStream> |
|
262 |
according to the formatting information in <inStream>. Place |
|
263 |
the interpretation on <outStream>. The interpretation is C |
|
264 |
printf(3) style, as described in the UTek manual page for |
|
265 |
printf(3). <inStream> is assumed to be positioned just past |
|
266 |
$%, and a complete control string is assumed available. |
|
267 |
||
268 |
Return when the conversion control string is consumed. |
|
269 |
Leave <inStream> pointing past the last character in the conversion control string. |
|
270 |
||
271 |
This code assumes that <inStream> is formatted according to |
|
272 |
specification, and error checking is minimal. Unexpected |
|
273 |
results will be obtained by illegal control strings, or when |
|
274 |
argument types do not match conversion codes, but it probably |
|
275 |
won't dump core, like C does in such cases!!!!" |
|
276 |
||
277 |
| ljust plus pound width precision pad char arg argString | |
|
278 |
||
279 |
ljust := plus := pound := false. |
|
280 |
width := 0. |
|
281 |
precision := SmallInteger maxVal. |
|
282 |
pad := $ . |
|
283 |
char := inStream peek. |
|
284 |
||
285 |
char == $% ifTrue:[ |
|
286 |
^ outStream nextPut: inStream next |
|
287 |
]. |
|
288 |
||
289 |
char == $- ifTrue:[ |
|
290 |
ljust := true. |
|
291 |
inStream next. |
|
292 |
char := inStream peek |
|
293 |
]. |
|
294 |
||
295 |
char == $ ifTrue:[ |
|
296 |
outStream space. |
|
297 |
inStream next. |
|
298 |
char := inStream peek |
|
299 |
]. |
|
300 |
||
301 |
char == $+ ifTrue:[ |
|
302 |
plus := true. |
|
303 |
inStream next. |
|
304 |
char := inStream peek |
|
305 |
]. |
|
306 |
||
307 |
char == $# ifTrue:[ |
|
308 |
pound := true. |
|
309 |
inStream next. |
|
310 |
char := inStream peek |
|
311 |
]. |
|
312 |
||
313 |
char == $* ifTrue:[ |
|
314 |
width := argStream next. |
|
315 |
inStream next. |
|
316 |
char := inStream peek |
|
317 |
]. |
|
318 |
||
319 |
char isDigit ifTrue:[ |
|
320 |
char == $0 ifTrue: [pad _ $0]. |
|
321 |
width := Integer readFrom: inStream. |
|
322 |
char := inStream peek |
|
323 |
]. |
|
324 |
||
325 |
char == $. ifTrue:[ |
|
326 |
inStream next. char _ inStream peek. |
|
327 |
char == $* |
|
328 |
ifTrue: [precision _ argStream next. inStream next.] |
|
329 |
ifFalse: [precision _ Integer readFrom: inStream.]. |
|
330 |
char _ inStream peek |
|
331 |
]. |
|
332 |
||
333 |
char == $l ifTrue:[ |
|
334 |
"Ignore long specifier." |
|
335 |
inStream next. char _ inStream peek |
|
336 |
]. |
|
337 |
||
338 |
('feg' includes: char) ifTrue:[ |
|
339 |
arg _ argStream next asFloat. |
|
340 |
precision _ precision min: 6. |
|
341 |
argString _ WriteStream on: String new. |
|
342 |
char == $g ifTrue: |
|
343 |
[self absPrintFloat:arg on: argString digits: precision + 1]. |
|
344 |
char == $f ifTrue: |
|
345 |
[self absDecimalPrintFloat:arg on: argString digits: precision + arg abs log + 1]. |
|
346 |
char == $e ifTrue: |
|
347 |
[self absScientificPrintFloat:arg on: argString digits: precision + 1]. |
|
348 |
argString _ argString contents. |
|
349 |
arg < 0 |
|
350 |
ifTrue: [argString _ '-', argString] |
|
351 |
ifFalse: [plus ifTrue: [argString _ '+', argString]]. |
|
352 |
(precision = 0 and: [pound not]) ifTrue: |
|
353 |
[(argString includes: $e) |
|
354 |
ifTrue: ["self halt"] |
|
355 |
ifFalse: |
|
356 |
[argString _ arg truncated printString]]. |
|
357 |
pound ifTrue: |
|
358 |
[(argString includes: $e) |
|
359 |
ifTrue: ["self halt"] |
|
360 |
ifFalse: |
|
361 |
[precision - (argString size - (argString indexOf: $.)) timesRepeat: |
|
362 |
[argString _ argString, '0']]]. |
|
363 |
ljust ifTrue: [outStream nextPutAll: argString]. |
|
364 |
width - argString size timesRepeat: [outStream nextPut: pad]. |
|
365 |
ljust ifFalse: [outStream nextPutAll: argString]. |
|
366 |
^inStream next |
|
367 |
]. |
|
368 |
||
369 |
char == $c ifTrue:[ |
|
370 |
arg _ String with: argStream next asCharacter |
|
371 |
]. |
|
372 |
||
373 |
char == $s ifTrue:[ |
|
374 |
"Assume the arg is a String or Symbol." |
|
375 |
arg _ argStream next asString |
|
376 |
]. |
|
377 |
||
378 |
char == $d ifTrue:[ |
|
379 |
arg _ argStream next asInteger printString. |
|
380 |
plus ifTrue: [arg _ '+', arg] |
|
381 |
]. |
|
382 |
||
383 |
char == $u ifTrue:[ |
|
384 |
arg _ argStream next asInteger abs printString |
|
385 |
]. |
|
386 |
||
387 |
char == $o ifTrue:[ |
|
388 |
arg _ argStream next asInteger abs printStringRadix: 8. |
|
389 |
pound ifTrue: [arg _ '0', arg] |
|
390 |
]. |
|
391 |
||
392 |
('xX' includes: char) ifTrue:[ |
|
393 |
arg _ argStream next asInteger abs printStringRadix: 16. |
|
394 |
pound ifTrue: [arg _ '0x', arg] |
|
395 |
]. |
|
396 |
||
397 |
char == $x ifTrue:[ |
|
398 |
1 to: arg size do: [:i | |
|
399 |
('ABCDEF' includes: (arg at: i)) ifTrue: |
|
400 |
[arg at: i put: ((arg at: i) asciiValue + 16r20) asCharacter]] |
|
401 |
]. |
|
402 |
||
403 |
precision _ precision min: arg size. |
|
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 |
||
418 |
aStream := WriteStream on:String new. |
|
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!!!!" |
|
515 |
return := WriteStream on:(String new:8). |
|
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) |
|
555 |
] |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
556 |
|
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
557 |
"Modified: / 29-11-2011 / 11:55:32 / cg" |
1463 | 558 |
! |
559 |
||
560 |
scanf:formatString fromStream:dataStream |
|
561 |
"Return a Collection of objects found in the Character Stream |
|
562 |
<dataStream> as interpreted according to the receiver. The |
|
563 |
receiver is assumed to be a conversion control string as |
|
564 |
specified in the UTek manual page for scanf(3)." |
|
565 |
||
566 |
|results format char| |
|
567 |
||
568 |
results := OrderedCollection new. |
|
569 |
format := ReadStream on:formatString. |
|
570 |
[ format atEnd ] whileFalse:[ |
|
571 |
char := format next. |
|
572 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
573 |
dataStream skipSeparators. |
|
574 |
format skipSeparators |
|
575 |
]. |
|
576 |
char == $% ifTrue:[ |
|
577 |
self |
|
578 |
scanArgFrom:dataStream |
|
579 |
to:results |
|
580 |
format:format |
|
581 |
] ifFalse:[ |
|
582 |
dataStream peekFor:char |
|
583 |
] |
|
584 |
]. |
|
585 |
^ results |
|
586 |
! |
|
587 |
||
588 |
sscanf:formatString fromString:aString |
|
589 |
"Return a Collection of objects found in <string> as |
|
590 |
interpreted according to the receiver. The receiver is |
|
591 |
assumed to be a conversion control string as specified in the |
|
592 |
UTek manual page for scanf(3)." |
|
593 |
||
594 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
595 |
||
596 |
" |
|
597 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
598 |
" |
|
599 |
! ! |
|
600 |
||
601 |
!PrintfScanf class methodsFor:'documentation'! |
|
602 |
||
603 |
version |
|
2875 | 604 |
^ '$Header: /cvs/stx/stx/libbasic2/PrintfScanf.st,v 1.6 2013-01-23 10:01:22 cg Exp $' |
1463 | 605 |
! ! |
2875 | 606 |