]> pd.if.org Git - pdclib/blob - functions/stdio/vsscanf.c
Renamed status->this to status->current to allow for use with C++ compilers.
[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     struct _PDCLIB_status_t status;
18     status.base = 0;
19     status.flags = 0;
20     status.n = 0; 
21     status.i = 0;
22     status.current = 0;
23     status.s = (char *)s;
24     status.width = 0;
25     status.prec = 0;
26     status.stream = NULL;
27     va_copy( status.arg, arg );
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                     /* Matching error */
50                     return status.n;
51                 }
52                 else
53                 {
54                     ++status.s;
55                     ++status.i;
56                 }
57             }
58             ++format;
59         }
60         else
61         {
62             /* NULL return code indicates input error */
63             if ( rc == NULL )
64             {
65                 break;
66             }
67             /* Continue parsing after conversion specifier */
68             format = rc;
69         }
70     }
71     va_end( status.arg );
72     return status.n;
73 }
74
75 #endif
76
77 #ifdef TEST
78 #include <_PDCLIB_test.h>
79
80 int main( void )
81 {
82     char const * teststring1 = "abc  def";
83     char const * teststring2 = "abcdef";
84     char const * teststring3 = "abc%def";
85     int x;
86     TESTCASE( sscanf( teststring2, "abcdef%n", &x ) == 0 );
87     TESTCASE( x == 6 );
88     TESTCASE( sscanf( teststring1, "abc def%n", &x ) == 0 );
89     TESTCASE( x == 8 );
90     TESTCASE( sscanf( teststring2, "abc def%n", &x ) == 0 );
91     TESTCASE( x == 6 );
92     TESTCASE( sscanf( teststring3, "abc%%def%n", &x ) == 0 );
93     TESTCASE( x == 7 );
94     return TEST_RESULTS;
95 }
96
97 #endif