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