forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile.cc
486 lines (419 loc) · 13 KB
/
file.cc
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// ****************************************************************************
// file.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Abstract interface for the zany DMCP filesystem
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2023 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "file.h"
#include "ff_ifc.h"
#include "recorder.h"
#include "text.h"
#include "utf8.h"
#include <unistd.h>
RECORDER(file, 16, "File operations");
RECORDER(file_error, 16, "File errors");
// The one and only open file in DMCP...
file *file::current = nullptr;
// ============================================================================
//
// DMCP wrappers
//
// ============================================================================
#ifndef SIMULATOR
static inline int fgetc(FIL &f)
// ----------------------------------------------------------------------------
// Read one character from a file - Wrapper for DMCP filesystem
// ----------------------------------------------------------------------------
{
UINT br = 0;
char c = 0;
if (f_read(&f, &c, 1, &br) != FR_OK || br != 1)
return EOF;
return c;
}
static inline int fputc(int c, FIL &f)
// ----------------------------------------------------------------------------
// Read one character from a file - Wrapper for DMCP filesystem
// ----------------------------------------------------------------------------
{
UINT bw = 0;
if (f_write(&f, &c, 1, &bw) != FR_OK || bw != 1)
return EOF;
return c;
}
#endif // SIMULATOR
file::file()
// ----------------------------------------------------------------------------
// Construct a file object
// ----------------------------------------------------------------------------
: data(), name(), closed(), previous(nullptr)
{}
file::file(cstring path, mode wrmode)
// ----------------------------------------------------------------------------
// Construct a file object for writing
// ----------------------------------------------------------------------------
: file()
{
open(path, wrmode);
}
file::file(text_p name, mode wrmode)
// ----------------------------------------------------------------------------
// Open a file from a text value
// ----------------------------------------------------------------------------
: file()
{
if (name)
{
static char buf[80];
size_t len = 0;
utf8 path = name->value(&len);
if (len < sizeof(buf))
{
memcpy(buf, path, len);
buf[len] = 0;
open(buf, wrmode);
}
else
{
rt.file_name_too_long_error();
}
}
}
file::~file()
// ----------------------------------------------------------------------------
// Close the help file
// ----------------------------------------------------------------------------
{
close();
}
void file::open(cstring path, mode wrmode)
// ----------------------------------------------------------------------------
// Open a file for reading
// ----------------------------------------------------------------------------
{
bool reading = wrmode == READING;
bool append = wrmode == APPEND;
writing = append || wrmode == WRITING;
previous = current;
if (previous)
previous->close(false);
current = this;
name = path;
#if SIMULATOR
data = fopen(path, reading ? "r" : append ? "a" : "w");
if (!data)
{
record(file_error, "Error %s opening %s", strerror(errno), path);
current = nullptr;
}
#else // !SIMULATOR
if (writing)
sys_disk_write_enable(1);
BYTE mode = (reading ? FA_READ
: append ? (FA_WRITE | FA_OPEN_APPEND)
: (FA_WRITE | FA_CREATE_ALWAYS));
FRESULT ok = f_open(&data, path, mode);
data.err = ok;
if (ok != FR_OK)
{
data.flag = 0;
sys_disk_write_enable(0);
current = nullptr;
}
#endif // SIMULATOR
}
void file::reopen()
// ----------------------------------------------------------------------------
// Reopen file from saved data
// ----------------------------------------------------------------------------
{
open(name, writing ? APPEND : READING);
if (valid() && !writing)
seek(closed);
}
void file::close(bool reopen)
// ----------------------------------------------------------------------------
// Close the help file
// ----------------------------------------------------------------------------
{
if (valid())
{
closed = ftell(data);
fclose(data);
#if SIMULATOR
data = nullptr;
#else
sys_disk_write_enable(0);
data.flag = 0;
#endif // SIMULATOR
}
current = nullptr;
if (previous && reopen)
{
previous->reopen();
previous = nullptr;
}
}
bool file::put(unicode cp)
// ----------------------------------------------------------------------------
// Emit a unicode character in the file
// ----------------------------------------------------------------------------
{
byte buffer[4];
size_t count = utf8_encode(cp, buffer);
#if SIMULATOR
return fwrite(buffer, 1, count, data) == count;
#else
UINT bw = 0;
return f_write(&data, buffer, count, &bw) == FR_OK && bw == count;
#endif
}
bool file::put(char c)
// ----------------------------------------------------------------------------
// Emit a single character in the file
// ----------------------------------------------------------------------------
{
#if SIMULATOR
return fwrite(&c, 1, 1, data) == 1;
#else
UINT bw = 0;
return f_write(&data, &c, 1, &bw) == FR_OK && bw == 1;
#endif
}
bool file::write(const char *buf, size_t len)
// ----------------------------------------------------------------------------
// Emit a buffer to a file
// ----------------------------------------------------------------------------
{
#if SIMULATOR
return fwrite(buf, 1, len, data) == len;
#else
UINT bw = 0;
return f_write(&data, buf, len, &bw) == FR_OK && bw == len;
#endif
}
bool file::read(char *buf, size_t len)
// ----------------------------------------------------------------------------
// Read data from a file
// ----------------------------------------------------------------------------
{
#if SIMULATOR
return fread(buf, 1, len, data) == len;
#else
UINT bw = 0;
return f_read(&data, buf, len, &bw) == FR_OK && bw == len;
#endif
}
char file::getchar()
// ----------------------------------------------------------------------------
// Read char code at offset
// ----------------------------------------------------------------------------
{
int c = valid() ? fgetc(data) : 0;
if (c == EOF)
c = 0;
return c;
}
unicode file::get()
// ----------------------------------------------------------------------------
// Read UTF8 code at offset
// ----------------------------------------------------------------------------
{
unicode code = valid() ? fgetc(data) : unicode(EOF);
if (code == unicode(EOF))
return 0;
if (code & 0x80)
{
// Reference: Wikipedia UTF-8 description
if ((code & 0xE0) == 0xC0)
code = ((code & 0x1F) << 6)
| (fgetc(data) & 0x3F);
else if ((code & 0xF0) == 0xE0)
code = ((code & 0xF) << 12)
| ((fgetc(data) & 0x3F) << 6)
| (fgetc(data) & 0x3F);
else if ((code & 0xF8) == 0xF0)
code = ((code & 0xF) << 18)
| ((fgetc(data) & 0x3F) << 12)
| ((fgetc(data) & 0x3F) << 6)
| (fgetc(data) & 0x3F);
}
return code;
}
uint file::find(unicode cp)
// ----------------------------------------------------------------------------
// Find a given code point in file looking forward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
unicode c;
uint off;
do
{
off = ftell(data);
c = get();
} while (c && c != cp);
return off;
}
uint file::find(unicode cp1, unicode cp2)
// ----------------------------------------------------------------------------
// Find a given code point in file looking forward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
unicode c;
uint off;
bool in = false;
do
{
off = ftell(data);
c = get();
} while (c && c != cp1 && (c != cp2 || (in = !in)));
return off;
}
uint file::rfind(unicode cp)
// ----------------------------------------------------------------------------
// Find a given code point in file looking backward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
uint off = ftell(data);
unicode c;
do
{
if (off == 0)
break;
fseek(data, --off, SEEK_SET);
c = get();
}
while (c != cp);
return off;
}
uint file::rfind(unicode cp1, unicode cp2)
// ----------------------------------------------------------------------------
// Find a given code point in file looking backward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
uint off = ftell(data);
unicode c;
bool in = false;
do
{
if (off == 0)
break;
fseek(data, --off, SEEK_SET);
c = get();
}
while (c != cp1 && (c != cp2 || (in = !in)));
return off;
}
cstring file::error(int err) const
// ----------------------------------------------------------------------------
// Return error from error code
// ----------------------------------------------------------------------------
{
#ifdef SIMULATOR
return strerror(err);
#else
switch (err)
{
case FR_OK:
return nullptr;
#define ERROR(name, msg)
#define FRROR(name, msg, sys) case FR_##sys: return msg; break;
#include "errors.tbl"
default: break;
}
return "Unkown error";
#endif // SIMULATOR
}
cstring file::error() const
// ----------------------------------------------------------------------------
// Return error from errno or data.err
// ----------------------------------------------------------------------------
{
#ifdef SIMULATOR
return error(errno);
#else
return error(data.err);
#endif
}
bool file::unlink(text_p name)
// ----------------------------------------------------------------------------
// Purge (unlink) a file
// ----------------------------------------------------------------------------
{
char buf[80];
size_t len = 0;
utf8 path = name->value(&len);
if (len < sizeof(buf))
{
memcpy(buf, path, len);
buf[len] = 0;
return unlink(buf);
}
rt.file_name_too_long_error();
return false;
}
bool file::unlink(cstring file)
// ----------------------------------------------------------------------------
// Purge (unlink) a file
// ----------------------------------------------------------------------------
{
#ifdef SIMULATOR
return ::unlink(file) == 0;
#else // !SIMULATOR
return f_unlink(file) == FR_OK;
#endif // SIMULATOR
}
cstring file::extension(cstring path)
// ----------------------------------------------------------------------------
// Extract the extension of the given path
// ----------------------------------------------------------------------------
{
cstring ext = nullptr;
if (path)
{
for (cstring p = path; *p; p++)
{
if (*p == '\\' || *p == '/')
ext = nullptr;
else if (*p == '.')
ext = p;
}
}
return ext;
}
cstring file::basename(cstring path)
// ----------------------------------------------------------------------------
// Extract the basename for the given path
// ----------------------------------------------------------------------------
{
for (cstring p = path; *p; p++)
if (*p == '/' || *p == '\\')
path = p + 1;
return path;
}