extensions.st
changeset 4473 2fbda206b293
parent 4448 f9bbcb40b337
child 4480 3149cd46d7d1
equal deleted inserted replaced
4472:cd76ffe41e4c 4473:2fbda206b293
   548     self printf:args on:outStream
   548     self printf:args on:outStream
   549 ! !
   549 ! !
   550 
   550 
   551 !CharacterArray methodsFor:'converting'!
   551 !CharacterArray methodsFor:'converting'!
   552 
   552 
   553 scanf:dataStream
   553 scanf:dataStreamOrString
   554     "Return a Collection of objects found in the Character Stream
   554     "Return a Collection of objects found in the Character Stream
   555      <dataStream> as interpreted according to the receiver.
   555      <dataStream> as interpreted according to the receiver.
   556      The receiver is assumed to be a conversion control string as
   556      The receiver is assumed to be a conversion control string as
   557      specified in the Unix C-language manual page for scanf(3).
   557      specified in the Unix C-language manual page for scanf(3).
   558      For copyright information, see goodies/String-printf_scanf.chg"
   558      For copyright information, see goodies/String-printf_scanf.chg"
   559 
   559 
   560     |results format char|
   560     |dataStream|
   561 
   561 
   562     results := OrderedCollection new.
   562     dataStream := dataStreamOrString readStream.
   563     format := ReadStream on:self.
   563     ^ PrintfScanf scanf:self fromStream:dataStream
   564     [ format atEnd ] whileFalse:[
   564     
   565 	char := format next.
   565 "/    |results format char|
   566 	(char == Character space or:[ char == Character tab ]) ifTrue:[
   566 "/
   567 	    dataStream skipSeparators.
   567 "/    results := OrderedCollection new.
   568 	    format skipSeparators
   568 "/    format := ReadStream on:self.
   569 	].
   569 "/    [ format atEnd ] whileFalse:[
   570 	char == $% ifTrue:[
   570 "/        char := format next.
   571 	    self
   571 "/        (char == Character space or:[ char == Character tab ]) ifTrue:[
   572 		scanf_scanArgFrom:dataStream
   572 "/            dataStream skipSeparators.
   573 		to:results
   573 "/            format skipSeparators
   574 		format:format
   574 "/        ].
   575 	] ifFalse:[
   575 "/        char == $% ifTrue:[
   576 	    dataStream peekFor:char
   576 "/            PrintfScanf scanArgFrom:dataStream to:results format:format.
   577 	]
   577 "/        ] ifFalse:[
   578     ].
   578 "/            dataStream peekFor:char
   579     ^ results
   579 "/        ]
       
   580 "/    ].
       
   581 "/    ^ results
   580 
   582 
   581     "
   583     "
   582      '%d %x' scanf:(ReadStream on:'1234 ff00')
   584      '%d %x' scanf:(ReadStream on:'1234 ff00')
   583     "
   585      '%d %s' scanf:(ReadStream on:'1234 ff00')
       
   586      '%d %b' scanf:(ReadStream on:'1234 1111')
       
   587      '%d %f' scanf:(ReadStream on:'1234 1111')
       
   588      '%d %f' scanf:(ReadStream on:'1234 1111.2345')
       
   589      '%d %q' scanf:(ReadStream on:'1234 1111.2345')
       
   590     "
       
   591 
       
   592     "Modified: / 14-07-2017 / 11:11:49 / cg"
   584 ! !
   593 ! !
   585 
   594 
   586 !CharacterArray methodsFor:'private'!
   595 !CharacterArray methodsFor:'private'!
   587 
   596 
   588 scanf_scanArgFrom:dataStream to:collection format:format
   597 scanf_scanArgFrom:dataStream to:collection format:format
   599      specified in <format>, or at the first character that doesn't
   608      specified in <format>, or at the first character that doesn't
   600      make sense for the <format>.
   609      make sense for the <format>.
   601 
   610 
   602      For copyright information, see goodies/String-printf_scanf.chg"
   611      For copyright information, see goodies/String-printf_scanf.chg"
   603 
   612 
   604     |final width char pos data scanset exclusive return last|
   613     PrintfScanf scanArgFrom:dataStream to:collection format:format.
   605 
       
   606     final := [:retval |
       
   607             collection add:retval.
       
   608             data == dataStream ifFalse:[
       
   609                 dataStream position:dataStream position + data position
       
   610             ].
       
   611             ^ self
       
   612         ].
       
   613     width := 0.
       
   614     char := format peek.
       
   615     char == $% ifTrue:[
       
   616         ^ dataStream peekFor:char
       
   617     ].
       
   618     char == $* ifTrue:[
       
   619         format next.
       
   620         char := format peek.
       
   621         final := [:retval |
       
   622                 data == dataStream ifFalse:[
       
   623                     dataStream position:dataStream position + data position
       
   624                 ].
       
   625                 ^ self
       
   626             ]
       
   627     ].
       
   628     char isDigit ifTrue:[
       
   629         width := Integer readFrom:format.
       
   630         char := format peek
       
   631     ].
       
   632     ('slhduoxfeg' includes:char) ifTrue:[
       
   633         dataStream skipSeparators
       
   634     ].
       
   635     width = 0 ifTrue:[
       
   636         data := dataStream
       
   637     ] ifFalse:[
       
   638         pos := dataStream position.
       
   639         data := ReadStream on:(dataStream next:width).
       
   640         dataStream position:pos
       
   641     ].
       
   642     char == $s ifTrue:[
       
   643         final value:(data upToSeparator)
       
   644     ].
       
   645     char == $c ifTrue:[
       
   646         width = 0 ifTrue:[
       
   647             final value:(String with:data next)
       
   648         ] ifFalse:[
       
   649             final value:data contents
       
   650         ]
       
   651     ].
       
   652     char == $[ "What a mess!!" ifTrue:[
       
   653         return := WriteStream on:(String new:8).
       
   654         scanset := IdentitySet new.
       
   655         format next.
       
   656         width = 0 ifTrue:[
       
   657             width := SmallInteger maxVal
       
   658         ].
       
   659         exclusive := format peekFor:$^.
       
   660         [
       
   661             last := char.
       
   662             char := format next.
       
   663             char == $]
       
   664         ] whileFalse:[
       
   665             char == $- ifFalse:[
       
   666                 scanset add:char
       
   667             ] ifTrue:[
       
   668                 last to:format next do:[:c |
       
   669                     scanset add:c
       
   670                 ]
       
   671             ]
       
   672         ].
       
   673         [
       
   674             data atEnd not and:[ (scanset includes:data peek) xor:exclusive ]
       
   675         ] whileTrue:[ return nextPut:data next ].
       
   676         final value:return contents
       
   677     ].
       
   678     ('lh' includes:char) ifTrue:[
       
   679         format next.
       
   680         char := format peek
       
   681     ].
       
   682     ('DUdu' includes:char) ifTrue:[
       
   683         final value:(Integer readFrom:data)
       
   684     ].
       
   685     ('FEGfeg' includes:char) ifTrue:[
       
   686         final value:(Float readFrom:data)
       
   687     ].
       
   688     ('b' includes:char) ifTrue:[
       
   689         final value:(Integer readFrom:data radix:2)
       
   690     ].
       
   691     ('Oo' includes:char) ifTrue:[
       
   692         final value:(Integer readFrom:data radix:8)
       
   693     ].
       
   694     ('Xx' includes:char) ifTrue:[
       
   695         final value:(Integer readFrom:data radix:16)
       
   696     ]
       
   697 
   614 
   698     "
   615     "
   699      '%d %x' sscanf:'1234 ff00'
   616      '%d %x' sscanf:'1234 ff00'
   700      '%d %x %b' sscanf:'1234 ff00 1001'
   617      '%d %x %b' sscanf:'1234 ff00 1001'
   701     "
   618     "
   702 
   619 
   703     "Modified: / 29-11-2011 / 11:55:39 / cg"
   620     "Modified: / 14-07-2017 / 11:09:35 / cg"
   704 ! !
   621 ! !
   705 
   622 
   706 !CharacterArray methodsFor:'converting'!
   623 !CharacterArray methodsFor:'converting'!
   707 
   624 
   708 sscanf:string
   625 sscanf:string