From 872faecf9660cc2469aa5586f81639f7986b6b8f Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 21 May 2021 17:41:06 +0200 Subject: Add fs_read and fs_write functions and some testing --- src/filesystem.c | 15 +++++++++++++++ src/main.c | 20 +++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'src') 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) { return n_bytes; } + +int +fs_read(u8 *dst, size_t n_bytes, u16 file_index, u16 offset) { + File *file = &filesystem.files[file_index]; + + // Check if the offset is within limits. + if (offset + n_bytes >= FILE_MAX_SIZE) { + return -1; + } + + // Copy n_bytes to destination. + _fs_read(dst, FILE_DATA_OFFSET + FILE_MAX_SIZE * file_index + offset, n_bytes); + + return n_bytes; +} 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) { // DEBUG:... int file_idx; + char tmp_buffer[KB(1)] = {0}; if ((file_idx = fs_open_file("HELLO_FILE")) >= 0) { fs_write("HEY", 3, file_idx, 0, false); fs_write("THERE", 3, file_idx, 1, true); - txt_printf("nam: %s\n", filesystem.files[file_idx].name); - txt_printf("num: %d\n", filesystem.num_files); - txt_printf("size: %d\n", filesystem.files[file_idx].size); + txt_printf("file name: %s\n", filesystem.files[file_idx].name); + txt_printf("file size: %d\n", filesystem.files[file_idx].size); + if (fs_read(&tmp_buffer, filesystem.files[file_idx].size, file_idx, 0) > 0) { + txt_printf("READ: %s\n", tmp_buffer); + } } if ((file_idx = fs_open_file("HELLO_FOLKS")) >= 0) { - fs_write("BY GOD", 6, file_idx, 0, false); + fs_write("AAAAAA", 6, file_idx, 0, false); fs_write("XXX", 3, file_idx, 1, true); - txt_printf("nam: %s\n", filesystem.files[file_idx].name); - txt_printf("num: %d\n", filesystem.num_files); - txt_printf("size: %d\n", filesystem.files[file_idx].size); + txt_printf("file name: %s\n", filesystem.files[file_idx].name); + txt_printf("file size: %d\n", filesystem.files[file_idx].size); + if (fs_read(&tmp_buffer, filesystem.files[file_idx].size - 1, file_idx, 1) > 0) { + txt_printf("READ: %s\n", tmp_buffer); + } } + txt_printf("num files: %d\n", filesystem.num_files); // Main loop. int frame_counter = 0; -- cgit v1.2.1