eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'PrintfScanf':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: PrintfScanf


Inheritance:

   Object
   |
   +--PrintfScanf

Package:
stx:libbasic2
Category:
System-Support
Version:
rev: 1.28 date: 2018/03/15 21:52:37
user: cg
file: PrintfScanf.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


 Contributed by Jan Steinman donated to the community in 1989.

 Provided AS-IS - no warranty, use at your own risk.

 Original comment:

     NAME            printf-scanf
     AUTHOR          Jan Steinman <jans@tekgvs.labs.tek.com>
     FUNCTION        printf and scanf for Smalltalk
     ST-VERSIONS     Tek 2.2.2a 4.0
     PREREQUISITES   CharacterComparing
     CONFLICTS       
     DISTRIBUTION    world
     VERSION         1.1
     DATE            Apr 1989?
     SUMMARY 

 The following methods implement printf and scanf functionality.  They
 are intended to be used to ease porting between Smalltalk and C, and
 for facilitating machine-machine communication.  They are not at all
 intended as replacements for Smalltalk's printOn: functionality.

 Jan Steinman - N7JDB
 Tektronix Electronic Systems Laboratory
 Box 500, MS 50-370, Beaverton, OR 97077
 (w)503/627-5881 (h)503/657-7703

 changes:
     slight changes to make it work with higher precision real numbers
     (i.e. asking for the precision instead of hard-coding it)

     no need for the singleton - classes are already singletons ready to use exactly for that.
     (no need to use C++/Java patterns here in Smalltalk)

 Caveat:
     the behavior when the format-width is less than the preferred default precision is suboptimal;
     it leads to problems when trying to format tables into columns...
     For now: always use with width.precision formats to enforce the width.
     The current behavior (generating longer strings like printf does) may change in the future.


Class protocol:

instance creation
o  new

printing
o  printArgFrom: formatStream to: outStream arguments: argStream
Interpret the required number of arguments from <argStream>
according to the formatting information in <formatStream>.
Place the interpretation on <outStream>.
The interpretation is C printf(3) style, as described in the UTek manual page for printf(3).
<formatStream> is assumed to be positioned just past
$%, and a complete control string is assumed available.

Return when the conversion control string is consumed.
Leave <formatStream> pointing past the last character in the conversion control string.

This code assumes that <formatStream> is formatted according to
specification, and error checking is minimal. Unexpected
results will be obtained by illegal control strings, or when
argument types do not match conversion codes, but it probably
won't dump core, like C does in such cases!!

o  printf: formatString argument: arg
Format and print the receiver with <arg> formatted in C style,
as described in the UTek manual page for printf(3).
usage example(s):
     self printf:'%e' on:Transcript argument:(1.234 asShortFloat)
     self printf:'%e' on:Transcript argument:(1.234 asFloat)     
     self printf:'%e' on:Transcript argument:(1.234 asLongFloat) 
     self printf:'%e' on:Transcript argument:(1.234 asQDouble)   
     self printf:'%e' on:Transcript argument:(1.234 asInteger)   

     self printf:'%10e' on:Transcript argument:(1.234 asShortFloat)
     self printf:'%10e' on:Transcript argument:(1.234 asFloat)     
     self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) 
     self printf:'%10e' on:Transcript argument:(1.234 asQDouble)   
     self printf:'%10e' on:Transcript argument:(1.234 asInteger)   

     self printf:'%010e' on:Transcript argument:(1.234 asInteger)   
     self printf:'%-10e' on:Transcript argument:(1.234 asInteger)   

     self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat)
     self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat)     
     self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) 
     self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble)   
     self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger)   

o  printf: formatString arguments: args
Format and print the receiver with <args> formatted in C style,
as described in the UTek manual page for printf(3).
usage example(s):
     self printf:'%e' on:Transcript arguments:{ (1.234 asShortFloat) }
     self printf:'%e' on:Transcript arguments:{ (1.234 asFloat)      }
     self printf:'%e' on:Transcript arguments:{ (1.234 asLongFloat)  }
     self printf:'%e' on:Transcript arguments:{ (1.234 asQDouble)    }
     self printf:'%e' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat)      }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat)  }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble)    }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger)    }
     self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat)      }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat)  }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble)    }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger)    }

