@@ -34,6 +34,51 @@ typedef enum {
34
34
FOSSIL_BUFFER_GIANT = 10000
35
35
} fossil_limit_t ;
36
36
37
+ typedef struct {
38
+ const char * keyword ;
39
+ const char * mode ;
40
+ } fossil_fstream_mode_entry_t ;
41
+
42
+ static const fossil_fstream_mode_entry_t fossil_fstream_mode_table [] = {
43
+ // Classic C modes (standard fopen strings)
44
+ { "r" , "r" }, { "rb" , "rb" },
45
+ { "w" , "w" }, { "wb" , "wb" },
46
+ { "a" , "a" }, { "ab" , "ab" },
47
+ { "r+" , "r+" }, { "rb+" , "r+b" }, { "r+b" , "r+b" },
48
+ { "w+" , "w+" }, { "wb+" , "w+b" }, { "w+b" , "w+b" },
49
+ { "a+" , "a+" }, { "ab+" , "a+b" }, { "a+b" , "a+b" },
50
+
51
+ // Extended readable modes
52
+ { "read" , "r" },
53
+ { "readb" , "rb" },
54
+ { "write" , "w" },
55
+ { "writeb" , "wb" },
56
+ { "append" , "a" },
57
+ { "appendb" , "ab" },
58
+ { "read+write" , "r+" },
59
+ { "read+writeb" , "r+b" },
60
+ { "write+read" , "w+" },
61
+ { "write+readb" , "w+b" },
62
+ { "append+read" , "a+" },
63
+ { "append+readb" , "a+b" },
64
+ { "read+t" , "rt" },
65
+ { "write+t" , "wt" },
66
+ { "read+write+t" , "r+t" },
67
+
68
+ // Optional end-of-table sentinel
69
+ { NULL , NULL }
70
+ };
71
+
72
+ static const char * fossil_fstream_mode_from_keyword (const char * keyword ) {
73
+ if (keyword == NULL ) return NULL ;
74
+ for (int i = 0 ; fossil_fstream_mode_table [i ].keyword != NULL ; i ++ ) {
75
+ if (strcmp (keyword , fossil_fstream_mode_table [i ].keyword ) == 0 ) {
76
+ return fossil_fstream_mode_table [i ].mode ;
77
+ }
78
+ }
79
+ return NULL ;
80
+ }
81
+
37
82
// Open a stream for file operations
38
83
int32_t fossil_fstream_open (fossil_fstream_t * stream , const char * filename , const char * mode ) {
39
84
if (stream == NULL || filename == NULL || mode == NULL ) {
@@ -46,7 +91,7 @@ int32_t fossil_fstream_open(fossil_fstream_t *stream, const char *filename, cons
46
91
return FOSSIL_ERROR_LIMIT_REACHED ;
47
92
}
48
93
49
- stream -> file = fopen (filename , mode );
94
+ stream -> file = fopen (filename , fossil_fstream_mode_from_keyword ( mode ) );
50
95
if (stream -> file == NULL ) {
51
96
fprintf (stderr , "Error: File not found - %s\n" , filename );
52
97
return FOSSIL_ERROR_FILE_NOT_FOUND ;
@@ -73,7 +118,7 @@ int32_t fossil_fstream_freopen(fossil_fstream_t *stream, const char *filename, c
73
118
return FOSSIL_ERROR_NULL_POINTER ;
74
119
}
75
120
76
- FILE * new_file = freopen (filename , mode , file );
121
+ FILE * new_file = freopen (filename , fossil_fstream_mode_from_keyword ( mode ) , file );
77
122
if (new_file == NULL ) {
78
123
fprintf (stderr , "Error: File not found - %s\n" , filename );
79
124
return FOSSIL_ERROR_FILE_NOT_FOUND ;
0 commit comments