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