Skip to content

Commit 12d3ac2

Browse files
Merge pull request #23 from dreamer-coding/add_kwargs_stream
Introduces a keyword-based file mode mapping
2 parents 23b3060 + 248ec92 commit 12d3ac2

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

code/logic/stream.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,51 @@ typedef enum {
3434
FOSSIL_BUFFER_GIANT = 10000
3535
} fossil_limit_t;
3636

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+
3782
// Open a stream for file operations
3883
int32_t fossil_fstream_open(fossil_fstream_t *stream, const char *filename, const char *mode) {
3984
if (stream == NULL || filename == NULL || mode == NULL) {
@@ -46,7 +91,7 @@ int32_t fossil_fstream_open(fossil_fstream_t *stream, const char *filename, cons
4691
return FOSSIL_ERROR_LIMIT_REACHED;
4792
}
4893

49-
stream->file = fopen(filename, mode);
94+
stream->file = fopen(filename, fossil_fstream_mode_from_keyword(mode));
5095
if (stream->file == NULL) {
5196
fprintf(stderr, "Error: File not found - %s\n", filename);
5297
return FOSSIL_ERROR_FILE_NOT_FOUND;
@@ -73,7 +118,7 @@ int32_t fossil_fstream_freopen(fossil_fstream_t *stream, const char *filename, c
73118
return FOSSIL_ERROR_NULL_POINTER;
74119
}
75120

76-
FILE *new_file = freopen(filename, mode, file);
121+
FILE *new_file = freopen(filename, fossil_fstream_mode_from_keyword(mode), file);
77122
if (new_file == NULL) {
78123
fprintf(stderr, "Error: File not found - %s\n", filename);
79124
return FOSSIL_ERROR_FILE_NOT_FOUND;

0 commit comments

Comments
 (0)