aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-21 17:41:06 +0200
committerBad Diode <bd@badd10de.dev>2021-05-21 17:41:06 +0200
commit872faecf9660cc2469aa5586f81639f7986b6b8f (patch)
tree76663221fb6f0480a31587abc39c5966fb6adee7 /src
parentaf98fa608fb4ba98138e2b1239f173e6d74d6d86 (diff)
downloaduxngba-872faecf9660cc2469aa5586f81639f7986b6b8f.tar.gz
uxngba-872faecf9660cc2469aa5586f81639f7986b6b8f.zip
Add fs_read and fs_write functions and some testing
Diffstat (limited to 'src')
-rw-r--r--src/filesystem.c15
-rw-r--r--src/main.c20
2 files changed, 28 insertions, 7 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index 2da77be..1521d69 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -116,3 +116,18 @@ fs_write(u8 *src, size_t n_bytes, u16 file_index, u16 offset, bool append) {
116 116
117 return n_bytes; 117 return n_bytes;
118} 118}
119
120int
121fs_read(u8 *dst, size_t n_bytes, u16 file_index, u16 offset) {
122 File *file = &filesystem.files[file_index];
123
124 // Check if the offset is within limits.
125 if (offset + n_bytes >= FILE_MAX_SIZE) {
126 return -1;
127 }
128
129 // Copy n_bytes to destination.
130 _fs_read(dst, FILE_DATA_OFFSET + FILE_MAX_SIZE * file_index + offset, n_bytes);
131
132 return n_bytes;
133}
diff --git a/src/main.c b/src/main.c
index 798dc18..79bf788 100644
--- a/src/main.c
+++ b/src/main.c
@@ -271,20 +271,26 @@ int main(void) {
271 271
272 // DEBUG:... 272 // DEBUG:...
273 int file_idx; 273 int file_idx;
274 char tmp_buffer[KB(1)] = {0};
274 if ((file_idx = fs_open_file("HELLO_FILE")) >= 0) { 275 if ((file_idx = fs_open_file("HELLO_FILE")) >= 0) {
275 fs_write("HEY", 3, file_idx, 0, false); 276 fs_write("HEY", 3, file_idx, 0, false);
276 fs_write("THERE", 3, file_idx, 1, true); 277 fs_write("THERE", 3, file_idx, 1, true);
277 txt_printf("nam: %s\n", filesystem.files[file_idx].name); 278 txt_printf("file name: %s\n", filesystem.files[file_idx].name);
278 txt_printf("num: %d\n", filesystem.num_files); 279 txt_printf("file size: %d\n", filesystem.files[file_idx].size);
279 txt_printf("size: %d\n", filesystem.files[file_idx].size); 280 if (fs_read(&tmp_buffer, filesystem.files[file_idx].size, file_idx, 0) > 0) {
281 txt_printf("READ: %s\n", tmp_buffer);
282 }
280 } 283 }
281 if ((file_idx = fs_open_file("HELLO_FOLKS")) >= 0) { 284 if ((file_idx = fs_open_file("HELLO_FOLKS")) >= 0) {
282 fs_write("BY GOD", 6, file_idx, 0, false); 285 fs_write("AAAAAA", 6, file_idx, 0, false);
283 fs_write("XXX", 3, file_idx, 1, true); 286 fs_write("XXX", 3, file_idx, 1, true);
284 txt_printf("nam: %s\n", filesystem.files[file_idx].name); 287 txt_printf("file name: %s\n", filesystem.files[file_idx].name);
285 txt_printf("num: %d\n", filesystem.num_files); 288 txt_printf("file size: %d\n", filesystem.files[file_idx].size);
286 txt_printf("size: %d\n", filesystem.files[file_idx].size); 289 if (fs_read(&tmp_buffer, filesystem.files[file_idx].size - 1, file_idx, 1) > 0) {
290 txt_printf("READ: %s\n", tmp_buffer);
291 }
287 } 292 }
293 txt_printf("num files: %d\n", filesystem.num_files);
288 294
289 // Main loop. 295 // Main loop.
290 int frame_counter = 0; 296 int frame_counter = 0;