]> pd.if.org Git - pdclib/blob - functions/stdio/vsnprintf.c
PDCLIB-20 #resolve Add support for "unusual" cases. Ammend test suite to verify support.
[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 #include <_PDCLIB_io.h>
14
15 int vsnprintf( char * _PDCLIB_restrict s, 
16                size_t n, 
17                const char * _PDCLIB_restrict format, 
18                _PDCLIB_va_list arg )
19 {
20     /* TODO: This function should interpret format as multibyte characters.  */
21     struct _PDCLIB_status_t status;
22     status.base = 0;
23     status.flags = 0;
24     status.n = n;
25     status.i = 0;
26     status.current = 0;
27     status.s = s;
28     status.width = 0;
29     status.prec = 0;
30     status.stream = NULL;
31     va_copy( status.arg, arg );
32
33     while ( *format != '\0' )
34     {
35         const char * rc;
36         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
37         {
38             /* No conversion specifier, print verbatim */
39             if ( status.i < n )
40             {
41                 s[ status.i ] = *format;
42             }
43             status.i++;
44             format++;
45         }
46         else
47         {
48             /* Continue parsing after conversion specifier */
49             format = rc;
50         }
51     }
52     if ( status.i  < n )
53     {
54         s[ status.i ] = '\0';
55     }
56     va_end( status.arg );
57     return status.i;
58 }
59
60 #endif
61
62 #ifdef TEST
63 #define _PDCLIB_FILEID "stdio/vsnprintf.c"
64 #define _PDCLIB_STRINGIO
65 #include <stdint.h>
66 #include <stddef.h>
67 #include <_PDCLIB_test.h>
68
69 static int testprintf( char * s, const char * format, ... )
70 {
71     int i;
72     va_list arg;
73     va_start( arg, format );
74     i = vsnprintf( s, 100, format, arg );
75     va_end( arg );
76     return i;
77 }
78
79 int main( void )
80 {
81     char target[100];
82 #include "printf_testcases.h"
83     return TEST_RESULTS;
84 }
85
86 #endif
87