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