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