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