2023-05-25 18:08:38 +00:00
|
|
|
/************************************************************//**
|
|
|
|
*
|
|
|
|
* @file: platform_io_common.c
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 25/05/2023
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
#include"platform_io.h"
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// File stream read/write API
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2023-06-13 13:56:19 +00:00
|
|
|
file_handle file_open(str8 path, file_access_rights rights, file_open_flags flags)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
2023-05-26 09:40:00 +00:00
|
|
|
io_req req = {.op = IO_OP_OPEN_AT,
|
2023-05-25 18:08:38 +00:00
|
|
|
.size = path.len,
|
|
|
|
.buffer = path.ptr,
|
2023-06-13 13:56:19 +00:00
|
|
|
.open.rights = rights,
|
|
|
|
.open.flags = flags };
|
2023-05-25 18:08:38 +00:00
|
|
|
|
|
|
|
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);
|
2023-05-25 18:08:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 13:56:19 +00:00
|
|
|
file_handle file_open_at(file_handle dir, str8 path, file_access_rights rights, file_open_flags flags)
|
2023-06-12 16:04:59 +00:00
|
|
|
{
|
|
|
|
io_req req = {.op = IO_OP_OPEN_AT,
|
|
|
|
.handle = dir,
|
|
|
|
.size = path.len,
|
|
|
|
.buffer = path.ptr,
|
2023-06-13 13:56:19 +00:00
|
|
|
.open.rights = rights,
|
|
|
|
.open.flags = flags,};
|
2023-06-12 16:04:59 +00:00
|
|
|
|
|
|
|
io_cmp cmp = io_wait_single_req(&req);
|
|
|
|
return(cmp.handle);
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:08:38 +00:00
|
|
|
void file_close(file_handle file)
|
|
|
|
{
|
|
|
|
io_req req = {.op = IO_OP_CLOSE,
|
|
|
|
.handle = file};
|
|
|
|
io_wait_single_req(&req);
|
|
|
|
}
|
|
|
|
|
2023-05-26 14:03:23 +00:00
|
|
|
i64 file_seek(file_handle file, i64 offset, file_whence whence)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
|
|
|
io_req req = {.op = IO_OP_SEEK,
|
|
|
|
.handle = file,
|
2023-05-26 14:03:23 +00:00
|
|
|
.offset = offset,
|
2023-05-25 18:08:38 +00:00
|
|
|
.whence = whence};
|
|
|
|
|
|
|
|
io_cmp cmp = io_wait_single_req(&req);
|
2023-05-26 09:40:00 +00:00
|
|
|
return(cmp.offset);
|
2023-05-25 18:08:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 09:38:17 +00:00
|
|
|
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)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
|
|
|
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-25 18:08:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 09:40:00 +00:00
|
|
|
u64 file_read(file_handle file, u64 size, char* buffer)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
|
|
|
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-25 18:08:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 09:40:00 +00:00
|
|
|
io_error file_last_error(file_handle file)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
|
|
|
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);
|
2023-05-25 18:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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};
|
2023-05-25 18:08:38 +00:00
|
|
|
|
|
|
|
io_cmp cmp = io_wait_single_req(&req);
|
|
|
|
return(status);
|
|
|
|
}
|
|
|
|
|
2023-05-26 09:40:00 +00:00
|
|
|
u64 file_size(file_handle file)
|
2023-05-25 18:08:38 +00:00
|
|
|
{
|
|
|
|
file_status status = file_get_status(file);
|
|
|
|
return(status.size);
|
|
|
|
}
|