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