-
Notifications
You must be signed in to change notification settings - Fork 1
RawDoFmt
Srinivas Nayak edited this page Nov 26, 2014
·
6 revisions
NAME
RawDoFmt - format data into a character stream.
SYNOPSIS
NextData = RawDoFmt(FormatString, DataStream, PutChProc, PutChData);
APTR RawDoFmt(STRPTR,APTR,void (*)(),APTR);
FUNCTION
Perform "C"-language-like formatting of a data stream, outputting
the result a character at a time. Where % formatting commands are
found in the FormatString, they will be replaced with the
corresponding element in the DataStream. %% must be used in the
string if a % is desired in the output.
RawDoFmt() returns a pointer to the end of the DataStream
(The next argument that would have been processed). This allows
multiple formatting passes to be made using the same data.
INPUTS
FormatString - a "C"-language-like NULL terminated format string,
with the following supported % options:
%[flags][width.precision][length]type
flags - supported values are:
0 - The field will be padded with leading 0's
for type X, x, B, b, O, o, u, i and d.
Valid only when width is specified.
- - Left-justify within the given field boundary.
This overrides 0 flag.
width - supported values are:
number - Minimum number of characters to be printed.
If the value to be printed is shorter than this number,
the result is padded with blank spaces.
* - The width is not specified in the format string,
but as an additional integer value argument preceding
the argument that has to be formatted.
.precision - supported values are:
.number - For type s, maximum number of characters to
output from a string. For type X, x, B, b, O, o, u, i and d,
minimum number of digits to output.
. - If the period is specified without an explicit value
for precision, 0 is assumed.
.* - The precision is not specified in the format string,
but as an additional integer value argument preceding
the argument that has to be formatted.
If precision is specified, it overrides 0 flag.
length - supported values are:
l - size of input data becomes long (default is int).
ll - size of input data becomes long long.
type - supported types are:
d - decimal
i - decimal with sign
u - unsigned decimal
c - character
s - string, a pointer to a NULL terminated
byte string. A NULL pointer is treated
as an empty string.
o - octal with preceding "0o".
O - octal
x - hexadecimal with preceding "0x".
X - hexadecimal
b - binaty with preceding "0b".
B - binary
p - pointer (with preceding "0x").
DataStream - a stream of data that is interpreted according to
the format string. Often this is a pointer into
the task's stack.
PutChProc - the procedure to call with each character to be
output, called as:
PutChProc(char, PutChData);
the procedure is called with a NULL Char ('\0') at the end of
the format string.
PutChData - a value that is passed through to the PutChProc
procedure. This is untouched by RawDoFmt, and may be
modified by the PutChProc.
EXAMPLE
#include "exec_funcs.h"
APTR SysBase = NULL;
void addch(char ch, char** str)
{
char* p = *str;
*p = ch;
(*str)++;
}
void (*paddch)(char , char** ) = &addch;
void mysprintf (char* buf, char* fmt, ...)
{
va_list vl;
va_start(vl,fmt);
RawDoFmt(fmt, vl, paddch, buf);
va_end(vl);
}
int main(APTR sysBase)
{
SysBase = sysBase;
char buffer [50];
int m = 10;
int n = 5;
int k = -6;
mysprintf (buffer, "%010d", k);
mysprintf (buffer, "%*.*s", m, n, "srinivas");
return 0;
}