author | Claus Gittinger <cg@exept.de> |
Mon, 19 Jun 2017 15:33:45 +0200 | |
changeset 4416 | 40f676e88785 |
parent 4401 | a29669a0e2a1 |
child 4417 | 01b4de1f0b01 |
permissions | -rw-r--r-- |
1463 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3539 | 3 |
"{ NameSpace: Smalltalk }" |
4 |
||
1463 | 5 |
Object subclass:#PrintfScanf |
6 |
instanceVariableNames:'' |
|
4084 | 7 |
classVariableNames:'Singleton' |
1463 | 8 |
poolDictionaries:'' |
9 |
category:'System-Support' |
|
10 |
! |
|
11 |
||
12 |
!PrintfScanf class methodsFor:'documentation'! |
|
13 |
||
1465 | 14 |
documentation |
15 |
" |
|
4416 | 16 |
Contributed by Jan Steinman donated to the community in 1989. |
1465 | 17 |
|
18 |
Provided AS-IS - no warranty, use at your own risk. |
|
19 |
||
20 |
Original comment: |
|
21 |
||
22 |
NAME printf-scanf |
|
23 |
AUTHOR Jan Steinman <jans@tekgvs.labs.tek.com> |
|
24 |
FUNCTION printf and scanf for Smalltalk |
|
25 |
ST-VERSIONS Tek 2.2.2a 4.0 |
|
26 |
PREREQUISITES CharacterComparing |
|
27 |
CONFLICTS |
|
28 |
DISTRIBUTION world |
|
29 |
VERSION 1.1 |
|
30 |
DATE Apr 1989? |
|
31 |
SUMMARY |
|
32 |
||
33 |
The following methods implement printf and scanf functionality. They |
|
34 |
are intended to be used to ease porting between Smalltalk and C, and |
|
35 |
for facilitating machine-machine communication. They are not at all |
|
36 |
intended as replacements for Smalltalk's printOn: functionality. |
|
37 |
||
38 |
Jan Steinman - N7JDB |
|
39 |
Tektronix Electronic Systems Laboratory |
|
40 |
Box 500, MS 50-370, Beaverton, OR 97077 |
|
41 |
(w)503/627-5881 (h)503/657-7703 |
|
4416 | 42 |
|
43 |
changes: |
|
44 |
slight changes to make it work with higher precision real numbers |
|
45 |
(i.e. asking for the precision instead of hard-coding it) |
|
1465 | 46 |
" |
47 |
! |
|
48 |
||
1463 | 49 |
examples |
50 |
" |
|
4416 | 51 |
self printf:'%#x %#X %03o%*.*s' arguments: #(16rABCD 16rEF 5 9 5 'ghijklmn') |
52 |
-> '0xabcd 0xEF 005 ghijk' |
|
1463 | 53 |
|
4416 | 54 |
self printf:'%- 10.4s%.2e' arguments: { 'abcdefghijkl' . Float pi } |
55 |
-> ' abcd 3.14e0' |
|
56 |
||
57 |
self printf:'%8.3f' arguments: { 200 sqrt negated } |
|
58 |
-> ' -14.142' |
|
1463 | 59 |
|
4394 | 60 |
self printf:'%8.3f' on:Transcript arguments: { 200 sqrt negated } |
61 |
self printf:'%10.4f' on:Transcript arguments: { 200 sqrt negated } |
|
62 |
self printf:'%20.10f' on:Transcript arguments: { 200 sqrt negated } |
|
63 |
||
64 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789f } |
|
65 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789q } |
|
1463 | 66 |
|
4416 | 67 |
self printf:'%x' arguments: #(16r41) -> '41' |
68 |
self printf:'%#x' arguments: #(16r41) -> '0x41' |
|
69 |
self printf:'%d' arguments: #(16r41) -> '65' |
|
70 |
self printf:'%b' arguments: #(16r41) -> '1000001' |
|
71 |
self printf:'%c' arguments: #(16r41) -> 'A' |
|
72 |
self printf:'%c' arguments: #( $A ) -> 'A' |
|
73 |
self printf:'%s' arguments: #( $A ) -> 'A' |
|
74 |
self printf:'%s' arguments: #( 'hello' ) -> 'hello' |
|
75 |
self printf:'%4s' arguments: #( 'hello' ) -> 'hello' |
|
76 |
self printf:'%7s' arguments: #( 'hello' ) -> ' hello' |
|
1463 | 77 |
|
4416 | 78 |
self sscanf:'%f%2s%s%s%s' fromString: '237.0 this is a test' |
79 |
-> OrderedCollection(237.0 'th' 'is' 'is' 'a') |
|
1463 | 80 |
|
4416 | 81 |
self sscanf:'%d%f%s' fromString: '25 54.32e-01 monday' |
82 |
-> OrderedCollection(25 5.432 'monday') |
|
1464 | 83 |
|
4416 | 84 |
self sscanf:'%f%*f %8[A-F0-9]%c%d 0x%x%f' fromString: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
85 |
-> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) |
|
1463 | 86 |
|
4416 | 87 |
'%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn') |
88 |
-> '0xabcd 0xEF 005 ghijk' |
|
1463 | 89 |
|
3115 | 90 |
'%- 10.4s%.2e' printf: { 'abcdefghijkl' . Float pi } |
4416 | 91 |
-> ' abcd 3.14e0' |
1463 | 92 |
|
3115 | 93 |
'%8.3f' printf: { 200 sqrt negated } |
4416 | 94 |
-> ' -14.142' |
1463 | 95 |
|
96 |
'%c' printf: #(16r41) |
|
4416 | 97 |
-> 'A' |
1463 | 98 |
|
99 |
'%f%2s%s%s%s' sscanf: '237.0 this is a test' |
|
4416 | 100 |
-> OrderedCollection(237.0 'th' 'is' 'is' 'a') |
1463 | 101 |
|
102 |
'%d%f%s' sscanf: '25 54.32e-01 monday' |
|
4416 | 103 |
-> OrderedCollection(25 5.432 'monday') |
1463 | 104 |
|
105 |
'%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
4416 | 106 |
-> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) |
1463 | 107 |
" |
108 |
! ! |
|
109 |
||
4084 | 110 |
!PrintfScanf class methodsFor:'instance creation'! |
111 |
||
112 |
new |
|
113 |
Singleton isNil ifTrue:[ |
|
114 |
Singleton := self basicNew |
|
115 |
]. |
|
116 |
^ Singleton |
|
117 |
! ! |
|
118 |
||
4416 | 119 |
!PrintfScanf class methodsFor:'helpers'! |
1463 | 120 |
|
121 |
absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
4394 | 122 |
"Place a string representation of the receiver on <aStream>, |
123 |
using <digits> significant digits, using decimal notation." |
|
1463 | 124 |
|
4416 | 125 |
" |
126 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789f } |
|
127 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789q } |
|
128 |
" |
|
4394 | 129 |
|
130 |
|absVal exp x fuzz i| |
|
131 |
||
132 |
absVal := aFloat abs. |
|
1463 | 133 |
|
134 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
|
4394 | 135 |
exp := absVal < 1.0 |
136 |
ifTrue:[ |
|
4416 | 137 |
(10.0 / absVal) log10 floor asInteger negated ] |
4394 | 138 |
ifFalse:[ |
4416 | 139 |
absVal log10 floor asInteger]. |
4394 | 140 |
x := absVal / (10.0 raisedTo:exp). |
1463 | 141 |
fuzz := 10.0 raisedTo:1 - digits. |
4416 | 142 |
"round the last digit to be printed" |
1463 | 143 |
x := 0.5 * fuzz + x. |
144 |
x >= 10.0 ifTrue:[ |
|
145 |
"check if rounding has unnormalized x" |
|
146 |
x := x / 10.0. |
|
147 |
exp := exp + 1 |
|
148 |
]. |
|
4394 | 149 |
|
1463 | 150 |
exp < 0 ifTrue:[ |
4394 | 151 |
1 to:(1 - exp) do:[:j | |
152 |
"/ cg: huh - what sort of code is that???? |
|
153 |
"/ aStream nextPut:('0.000000000000' at:j) |
|
154 |
aStream nextPut:(j == 2 ifTrue:[$.] ifFalse:[$0]) |
|
155 |
]. |
|
156 |
||
1463 | 157 |
]. |
158 |
[ x >= fuzz ] whileTrue:[ |
|
159 |
"use fuzz to track significance" |
|
4401 | 160 |
i := x truncated asInteger. |
1463 | 161 |
aStream nextPut:(48 + i) asCharacter. |
162 |
x := (x - i) * 10.0. |
|
163 |
fuzz := fuzz * 10.0. |
|
164 |
exp := exp - 1. |
|
165 |
exp = -1 ifTrue:[ |
|
166 |
aStream nextPut:$. |
|
167 |
] |
|
168 |
]. |
|
169 |
[ exp >= -1 ] whileTrue:[ |
|
170 |
aStream nextPut:$0. |
|
171 |
exp := exp - 1. |
|
172 |
exp = -1 ifTrue:[ |
|
173 |
aStream nextPut:$. |
|
174 |
] |
|
175 |
] |
|
4394 | 176 |
|
4416 | 177 |
"Modified (comment): / 19-06-2017 / 14:56:40 / cg" |
1463 | 178 |
! |
179 |
||
180 |
absPrintFloat:aFloat on:aStream digits:digits |
|
4394 | 181 |
"Place a string representation of the receiver on <aStream>, |
182 |
using <digits> significant digits." |
|
1463 | 183 |
|
184 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
4416 | 185 |
self absDecimalPrintFloat:aFloat on:aStream digits:digits |
1463 | 186 |
] ifFalse:[ |
4416 | 187 |
self absScientificPrintFloat:aFloat on:aStream digits:digits |
1463 | 188 |
] |
4394 | 189 |
|
4416 | 190 |
"Modified: / 19-06-2017 / 15:03:40 / cg" |
1463 | 191 |
! |
192 |
||
193 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
|
4394 | 194 |
"Place a string representation of the receiver on <aStream>, |
195 |
using <digits> significant digits, using scientific notation." |
|
1463 | 196 |
|
4394 | 197 |
|absVal exp fuzz x q i| |
1463 | 198 |
|
4394 | 199 |
absVal := aFloat abs. |
200 |
||
1463 | 201 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
4394 | 202 |
exp := absVal < 1.0 |
203 |
ifTrue:[ |
|
4416 | 204 |
(10.0 / absVal) log10 floor asInteger negated] |
4394 | 205 |
ifFalse:[ |
4416 | 206 |
absVal log10 floor asInteger]. |
4394 | 207 |
x := absVal / (10.0 raisedTo:exp). |
4416 | 208 |
"round the last digit to be printed" |
1463 | 209 |
fuzz := 10.0 raisedTo:1 - digits. |
210 |
x := 0.5 * fuzz + x. |
|
211 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
212 |
x := x / 10.0. |
|
213 |
exp := exp + 1 |
|
214 |
]. |
|
215 |
q := exp. |
|
216 |
exp := 0. |
|
217 |
[ x >= fuzz ] whileTrue:[ |
|
218 |
"use fuzz to track significance" |
|
219 |
i := x truncated. |
|
220 |
aStream nextPut:(48 + i) asCharacter. |
|
221 |
x := (x - i) * 10.0. |
|
222 |
fuzz := fuzz * 10.0. |
|
223 |
exp := exp - 1. |
|
224 |
exp = -1 ifTrue:[ |
|
225 |
aStream nextPut:$. |
|
226 |
] |
|
227 |
]. |
|
228 |
[ exp >= -1 ] whileTrue:[ |
|
229 |
aStream nextPut:$0. |
|
230 |
exp := exp - 1. |
|
231 |
exp = -1 ifTrue:[ |
|
232 |
aStream nextPut:$. |
|
233 |
] |
|
234 |
]. |
|
235 |
aStream nextPut:$e. |
|
236 |
q printOn:aStream |
|
4394 | 237 |
|
4416 | 238 |
"Modified (comment): / 19-06-2017 / 14:58:53 / cg" |
1463 | 239 |
! |
240 |
||
241 |
formatArgCountFor:aFormatString |
|
242 |
"Return the number of arguments required/produced, |
|
243 |
if the argument is interpreted as a printf/scanf format control string." |
|
244 |
||
245 |
|nonConsecutive count| |
|
246 |
||
247 |
nonConsecutive := true. |
|
248 |
count := 0. |
|
249 |
aFormatString do:[:c | |
|
250 |
c == $% ifTrue:[ |
|
251 |
nonConsecutive ifTrue:[ |
|
252 |
count := count + 1. |
|
253 |
nonConsecutive := false |
|
254 |
] ifFalse:[ |
|
255 |
count := count - 1. |
|
256 |
nonConsecutive := true |
|
257 |
] |
|
258 |
] ifFalse:[ |
|
259 |
nonConsecutive := true |
|
260 |
] |
|
261 |
]. |
|
262 |
^ count |
|
263 |
! ! |
|
264 |
||
4416 | 265 |
!PrintfScanf class methodsFor:'printing'! |
2875 | 266 |
|
4416 | 267 |
printArgFrom:formatStream to:outStream arguments:argStream |
2875 | 268 |
"Interpret the required number of arguments from <argStream> |
4416 | 269 |
according to the formatting information in <formatStream>. Place |
2875 | 270 |
the interpretation on <outStream>. The interpretation is C |
271 |
printf(3) style, as described in the UTek manual page for |
|
4416 | 272 |
printf(3). <formatStream> is assumed to be positioned just past |
2875 | 273 |
$%, and a complete control string is assumed available. |
274 |
||
275 |
Return when the conversion control string is consumed. |
|
4416 | 276 |
Leave <formatStream> pointing past the last character in the conversion control string. |
2875 | 277 |
|
4416 | 278 |
This code assumes that <formatStream> is formatted according to |
2875 | 279 |
specification, and error checking is minimal. Unexpected |
280 |
results will be obtained by illegal control strings, or when |
|
281 |
argument types do not match conversion codes, but it probably |
|
282 |
won't dump core, like C does in such cases!!!!" |
|
283 |
||
284 |
| ljust plus pound width precision pad char arg argString | |
|
285 |
||
286 |
ljust := plus := pound := false. |
|
287 |
width := 0. |
|
4401 | 288 |
"/ precision := SmallInteger maxVal. |
289 |
precision := nil. |
|
2875 | 290 |
pad := $ . |
4416 | 291 |
char := formatStream peek. |
2875 | 292 |
|
293 |
char == $% ifTrue:[ |
|
4416 | 294 |
^ outStream nextPut: formatStream next |
2875 | 295 |
]. |
296 |
||
297 |
char == $- ifTrue:[ |
|
298 |
ljust := true. |
|
4416 | 299 |
formatStream next. |
300 |
char := formatStream peek |
|
2875 | 301 |
]. |
302 |
||
303 |
char == $ ifTrue:[ |
|
304 |
outStream space. |
|
4416 | 305 |
formatStream next. |
306 |
char := formatStream peek |
|
2875 | 307 |
]. |
308 |
||
309 |
char == $+ ifTrue:[ |
|
310 |
plus := true. |
|
4416 | 311 |
formatStream next. |
312 |
char := formatStream peek |
|
2875 | 313 |
]. |
314 |
||
315 |
char == $# ifTrue:[ |
|
316 |
pound := true. |
|
4416 | 317 |
formatStream next. |
318 |
char := formatStream peek |
|
2875 | 319 |
]. |
320 |
||
321 |
char == $* ifTrue:[ |
|
322 |
width := argStream next. |
|
4416 | 323 |
formatStream next. |
324 |
char := formatStream peek |
|
2875 | 325 |
]. |
326 |
||
327 |
char isDigit ifTrue:[ |
|
2889 | 328 |
char == $0 ifTrue: [pad := $0]. |
4416 | 329 |
width := Integer readFrom: formatStream. |
330 |
char := formatStream peek |
|
2875 | 331 |
]. |
332 |
||
333 |
char == $. ifTrue:[ |
|
4416 | 334 |
formatStream next. char := formatStream peek. |
2875 | 335 |
char == $* |
4416 | 336 |
ifTrue: [precision := argStream next. formatStream next.] |
337 |
ifFalse: [precision := Integer readFrom: formatStream.]. |
|
338 |
char := formatStream peek |
|
2875 | 339 |
]. |
340 |
||
341 |
char == $l ifTrue:[ |
|
342 |
"Ignore long specifier." |
|
4416 | 343 |
formatStream next. char := formatStream peek |
2875 | 344 |
]. |
345 |
||
346 |
('feg' includes: char) ifTrue:[ |
|
4401 | 347 |
arg := argStream next. |
4394 | 348 |
arg isLimitedPrecisionReal ifTrue:[ |
4401 | 349 |
precision := precision ? (arg class defaultPrintPrecision). |
350 |
] ifFalse:[ |
|
351 |
arg := arg asFloat. |
|
352 |
precision := precision ? (Float defaultPrintPrecision). |
|
4394 | 353 |
]. |
354 |
argString := WriteStream on:''. |
|
355 |
char == $g ifTrue: [ |
|
356 |
self absPrintFloat:arg on: argString digits: precision + 1 ]. |
|
357 |
char == $f ifTrue: [ |
|
4416 | 358 |
self absDecimalPrintFloat:arg on: argString digits: precision + arg abs log10 + 1]. |
4394 | 359 |
char == $e ifTrue: [ |
360 |
self absScientificPrintFloat:arg on: argString digits: precision + 1]. |
|
361 |
argString := argString contents. |
|
362 |
arg < 0 |
|
363 |
ifTrue: [argString := '-', argString] |
|
364 |
ifFalse: [plus ifTrue: [argString := '+', argString]]. |
|
365 |
(precision = 0 and: [pound not]) ifTrue:[ |
|
366 |
(argString includes: $e) |
|
367 |
ifTrue: ["self halt"] |
|
368 |
ifFalse: [ |
|
369 |
argString := arg truncated printString]]. |
|
370 |
pound ifTrue:[ |
|
371 |
(argString includes: $e) |
|
372 |
ifTrue: ["self halt"] |
|
373 |
ifFalse:[ |
|
374 |
precision - (argString size - (argString indexOf: $.)) |
|
375 |
timesRepeat:[ |
|
376 |
argString := argString, '0']]]. |
|
377 |
ljust ifTrue: [outStream nextPutAll: argString]. |
|
378 |
width - argString size timesRepeat: [outStream nextPut: pad]. |
|
379 |
ljust ifFalse: [outStream nextPutAll: argString]. |
|
4416 | 380 |
^ formatStream next |
2875 | 381 |
]. |
382 |
||
383 |
char == $c ifTrue:[ |
|
3539 | 384 |
arg := argStream next asCharacter asString |
2875 | 385 |
]. |
386 |
||
387 |
char == $s ifTrue:[ |
|
388 |
"Assume the arg is a String or Symbol." |
|
2889 | 389 |
arg := argStream next asString |
2875 | 390 |
]. |
391 |
||
392 |
char == $d ifTrue:[ |
|
2889 | 393 |
arg := argStream next asInteger printString. |
394 |
plus ifTrue: [arg := '+', arg] |
|
2875 | 395 |
]. |
396 |
||
397 |
char == $u ifTrue:[ |
|
2889 | 398 |
arg := argStream next asInteger abs printString |
2875 | 399 |
]. |
400 |
||
401 |
char == $o ifTrue:[ |
|
2889 | 402 |
arg := argStream next asInteger abs printStringRadix: 8. |
4394 | 403 |
pound ifTrue: [arg := '0', arg] |
2875 | 404 |
]. |
405 |
||
406 |
('xX' includes: char) ifTrue:[ |
|
2889 | 407 |
arg := argStream next asInteger abs printStringRadix: 16. |
408 |
pound ifTrue: [arg := '0x', arg] |
|
409 |
]. |
|
410 |
('bB' includes: char) ifTrue:[ |
|
411 |
arg := argStream next asInteger abs printStringRadix: 2. |
|
412 |
pound ifTrue: [arg := '0b', arg] |
|
2875 | 413 |
]. |
414 |
||
415 |
char == $x ifTrue:[ |
|
416 |
1 to: arg size do: [:i | |
|
4394 | 417 |
('ABCDEF' includes: (arg at: i)) ifTrue:[ |
418 |
arg at: i put: (arg at: i) asLowercase |
|
419 |
] |
|
3719 | 420 |
] |
2875 | 421 |
]. |
422 |
||
4401 | 423 |
precision := precision ? arg size. |
2875 | 424 |
ljust ifTrue: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
425 |
width - precision timesRepeat: [outStream nextPut: pad]. |
|
426 |
ljust ifFalse: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
|
4416 | 427 |
^ formatStream next |
2875 | 428 |
|
4416 | 429 |
"Modified (comment): / 19-06-2017 / 15:20:11 / cg" |
2875 | 430 |
! |
431 |
||
4416 | 432 |
printf:formatString argument:arg |
433 |
"Format and print the receiver with <arg> formatted in C style, |
|
434 |
as described in the UTek manual page for printf(3)." |
|
435 |
||
436 |
^ self printf:formatString arguments:{ arg } |
|
437 |
||
438 |
" |
|
439 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat) |
|
440 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) |
|
441 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) |
|
442 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) |
|
443 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) |
|
444 |
||
445 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat) |
|
446 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) |
|
447 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) |
|
448 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) |
|
449 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) |
|
450 |
||
451 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) |
|
452 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) |
|
453 |
||
454 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat) |
|
455 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) |
|
456 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) |
|
457 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) |
|
458 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) |
|
459 |
" |
|
460 |
||
461 |
"Created: / 16-06-2017 / 14:50:08 / cg" |
|
462 |
! |
|
463 |
||
464 |
printf:formatString arguments:args |
|
2875 | 465 |
"Format and print the receiver with <args> formatted in C style, |
4416 | 466 |
as described in the UTek manual page for printf(3)." |
467 |
||
2875 | 468 |
|aStream| |
469 |
||
4416 | 470 |
aStream := WriteStream on:(formatString species new:100). |
471 |
self printf:formatString on:aStream arguments:args. |
|
2875 | 472 |
^ aStream contents |
473 |
||
474 |
" |
|
4416 | 475 |
self printf:'%e' on:Transcript arguments:{ (1.234 asShortFloat) } |
476 |
self printf:'%e' on:Transcript arguments:{ (1.234 asFloat) } |
|
477 |
self printf:'%e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
478 |
self printf:'%e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
479 |
self printf:'%e' on:Transcript arguments:{ (1.234 asInteger) } |
|
480 |
||
481 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
482 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
483 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
484 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
485 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
486 |
||
487 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
488 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
489 |
||
490 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
491 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
492 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
493 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
494 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
2875 | 495 |
" |
4394 | 496 |
|
4416 | 497 |
"Modified: / 19-06-2017 / 15:21:34 / cg" |
498 |
! |
|
499 |
||
500 |
printf:formatString on:outStream argument: arg |
|
501 |
"Format and print formatString on <outStream> with <arg> |
|
502 |
formatted in C style, as described in the UTek manual page for |
|
503 |
printf(3)." |
|
504 |
||
505 |
^ self printf:formatString on:outStream arguments:{ arg } |
|
506 |
||
507 |
" |
|
508 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
509 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
510 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
511 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
512 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
513 |
||
514 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
515 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
516 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
517 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
518 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
519 |
||
520 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
521 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
522 |
||
523 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat). Transcript cr. |
|
524 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) . Transcript cr. |
|
525 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) . Transcript cr. |
|
526 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) . Transcript cr. |
|
527 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) . Transcript cr. |
|
528 |
" |
|
529 |
||
530 |
"Created: / 16-06-2017 / 14:50:40 / cg" |
|
531 |
"Modified (comment): / 19-06-2017 / 15:07:22 / cg" |
|
2875 | 532 |
! |
533 |
||
534 |
printf:aFormatString on:outStream arguments: args |
|
535 |
"Format and print aFormatString on <outStream> with <args> |
|
536 |
formatted in C style, as described in the UTek manual page for |
|
4416 | 537 |
printf(3)." |
2875 | 538 |
|
539 |
| argStream inStream char | |
|
540 |
||
541 |
argStream := ReadStream on: args. |
|
542 |
inStream := ReadStream on: aFormatString. |
|
543 |
[inStream atEnd] whileFalse:[ |
|
544 |
(char := inStream next) == $% ifFalse: [ |
|
545 |
outStream nextPut: char |
|
546 |
] ifTrue: [ |
|
547 |
self printArgFrom:inStream to:outStream arguments:argStream |
|
548 |
] |
|
549 |
] |
|
4394 | 550 |
|
4416 | 551 |
" |
552 |
self printf:'%e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
553 |
self printf:'%e' on:Transcript arguments:{ (1.234 asFloat) } |
|
554 |
self printf:'%e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
555 |
self printf:'%e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
556 |
self printf:'%e' on:Transcript arguments:{ (1.234 asInteger) } |
|
557 |
||
558 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
559 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
560 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
561 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
562 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
563 |
||
564 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
565 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
566 |
||
567 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
568 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
569 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
570 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
571 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
572 |
" |
|
573 |
||
574 |
"Modified: / 19-06-2017 / 15:21:07 / cg" |
|
575 |
! ! |
|
576 |
||
577 |
!PrintfScanf class methodsFor:'scanning'! |
|
578 |
||
579 |
scanf:formatString fromStream:dataStream |
|
580 |
"Return a Collection of objects found in the Character Stream |
|
581 |
<dataStream> as interpreted according to the receiver. The |
|
582 |
receiver is assumed to be a conversion control string as |
|
583 |
specified in the UTek manual page for scanf(3)." |
|
584 |
||
585 |
^ self new scanf:formatString fromStream:dataStream |
|
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 new sscanf:formatString fromString:aString |
|
595 |
! ! |
|
596 |
||
597 |
!PrintfScanf methodsFor:'helpers'! |
|
598 |
||
599 |
absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
600 |
^ self class absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
601 |
! |
|
602 |
||
603 |
absPrintFloat:arg1 on:arg2 digits:arg3 |
|
604 |
^ self class absPrintFloat:arg1 on:arg2 digits:arg3 |
|
605 |
! |
|
606 |
||
607 |
absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
608 |
^ self class absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
609 |
! |
|
610 |
||
611 |
formatArgCountFor:arg |
|
612 |
^ self class formatArgCountFor:arg |
|
613 |
! ! |
|
614 |
||
615 |
!PrintfScanf methodsFor:'printing'! |
|
616 |
||
617 |
printArgFrom:arg1 to:arg2 arguments:arg3 |
|
618 |
^ self class printArgFrom:arg1 to:arg2 arguments:arg3 |
|
619 |
! |
|
620 |
||
621 |
printf:aString arguments:args |
|
622 |
"Format and print the receiver with <args> formatted in C style, |
|
623 |
as described in the UTek manual page for printf(3). |
|
624 |
Returns the formatted printString." |
|
625 |
||
626 |
^ self class printf:aString arguments:args |
|
627 |
||
628 |
" |
|
629 |
self new printf:'%d %x' arguments:#(1234 45054) |
|
630 |
" |
|
631 |
||
632 |
"Modified: / 19-06-2017 / 15:21:56 / cg" |
|
633 |
! |
|
634 |
||
635 |
printf:aFormatString on:outStream arguments: args |
|
636 |
"Format and print aFormatString on <outStream> with <args> |
|
637 |
formatted in C style, as described in the UTek manual page for |
|
638 |
printf(3)." |
|
639 |
||
640 |
self class printf:aFormatString on:outStream arguments: args |
|
641 |
||
642 |
"Modified (comment): / 19-06-2017 / 15:22:15 / cg" |
|
2875 | 643 |
! ! |
644 |
||
1463 | 645 |
!PrintfScanf methodsFor:'scanning'! |
646 |
||
647 |
scanArgFrom:dataStream to:collection format:format |
|
648 |
"Add to <collection> an object who's representation is found |
|
649 |
in <dataStream> interpreted according to the conversion |
|
650 |
control string in the Stream <format>. <format> is assumed to |
|
651 |
be positioned just past a $%, and a complete control string is |
|
652 |
assumed available. |
|
653 |
||
654 |
Return when the conversion control string is consumed. Leave |
|
655 |
<format> pointing past the last character in the conversion |
|
656 |
control string, leave <dataStream> pointing past any width |
|
657 |
specified in <format>, or at the first character that doesn't |
|
658 |
make sense for the <format>." |
|
659 |
||
660 |
|final width char pos data scanset exclusive return last| |
|
661 |
||
662 |
final := [:retval | |
|
663 |
collection add:retval. |
|
664 |
data == dataStream ifFalse:[ |
|
665 |
dataStream position:dataStream position + data position |
|
666 |
]. |
|
667 |
^ self |
|
668 |
]. |
|
1464 | 669 |
|
1463 | 670 |
char := format peek. |
671 |
char == $% ifTrue:[ |
|
672 |
^ dataStream peekFor:char |
|
673 |
]. |
|
674 |
char == $* ifTrue:[ |
|
675 |
format next. |
|
676 |
char := format peek. |
|
677 |
final := [:retval | |
|
678 |
data == dataStream ifFalse:[ |
|
679 |
dataStream position:dataStream position + data position |
|
680 |
]. |
|
681 |
^ self |
|
682 |
] |
|
683 |
]. |
|
1464 | 684 |
width := 0. |
1463 | 685 |
char isDigit ifTrue:[ |
686 |
width := Integer readFrom:format. |
|
687 |
char := format peek |
|
688 |
]. |
|
689 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
690 |
dataStream skipSeparators |
|
691 |
]. |
|
692 |
width = 0 ifTrue:[ |
|
693 |
data := dataStream |
|
694 |
] ifFalse:[ |
|
695 |
pos := dataStream position. |
|
696 |
data := ReadStream on:(dataStream next:width). |
|
697 |
dataStream position:pos |
|
698 |
]. |
|
699 |
char == $s ifTrue:[ |
|
700 |
final value:(data upToSeparator) |
|
701 |
]. |
|
702 |
char == $c ifTrue:[ |
|
703 |
width = 0 ifTrue:[ |
|
704 |
final value:(String with:data next) |
|
705 |
] ifFalse:[ |
|
706 |
final value:data contents |
|
707 |
] |
|
708 |
]. |
|
709 |
char == $[ ifTrue:[ |
|
710 |
"What a mess!!!!" |
|
3539 | 711 |
return := CharacterWriteStream on:(String new:8). |
1463 | 712 |
scanset := IdentitySet new. |
713 |
format next. |
|
714 |
width = 0 ifTrue:[ |
|
715 |
width := SmallInteger maxVal |
|
716 |
]. |
|
717 |
exclusive := format peekFor:$^. |
|
718 |
[ |
|
719 |
last := char. |
|
720 |
char := format next. |
|
721 |
char == $] |
|
722 |
] whileFalse:[ |
|
723 |
char == $- ifFalse:[ |
|
724 |
scanset add:char |
|
725 |
] ifTrue:[ |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
726 |
last to:format next do:[:c | |
1463 | 727 |
scanset add:c |
728 |
] |
|
729 |
] |
|
730 |
]. |
|
731 |
[ |
|
732 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
733 |
] whileTrue:[ return nextPut:data next ]. |
|
734 |
final value:return contents |
|
735 |
]. |
|
736 |
('lh' includes:char) ifTrue:[ |
|
737 |
format next. |
|
738 |
char := format peek |
|
739 |
]. |
|
740 |
('DUdu' includes:char) ifTrue:[ |
|
741 |
final value:(Integer readFrom:data) |
|
742 |
]. |
|
743 |
('FEGfeg' includes:char) ifTrue:[ |
|
744 |
final value:(Float readFrom:data) |
|
745 |
]. |
|
746 |
('Oo' includes:char) ifTrue:[ |
|
747 |
final value:(Integer readFrom:data radix:8) |
|
748 |
]. |
|
749 |
('Xx' includes:char) ifTrue:[ |
|
750 |
final value:(Integer readFrom:data radix:16) |
|
2889 | 751 |
]. |
752 |
('Bb' includes:char) ifTrue:[ |
|
753 |
final value:(Integer readFrom:data radix:2) |
|
754 |
]. |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
755 |
|
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
756 |
"Modified: / 29-11-2011 / 11:55:32 / cg" |
1463 | 757 |
! |
758 |
||
759 |
scanf:formatString fromStream:dataStream |
|
760 |
"Return a Collection of objects found in the Character Stream |
|
761 |
<dataStream> as interpreted according to the receiver. The |
|
762 |
receiver is assumed to be a conversion control string as |
|
763 |
specified in the UTek manual page for scanf(3)." |
|
764 |
||
765 |
|results format char| |
|
766 |
||
767 |
results := OrderedCollection new. |
|
768 |
format := ReadStream on:formatString. |
|
769 |
[ format atEnd ] whileFalse:[ |
|
770 |
char := format next. |
|
771 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
772 |
dataStream skipSeparators. |
|
773 |
format skipSeparators |
|
774 |
]. |
|
775 |
char == $% ifTrue:[ |
|
4416 | 776 |
self scanArgFrom:dataStream to:results format:format |
1463 | 777 |
] ifFalse:[ |
778 |
dataStream peekFor:char |
|
779 |
] |
|
780 |
]. |
|
781 |
^ results |
|
4416 | 782 |
|
783 |
"Modified (format): / 19-06-2017 / 15:22:46 / cg" |
|
1463 | 784 |
! |
785 |
||
786 |
sscanf:formatString fromString:aString |
|
787 |
"Return a Collection of objects found in <string> as |
|
788 |
interpreted according to the receiver. The receiver is |
|
789 |
assumed to be a conversion control string as specified in the |
|
790 |
UTek manual page for scanf(3)." |
|
791 |
||
792 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
793 |
||
794 |
" |
|
795 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
796 |
" |
|
797 |
! ! |
|
798 |
||
799 |
!PrintfScanf class methodsFor:'documentation'! |
|
800 |
||
801 |
version |
|
3719 | 802 |
^ '$Header$' |
3115 | 803 |
! |
804 |
||
805 |
version_CVS |
|
3719 | 806 |
^ '$Header$' |
1463 | 807 |
! ! |
2875 | 808 |