F
Francois Grieu
Hello,
I'm tring to implement a function that behaves like sprintf,
but returns what it produce into newly mallocated memory block.
I opened ISO/IEC 9899:1999, found
int vsnprintf(char * restrict s, size_t n,
const char * restrict format,
va_list arg);
[note: my purchased copy of the standard has a typo, says vsprintf]
My goal can be achieved using this function: it can be passed NULL
in s and 0 in n, and then will preflight format and arg to determine
the output length, which let us know the size to malloc, then make
a second call to the function this time producing output.
Unfortunately, on many target platform vsnprintf is not implemented.
MS C runtime has _vsnprintf, which does not allow the above
(NULL in s and 0 in n cause a runtime exception).
However it is still usable by iteration: start with some arbitrary
output size likely suitable in the context, try that, if it does
not fit, double the size and try again until it works, finally trim
the block to approriate size. This likely is more efficient, as
most of the time only one parsing occurs.
Any idea for platforms without any form of vsnprintf?
Or any pointer to a compact standard-compliant implementation of the
"printf" parsing gear, which seems a lot of work?
Francois Grieu
I'm tring to implement a function that behaves like sprintf,
but returns what it produce into newly mallocated memory block.
I opened ISO/IEC 9899:1999, found
int vsnprintf(char * restrict s, size_t n,
const char * restrict format,
va_list arg);
[note: my purchased copy of the standard has a typo, says vsprintf]
My goal can be achieved using this function: it can be passed NULL
in s and 0 in n, and then will preflight format and arg to determine
the output length, which let us know the size to malloc, then make
a second call to the function this time producing output.
Unfortunately, on many target platform vsnprintf is not implemented.
MS C runtime has _vsnprintf, which does not allow the above
(NULL in s and 0 in n cause a runtime exception).
However it is still usable by iteration: start with some arbitrary
output size likely suitable in the context, try that, if it does
not fit, double the size and try again until it works, finally trim
the block to approriate size. This likely is more efficient, as
most of the time only one parsing occurs.
Any idea for platforms without any form of vsnprintf?
Or any pointer to a compact standard-compliant implementation of the
"printf" parsing gear, which seems a lot of work?
Francois Grieu