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.
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 )
29 rc |= _PDCLIB_FAPPEND;
32 /* Other than read, write, or append - invalid */
35 for ( size_t i = 1; i < 4; ++i )
40 if ( rc & _PDCLIB_FRW ) return 0; /* Duplicates are invalid */
44 if ( rc & _PDCLIB_FBIN ) return 0; /* Duplicates are invalid */
51 /* Other than read/write or binary - invalid. */
55 /* Longer than three chars - invalid. */
62 #include <_PDCLIB_test.h>
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 ) );
83 puts( " NOTEST fopen() test driver is PDCLib-specific." );