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