File: fileio.c

package info (click to toggle)
hedgewars 1.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 219,040 kB
  • sloc: pascal: 54,830; cpp: 27,224; ansic: 22,809; java: 8,210; haskell: 6,797; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 32
file content (275 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * XXX: assume all files are text files
 */

#include "misc.h"
#include "fileio.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <unistd.h>

io_result_t IOResult;
int FileMode;
char cwd[1024];

static void init(File f) {
    f->fp = NULL;
    f->eof = 0;
    f->mode = NULL;
    f->record_len = 0;
}

void fpcrtl_assign__vars(File *f, string255 name) {
    FIX_STRING(name);
    *f = (File) malloc(sizeof(file_wrapper_t));
    strcpy((*f)->file_name, name.str);
    init(*f);
}

void fpcrtl_reset1(File f) {
    f->fp = fopen(f->file_name, "r");
    if (!f->fp) {
        IOResult = IO_ERROR_DUMMY;
        printf("Failed to open %s\n", f->file_name);
        return;
    } else {
#ifdef FPCRTL_DEBUG
        printf("Opened %s\n", f->file_name);
#endif
    }
    IOResult = IO_NO_ERROR;
    f->mode = "r";
}

void fpcrtl_reset2(File f, int l) {
    f->eof = 0;
    f->fp = fopen(f->file_name, "rb");
    if (!f->fp) {
        IOResult = IO_ERROR_DUMMY;
        printf("Failed to open %s\n", f->file_name);
        return;
    }
    IOResult = IO_NO_ERROR;
    f->mode = "rb";
    f->record_len = l;
}

void __attribute__((overloadable)) fpcrtl_rewrite(File f) {
    f->fp = fopen(f->file_name, "w+");
    if (!f->fp) {
        IOResult = IO_ERROR_DUMMY;
        return;
    }
    IOResult = IO_NO_ERROR;
    f->mode = "w+";
}

void __attribute__((overloadable)) fpcrtl_rewrite(File f, Integer l) {
    IOResult = IO_NO_ERROR;
    fpcrtl_rewrite(f);
    if (f->fp) {
        f->record_len = l;
    }
}

void fpcrtl_close(File f) {
    IOResult = IO_NO_ERROR;
    fclose(f->fp);
    free(f);
}

boolean fpcrtl_eof(File f) {
    IOResult = IO_NO_ERROR;
    if (f->eof || f->fp == NULL || feof(f->fp)) {
        return true;
    } else {
        return false;
    }
}

void __attribute__((overloadable)) fpcrtl_readLn(File f) {
    IOResult = IO_NO_ERROR;
    char line[256];
    if (fgets(line, sizeof(line), f->fp) == NULL) {
        f->eof = 1;
    }
    if (feof(f->fp)) {
        f->eof = 1;
    }
}

void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, Integer *i) {
    string255 s;

    if (feof(f->fp)) {
        f->eof = 1;
        return;
    }

    fpcrtl_readLn__vars(f, &s);

    *i = atoi(s.str);
}

void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, LongWord *i) {
    string255 s;

    if (feof(f->fp)) {
        f->eof = 1;
        return;
    }

    fpcrtl_readLn__vars(f, &s);

    *i = atoi(s.str);
}

void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, string255 *s) {

    if (fgets(s->str, 255, f->fp) == NULL) {

        s->len = 0;
        s->str[0] = 0;

        f->eof = 1;
        return;
    }

    if (feof(f->fp)) {
        s->len = 0;
        f->eof = 1;
        return;
    }

    IOResult = IO_NO_ERROR;

    s->len = strlen(s->str);
    if ((s->len > 0) && (s->str[s->len - 1] == '\n')) {
        s->str[s->len - 1] = 0;
        s->len--;
    }
}

void __attribute__((overloadable)) fpcrtl_write(File f, string255 s) {
    FIX_STRING(s);
    fprintf(f->fp, "%s", s.str);
}

void __attribute__((overloadable)) fpcrtl_write(FILE *f, string255 s) {
    FIX_STRING(s);
    fprintf(f, "%s", s.str);
}

void __attribute__((overloadable)) fpcrtl_writeLn(File f, string255 s) {
    FIX_STRING(s);
    // filthy hack to write to stderr
    if (!f->fp)
        fprintf(stderr, "%s\n", s.str);
    else
        fprintf(f->fp, "%s\n", s.str);
}

void __attribute__((overloadable)) fpcrtl_writeLn(FILE *f, string255 s) {
    FIX_STRING(s);
    fprintf(f, "%s\n", s.str);
}

void fpcrtl_blockRead__vars(File f, void *buf, Integer count, Integer *result) {
    assert(f->record_len > 0);
    *result = fread(buf, f->record_len, count, f->fp);
}

/*
 * XXX: dummy blockWrite
 */
void fpcrtl_blockWrite__vars(File f, const void *buf, Integer count,
        Integer *result) {
    assert(f->record_len > 0);
    *result = fwrite(buf, f->record_len, count, f->fp);
}

bool fpcrtl_directoryExists(string255 dir) {

    struct stat st;
    FIX_STRING(dir);

    IOResult = IO_NO_ERROR;

#ifdef FPCRTL_DEBUG
    printf("Warning: directoryExists is called. This may not work when compiled to js.\n");
#endif

    if (stat(dir.str, &st) == 0) {
        return true;
    }

    return false;
}

bool fpcrtl_fileExists(string255 filename) {

    FIX_STRING(filename);

    IOResult = IO_NO_ERROR;

    FILE *fp = fopen(filename.str, "r");
    if (fp) {
        fclose(fp);
        return true;
    }
    return false;
}

char * fpcrtl_getCurrentDir(void) {

    IOResult = IO_NO_ERROR;

    if (getcwd(cwd, sizeof(cwd)) != NULL)
        return cwd;

    IOResult = IO_ERROR_DUMMY;
    return "";
}

void __attribute__((overloadable)) fpcrtl_flush(Text f) {
    fflush(f->fp);
}

void __attribute__((overloadable)) fpcrtl_flush(FILE *f) {
    fflush(f);
}

Int64 fpcrtl_fileSize(File f)
{
    assert(f->record_len > 0);

    IOResult = IO_NO_ERROR;
    int i = fseek(f->fp, 0, SEEK_END);
    if (i == -1) {
        IOResult = IO_ERROR_DUMMY;
        return -1;
    }
    long size = ftell(f->fp);
    if (size == -1) {
        IOResult = IO_ERROR_DUMMY;
        return -1;
    }
    return size / f->record_len;
}

bool fpcrtl_deleteFile(string255 filename)
{
    FIX_STRING(filename);

    int ret = remove(filename.str);
    if(ret == 0) {
       IOResult = IO_NO_ERROR;
       return true;
    } else {
       IOResult = IO_ERROR_DUMMY;
       return false;
    }
}