commit c7ad87c
Devine Lu Linvega
·
2025-01-22 23:28:21 +0000 UTC
parent f2469bc
(file.c) Removed indirections
1 files changed,
+33,
-28
+33,
-28
1@@ -85,14 +85,16 @@ put_statfile(Uint8 *dest, unsigned int len, const char *filepath, const char *ba
2 }
3
4 static int
5-put_fdir(UxnFile *uf, Uint8 *dest, int len)
6+put_fdir(int id, Uint8 *dest, int len)
7 {
8+ UxnFile *uf = &uxn_file[id];
9 Uint8 *_dest = dest;
10 static char pathname[0x2000];
11+ int in_folder = uf->filepath[0] == '.';
12 struct dirent *de = readdir(uf->dir);
13 for(; de != NULL; de = readdir(uf->dir)) {
14 if(de->d_name[0] == '.' && de->d_name[1] == 0) continue;
15- snprintf(pathname, 0x2000, uf->filepath[0] == '.' ? "%s/%s" : "%s%s", uf->filepath, de->d_name);
16+ snprintf(pathname, 0x2000, in_folder ? "%s/%s" : "%s%s", uf->filepath, de->d_name);
17 dest += put_statfile(dest, len, pathname, de->d_name);
18 }
19 return dest - _dest;
20@@ -109,7 +111,7 @@ is_dir_path(char *p)
21 }
22
23 static int
24-dir_exists(char *p)
25+is_dir_real(char *p)
26 {
27 struct stat st;
28 return stat(p, &st) == 0 && S_ISDIR(st.st_mode);
29@@ -123,7 +125,7 @@ file_write_dir(char *p)
30 for(; ok && (c = *p); p++) {
31 if(c == DIR_SEP_CHAR) {
32 *p = '\0';
33- ok = dir_exists(s) || mkdir(s);
34+ ok = is_dir_real(s) || mkdir(s);
35 *p = c;
36 }
37 }
38@@ -131,27 +133,29 @@ file_write_dir(char *p)
39 }
40
41 static void
42-file_reset(UxnFile *uf)
43+file_reset(int id)
44 {
45+ UxnFile *uf = &uxn_file[id];
46 if(uf->f != NULL) fclose(uf->f), uf->f = NULL;
47 if(uf->dir != NULL) closedir(uf->dir), uf->dir = NULL;
48 uf->state = IDLE;
49 }
50
51 static int
52-file_init(UxnFile *uf, Uint16 addr)
53+file_init(int id, Uint16 addr)
54 {
55- file_reset(uf);
56- uf->filepath = (char *)&uxn.ram[addr];
57+ file_reset(id);
58+ uxn_file[id].filepath = (char *)&uxn.ram[addr];
59 return 0;
60 }
61
62 static int
63-file_read(UxnFile *uf, Uint16 addr, int len)
64+file_read(int id, Uint16 addr, int len)
65 {
66+ UxnFile *uf = &uxn_file[id];
67 void *dest = &uxn.ram[addr];
68 if(uf->state != FILE_READ && uf->state != DIR_READ) {
69- file_reset(uf);
70+ file_reset(id);
71 if((uf->dir = opendir(uf->filepath)) != NULL)
72 uf->state = DIR_READ;
73 else if((uf->f = fopen(uf->filepath, "rb")) != NULL)
74@@ -160,17 +164,18 @@ file_read(UxnFile *uf, Uint16 addr, int len)
75 if(uf->state == FILE_READ)
76 return fread(dest, 1, len, uf->f);
77 if(uf->state == DIR_READ)
78- return put_fdir(uf, dest, len);
79+ return put_fdir(id, dest, len);
80 return 0;
81 }
82
83 static int
84-file_write(UxnFile *uf, Uint16 addr, int len, Uint8 flags)
85+file_write(int id, Uint16 addr, int len, Uint8 flags)
86 {
87 int ret = 0;
88+ UxnFile *uf = &uxn_file[id];
89 file_write_dir(uf->filepath);
90 if(uf->state != FILE_WRITE && uf->state != DIR_WRITE) {
91- file_reset(uf);
92+ file_reset(id);
93 if(is_dir_path(uf->filepath))
94 uf->state = DIR_WRITE;
95 else if((uf->f = fopen(uf->filepath, (flags & 0x01) ? "ab" : "wb")) != NULL)
96@@ -180,24 +185,24 @@ file_write(UxnFile *uf, Uint16 addr, int len, Uint8 flags)
97 if((ret = fwrite(&uxn.ram[addr], 1, len, uf->f)) > 0 && fflush(uf->f) != 0)
98 ret = 0;
99 if(uf->state == DIR_WRITE)
100- ret = dir_exists(uf->filepath);
101+ ret = is_dir_real(uf->filepath);
102 return ret;
103 }
104
105 static int
106-file_stat(UxnFile *uf, Uint16 addr, int len)
107+file_stat(int id, Uint16 addr, int len)
108 {
109 int err, dir;
110 struct stat st;
111- err = stat(uf->filepath, &st);
112+ err = stat(uxn_file[id].filepath, &st);
113 dir = S_ISDIR(st.st_mode);
114 return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
115 }
116
117 static int
118-file_delete(UxnFile *uf)
119+file_delete(int id)
120 {
121- return unlink(uf->filepath);
122+ return unlink(uxn_file[id].filepath);
123 }
124
125 static void
126@@ -215,18 +220,18 @@ file_deo(Uint8 port)
127 {
128 switch(port) {
129 /* File 1 */
130- case 0xa5: file_success(0xa2, file_stat(&uxn_file[0], PEEK2(&uxn.dev[0xa4]), rL1)); break;
131- case 0xa6: file_success(0xa2, file_delete(&uxn_file[0])); break;
132- case 0xa9: file_success(0xa2, file_init(&uxn_file[0], PEEK2(&uxn.dev[0xa8]))); break;
133 case 0xab: rL1 = PEEK2(&uxn.dev[0xaa]); break;
134- case 0xad: file_success(0xa2, file_read(&uxn_file[0], PEEK2(&uxn.dev[0xac]), rL1)); break;
135- case 0xaf: file_success(0xa2, file_write(&uxn_file[0], PEEK2(&uxn.dev[0xae]), rL1, uxn.dev[0xa7])); break;
136+ case 0xa5: file_success(0xa2, file_stat(0, PEEK2(&uxn.dev[0xa4]), rL1)); break;
137+ case 0xa6: file_success(0xa2, file_delete(0)); break;
138+ case 0xa9: file_success(0xa2, file_init(0, PEEK2(&uxn.dev[0xa8]))); break;
139+ case 0xad: file_success(0xa2, file_read(0, PEEK2(&uxn.dev[0xac]), rL1)); break;
140+ case 0xaf: file_success(0xa2, file_write(0, PEEK2(&uxn.dev[0xae]), rL1, uxn.dev[0xa7])); break;
141 /* File 2 */
142- case 0xb5: file_success(0xb2, file_stat(&uxn_file[1], PEEK2(&uxn.dev[0xb4]), rL2)); break;
143- case 0xb6: file_success(0xb2, file_delete(&uxn_file[1])); break;
144- case 0xb9: file_success(0xb2, file_init(&uxn_file[1], PEEK2(&uxn.dev[0xb8]))); break;
145 case 0xbb: rL2 = PEEK2(&uxn.dev[0xba]); break;
146- case 0xbd: file_success(0xb2, file_read(&uxn_file[1], PEEK2(&uxn.dev[0xbc]), rL2)); break;
147- case 0xbf: file_success(0xb2, file_write(&uxn_file[1], PEEK2(&uxn.dev[0xbe]), rL2, uxn.dev[0xb7])); break;
148+ case 0xb5: file_success(0xb2, file_stat(1, PEEK2(&uxn.dev[0xb4]), rL2)); break;
149+ case 0xb6: file_success(0xb2, file_delete(1)); break;
150+ case 0xb9: file_success(0xb2, file_init(1, PEEK2(&uxn.dev[0xb8]))); break;
151+ case 0xbd: file_success(0xb2, file_read(1, PEEK2(&uxn.dev[0xbc]), rL2)); break;
152+ case 0xbf: file_success(0xb2, file_write(1, PEEK2(&uxn.dev[0xbe]), rL2, uxn.dev[0xb7])); break;
153 }
154 }