Main Page | Modules | Data Structures | File List | Data Fields | Globals

bprintf.h File Reference

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Defines

#define INT2STRINGPADDED   /* ... for zero-padding, +signs, thousand grouping */

Functions

void putchar (char c)
 Calls sci_tx() to transmit a char over std SCI port.

void bputs (char *pSrc)
 POSIX puts() look-alike. Prints a string to "stdout".

void int2stringpadded (char **ppDst, sshort number, uchar intbase, uchar padsize, bool numgroup, bool plussign)
 Prints a number to a pre-allocated ASCII string, with extra formatting.

__inline__ char digit2char (char num) __attribute__((pure))
void bvsnprintf (char *pDst, ushort maxn, const char *pSrc, va_list ap)
 POSIX vsnprintf() look-alike.

void bsnprintf (char *pDst, ushort maxn, const char *pSrc,...)
 POSIX sprintf() look-alike.

void bprintf (ushort extra, const char *pString,...)
 POSIX printf look-alike, but is NOT COMPATIBLE.


Variables

bool lk_bputs
bool lk_putchar


Define Documentation

#define INT2STRINGPADDED   /* ... for zero-padding, +signs, thousand grouping */
 

Definition at line 39 of file bprintf.h.


Function Documentation

void bprintf ushort  extra,
const char *  pString,
  ...
 

POSIX printf look-alike, but is NOT COMPATIBLE.

Parameters:
extra This argument makes bprintf() incompatible. The caller must "guess" how many bytes MORE than the length of the pString char string the final output will be. extra is used to allocate the intermediate memory that is required.
pString A NULL-terminated, formatted ASCII char string.
... Variable argument list. There must be a matching argument for each '%' format specifier that appears in pString.
Warning:
bprintf() is NOT COMPATIBLE with the similar POSIX function printf().
See notes on thread locking.

Assumes the final output will be (strlen(pString) + extra) bytes in length. This function is a wrapper to bvsnprintf().

Thread locking
It's dodgy. See bputs().
See also:
bputs.
Note:
actually the "extra" is temporarily disabled for now, str fixed @ 40

Definition at line 400 of file bprintf.c.

References bputs(), bvsnprintf(), and ushort.

Referenced by bfree(), bmalloc(), bmallocinit(), bremalloc(), btnscan_chk(), exec_idle(), and task_dumpinfo().

00401 {   
00408     char pWorking[80]; 
00409     va_list ap;
00410     
00411     va_start(ap, pString);
00412     bvsnprintf(pWorking, 80, pString, ap);
00413     va_end(ap);
00414     bputs(pWorking);
00415         
00416     return;
00417 }

Here is the call graph for this function:

void bputs char *  pSrc  ) 
 

POSIX puts() look-alike. Prints a string to "stdout".

Parameters:
pSrc Source char string.

Definition at line 78 of file bprintf.c.

References lk_bputs, and putchar().

Referenced by bprintf(), exec_idle(), and task_dumpinfo().

00080 {   
00081 /*@-nullderef@*/ /*@-retvalint@*/ /*@-internalglobs@*/ /*@-modfilesys@*/
00082     while (lk_bputs);
00083     lk_bputs = true;
00084     do {
00085         putchar(*pSrc);
00086     } while ('\0' != *pSrc++);
00087     lk_bputs = false;
00088 /*@=nullderef@*/ /*@=retvalint@*/ /*@=internalglobs@*/ /*@=modfilesys@*/
00089     
00090     return;
00091 }

Here is the call graph for this function:

void bsnprintf char *  pDst,
ushort  maxn,
const char *  pSrc,
  ...
 

POSIX sprintf() look-alike.

Parameters:
pDst Pre-allocated destination char string. Must have at least maxn bytes allocated.
maxn Maximum number of characters to write to pDst.
pSrc NULL-terminated, formatted ASCII string to print from.
... Variable argument list. There must be a matching argument for each '%' format specifier that appears in pString.

Prints a formatted string to another string. A wrapper to bvsnprintf().

Definition at line 374 of file bprintf.c.

References bvsnprintf(), and ushort.

00375 {   
00377     va_list ap;
00378     
00379     va_start(ap, pSrc);
00380     bvsnprintf(pDst, maxn, pSrc, ap);
00381     va_end(ap);
00382     
00383     return;
00384 }

Here is the call graph for this function:

void bvsnprintf char *  pDst,
ushort  maxn,
const char *  pSrc,
va_list  ap
 

POSIX vsnprintf() look-alike.

Parameters:
pDst Pre-allocated destination char string.
maxn Maximum number of characters to print.
pSrc Formatted source char string.
ap <stdarg.h> va_list argument list. There must be a matching argument for each '%' format specifier that appears in pString.

