]> pd.if.org Git - pdclib/blob - functions/stdio/vsscanf.c
More improved unified tests (now life).
[pdclib] / functions / stdio / vsscanf.c
1 /* $Id$ */
2
3 /* vsscanf( const char *, const char *, va_list arg )
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 <ctype.h>
14
15 int vsscanf( const char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, va_list arg )
16 {
17     /* TODO: This function should interpret format as multibyte characters.  */
18     struct _PDCLIB_status_t status;
19     status.base = 0;
20     status.flags = 0;
21     status.n = 0;
22     status.i = 0;
23     status.current = 0;
24     status.s = (char *) s;
25     status.width = 0;
26     status.prec = 0;
27     status.stream = NULL;
28     va_copy( status.arg, arg );
29
30     while ( *format != '\0' )
31     {
32         const char * rc;
33         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) )
34         {
35             /* No conversion specifier, match verbatim */
36             if ( isspace( *format ) )
37             {
38                 /* Whitespace char in format string: Skip all whitespaces */
39                 /* No whitespaces in input do not result in matching error */
40                 while ( isspace( *status.s ) )
41                 {
42                     ++status.s;
43                     ++status.i;
44                 }
45             }
46             else
47             {
48                 /* Non-whitespace char in format string: Match verbatim */
49                 if ( *status.s != *format )
50                 {
51                     /* Matching error */
52                     return status.n;
53                 }
54                 else
55                 {
56                     ++status.s;
57                     ++status.i;
58                 }
59             }
60             ++format;
61         }
62         else
63         {
64             /* NULL return code indicates input error */
65             if ( rc == NULL )
66             {
67                 break;
68             }
69             /* Continue parsing after conversion specifier */
70             format = rc;
71         }
72     }
73     va_end( status.arg );
74     return status.n;
75 }
76
77 #endif
78
79 #ifdef TEST
80 #include <_PDCLIB_test.h>
81
82 #include "scan_test.h"
83
84 static int SCANFUNC( char const * stream, char const * format, ... )
85 {
86     va_list ap;
87     va_start( ap, format );
88     int result = vsscanf( stream, format, ap );
89     va_end( ap );
90     return result;
91 }
92
93 int main( void )
94 {
95 #include "sscan_sources.incl"
96 #include "scanf_testcases.incl"
97     return TEST_RESULTS;
98 }
99
100 #endif