]> pd.if.org Git - pdclib/blob - functions/stdio/vsscanf.c
Compacted initializing of status struct.
[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     /* base, flag, n, i, current, s, width, prec, stream, arg */
18     struct _PDCLIB_status_t status = { 0, 0, 0, 0, 0, (char *)s, 0, 0, NULL, NULL };
19     va_copy( status.arg, arg );
20
21     while ( *format != '\0' )
22     {
23         const char * rc;
24         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) )
25         {
26             /* No conversion specifier, match verbatim */
27             if ( isspace( *format ) )
28             {
29                 /* Whitespace char in format string: Skip all whitespaces */
30                 /* No whitespaces in input do not result in matching error */
31                 while ( isspace( *status.s ) )
32                 {
33                     ++status.s;
34                     ++status.i;
35                 }
36             }
37             else
38             {
39                 /* Non-whitespace char in format string: Match verbatim */
40                 if ( *status.s != *format )
41                 {
42                     /* Matching error */
43                     return status.n;
44                 }
45                 else
46                 {
47                     ++status.s;
48                     ++status.i;
49                 }
50             }
51             ++format;
52         }
53         else
54         {
55             /* NULL return code indicates input error */
56             if ( rc == NULL )
57             {
58                 break;
59             }
60             /* Continue parsing after conversion specifier */
61             format = rc;
62         }
63     }
64     va_end( status.arg );
65     return status.n;
66 }
67
68 #endif
69
70 #ifdef TEST
71 #include <_PDCLIB_test.h>
72
73 int main( void )
74 {
75     char const * teststring1 = "abc  def";
76     char const * teststring2 = "abcdef";
77     char const * teststring3 = "abc%def";
78     int x;
79     TESTCASE( sscanf( teststring2, "abcdef%n", &x ) == 0 );
80     TESTCASE( x == 6 );
81     TESTCASE( sscanf( teststring1, "abc def%n", &x ) == 0 );
82     TESTCASE( x == 8 );
83     TESTCASE( sscanf( teststring2, "abc def%n", &x ) == 0 );
84     TESTCASE( x == 6 );
85     TESTCASE( sscanf( teststring3, "abc%%def%n", &x ) == 0 );
86     TESTCASE( x == 7 );
87     return TEST_RESULTS;
88 }
89
90 #endif