[io, wip] read/write/seek/pos/size operations (todo: error on handle)

This commit is contained in:
Martin Fouilleul 2023-05-10 16:25:51 +02:00
parent 4ae51d7a23
commit 25f66b3954
3 changed files with 149 additions and 2 deletions

View File

@ -42,7 +42,18 @@ void OnInit(void)
canvas = mg_canvas_create(); canvas = mg_canvas_create();
file_handle file = file_open(STR8("test_file.txt") , IO_OPEN_CREATE | IO_OPEN_WRITE); 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_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) void OnFrameResize(u32 width, u32 height)

View File

@ -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; typedef u32 file_open_flags;
enum { enum {

View File

@ -7,6 +7,7 @@
*****************************************************************/ *****************************************************************/
#include<fcntl.h> #include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h> #include<unistd.h>
#include"io_common.h" #include"io_common.h"
@ -196,6 +197,116 @@ io_cmp io_close(io_req* req)
return(cmp); 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) 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; u32 memSize = 0;
char* memory = (char*)m3_GetMemory(__orcaApp.runtime.m3Runtime, &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) if(bufferIndex + req->size > memSize)
{ {
@ -225,6 +336,7 @@ io_cmp io_wait_single_req(io_req* req)
} }
else else
{ {
//TODO: avoid modifying req.
req->buffer = memory + bufferIndex; req->buffer = memory + bufferIndex;
switch(req->op) switch(req->op)
@ -237,6 +349,30 @@ io_cmp io_wait_single_req(io_req* req)
cmp = io_close(req); cmp = io_close(req);
break; 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: default:
cmp.error = IO_ERR_INVALID; cmp.error = IO_ERR_INVALID;
break; break;