]> git.gag.com Git - fw/pdclib/blob - functions/_PDCLIB/filemode.c
Misallocation in realloc() was not caught. Fixed.
[fw/pdclib] / functions / _PDCLIB / filemode.c
1 /* $Id$ */
2
3 /* _PDCLIB_filemode( 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 #ifndef REGTEST
10
11 #include <stddef.h>
12
13 /* Helper function that parses the C-style mode string passed to fopen() into
14    the PDCLib flags FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (binary
15    mode).
16 */
17 unsigned int _PDCLIB_filemode( char const * const mode )
18 {
19     int rc = 0;
20     switch ( mode[0] )
21     {
22         case 'r':
23             rc |= _PDCLIB_FREAD;
24             break;
25         case 'w':
26             rc |= _PDCLIB_FWRITE;
27             break;
28         case 'a':
29             rc |= _PDCLIB_FAPPEND;
30             break;
31         default:
32             /* Other than read, write, or append - invalid */
33             return 0;
34     }
35     for ( size_t i = 1; i < 4; ++i )
36     {
37         switch ( mode[i] )
38         {
39             case '+':
40                 if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
41                 rc |= _PDCLIB_FRW;
42                 break;
43             case 'b':
44                 if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */
45                 rc |= _PDCLIB_FBIN;
46                 break;
47             case '\0':
48                 /* End of mode */
49                 return rc;
50             default:
51                 /* Other than read/write or binary - invalid. */
52                 return 0;
53         }
54     }
55     /* Longer than three chars - invalid. */
56     return 0;
57 }
58
59 #endif
60
61 #ifdef TEST
62 #include <_PDCLIB_test.h>
63
64 int main( void )
65 {
66 #ifndef REGTEST
67     TESTCASE( _PDCLIB_filemode( "r" ) == _PDCLIB_FREAD );
68     TESTCASE( _PDCLIB_filemode( "w" ) == _PDCLIB_FWRITE );
69     TESTCASE( _PDCLIB_filemode( "a" ) == _PDCLIB_FAPPEND );
70     TESTCASE( _PDCLIB_filemode( "r+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW ) );
71     TESTCASE( _PDCLIB_filemode( "w+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW ) );
72     TESTCASE( _PDCLIB_filemode( "a+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW ) );
73     TESTCASE( _PDCLIB_filemode( "rb" ) == ( _PDCLIB_FREAD | _PDCLIB_FBIN ) );
74     TESTCASE( _PDCLIB_filemode( "wb" ) == ( _PDCLIB_FWRITE | _PDCLIB_FBIN ) );
75     TESTCASE( _PDCLIB_filemode( "ab" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FBIN ) );
76     TESTCASE( _PDCLIB_filemode( "r+b" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
77     TESTCASE( _PDCLIB_filemode( "w+b" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
78     TESTCASE( _PDCLIB_filemode( "a+b" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW | _PDCLIB_FBIN ) );
79     TESTCASE( _PDCLIB_filemode( "rb+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
80     TESTCASE( _PDCLIB_filemode( "wb+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
81     TESTCASE( _PDCLIB_filemode( "ab+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FRW | _PDCLIB_FBIN ) );
82 #else
83     puts( " NOTEST fopen() test driver is PDCLib-specific." );
84 #endif
85     return TEST_RESULTS;
86 }
87
88 #endif