orca/src/platform/platform_io_common.c

104 lines
2.4 KiB
C
Raw Normal View History

/************************************************************//**
*
* @file: platform_io_common.c
* @author: Martin Fouilleul
* @date: 25/05/2023
*
*****************************************************************/
#include"platform_io.h"
//------------------------------------------------------------------------------
// File stream read/write API
//------------------------------------------------------------------------------
file_handle file_open(str8 path, file_open_flags flags)
{
2023-05-26 09:40:00 +00:00
io_req req = {.op = IO_OP_OPEN_AT,
.size = path.len,
.buffer = path.ptr,
.openFlags = flags};
io_cmp cmp = io_wait_single_req(&req);
//WARN: we always return a handle that can be queried for errors. Handles must be closed
// even if there was an error when opening
2023-05-26 09:40:00 +00:00
return(cmp.handle);
}
void file_close(file_handle file)
{
io_req req = {.op = IO_OP_CLOSE,
.handle = file};
io_wait_single_req(&req);
}
2023-05-26 09:40:00 +00:00
i64 file_pos(file_handle file)
{
io_req req = {.op = IO_OP_POS,
.handle = file};
io_cmp cmp = io_wait_single_req(&req);
2023-05-26 09:40:00 +00:00
return(cmp.offset);
}
i64 file_seek(file_handle file, i64 offset, file_whence whence)
{
io_req req = {.op = IO_OP_SEEK,
.handle = file,
.offset = offset,
.whence = whence};
io_cmp cmp = io_wait_single_req(&req);
2023-05-26 09:40:00 +00:00
return(cmp.offset);
}
2023-05-26 09:40:00 +00:00
u64 file_write(file_handle file, u64 size, char* buffer)
{
io_req req = {.op = IO_OP_WRITE,
.handle = file,
.size = size,
.buffer = buffer};
io_cmp cmp = io_wait_single_req(&req);
2023-05-26 09:40:00 +00:00
return(cmp.size);
}
2023-05-26 09:40:00 +00:00
u64 file_read(file_handle file, u64 size, char* buffer)
{
io_req req = {.op = IO_OP_READ,
.handle = file,
.size = size,
.buffer = buffer};
io_cmp cmp = io_wait_single_req(&req);
2023-05-26 09:40:00 +00:00
return(cmp.size);
}
2023-05-26 09:40:00 +00:00
io_error file_last_error(file_handle file)
{
io_req req = {.op = IO_OP_ERROR,
.handle = file};
io_cmp cmp = io_wait_single_req(&req);
2023-05-26 09:40:00 +00:00
return((io_error)cmp.result);
}
file_status file_get_status(file_handle file)
{
file_status status = {0};
io_req req = {.op = IO_OP_FSTAT,
.handle = file,
.size = sizeof(file_status),
2023-05-26 09:40:00 +00:00
.buffer = (char*)&status};
io_cmp cmp = io_wait_single_req(&req);
return(status);
}
2023-05-26 09:40:00 +00:00
u64 file_size(file_handle file)
{
file_status status = file_get_status(file);
return(status.size);
}