author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 16 Mar 2018 16:35:08 +0100 | |
changeset 13227 | 26c739440bef |
parent 11682 | 2c21bc80c95d |
child 14423 | a32b967f1341 |
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) { |
|
189 |
assert(0); |
|
190 |
} |
|
191 |
||
192 |
bool fpcrtl_directoryExists(string255 dir) { |
|
193 |
||
194 |
struct stat st; |
|
195 |
FIX_STRING(dir); |
|
196 |
||
197 |
IOResult = IO_NO_ERROR; |
|
198 |
||
199 |
#ifdef FPCRTL_DEBUG |
|
200 |
printf("Warning: directoryExists is called. This may not work when compiled to js.\n"); |
|
201 |
#endif |
|
202 |
||
203 |
if (stat(dir.str, &st) == 0) { |
|
204 |
return true; |
|
205 |
} |
|
206 |
||
207 |
return false; |
|
208 |
} |
|
209 |
||
210 |
bool fpcrtl_fileExists(string255 filename) { |
|
211 |
||
212 |
FIX_STRING(filename); |
|
213 |
||
214 |
IOResult = IO_NO_ERROR; |
|
215 |
||
216 |
FILE *fp = fopen(filename.str, "r"); |
|
217 |
if (fp) { |
|
218 |
fclose(fp); |
|
219 |
return true; |
|
220 |
} |
|
221 |
return false; |
|
222 |
} |
|
223 |
||
11682 | 224 |
char * fpcrtl_getCurrentDir(void) { |
225 |
||
226 |
IOResult = IO_NO_ERROR; |
|
227 |
||
228 |
if (getcwd(cwd, sizeof(cwd)) != NULL) |
|
229 |
return cwd; |
|
230 |
||
231 |
IOResult = IO_ERROR_DUMMY; |
|
232 |
return ""; |
|
233 |
} |
|
234 |
||
7983 | 235 |
void __attribute__((overloadable)) fpcrtl_flush(Text f) { |
9966 | 236 |
fflush(f->fp); |
7983 | 237 |
} |
238 |
||
239 |
void __attribute__((overloadable)) fpcrtl_flush(FILE *f) { |
|
240 |
fflush(f); |
|
241 |
} |
|
242 |