author | sheepluva |
Sun, 09 Aug 2015 00:25:35 +0200 | |
changeset 11040 | 3ab93d702fb1 |
parent 10575 | 13b1e9008f4b |
child 11682 | 2c21bc80c95d |
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> |
|
11 |
||
12 |
io_result_t IOResult; |
|
13 |
int FileMode; |
|
14 |
||
15 |
static void init(File f) { |
|
16 |
f->fp = NULL; |
|
17 |
f->eof = 0; |
|
18 |
f->mode = NULL; |
|
19 |
f->record_len = 0; |
|
20 |
} |
|
21 |
||
22 |
void fpcrtl_assign__vars(File *f, string255 name) { |
|
23 |
FIX_STRING(name); |
|
24 |
*f = (File) malloc(sizeof(file_wrapper_t)); |
|
25 |
strcpy((*f)->file_name, name.str); |
|
26 |
init(*f); |
|
27 |
} |
|
28 |
||
29 |
void fpcrtl_reset1(File f) { |
|
30 |
f->fp = fopen(f->file_name, "r"); |
|
31 |
if (!f->fp) { |
|
32 |
IOResult = IO_ERROR_DUMMY; |
|
33 |
printf("Failed to open %s\n", f->file_name); |
|
34 |
return; |
|
35 |
} else { |
|
36 |
#ifdef FPCRTL_DEBUG |
|
37 |
printf("Opened %s\n", f->file_name); |
|
38 |
#endif |
|
39 |
} |
|
40 |
IOResult = IO_NO_ERROR; |
|
41 |
f->mode = "r"; |
|
42 |
} |
|
43 |
||
44 |
void fpcrtl_reset2(File f, int l) { |
|
45 |
f->eof = 0; |
|
46 |
f->fp = fopen(f->file_name, "rb"); |
|
47 |
if (!f->fp) { |
|
48 |
IOResult = IO_ERROR_DUMMY; |
|
49 |
printf("Failed to open %s\n", f->file_name); |
|
50 |
return; |
|
51 |
} |
|
52 |
IOResult = IO_NO_ERROR; |
|
53 |
f->mode = "rb"; |
|
54 |
f->record_len = l; |
|
55 |
} |
|
56 |
||
57 |
void __attribute__((overloadable)) fpcrtl_rewrite(File f) { |
|
58 |
f->fp = fopen(f->file_name, "w+"); |
|
59 |
if (!f->fp) { |
|
60 |
IOResult = IO_ERROR_DUMMY; |
|
61 |
return; |
|
62 |
} |
|
63 |
IOResult = IO_NO_ERROR; |
|
64 |
f->mode = "w+"; |
|
65 |
} |
|
66 |
||
67 |
void __attribute__((overloadable)) fpcrtl_rewrite(File f, Integer l) { |
|
68 |
IOResult = IO_NO_ERROR; |
|
69 |
fpcrtl_rewrite(f); |
|
70 |
if (f->fp) { |
|
71 |
f->record_len = l; |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
void fpcrtl_close(File f) { |
|
76 |
IOResult = IO_NO_ERROR; |
|
77 |
fclose(f->fp); |
|
78 |
free(f); |
|
79 |
} |
|
80 |
||
81 |
boolean fpcrtl_eof(File f) { |
|
82 |
IOResult = IO_NO_ERROR; |
|
83 |
if (f->eof || f->fp == NULL || feof(f->fp)) { |
|
84 |
return true; |
|
85 |
} else { |
|
86 |
return false; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
void __attribute__((overloadable)) fpcrtl_readLn(File f) { |
|
91 |
IOResult = IO_NO_ERROR; |
|
92 |
char line[256]; |
|
93 |
if (fgets(line, sizeof(line), f->fp) == NULL) { |
|
94 |
f->eof = 1; |
|
95 |
} |
|
96 |
if (feof(f->fp)) { |
|
97 |
f->eof = 1; |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, Integer *i) { |
|
102 |
string255 s; |
|
103 |
||
104 |
if (feof(f->fp)) { |
|
105 |
f->eof = 1; |
|
106 |
return; |
|
107 |
} |
|
108 |
||
109 |
fpcrtl_readLn__vars(f, &s); |
|
110 |
||
111 |
*i = atoi(s.str); |
|
112 |
} |
|
113 |
||
114 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, LongWord *i) { |
|
115 |
string255 s; |
|
116 |
||
117 |
if (feof(f->fp)) { |
|
118 |
f->eof = 1; |
|
119 |
return; |
|
120 |
} |
|
121 |
||
122 |
fpcrtl_readLn__vars(f, &s); |
|
123 |
||
124 |
*i = atoi(s.str); |
|
125 |
} |
|
126 |
||
127 |
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, string255 *s) { |
|
128 |
||
129 |
if (fgets(s->str, 255, f->fp) == NULL) { |
|
130 |
||
131 |
s->len = 0; |
|
132 |
s->str[0] = 0; |
|
133 |
||
134 |
f->eof = 1; |
|
135 |
return; |
|
136 |
} |
|
137 |
||
138 |
if (feof(f->fp)) { |
|
139 |
s->len = 0; |
|
140 |
f->eof = 1; |
|
141 |
return; |
|
142 |
} |
|
143 |
||
144 |
IOResult = IO_NO_ERROR; |
|
145 |
||
146 |
s->len = strlen(s->str); |
|
147 |
if ((s->len > 0) && (s->str[s->len - 1] == '\n')) { |
|
148 |
s->str[s->len - 1] = 0; |
|
149 |
s->len--; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
void __attribute__((overloadable)) fpcrtl_write(File f, string255 s) { |
|
154 |
FIX_STRING(s); |
|
155 |
fprintf(f->fp, "%s", s.str); |
|
156 |
} |
|
157 |
||
158 |
void __attribute__((overloadable)) fpcrtl_write(FILE *f, string255 s) { |
|
159 |
FIX_STRING(s); |
|
160 |
fprintf(f, "%s", s.str); |
|
161 |
} |
|
162 |
||
163 |
void __attribute__((overloadable)) fpcrtl_writeLn(File f, string255 s) { |
|
164 |
FIX_STRING(s); |
|
10575
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
165 |
// filthy hack to write to stderr |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
166 |
if (!f->fp) |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
167 |
fprintf(stderr, "%s\n", s.str); |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
168 |
else |
13b1e9008f4b
super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents:
9966
diff
changeset
|
169 |
fprintf(f->fp, "%s\n", s.str); |
7983 | 170 |
} |
171 |
||
172 |
void __attribute__((overloadable)) fpcrtl_writeLn(FILE *f, string255 s) { |
|
173 |
FIX_STRING(s); |
|
174 |
fprintf(f, "%s\n", s.str); |
|
175 |
} |
|
176 |
||
177 |
void fpcrtl_blockRead__vars(File f, void *buf, Integer count, Integer *result) { |
|
178 |
assert(f->record_len > 0); |
|
179 |
*result = fread(buf, f->record_len, count, f->fp); |
|
180 |
} |
|
181 |
||
182 |
/* |
|
183 |
* XXX: dummy blockWrite |
|
184 |
*/ |
|
185 |
void fpcrtl_blockWrite__vars(File f, const void *buf, Integer count, |
|
186 |
Integer *result) { |
|
187 |
assert(0); |
|
188 |
} |
|
189 |
||
190 |
bool fpcrtl_directoryExists(string255 dir) { |
|
191 |
||
192 |
struct stat st; |
|
193 |
FIX_STRING(dir); |
|
194 |
||
195 |
IOResult = IO_NO_ERROR; |
|
196 |
||
197 |
#ifdef FPCRTL_DEBUG |
|
198 |
printf("Warning: directoryExists is called. This may not work when compiled to js.\n"); |
|
199 |
#endif |
|
200 |
||
201 |
if (stat(dir.str, &st) == 0) { |
|
202 |
return true; |
|
203 |
} |
|
204 |
||
205 |
return false; |
|
206 |
} |
|
207 |
||
208 |
bool fpcrtl_fileExists(string255 filename) { |
|
209 |
||
210 |
FIX_STRING(filename); |
|
211 |
||
212 |
IOResult = IO_NO_ERROR; |
|
213 |
||
214 |
FILE *fp = fopen(filename.str, "r"); |
|
215 |
if (fp) { |
|
216 |
fclose(fp); |
|
217 |
return true; |
|
218 |
} |
|
219 |
return false; |
|
220 |
} |
|
221 |
||
222 |
void __attribute__((overloadable)) fpcrtl_flush(Text f) { |
|
9966 | 223 |
fflush(f->fp); |
7983 | 224 |
} |
225 |
||
226 |
void __attribute__((overloadable)) fpcrtl_flush(FILE *f) { |
|
227 |
fflush(f); |
|
228 |
} |
|
229 |