]> pd.if.org Git - pdclib/blobdiff - functions/stdio/vsnprintf.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / vsnprintf.c
index 84ad51dc462d41a7e14c4b4a747a3ec076e1d2dc..761853ff8f627e38a7766e4b79184887f0b666ac 100644 (file)
@@ -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.
 #include <stdarg.h>
 
 #ifndef REGTEST
+#include "_PDCLIB_io.h"
+#include <string.h>
+
+struct state {
+    size_t bufrem;
+    char *bufp;
+};
+
+static size_t strout( void *p, const char *buf, size_t sz )
+{
+    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;
+}
 
-int vsnprintf( char * s, size_t n, const char * format, _PDCLIB_va_list arg )
+int vsnprintf( char * _PDCLIB_restrict s,
+               size_t n,
+               const char * _PDCLIB_restrict format,
+               _PDCLIB_va_list arg )
 {
-    /* TODO: This function should interpret format as multibyte characters.  */
-    /* Members: base, flags, n, i, this, s, width, prec, stream, arg         */
-    struct _PDCLIB_status_t status = { 0, 0, n, 0, 0, s, 0, 0, NULL, arg };
-    while ( *format != '\0' )
+    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';
-    return status.i;
+
+    return r;
 }
 
 #endif
 
 #ifdef TEST
-#include <_PDCLIB_test.h>
+#define _PDCLIB_FILEID "stdio/vsnprintf.c"
+#define _PDCLIB_STRINGIO
+#include <stdint.h>
+#include <stddef.h>
+#include "_PDCLIB_test.h"
+
+static int testprintf( char * s, const char * format, ... )
+{
+    int i;
+    va_list arg;
+    va_start( arg, format );
+    i = vsnprintf( s, 100, format, arg );
+    va_end( arg );
+    return i;
+}
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    char target[100];
+#include "printf_testcases.h"
     return TEST_RESULTS;
 }
 
 #endif
+