C-formatted Output

The service c_output provides methods for using the C function sprintf to convert constants to strings. It provides single specification output conversion interface, supporting all sprintf capabilities, except for 'p' conversion, 'n' reflection and '*' reference (see: Webmonkeys c_guide: sprintf function for details).

The input is a <specification>, whose BNF is:

 <specification> ::= <letter>(<cspec_value>)
                     <value>

 <letter>        ::= c | d | e | E | f | g | G | i | o | s | u | x | X

 <cspec_value>   ::= <value>
                     <cspec> , <value>

 <cspec>         ::= <parameters>
                     "<flags>"
                     "<flags>" , <parameters>

 <parameters>    ::= <integer>
                     <integer> , <integer>

 <flag>          ::= - | + | <space> | 0 | #

 <flags>         ::= <flag>
                     <flag> <flags>

 <value>         ::= <number>
                     <string>

Four entries are provided:

  1. c_output#prepare(<specification>, <format_string>, <value>)

    transforms <specification> into output <format_string> and <value>.

    Note that the input <value> may be converted to an output <value> so as to be consistent with the <specification> <letter>. Errors are displayed.

    e.g.

        @X = 776
        @c_output#prepare("E"("+", 10, 20, X?), Format^, Value^)

    produces output:

        Value = 776.0
        Format = %+10.20E
  2. c_output#prepare(<specification>, <format_string>, <value>, <errors>)

    is just like 1, except that <errors> is a (possibly empty) stream of diagnostics.

  3. c_output#sprint(<specification>, <formatted_output>) where <formatted_output> is the formatted (possibly converted) <value>. In case of error, a stream of diagnostics is displayed.

    e.g.

        @X = 1234567.89
        @c_output#sprint('G'('+', 10, 5, X?), FO^)

    produces output:

        @FO = +1.2346E+06
  4. c_output#sprint_list(<specification_list>, <formatted_output_list>, <errors_list>)

    where <specification_list> is a (square-bracketed) list of <specification>,
    <formatted_output_list> is the corresponding list of <formatted_output>, and
    <errors_list> is the corresponding list of <errors>.

Possible items in the <errors> stream are largely self-explanatory:



This document is licensed under: Gnu General Public License - Version 3