o  printf: formatString on: outStream argument: arg
Format and print formatString on <outStream> with <arg>
formatted in C style, as described in the UTek manual page for
printf(3).
usage example(s):
     self printf:'%e' on:Transcript argument:(1.234 asShortFloat). Transcript cr.
     self printf:'%e' on:Transcript argument:(1.234 asFloat)     . Transcript cr.
     self printf:'%e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr.
     self printf:'%e' on:Transcript argument:(1.234 asQDouble)   . Transcript cr.
     self printf:'%e' on:Transcript argument:(1.234 asInteger)   . Transcript cr.

     self printf:'%10e' on:Transcript argument:(1.234 asShortFloat). Transcript cr.
     self printf:'%10e' on:Transcript argument:(1.234 asFloat)     . Transcript cr.
     self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr.
     self printf:'%10e' on:Transcript argument:(1.234 asQDouble)   . Transcript cr.
     self printf:'%10e' on:Transcript argument:(1.234 asInteger)   . Transcript cr.

     self printf:'%010e' on:Transcript argument:(1.234 asInteger)  . Transcript cr.
     self printf:'%-10e' on:Transcript argument:(1.234 asInteger)  . Transcript cr.

     self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat). Transcript cr.
     self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat)     . Transcript cr.
     self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) . Transcript cr.
     self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble)   . Transcript cr.
     self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger)   . Transcript cr.

o  printf: aFormatString on: outStream arguments: args
Format and print aFormatString on <outStream> with <args>
formatted in C style, as described in the UTek manual page for
printf(3).
usage example(s):
     self printf:'%e\n' on:Transcript arguments:{ (1.234 asShortFloat) }
     self printf:'%f\n' on:Transcript arguments:{ (1.234 asShortFloat) }
     self printf:'%g\n' on:Transcript arguments:{ (1.234 asShortFloat) }

     self printf:'%e\n' on:Transcript arguments:{ (1.234 asFloat)      }
     self printf:'%f\n' on:Transcript arguments:{ (1.234 asFloat)      }
     self printf:'%g\n' on:Transcript arguments:{ (1.234 asFloat)      }

     self printf:'%e\n' on:Transcript arguments:{ (1.234 asLongFloat)  }
     self printf:'%e\n' on:Transcript arguments:{ (1.234 asQDouble)    }
     self printf:'%e\n' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat)      }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat)  }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble)    }
     self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger)    }
     self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger)    }

     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat)      }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat)  }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble)    }
     self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger)    }

private helpers
o  absDecimalPrintFloat: aFloat on: aStream digits: digits
Place a string representation of the receiver on <aStream>,
using <digits> significant digits, using decimal notation.

o  absPrintFloat: aFloat on: aStream digits: digits
Place a string representation of the receiver on <aStream>,
using <digits> significant digits.

o  absScientificPrintFloat: aFloat on: aStream digits: digits
Place a string representation of the receiver on <aStream>,
using <digits> significant digits, using scientific notation.

o  formatArgCountFor: aFormatString
Return the number of arguments required/produced,
if the argument is interpreted as a printf/scanf format control string.

o  scanArgFrom: dataStream to: collection format: format
Add to <collection> an object who's representation is found
in <dataStream> interpreted according to the conversion
control string in the Stream <format>. <format> is assumed to
be positioned just past a $%, and a complete control string is
assumed available.

Return when the conversion control string is consumed. Leave
<format> pointing past the last character in the conversion
control string, leave <dataStream> pointing past any width
specified in <format>, or at the first character that doesn't
make sense for the <format>.

scanning
o  scanf: formatString fromStream: dataStream
Return a Collection of objects found in the Character Stream
<dataStream> as interpreted according to the receiver. The
receiver is assumed to be a conversion control string as
specified in the UTek manual page for scanf(3).

o  sscanf: formatString fromString: aString
Return a Collection of objects found in <string> as
interpreted according to the receiver. The receiver is
assumed to be a conversion control string as specified in the
UTek manual page for scanf(3).


Instance protocol:

helpers
o  absDecimalPrintFloat: arg1 on: arg2 digits: arg3

o  absPrintFloat: arg1 on: arg2 digits: arg3

o  absScientificPrintFloat: arg1 on: arg2 digits: arg3

o  formatArgCountFor: arg

printing
o  printArgFrom: arg1 to: arg2 arguments: arg3

