author | nemo |
Sun, 24 Mar 2024 14:34:27 -0400 | |
changeset 15996 | 19927b70588c |
parent 14913 | 68e1783762bc |
permissions | -rw-r--r-- |
7983 | 1 |
/* |
2 |
* XXX: assume all files are text files |
|
3 |
*/ |
|
4 |
||
5 |
#include "misc.h" |
|
6 |
#include "fileio.h" |
|
7 |
#include <string.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <assert.h> |
|
10 |
#include <sys/stat.h> |
|
11682 | 11 |
#include <unistd.h> |
7983 | 12 |
|
13 |
io_result_t IOResult; |
|
14 |
int FileMode; |
|
11682 | 15 |
char cwd[1024]; |
7983 | 16 |
|
17 |
static void init(File f) { |
|
18 |
f->fp = NULL; |
|
19 |
f->eof = 0; |
|
20 |
f->mode = NULL; |
|
21 |
f->record_len = 0; |
|
22 |
} |
|
23 |
||
24 |
void fpcrtl_assign__vars(File *f, string255 name) { |
|
25 |
FIX_STRING(name); |
|
26 |
*f = (File) malloc(sizeof(file_wrapper_t)); |
|
27 |
strcpy((*f)->file_name, name.str); |
|
28 |
init(*f); |
|
29 |
} |
|
30 |
||
31 |
void fpcrtl_reset1(File f) { |
|
32 |
f->fp = fopen(f->file_name, "r"); |
|
33 |
if (!f->fp) { |
|
34 |
IOResult = IO_ERROR_DUMMY; |
|
35 |
printf("Failed to open %s\n", f->file_name); |
|
36 |
return; |
|
37 |
} else { |
|
38 |
#ifdef FPCRTL_DEBUG |
|
39 |
printf("Opened %s\n", f->file_name); |
|
40 |
#endif |
|
41 |
} |
|
42 |
IOResult = IO_NO_ERROR; |
|
43 |
f->mode = "r"; |
|
44 |
} |
|
45 |
||
46 |
void fpcrtl_reset2(File f, int l) { |
|
47 |
f->eof = 0; |
|
48 |
f->fp = fopen(f->file_name, "rb"); |
|
49 |
if (!f->fp) { |
|
50 |
IOResult = IO_ERROR_DUMMY; |
|
51 |
printf("Failed to open %s\n", f->file_name); |
|
52 |
return; |
|
53 |
} |
|
54 |
IOResult = IO_NO_ERROR; |
|
55 |
f->mode = "rb"; |
|
56 |
f->record_len = l; |
|
57 |
} |
|
58 |
||
59 |
void __attribute__((overloadable)) fpcrtl_rewrite(File f) { |
|
60 |
f->fp = fopen(f->file_name, "w+"); |
|
61 |
if (!f->fp) { |
|
62 |
IOResult = IO_ERROR_DUMMY; |
|
63 |
return; |
|
64 |
} |
|
65 |
IOResult = IO_NO_ERROR; |
|
66 |
f->mode = "w+"; |
|
67 |
} |
|
68 |
||
69 |
void __attribute__((overloadable)) fpcrtl_rewrite(File f, Integer l) { |
|
70 |
IOResult = IO_NO_ERROR; |
|
71 |
fpcrtl_rewrite(f); |
|
72 |
if (f->fp) { |
|
73 |
f->record_len = l; |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
void fpcrtl_close(File f) { |
|
78 |
IOResult = IO_NO_ERROR; |
|
79 |
fclose(f->fp); |
|
80 |
free(f); |
|
81 |
} |
|
82 |
||
83 |
boolean fpcrtl_eof(File f) { |
|
84 |
IOResult = IO_NO_ERROR; |
|
85 |
if (f->eof || f->fp == NULL || feof(f->fp)) { |
|
86 |
return true; |
|
87 |
} else { |
|
88 |
return false; |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
void __attribute__((overloadable)) fpcrtl_readLn(File f) { |
|
93 |
IOResult = IO_NO_ERROR; |
|
94 |
char line[256]; |
|
95 |
if (fgets(line, sizeof(line), f->fp) == NULL) { |
|
96 |
f->eof = 1; |
|
97 |
} |
|
98 |
if (feof(f->fp)) { |
|
99 |
f->eof = 1; |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, Integer *i) { |
|
104 |
string255 s; |
|
105 |
||
106 |
if (feof(f->fp)) { |
|
107 |
f->eof = 1; |
|
108 |
return; |
|
109 |
} |
|
110 |
||
111 |
fpcrtl_readLn__vars(f, &s); |
|
112 |
||
113 |
*i = atoi(s.str); |
|
114 |
} |
|
115 |
||
116 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, LongWord *i) { |
|
117 |
string255 s; |
|
118 |
||
119 |
if (feof(f->fp)) { |
|
120 |
f->eof = 1; |
|
121 |
return; |
|
122 |
} |
|
123 |
||
124 |
fpcrtl_readLn__vars(f, &s); |
|
125 |
||
126 |
*i = atoi(s.str); |
|
127 |
} |
|
128 |
||
129 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, string255 *s) { |
|
130 |
||
131 |
if (fgets(s->str, 255, f->fp) == NULL) { |
|
132 |
||
133 |
s->len = 0; |
|
134 |
s->str[0] = 0; |
|
135 |
||
136 |
f->eof = 1; |
|
137 |
return; |
|
138 |
} |
|
139 |
||
140 |
if (feof(f->fp)) { |
|
141 |
s->len = 0; |
|
142 |
f->eof = 1; |
|
143 |
return; |
|
144 |
} |
|
145 |
||
146 |
IOResult = IO_NO_ERROR; |
|
147 |
||
148 |
s->len = strlen(s->str); |
|
149 |
if ((s->len > 0) && (s->str[s->len - 1] == '\n')) { |
|
150 |
s->str[s->len - 1] = 0; |
|
151 |
s->len--; |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
void __attribute__((overloadable)) fpcrtl_write(File f, string255 s) { |
|
156 |
FIX_STRING(s); |
|
157 |
fprintf(f->fp, "%s", s.str); |
|
158 |
} |
|
159 |
||
160 |
void __attribute__((overloadable)) fpcrtl_write(FILE *f, string255 s) { |
|
161 |
FIX_STRING(s); |
|
162 |
fprintf(f, "%s", s.str); |
|
163 |
} |
|
164 |
||
165 |
void __attribute__((overloadable)) fpcrtl_writeLn(File f, string255 s) { |
|
166 |
FIX_STRING(s); |
|
10575
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
167 |
// filthy hack to write to stderr |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
168 |
if (!f->fp) |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
169 |
fprintf(stderr, "%s\n", s.str); |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
170 |
else |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
171 |
fprintf(f->fp, "%s\n", s.str); |
7983 | 172 |
} |
173 |
||
174 |
void __attribute__((overloadable)) fpcrtl_writeLn(FILE *f, string255 s) { |
|
175 |
FIX_STRING(s); |
|
176 |
fprintf(f, "%s\n", s.str); |
|
177 |
} |
|
178 |
||
179 |
void fpcrtl_blockRead__vars(File f, void *buf, Integer count, Integer *result) { |
|
180 |
assert(f->record_len > 0); |
|
181 |
*result = fread(buf, f->record_len, count, f->fp); |
|
182 |
} |
|
183 |
||
184 |
/* |
|
185 |
* XXX: dummy blockWrite |
|
186 |
*/ |
|
187 |
void fpcrtl_blockWrite__vars(File f, const void *buf, Integer count, |
|
188 |
Integer *result) { |
|
14423 | 189 |
assert(f->record_len > 0); |
190 |
*result = fwrite(buf, f->record_len, count, f->fp); |
|
7983 | 191 |
} |
192 |
||
193 |
bool fpcrtl_directoryExists(string255 dir) { |
|
194 |
||
195 |
struct stat st; |
|
196 |
FIX_STRING(dir); |
|
197 |
||
198 |
IOResult = IO_NO_ERROR; |
|
199 |
||
200 |
#ifdef FPCRTL_DEBUG |
|
201 |
printf("Warning: directoryExists is called. This may not work when compiled to js.\n"); |
|
202 |
#endif |
|
203 |
||
204 |
if (stat(dir.str, &st) == 0) { |
|
205 |
return true; |
|
206 |
} |
|
207 |
||
208 |
return false; |
|
209 |
} |
|
210 |
||
211 |
bool fpcrtl_fileExists(string255 filename) { |
|
212 |
||
213 |
FIX_STRING(filename); |
|
214 |
||
215 |
IOResult = IO_NO_ERROR; |
|
216 |
||
217 |
FILE *fp = fopen(filename.str, "r"); |
|
218 |
if (fp) { |
|
219 |
fclose(fp); |
|
220 |
return true; |
|
221 |
} |
|
222 |
return false; |
|
223 |
} |
|
224 |
||
11682 | 225 |
char * fpcrtl_getCurrentDir(void) { |
226 |
||
227 |
IOResult = IO_NO_ERROR; |
|
228 |
||
229 |
if (getcwd(cwd, sizeof(cwd)) != NULL) |
|
230 |
return cwd; |
|
231 |
||
232 |
IOResult = IO_ERROR_DUMMY; |
|
233 |
return ""; |
|
234 |
} |
|
235 |
||
7983 | 236 |
void __attribute__((overloadable)) fpcrtl_flush(Text f) { |
9966 | 237 |
fflush(f->fp); |
7983 | 238 |
} |
239 |
||
240 |
void __attribute__((overloadable)) fpcrtl_flush(FILE *f) { |
|
241 |
fflush(f); |
|
242 |
} |
|
243 |
||
14913
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
244 |
Int64 fpcrtl_fileSize(File f) |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
245 |
{ |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
246 |
assert(f->record_len > 0); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
247 |
|
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
248 |
IOResult = IO_NO_ERROR; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
249 |
int i = fseek(f->fp, 0, SEEK_END); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
250 |
if (i == -1) { |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
251 |
IOResult = IO_ERROR_DUMMY; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
252 |
return -1; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
253 |
} |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
254 |
long size = ftell(f->fp); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
255 |
if (size == -1) { |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
256 |
IOResult = IO_ERROR_DUMMY; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
257 |
return -1; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
258 |
} |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
259 |
return size / f->record_len; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
260 |
} |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
261 |
|
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
262 |
bool fpcrtl_deleteFile(string255 filename) |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
263 |
{ |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
264 |
FIX_STRING(filename); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
265 |
|
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
266 |
int ret = remove(filename.str); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
267 |
if(ret == 0) { |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
268 |
IOResult = IO_NO_ERROR; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
269 |
return true; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
270 |
} else { |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
271 |
IOResult = IO_ERROR_DUMMY; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
272 |
return false; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
273 |
} |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
274 |
} |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
14423
diff
changeset
|
275 |