Supports only the following format specifiers: %, i, X, o, B, s. If INT2STRINGPADDED is defined, additional format modifiers are supported:

  • ' (apostrophe - thousand grouping),
  • + (plus sign - writes plus sign if number is positive) and
  • 0n (zero padding modifier, followed by n zeroes)
eg. bvsnprintf(pDst, 20, "Binary number: %'+08B.\n", ap);

Definition at line 268 of file bprintf.c.

References bstrncpy(), int2stringpadded(), NULL, sshort, uchar, and ushort.

Referenced by bprintf(), and bsnprintf().

00270 {   
00279     char *pString = NULL;
00280     ushort ncopied = 0;
00281     sshort intvar = 0;
00282     uchar intbase = 0;
00283     uchar padzeroes = 0;
00284     bool plussign = false, numgroup = false;
00285     bool foundvar = false;
00286     
00287 #ifdef BPRINTF_DEBUG
00288     printf("bprintf(): pDst(%p), maxn(%i), pSrc = \"%s\"\n", pDst, maxn, pSrc);
00289 #endif
00290     do {
00291         if (*pSrc == '%') {
00292             foundvar = true;
00293         }
00294         if (foundvar) { /* If a '%' was hit, then interpret following chars. */
00295             switch (*(++pSrc)) {
00296                 case '%':   /* '%%' == '%' */
00297                     *pDst++ = *pSrc++;
00298                     ncopied++;
00299                     foundvar = false;
00300                 /*@switchbreak@*/break; 
00301                 case 'i':   /* decimal int */
00302                     intbase = 10;
00303                 /*@switchbreak@*/break;
00304                 case 'X':   /* hex int */
00305                 case 'x':
00306                 case 'p':
00307                     intbase = 0x10;
00308                 /*@switchbreak@*/break;
00309                 case 'o':   /* octal int */
00310                     intbase = 010;
00311                 /*@switchbreak@*/break;
00312                 case 'B':   /* binary int */
00313                     intbase = 2;
00314                 /*@switchbreak@*/break;
00315                 case 'c':   /* character char */
00316                     *pDst++ = (char) va_arg(ap, int);
00317                     ncopied++;      /*after here,finished with vartype*/
00318                 /*@switchbreak@*/break;
00319                 case 's':   /* a string var is to be printed */
00320                     pString = va_arg(ap, char *);
00321                     ncopied += bstrncpy(pDst, pString, maxn - ncopied);
00322                 /*@switchbreak@*/break;
00323                 case '0':   /* Zero padding */
00324                     padzeroes = *(pSrc + 1) - '0';
00325                 /*@switchbreak@*/break;
00326                 case '+':
00327                     plussign = true;
00328                 /*@switchbreak@*/break;
00329                 case '\'':
00330                     numgroup = true;
00331                 /*@switchbreak@*/break;
00332                 default:
00333                 /*@switchbreak@*/break;
00334             }
00335             if (0 < intbase) {
00336                 intvar = (sshort) (va_arg(ap, int));
00337                 pString = pDst;
00338 /*@-nullstate@*/
00339 #ifdef INT2STRINGPADDED
00340                 int2stringpadded(&pDst, intvar, intbase, padzeroes, numgroup, plussign);
00341 #else
00342 #ifdef INT2STRING
00343                 int2string(&pDst, intvar, intbase);
00344 #endif
00345 #endif
00346 /*@=nullstate@*/
00347                 pSrc++; /* Inc. past vartype char */
00348                 ncopied += pDst - pString;
00349                 intbase = 0;
00350                 foundvar = false;
00351             }
00352         } else {
00353             *(pDst++) = *pSrc;
00354             if ('\0' != *pSrc) pSrc++;
00355             ncopied++;
00356         }
00357     } while ( ((ushort)ncopied < maxn) && ('\0' != *(pSrc)) );
00358 /*@-nullderef@*/
00359     *pDst = '\0';
00360 /*@=nullderef@*/
00361     
00362     return;
00363 }

Here is the call graph for this function:

__inline__ char digit2char char  num  ) 
 

void int2stringpadded char **  ppDst,
sshort  number,
uchar  intbase,
uchar  padsize,
bool  numgroup,
bool  plussign
 

Prints a number to a pre-allocated ASCII string, with extra formatting.

Parameters:
ppDst Address of pre-allocated destination char pointer.
number The number to printed; 16 bit signed.
intbase The base value, eg. 10 for decimal, 2 for binary, 16 for hex.
padsize Number of zeroes to pad the number with.
numgroup Enable comma-separated "thousand" grouping of digits.
plussign Enable forced plus sign '+' if number is positive.

