1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
char * format (char * form, ...) {
va_list args ; va_start (args, form) ;
char *s, *s0 ;
Uint weight ;
// Determine weight (count of bytes) in output (NUL excluded):
s0 = malloc (1 * sizeof(char)) ;
assert (s0 != NULL) ;
weight = vsnprintf (s0, 1, form, args) ;
printf ("weight:%i\n", weight) ;
assert (weight > 0) ;
weight ++ ; // for NUL terminator
free (s0) ; s0 = NULL ;
// Write into new string of that weight:
s = malloc (weight * sizeof(char)) ;
assert (s != NULL) ;
weight = vsprintf (s, form, args) ;
//~ printf ("weight:%i\n", weight) ;
va_end (args) ;
return s ;
}
void test () {
char * s = format ("int:%05i float:%+9.3f string:'%9s'",
123, 1.23, "123") ;
puts (s) ;
} |
Partager