Xavax C++ Library Class Index   FAQ   Overview

Class xavax::Formatter

Formatter formats and prints values under control of a format string or precompiled format in a manner similar to the printf function in the standard C library; however, unlike printf and other varargs functions, Formatter provides a type-safe mechanism for passing parameters. Each parameter is converted to a Variant which records the type of the parameter. This conversion happens automatically and is essentially invisible to the user.

The format string contains three types of objects: plain characters, which are copied to the output, escape sequences which are converted and copied to the output, and format specifications, which specify the type and formatting of a value from the parameter list. Escape sequences are in backslash notation as defined in X3.159-1989 (ANSI C). The escape sequences and their meanings are described in the following table.

Escape
Sequence
Description
\\ output a backslash character
\' output a single quote character
\a output a bell character
\b output a backspace character
\e output an escape character
\f output a form feed character
\n output a new line character
\r output a carriage return character
\t output a tab character
\v output a vertical tab character
\num output a character whose ASCII value is the 1-, 2-, or 3-digit octal number num

Each format specification begins with a '%' character. The remainder of the specification consists of zero or more of the following flags in the following order.

Flag Description
- A minus sign specifies left adjustment of the output for this field.
+ A plus sign specifies that there should always be a sign character placed before the number when using signed formats.
space A space specifies that a blank should be left before a positive number for a signed field. A '+' overrides a space if both are used.
0 A zero specifies that zero-padding should be used rather than space-padding. A '-' overrides a zero if both are used.
param An optional digit string specifying a parameter number followed by a '$' character.
width An optional digit string specifying a field width. If the output string has fewer characters than the field width it will be blank-padded on the left (or right, if the left-adjustment indicator has been given) to make up the field width.
precision An optional period followed by an optional digit string which specifies the number of digits to be output after the decimal point, for floating point fields, or the maximum number of characters to be output for a string field. If the digit string is omitted, the precision defaults to zero.
format One of the characters [%bdefgosuxEG] which specifies the type of format to use.

The format characters have the following meaning.

Format Description
% Output a '%' character.
b Output a boolean value as "true" or "false".
d Output an integer as a decimal number.
e Output a double precision floating point parameter in the style [-]d.ddde+dd where there is one digit before the decimal point and the number of digits after the decimal point is equal to the precision specification. When the precision specification is missing, 6 digits are produced. With the E format, an upper-case E is used for the exponent.
E
f Output a double precision floating point parameter in the style [-]ddd.ddd where the number of digits after the decimal point is equal to the precision specification.
g Output a double precision floating point parameter in the format style f or e (E), whichever gives full precision in minimum space.
G
o Output an unsigned integer as an octal number.
s Output a string.
u Output an unsigned integer as a decimal number.
x Output an unsigned integer as a hexadecimal number.

Constructor Summary
Formatter()
         Construct a Formatter object.

Method Summary
void done()
         Perform any operations that should occur when formatting is complete.
void output(char c)
         Output a character.
void output(const char *s, int n)
         Output characters.
void print(const char* format, const Variant& param1, [const Variant& param2,
      ... const Variant& paramN])

         Format output.
void print(const Format& format, const Variant& param1, [const Variant& param2,
      ... const Variant& paramN])

         Format output using a precompiled format.
void print(const char* format, const Variant** params, int paramCount)
         Format output.
void print(const Format& format, const Variant** params, int paramCount)
         Format output using a precompiled format.
void start()
         Perform any operations that should occur before formatting begins.

Related Classes
CStringFormatter, Format, StreamFormatter, StringFormatter, Variant

Constructor Detail

Formatter

Formatter()
Construct a Formatter object.

Method Detail

done

void done()
This is a virtual function that can be overriden by a derived class to perform operations that must occur after formatting is complete such as flushing a stream or null-terminating a string. The default implementation does nothing.

output

void output(char c)
Output character c. This is a pure virtual function that must be defined by any concrete derived class.
Parameters:
c - the character to output.

output

void output(const char* s, int n)
Output n characters from string s. This is a virtual function that can be overriden by a derived class for better performance. The default implementation iterates over s calling output for each character.
Parameters:
s - the characters to output.
n - the number of characters to output.

print

void print(const char* format, const Variant& param1, [const Variant& param2,
           ... const Variant& paramN])
Format output under control of a format string. Up to 20 parameters may be passed.
Parameters:
format - the format control string.
parami - any of up to 20 parameters.

print

void print(const Format& format, const Variant& param1, [const Variant& param2,
           ... const Variant& paramN])
Format output under control of a precompiled format. Up to 20 parameters may be passed.
Parameters:
format - the precompiled format.
parami - any of up to 20 parameters.

print

void print(const char* format, const Variant** params, int paramCount)
Format output under control of a format string.
Parameters:
format - the format control string.
params - pointer to an array of parameters.
paramCount - the parameter count.

print

void print(const Format& format, const Variant** params, int paramCount)
Format output under control of a precompiled format.
Parameters:
format - the precompiled format.
params - pointer to an array of parameters.
paramCount - the parameter count.

start

void start()
This is a virtual function that can be overriden by a derived class to perform operations that must occur before formatting begins such as initializing a buffer. The default implementation does nothing.

Example Code

Formatter is an abstract base class. See the derived classes StreamFormatter, StringFormatter, and CStringFormatter for example code.

Copyright © 2003 Xavax Inc. -- All Rights Reserved