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