Reworked scanf() testing. General cleanups.
[fw/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                     if ( *status.s == '\0' && status.n == 0 )
52                     {
53                         /* Early input error */
54                         return EOF;
55                     }
56                     /* Matching error */
57                     return status.n;
58                 }
59                 else
60                 {
61                     ++status.s;
62                     ++status.i;
63                 }
64             }
65             ++format;
66         }
67         else
68         {
69             /* NULL return code indicates error */
70             if ( rc == NULL )
71             {
72                 if ( ( *status.s == '\n' ) && ( status.n == 0 ) )
73                 {
74                     status.n = EOF;
75                 }
76                 break;
77             }
78             /* Continue parsing after conversion specifier */
79             format = rc;
80         }
81     }
82     va_end( status.arg );
83     return status.n;
84 }
85
86 #endif
87
88 #ifdef TEST
89 #define _PDCLIB_FILEID "stdio/vsscanf.c"
90 #define _PDCLIB_STRINGIO
91
92 #include <_PDCLIB_test.h>
93
94 static int testscanf( char const * stream, char const * format, ... )
95 {
96     va_list ap;
97     va_start( ap, format );
98     int result = vsscanf( stream, format, ap );
99     va_end( ap );
100     return result;
101 }
102
103 int main( void )
104 {
105     char source[100];
106 #include "scanf_testcases.h"
107     return TEST_RESULTS;
108 }
109
110 #endif