]> pd.if.org Git - pdclib/blob - functions/stdio/fscanf.c
restrict keyword cleanup.
[pdclib] / functions / stdio / fscanf.c
1 /* $Id$ */
2
3 /* fscanf( FILE *, const char *, ... )
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
14 int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... )
15 {
16     int rc;
17     va_list ap;
18     va_start( ap, format );
19     rc = vfscanf( stream, format, ap );
20     va_end( ap );
21     return rc;
22 }
23
24 #endif
25
26 #ifdef TEST
27 #include <_PDCLIB_test.h>
28
29 #include <string.h>
30
31 char scanstring[] = "  1 23\00045\000\00067 ";
32
33 void scantest( int testnr, FILE * fh, size_t position, char const * format, 
34                int expected_fscan_rc, char const * expected_fscan_output, size_t expected_fscan_length, 
35                int expected_sscan_rc, char const * expected_sscan_output, size_t expected_sscan_length )
36 {
37     char buffer[15];
38     printf( "Test %d\n", testnr );
39     TESTCASE( memset( buffer, -1, 15 ) == buffer );
40     TESTCASE( fseek( fh, position, SEEK_SET ) == 0 );
41     TESTCASE( fscanf( fh, format, buffer ) == expected_fscan_rc );
42     TESTCASE( memcmp( buffer, expected_fscan_output, expected_fscan_length ) == 0 );
43     TESTCASE( memset( buffer, -1, 15 ) == buffer );
44     TESTCASE( sscanf( scanstring + position, format, buffer ) == expected_sscan_rc );
45     TESTCASE( memcmp( buffer, expected_sscan_output, expected_sscan_length ) == 0 );
46 }
47
48 int main( void )
49 {
50     FILE * fh;
51     TESTCASE( ( fh = fopen( "testfile", "w+" ) ) != NULL );
52     TESTCASE( fwrite( scanstring, 14, 1, fh ) == 1 );
53     rewind( fh );
54
55     /* %14c - full scan */
56     scantest( 1, fh, 0, "%14c",
57               1, "  1 23\00045\000\00067 \377", 15,
58               1, "  1 23\377", 7 );
59
60     /* %c - default to one, reading whitespace */
61     scantest( 2, fh, 0, "%c",
62               1, " \377", 2,
63               1, " \377", 2 );
64
65     /* %1c - reading zero byte */
66     scantest( 3, fh, 9, "%1c",
67               1, "\000\377", 2,
68               -1, "\377", 1 );
69
70     /* %0c - NOT reading EOF */
71     scantest( 4, fh, 13, "%0c",
72               0, "\377", 1,
73               0, "\377", 1 );
74               
75     TESTCASE( fclose( fh ) == 0 );
76     //TESTCASE( remove( "testfile" ) == 0 );
77
78     return TEST_RESULTS;
79 }
80
81 #endif