commit 4f919a2
Devine Lu Linvega
·
2026-06-13 16:25:58 +0000 UTC
parent 9fa80b9
Merged both file types and created a starting function for both
1 files changed,
+70,
-58
+70,
-58
1@@ -642,15 +642,16 @@ mouse_deo_vector(void)
2 #include <string.h>
3
4 typedef struct {
5- FILE *f;
6- DIR *dir;
7+ union {
8+ FILE *f;
9+ DIR *dir;
10+ } h;
11 char *filepath;
12 enum { IDLE,
13 FILE_READ,
14 FILE_WRITE,
15 DIR_READ,
16- DIR_WRITE
17- } state;
18+ DIR_WRITE } state;
19 } UxnFile;
20
21 static UxnFile ufs[2];
22@@ -671,10 +672,13 @@ make_pathfile(char *pathbuf, unsigned int len, const char *filepath, const char
23 }
24
25 static inline unsigned int
26-put_fill(Uint8 *dest, unsigned int len, char c)
27+put_text(Uint8 *dest, const char *text)
28 {
29- memset(dest, c, len);
30- return len;
31+ Uint8 *anchor = dest;
32+ while(*text)
33+ *dest = *text++, dest++;
34+ *dest = 0;
35+ return dest - anchor;
36 }
37
38 static inline unsigned int
39@@ -686,23 +690,19 @@ put_size(Uint8 *dest, unsigned int len, unsigned int size)
40 return len;
41 }
42
43-static inline unsigned int
44-put_text(Uint8 *dest, const char *text)
45-{
46- Uint8 *anchor = dest;
47- while(*text)
48- *dest = *text++, dest++;
49- *dest = 0;
50- return dest - anchor;
51-}
52-
53 static unsigned int
54 put_stat(Uint8 *dest, unsigned int len, unsigned int size, unsigned int err, unsigned int dir, unsigned int capsize)
55 {
56- if(err) return put_fill(dest, len, '!');
57- if(dir) return put_fill(dest, len, '-');
58- if(capsize && size >= 0x10000) return put_fill(dest, len, '?');
59- return put_size(dest, len, size);
60+ unsigned int i;
61+ if(err || dir || (capsize && size >= 0x10000)) {
62+ memset(dest, err ? '!' : dir ? '-' :
63+ '?',
64+ len);
65+ return len;
66+ }
67+ for(i = 0, dest += len; i < len; i++, size >>= 4)
68+ *(--dest) = "0123456789abcdef"[(Uint8)(size & 0xf)];
69+ return len;
70 }
71
72 static unsigned int
73@@ -773,11 +773,44 @@ file_write_dir(const char *p)
74 static void
75 file_reset(unsigned int id)
76 {
77- if(ufs[id].f != NULL) fclose(ufs[id].f), ufs[id].f = NULL;
78- if(ufs[id].dir != NULL) closedir(ufs[id].dir), ufs[id].dir = NULL;
79+ switch(ufs[id].state) {
80+ case FILE_READ:
81+ case FILE_WRITE: fclose(ufs[id].h.f); break;
82+ case DIR_READ: closedir(ufs[id].h.dir); break;
83+ default: break;
84+ }
85 ufs[id].state = IDLE;
86 }
87
88+static unsigned int
89+file_open(unsigned int id, unsigned int want_write, Uint8 flags)
90+{
91+ UxnFile *u = &ufs[id];
92+ struct stat st;
93+ if(want_write) {
94+ if(u->state == FILE_WRITE || u->state == DIR_WRITE)
95+ return 1;
96+ file_reset(id);
97+ file_write_dir(u->filepath);
98+ if(is_dir_path(u->filepath)) {
99+ u->state = is_dir_real(u->filepath) ? DIR_WRITE : IDLE;
100+ } else if((u->h.f = fopen(u->filepath, (flags & 0x01) ? "ab" : "wb"))) {
101+ u->state = FILE_WRITE;
102+ }
103+ } else {
104+ if(u->state == FILE_READ || u->state == DIR_READ)
105+ return 1;
106+ file_reset(id);
107+ if(stat(u->filepath, &st) == 0 && S_ISDIR(st.st_mode)) {
108+ if((u->h.dir = opendir(u->filepath)))
109+ u->state = DIR_READ;
110+ } else if((u->h.f = fopen(u->filepath, "rb"))) {
111+ u->state = FILE_READ;
112+ }
113+ }
114+ return u->state != IDLE;
115+}
116+
117 static unsigned int
118 file_init(unsigned int id, Uint16 addr)
119 {
120@@ -790,56 +823,35 @@ static unsigned int
121 file_read(unsigned int id, Uint16 addr, unsigned int len)
122 {
123 void *dest = &ram[addr];
124- struct stat st;
125 unsigned int n;
126 if(ufs[id].filepath == 0)
127 return 0;
128 if(addr + len > 0x10000)
129 len = 0x10000 - addr;
130- if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
131- file_reset(id);
132- if(stat(ufs[id].filepath, &st) == 0 && S_ISDIR(st.st_mode)) {
133- if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
134- ufs[id].state = DIR_READ;
135- } else if((ufs[id].f = fopen(ufs[id].filepath, "rb")) != NULL) {
136- ufs[id].state = FILE_READ;
137- }
138- }
139- if(ufs[id].state == FILE_READ) {
140- n = fread(dest, 1, len, ufs[id].f);
141- if(n == 0) file_reset(id);
142- return n;
143- }
144- if(ufs[id].state == DIR_READ) {
145- n = put_fdir(dest, len, ufs[id].filepath, ufs[id].dir);
146- if(n == 0) file_reset(id);
147- return n;
148- }
149- return 0;
150+ if(!file_open(id, 0, 0))
151+ return 0;
152+ if(ufs[id].state == FILE_READ)
153+ n = fread(dest, 1, len, ufs[id].h.f);
154+ else
155+ n = put_fdir(dest, len, ufs[id].filepath, ufs[id].h.dir);
156+ if(n == 0) file_reset(id);
157+ return n;
158 }
159
160 static unsigned int
161 file_write(unsigned int id, Uint16 addr, unsigned int len, Uint8 flags)
162 {
163- unsigned int ret = 0;
164 if(ufs[id].filepath == 0)
165 return 0;
166 if(addr + len > 0x10000)
167 len = 0x10000 - addr;
168- file_write_dir(ufs[id].filepath);
169- if(ufs[id].state != FILE_WRITE && ufs[id].state != DIR_WRITE) {
170- file_reset(id);
171- if(is_dir_path(ufs[id].filepath))
172- ufs[id].state = DIR_WRITE;
173- else if((ufs[id].f = fopen(ufs[id].filepath, (flags & 0x01) ? "ab" : "wb")) != NULL)
174- ufs[id].state = FILE_WRITE;
175+ if(!file_open(id, 1, flags))
176+ return 0;
177+ if(ufs[id].state == FILE_WRITE) {
178+ unsigned int ret = fwrite(&ram[addr], 1, len, ufs[id].h.f);
179+ return (ret > 0 && fflush(ufs[id].h.f) != 0) ? 0 : ret;
180 }
181- if(ufs[id].state == FILE_WRITE)
182- if((ret = fwrite(&ram[addr], 1, len, ufs[id].f)) > 0 && fflush(ufs[id].f) != 0)
183- ret = 0;
184- if(ufs[id].state == DIR_WRITE)
185- ret = is_dir_real(ufs[id].filepath);
186- return ret;
187+ return is_dir_real(ufs[id].filepath);
188 }
189
190 static unsigned int