]> pd.if.org Git - pdclib/blob - functions/stdio/vsnprintf.c
Minimize the amount of internal definitions which get exposed via the user-visible...
[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
66 #include <_PDCLIB_test.h>
67
68 static int testprintf( char * s, const char * format, ... )
69 {
70     int i;
71     va_list arg;
72     va_start( arg, format );
73     i = vsnprintf( s, 100, format, arg );
74     va_end( arg );
75     return i;
76 }
77
78 int main( void )
79 {
80     char target[100];
81 #include "printf_testcases.h"
82     return TEST_RESULTS;
83 }
84
85 #endif
86