]> pd.if.org Git - pdclib/blob - functions/stdio/vfscanf.c
Compacted initializing of status struct.
[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     /* base, flags, n, i, current, s, width, prec, stream, arg */
18     struct _PDCLIB_status_t status = { 0, 0, 0, 0, 0, NULL, 0, 0, stream, NULL };
19     va_copy( status.arg, arg );
20     while ( *format != '\0' )
21     {
22         const char * rc;
23         if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) )
24         {
25             int c;
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 does not result in matching error */
31                 while ( isspace( c = getc( stream ) ) )
32                 {
33                     ++status.i;
34                 }
35                 if ( c != EOF )
36                 {
37                     ungetc( c, stream );
38                 }
39             }
40             else
41             {
42                 /* Non-whitespace char in format string: Match verbatim */
43                 if ( ( c = getc( stream ) ) != *format )
44                 {
45                     /* Matching error */
46                     ungetc( c, stream );
47                     return status.n;
48                 }
49                 else
50                 {
51                     ++status.i;
52                 }
53             }
54             ++format;
55         }
56         else
57         {
58             /* NULL return code indicates matching error */
59             if ( rc == NULL )
60             {
61                 break;
62             }
63             /* Continue parsing after conversion specifier */
64             format = rc;
65         }
66     }
67     va_end( status.arg );
68     return status.n;
69 }
70
71 #endif
72
73 #ifdef TEST
74 #include <_PDCLIB_test.h>
75
76 int main( void )
77 {
78     /* TODO: Check whitespace / EOF / ungetc handling */
79     TESTCASE( NO_TESTDRIVER );
80     return TEST_RESULTS;
81 }
82
83 #endif