]> pd.if.org Git - pdclib/blob - functions/stdio/vfscanf.c
Proper handling of NULL return code.
[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.this = 0;
23     status.s = NULL;
24     status.width = 0;
25     status.prec = 0;
26     status.stream = stream;
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 matching 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     TESTCASE( NO_TESTDRIVER );
83     return TEST_RESULTS;
84 }
85
86 #endif