orca/src/platform/platform_io_common.c

123 lines
2.9 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
//------------------------------------------------------------------------------
2023-06-16 09:57:53 +00:00
file_handle file_handle_nil()
{
return((file_handle){0});
}
bool file_handle_is_nil(file_handle handle)
{
return(handle.h == 0);
}
file_handle file_open(str8 path, file_access_rights rights, 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,
.open.rights = rights,
.open.flags = 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);
}
file_handle file_open_at(file_handle dir, str8 path, file_access_rights rights, file_open_flags flags)
{
io_req req = {.op = IO_OP_OPEN_AT,
.handle = dir,
.size = path.len,
.buffer = path.ptr,
.open.rights = rights,
.open.flags = flags,};
io_cmp cmp = io_wait_single_req(&req);
return(cmp.handle);
}
void file_close(file_handle file)
{
io_req req = {.op = IO_OP_CLOSE,
.handle = file};
io_wait_single_req(&req);
}
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);
}
i64 file_pos(file_handle file)
{
return(file_seek(file, 0, FILE_SEEK_CURRENT));
}
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);
}