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