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