X-Git-Url: https://pd.if.org/git/?p=pdclib.old;a=blobdiff_plain;f=functions%2Fstdio%2Fvsnprintf.c;h=6cc233d97f4bed54e2cea64b20ac092734f2c163;hp=fd9b4ac3fc34e5cb57477395371164b6536e3eaf;hb=3309ec3ad8a5db735eaa2de7f5dc6a331d8e7319;hpb=6bd7f065eafd2d53f82c6bf4525456fad56b8a47 diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index fd9b4ac..6cc233d 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -10,66 +10,64 @@ #include #ifndef REGTEST +#include <_PDCLIB_io.h> +#include + +struct state { + size_t bufrem; + char *bufp; +}; -int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) +static size_t strout( void *p, const char *buf, size_t sz ) { - /* TODO: This function should interpret format as multibyte characters. */ - struct _PDCLIB_status_t status; - status.base = 0; - status.flags = 0; - status.n = n; - status.i = 0; - status.current = 0; - status.s = s; - status.width = 0; - status.prec = 0; - status.stream = NULL; - va_copy( status.arg, arg ); + struct state *s = p; + size_t copy = s->bufrem >= sz ? sz : s->bufrem; + memcpy( s->bufp, buf, copy ); + s->bufrem -= copy; + s->bufp += copy; + return sz; +} - while ( *format != '\0' ) +int vsnprintf( char * _PDCLIB_restrict s, + size_t n, + const char * _PDCLIB_restrict format, + _PDCLIB_va_list arg ) +{ + struct state st; + st.bufrem = n; + st.bufp = s; + int r = _vcbprintf( &st, strout, format, arg ); + if ( st.bufrem ) { - const char * rc; - if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) ) - { - /* No conversion specifier, print verbatim */ - s[ status.i++ ] = *(format++); - } - else - { - /* Continue parsing after conversion specifier */ - format = rc; - } + *st.bufp = 0; } - s[ status.i ] = '\0'; - va_end( status.arg ); - return status.i; + + return r; } #endif #ifdef TEST -#include <_PDCLIB_test.h> - -#include +#define _PDCLIB_FILEID "stdio/vsnprintf.c" +#define _PDCLIB_STRINGIO #include -#include +#include +#include <_PDCLIB_test.h> -static int testprintf( char * s, size_t n, const char * format, ... ) +static int testprintf( char * s, const char * format, ... ) { int i; va_list arg; va_start( arg, format ); - i = vsnprintf( s, n, format, arg ); + i = vsnprintf( s, 100, format, arg ); va_end( arg ); return i; } -#define TESTCASE_SPRINTF( x ) TESTCASE( x ) - int main( void ) { - char buffer[100]; -#include "printf_testcases.incl" + char target[100]; +#include "printf_testcases.h" return TEST_RESULTS; }