ppDst
Increments *ppDst as it goes. *ppDst will _NOT_ be NULL terminated, it is up to the caller to do so if necessary. On return, *ppDst will point to an as-yet unused byte.
intbase
int2string() can print to any arbitrary (even-integer) base number format as specified by the intbase parameter. Tested values: 2 (binary), 8 (octal), 16 (hex), 10 (decimal). For different values, please check that the switch(intbase) block sets multiplier to an appropriate value. Uses digit2char() for digit-to-character conversion.
Note:
int2stringpadded() performs the functions of int2string(), but may also (left) pad a number with a specified number of zeroes (padsize), can perform "thousand" grouping (numgroup), as well as enforcing a '+' sign if a number is positive (plussign).
Warning:
YOU NEED TO SET THE INT2STRINGPADDED #define TO USE THIS FUNCTION .

Definition at line 161 of file bprintf.c.

References digit2char, sshort, uchar, and ushort.

Referenced by bvsnprintf().

00165 {   
00186     ushort multiplier, i;
00187     uchar quotient, groupsize;
00188     uchar ndigit = (uchar)0;    /* ndigit: counts digit no. for grouping */
00189 
00190     /* Calc. groupsize, "thousand" comma grouping. Should handle 16 bit ints. */
00191     switch (intbase) {
00192         case 10:    
00193             groupsize = 3;  
00194             multiplier = 10000;     /* For decimal numbers */
00195         break;
00196         case 0x10:
00197             groupsize = 4;
00198             multiplier = 0x1000;    /* For hex numbers */
00199         break;
00200         default:    
00201             groupsize = 4;
00202             multiplier = 0x8000;    /* To get full 16bit range for oct+bin */
00203         break;
00204     }
00205     if (!numgroup) {
00206         groupsize = 127;    /* Cheap way of saying "no grouping" */
00207     }
00208     /* Deal with +/- signs. */
00209     if (0 > number) {
00210 /*@-nullderef@*/
00211         *(*ppDst)++ = '-';
00212         number *= -1;               /* Int printing assumes positive numbers */
00213     } else if (0 == number) {
00214         if (0 != padsize) padsize--;
00215         *(*ppDst)++ = '0';
00216     } else if (plussign) {
00217         *(*ppDst)++ = '+';  /* Mandatory plussign if needed */
00218     }
00219     /* Calc. no. digits; necessary for "thousand" grouping commas. */
00220     for (i = number; 0 < i; i /= intbase) {
00221         ndigit++;
00222     }
00223     /* Do zero padding, with "thousand" grouping of those zeroes */
00224     for (i = padsize; i > ndigit; i--) {
00225             *(*ppDst)++ = '0';
00226         if ((0 == ((i - 1) % (groupsize))) && (1 != i)) {   /* != 1: no end comma */
00227             *(*ppDst)++ = ',';
00228         }
00229     }
00230     /* The main int printing loop.  */
00231     do {                            /* Deal with non-zero, positive int */
00232         if (number >= multiplier) { /* Find a base multiple for number */
00233             quotient = number/multiplier;   /* quotient will be printed, */
00234             number -= quotient*multiplier;  /* so update number accordingly */
00235             digit2char(**ppDst, quotient);  /* print the quotient to string */
00236             (*ppDst)++;
00237             ndigit--;
00238             if ((0 == (ndigit % groupsize)) && (0 != ndigit)) {
00239                 *(*ppDst)++ = ',';  /* "thousand" comma grouping */
00240             }
00241             if (multiplier/intbase > number) {  /* Detect zero printing */
00242                 for (i = multiplier/intbase; i > number; i /= intbase) {
00243                     *(*ppDst)++ = '0';  /* otherwise they will be skipped */
00244                     ndigit--;
00245                     if ((0 == (ndigit % groupsize)) && (0 != ndigit)) {
00246                         *(*ppDst)++ = ',';
00247                     }
00248 /*@=nullderef@*/
00249                 }
00250             }
00251         }
00252         multiplier /= intbase;      /* Update multiplier for next digit print */
00253     } while (multiplier > 0);
00254     
00255     return;
00256 }

void putchar char  c  ) 
 

Calls sci_tx() to transmit a char over std SCI port.

Parameters:
c The char to be sent

Thread locking
It's dodgy.

Definition at line 61 of file bprintf.c.

References lk_putchar, SCI0BDH, sci_tx(), and uchar.

Referenced by bputs().

00062 {
00066 /*  while (lk_putchar);*/
00067     lk_putchar = true;
00068     sci_tx(((uchar *) (&SCI0BDH)), c);
00069     lk_putchar = false;
00070 }

Here is the call graph for this function:


Variable Documentation

bool lk_bputs
 

Definition at line 7 of file bprintf.h.

Referenced by _start(), and bputs().

bool lk_putchar
 

Definition at line 8 of file bprintf.h.

Referenced by _start(), and putchar().


Generated on Sat Apr 10 17:08:10 2004 for BeeOS by doxygen 1.3.6-20040222