3 /* _PDCLIB_filemode( const char * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
12 #include <_PDCLIB_io.h>
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
17 unsigned int _PDCLIB_filemode( char const * const mode )
31 rc |= _PDCLIB_FAPPEND | _PDCLIB_FWRITE;
34 /* Other than read, write, or append - invalid */
37 for ( size_t i = 1; i < 4; ++i )
42 if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
46 if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */
53 /* Other than read/write or binary - invalid. */
57 /* Longer than three chars - invalid. */
63 #include <_PDCLIB_test.h>
68 TESTCASE( _PDCLIB_filemode( "r" ) == _PDCLIB_FREAD );
69 TESTCASE( _PDCLIB_filemode( "w" ) == _PDCLIB_FWRITE );
70 TESTCASE( _PDCLIB_filemode( "a" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE ) );
71 TESTCASE( _PDCLIB_filemode( "r+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW ) );
72 TESTCASE( _PDCLIB_filemode( "w+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW ) );
73 TESTCASE( _PDCLIB_filemode( "a+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE | _PDCLIB_FRW ) );
74 TESTCASE( _PDCLIB_filemode( "rb" ) == ( _PDCLIB_FREAD | _PDCLIB_FBIN ) );
75 TESTCASE( _PDCLIB_filemode( "wb" ) == ( _PDCLIB_FWRITE | _PDCLIB_FBIN ) );
76 TESTCASE( _PDCLIB_filemode( "ab" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE | _PDCLIB_FBIN ) );
77 TESTCASE( _PDCLIB_filemode( "r+b" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
78 TESTCASE( _PDCLIB_filemode( "w+b" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
79 TESTCASE( _PDCLIB_filemode( "a+b" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
80 TESTCASE( _PDCLIB_filemode( "rb+" ) == ( _PDCLIB_FREAD | _PDCLIB_FRW | _PDCLIB_FBIN ) );
81 TESTCASE( _PDCLIB_filemode( "wb+" ) == ( _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
82 TESTCASE( _PDCLIB_filemode( "ab+" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE | _PDCLIB_FRW | _PDCLIB_FBIN ) );
83 TESTCASE( _PDCLIB_filemode( "x" ) == 0 );
84 TESTCASE( _PDCLIB_filemode( "r++" ) == 0 );
85 TESTCASE( _PDCLIB_filemode( "wbb" ) == 0 );
86 TESTCASE( _PDCLIB_filemode( "a+bx" ) == 0 );