X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Fvsnprintf.c;h=761853ff8f627e38a7766e4b79184887f0b666ac;hp=0cccf7a0842d5814953d10c5b53d825104029a2b;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=60dae0fec674240bad2a6acc58dadbd81cbd9c3a diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 0cccf7a..761853f 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -1,6 +1,4 @@ -/* $Id$ */ - -/* vsnprintf( char *, size_t, const char *, va_list ap ) +/* vsnprintf( char *, size_t, const char *, va_list ) This file is part of the Public Domain C Library (PDCLib). Permission is granted to use, modify, and / or redistribute at will. @@ -10,65 +8,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; } int main( void ) { - char buffer[100]; -#include "printf_testcases.incl" + char target[100]; +#include "printf_testcases.h" return TEST_RESULTS; }