aboutsummaryrefslogtreecommitdiffstats
path: root/src/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/file.c')
-rw-r--r--src/file.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/file.c b/src/file.c
new file mode 100644
index 0000000..a45d59f
--- /dev/null
+++ b/src/file.c
@@ -0,0 +1,83 @@
1/*
2Copyright (c) 2021 Devine Lu Linvega
3Copyright (c) 2021 Andrew Alderwick
4Copyright (c) 2022 Bad Diode
5
6Permission to use, copy, modify, and distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE.
12*/
13
14#include "posprintf.h"
15
16static File f = {0};
17static char *current_filename = "";
18
19static enum {
20 IDLE,
21 FILE_READ,
22 FILE_WRITE,
23} file_state;
24
25void
26file_reset(void) {
27 f = (File){0};
28 file_state = IDLE;
29}
30
31u16
32file_init(void *filename) {
33 file_reset();
34 current_filename = filename;
35 return 0;
36}
37
38u16
39file_read(void *dest, u16 len) {
40 if(file_state != FILE_READ) {
41 file_reset();
42 f = fs_open_file(current_filename, FS_OPEN_READ);
43 file_state = FILE_READ;
44 }
45 if (f.index == FS_NULL) {
46 return 0;
47 }
48 return fs_read(dest, len, &f);
49}
50
51u16
52file_write(void *src, u16 len, u8 flags) {
53 if(file_state != FILE_WRITE) {
54 file_reset();
55 OpenMode mode = (flags & 0x01) ? FS_OPEN_APPEND : FS_OPEN_WRITE;
56 f = fs_open_file(current_filename, mode);
57 file_state = FILE_WRITE;
58 }
59 if (f.index == FS_NULL) {
60 return 0;
61 }
62 return fs_write(src, len, &f);
63}
64
65u16
66file_stat(void *dest, u16 len) {
67 if(len < strlen(current_filename) + 7) {
68 return 0;
69 }
70 if (f.index == FS_NULL) {
71 posprintf(dest, "!!!! %s\n", current_filename);
72 } else {
73 u16 file_size = fs_file_size(&f);
74 posprintf(dest, "%04x %s\n", (unsigned int)file_size, current_filename);
75 }
76 return 0;
77}
78
79// static u16
80// file_delete(void)
81// {
82// return unlink(current_filename);
83// }