]> pd.if.org Git - pdclib/blob - functions/stdio/vfscanf.c
Copy & paste error having sscanf code in there. Fixed.
[pdclib] / functions / stdio / vfscanf.c
1 /* $Id$ */
2
3 /* vfscanf( FILE *, const char *, va_list )
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 #include <ctype.h>
12
13 #ifndef REGTEST
14
15 int vfscanf( FILE * _PDCLIB_restrict stream, 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 = NULL;
24     status.width = 0;
25     status.prec = 0;
26     status.stream = stream;
27     // = { 0, 0, 0, 0, 0, NULL, 0, 0, stream }
28     va_copy( status.arg, arg );
29     while ( *format != '\0' )
30     {
31         const char * rc;
32         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) )
33         {
34             int c;
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 does not result in matching error */
40                 while ( isspace( c = getc( stream ) ) )
41                 {
42                     ++status.i;
43                 }
44                 if ( c != EOF )
45                 {
46                     ungetc( c, stream );
47                 }
48             }
49             else
50             {
51                 /* Non-whitespace char in format string: Match verbatim */
52                 if ( ( c = getc( stream ) ) != *format )
53                 {
54                     /* Matching error */
55                     ungetc( c, stream );
56                     return status.n;
57                 }
58                 else
59                 {
60                     ++status.i;
61                 }
62             }
63             ++format;
64         }
65         else
66         {
67             /* NULL return code indicates matching error */
68             if ( rc == NULL )
69             {
70                 break;
71             }
72             /* Continue parsing after conversion specifier */
73             format = rc;
74         }
75     }
76     va_end( status.arg );
77     return status.n;
78 }
79
80 #endif
81
82 #ifdef TEST
83 #include <_PDCLIB_test.h>
84
85 int main( void )
86 {
87     /* TODO: Check whitespace / EOF / ungetc handling */
88     TESTCASE( NO_TESTDRIVER );
89     return TEST_RESULTS;
90 }
91
92 #endif