o  printf: aString arguments: args
Format and print the receiver with <args> formatted in C style,
as described in the UTek manual page for printf(3).
Returns the formatted printString.

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  printf: aFormatString on: outStream arguments: args
Format and print aFormatString on <outStream> with <args>
formatted in C style, as described in the UTek manual page for
printf(3).

** This is an obsolete interface - do not use it (it may vanish in future versions) **

scanning
o  scanArgFrom: dataStream to: collection format: format
Add to <collection> an object who's representation is found
in <dataStream> interpreted according to the conversion
control string in the Stream <format>. <format> is assumed to
be positioned just past a $%, and a complete control string is
assumed available.

Return when the conversion control string is consumed. Leave
<format> pointing past the last character in the conversion
control string, leave <dataStream> pointing past any width
specified in <format>, or at the first character that doesn't
make sense for the <format>.

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  scanf: formatString fromStream: dataStream
Return a Collection of objects found in the Character Stream
<dataStream> as interpreted according to the receiver. The
receiver is assumed to be a conversion control string as
specified in the UTek manual page for scanf(3).

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  sscanf: formatString fromString: aString
Return a Collection of objects found in <string> as
interpreted according to the receiver. The receiver is
assumed to be a conversion control string as specified in the
UTek manual page for scanf(3).

** This is an obsolete interface - do not use it (it may vanish in future versions) **


Examples:


self printf:'%#x %#X %03o%*.*s' arguments: #(16rABCD 16rEF 5 9 5 'ghijklmn') -> '0xabcd 0xEF 005 ghijk' self printf:'%- 10.4s%.2e' arguments: { 'abcdefghijkl' . Float pi } -> ' abcd 3.14e0' self printf:'%8.3f' arguments: { 200 sqrt negated } -> ' -14.142' self printf:'%8.3f' on:Transcript arguments: { 200 sqrt negated } self printf:'%10.4f' on:Transcript arguments: { 200 sqrt negated } self printf:'%20.10f' on:Transcript arguments: { 200 sqrt negated } self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789f } self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789q } self printf:'%x' arguments: #(16r41) -> '41' self printf:'%#x' arguments: #(16r41) -> '0x41' self printf:'%d' arguments: #(16r41) -> '65' self printf:'%b' arguments: #(16r41) -> '1000001' self printf:'%c' arguments: #(16r41) -> 'A' self printf:'%c' arguments: #( $A ) -> 'A' self printf:'%s' arguments: #( $A ) -> 'A' self printf:'%s' arguments: #( 'hello' ) -> 'hello' self printf:'%4s' arguments: #( 'hello' ) -> 'hello' self printf:'%7s' arguments: #( 'hello' ) -> ' hello' self sscanf:'%f%2s%s%s%s' fromString: '237.0 this is a test' -> OrderedCollection(237.0 'th' 'is' 'is' 'a') self sscanf:'%d%f%s' fromString: '25 54.32e-01 monday' -> OrderedCollection(25 5.432 'monday') self sscanf:'%f%*f %8[A-F0-9]%c%d 0x%x%f' fromString: '12.45 1048.73 AE40Z527 0x75BCD15 34' -> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) '%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn') -> '0xabcd 0xEF 005 ghijk' '%- 10.4s%.2e' printf: { 'abcdefghijkl' . Float pi } -> ' abcd 3.14e0' '%8.3f' printf: { 200 sqrt negated } -> ' -14.142' '%c' printf: #(16r41) -> 'A' '%f%2s%s%s%s' sscanf: '237.0 this is a test' -> OrderedCollection(237.0 'th' 'is' 'is' 'a') '%d%f%s' sscanf: '25 54.32e-01 monday' -> OrderedCollection(25 5.432 'monday') '%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34' -> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) '%d\n' printf:{ 1234 } on:Transcript '%f\n' printf:{ 1234.0 } on:Transcript '%f\n' printf:{ 1234.0 asShortFloat } on:Transcript '%f\n' printf:{ 1234.0 asLongFloat } on:Transcript '%f\n' printf:{ 1234.0 asQDouble } on:Transcript '%p\n' printf:{ 10@20 } on:Transcript '%P\n' printf:{ 10@20 } on:Transcript '%S\n' printf:{ 10@20 } on:Transcript

ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Mon, 16 Jul 2018 14:22:52 GMT