]> pd.if.org Git - pdclib/blob - functions/stdio/vfscanf.c
Individual init to get around warnings on 64bit.
[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     /* 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 = NULL;
25     status.width = 0;
26     status.prec = 0;
27     status.stream = stream;
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             int c;
36             /* No conversion specifier, match verbatim */
37             if ( isspace( *format ) )
38             {
39                 /* Whitespace char in format string: Skip all whitespaces */
40                 /* No whitespaces in input does not result in matching error */
41                 while ( isspace( c = getc( stream ) ) )
42                 {
43                     ++status.i;
44                 }
45                 if ( c != EOF )
46                 {
47                     ungetc( c, stream );
48                 }
49             }
50             else
51             {
52                 /* Non-whitespace char in format string: Match verbatim */
53                 if ( ( c = getc( stream ) ) != *format )
54                 {
55                     /* Matching error */
56                     ungetc( c, stream );
57                     return status.n;
58                 }
59                 else
60                 {
61                     ++status.i;
62                 }
63             }
64             ++format;
65         }
66         else
67         {
68             /* NULL return code indicates matching error */
69             if ( rc == NULL )
70             {
71                 break;
72             }
73             /* Continue parsing after conversion specifier */
74             format = rc;
75         }
76     }
77     va_end( status.arg );
78     return status.n;
79 }
80
81 #endif
82
83 #ifdef TEST
84 #include <_PDCLIB_test.h>
85
86 int main( void )
87 {
88     /* TODO: Check whitespace / EOF / ungetc handling */
89     TESTCASE( NO_TESTDRIVER );
90     return TEST_RESULTS;
91 }
92
93 #endif