]> pd.if.org Git - pdclib/blob - functions/stdio/vsnprintf.c
Streamlined printf testing.
[pdclib] / functions / stdio / vsnprintf.c
1 /* $Id$ */
2
3 /* vsnprintf( char *, size_t, const char *, va_list ap )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 #ifndef REGTEST
13
14 int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg )
15 {
16     /* TODO: This function should interpret format as multibyte characters.  */
17     struct _PDCLIB_status_t status;
18     status.base = 0;
19     status.flags = 0;
20     status.n = n;
21     status.i = 0;
22     status.current = 0;
23     status.s = s;
24     status.width = 0;
25     status.prec = 0;
26     status.stream = NULL;
27     va_copy( status.arg, arg );
28
29     while ( *format != '\0' )
30     {
31         const char * rc;
32         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
33         {
34             /* No conversion specifier, print verbatim */
35             s[ status.i++ ] = *(format++);
36         }
37         else
38         {
39             /* Continue parsing after conversion specifier */
40             format = rc;
41         }
42     }
43     s[ status.i ] = '\0';
44     va_end( status.arg );
45     return status.i;
46 }
47
48 #endif
49
50 #ifdef TEST
51 #include <_PDCLIB_test.h>
52
53 #include <limits.h>
54 #include <stdint.h>
55 #include <string.h>
56
57 static int testprintf( char * s, const char * format, ... )
58 {
59     int i;
60     va_list arg;
61     va_start( arg, format );
62     i = vsnprintf( s, 100, format, arg );
63     va_end( arg );
64     return i;
65 }
66
67 #define TESTCASE_SPRINTF( x ) if ( strcmp( target, x ) == 0 ) {} \
68                               else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - \"%s\" != %s\n", __LINE__, target, #x ); }
69
70 int main( void )
71 {
72     char target[100];
73 #include "printf_testcases.incl"
74     return TEST_RESULTS;
75 }
76
77 #endif
78