From 25f66b395428ca99a8d7bb1d6f0c3a8eff85b88d Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Wed, 10 May 2023 16:25:51 +0200 Subject: [PATCH] [io, wip] read/write/seek/pos/size operations (todo: error on handle) --- samples/pong/src/main.c | 11 ++++ src/io_common.h | 2 +- src/io_impl.c | 138 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/samples/pong/src/main.c b/samples/pong/src/main.c index ae82918..0463c46 100644 --- a/samples/pong/src/main.c +++ b/samples/pong/src/main.c @@ -42,7 +42,18 @@ void OnInit(void) canvas = mg_canvas_create(); file_handle file = file_open(STR8("test_file.txt") , IO_OPEN_CREATE | IO_OPEN_WRITE); + + str8 string = STR8("Hello, file!\n"); + file_write(file, string.len, string.ptr); file_close(file); + + file = file_open(STR8("test_file.txt") , IO_OPEN_READ); + u64 size = file_size(file); + char* buffer = mem_arena_alloc(mem_scratch(), size); + file_read(file, size, buffer); + file_close(file); + + log_info("read file: %.*s", (int)size, buffer); } void OnFrameResize(u32 width, u32 height) diff --git a/src/io_common.h b/src/io_common.h index e7c52c8..a5fbac4 100644 --- a/src/io_common.h +++ b/src/io_common.h @@ -32,7 +32,7 @@ enum //... }; -typedef enum { FILE_SET, FILE_END, FILE_CURR } file_whence; +typedef enum { IO_SEEK_SET, IO_SEEK_END, IO_SEEK_CURRENT } file_whence; typedef u32 file_open_flags; enum { diff --git a/src/io_impl.c b/src/io_impl.c index 27dbe29..695d359 100644 --- a/src/io_impl.c +++ b/src/io_impl.c @@ -7,6 +7,7 @@ *****************************************************************/ #include +#include #include #include"io_common.h" @@ -196,6 +197,116 @@ io_cmp io_close(io_req* req) return(cmp); } +io_cmp io_size(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + struct stat s; + fstat(slot->fd, &s); + cmp.result = s.st_size; + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + +io_cmp io_pos(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + cmp.result = lseek(slot->fd, 0, SEEK_CUR); + //TODO: check for errors + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + +io_cmp io_seek(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + int whence; + switch(req->whence) + { + case IO_SEEK_CURRENT: + whence = SEEK_CUR; + break; + + case IO_SEEK_SET: + whence = SEEK_SET; + break; + + case IO_SEEK_END: + whence = SEEK_END; + } + cmp.result = lseek(slot->fd, req->size, whence); + //TODO: check for errors + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + +io_cmp io_read(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + cmp.result = read(slot->fd, req->buffer, req->size); + //TODO: check for errors + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + +io_cmp io_write(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + cmp.result = write(slot->fd, req->buffer, req->size); + //TODO: check for errors + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + +io_cmp io_get_error(io_req* req) +{ + io_cmp cmp = {0}; + file_slot* slot = file_slot_from_handle(&__globalFileTable, req->handle); + if(slot) + { + //TODO get error from slot + } + else + { + cmp.error = IO_ERR_HANDLE; + } + return(cmp); +} + io_cmp io_wait_single_req(io_req* req) { @@ -217,7 +328,7 @@ io_cmp io_wait_single_req(io_req* req) u32 memSize = 0; char* memory = (char*)m3_GetMemory(__orcaApp.runtime.m3Runtime, &memSize, 0); - u64 bufferIndex = (u64)req->buffer & 0xffffff; + u64 bufferIndex = (u64)req->buffer & 0xffffffff; if(bufferIndex + req->size > memSize) { @@ -225,6 +336,7 @@ io_cmp io_wait_single_req(io_req* req) } else { + //TODO: avoid modifying req. req->buffer = memory + bufferIndex; switch(req->op) @@ -237,6 +349,30 @@ io_cmp io_wait_single_req(io_req* req) cmp = io_close(req); break; + case IO_OP_SIZE: + cmp = io_size(req); + break; + + case IO_OP_READ: + cmp = io_read(req); + break; + + case IO_OP_WRITE: + cmp = io_write(req); + break; + + case IO_OP_POS: + cmp = io_pos(req); + break; + + case IO_OP_SEEK: + cmp = io_seek(req); + break; + + case IO_OP_ERROR: + cmp = io_get_error(req); + break; + default: cmp.error = IO_ERR_INVALID; break;