Update file IO API
This commit is contained in:
@@ -8,7 +8,9 @@
|
||||
#include "../../base/strings/str8/str8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
File *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
||||
WFile *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
||||
wapp_debug_assert(filepath != NULL, "`filepath` should not be NULL");
|
||||
|
||||
wapp_persist const char *modes[FILE_ACCESS_MODE_COUNT] = {
|
||||
[WAPP_FA_MODE_R] = "r",
|
||||
[WAPP_FA_MODE_W] = "w",
|
||||
@@ -36,25 +38,25 @@ File *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
||||
return fopen((const char *)tmp, modes[mode]);
|
||||
}
|
||||
|
||||
u64 wapp_file_get_current_position(File *file) {
|
||||
i64 wapp_file_get_current_position(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return (u64)ftell(file);
|
||||
return ftell(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin) {
|
||||
i32 wapp_file_seek(WFile *file, u64 offset, FileSeekOrigin origin) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
// TODO (Abdelrahman): Revisit conversion to long
|
||||
return fseek(file, (long)offset, origin);
|
||||
}
|
||||
|
||||
u64 wapp_file_get_length(File *file) {
|
||||
i64 wapp_file_get_length(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
|
||||
u64 current = wapp_file_get_current_position(file);
|
||||
i64 current = wapp_file_get_current_position(file);
|
||||
|
||||
wapp_file_seek(file, 0, WAPP_SEEK_END);
|
||||
|
||||
u64 output = ftell(file);
|
||||
i64 output = wapp_file_get_current_position(file);
|
||||
|
||||
// Restore position
|
||||
wapp_file_seek(file, current, WAPP_SEEK_START);
|
||||
@@ -62,7 +64,27 @@ u64 wapp_file_get_length(File *file) {
|
||||
return output;
|
||||
}
|
||||
|
||||
u64 wapp_file_read(GenericArray dst_buf, File *file, u64 item_count) {
|
||||
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count) {
|
||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||
"`dst_buf` and `file` should not be NULL.");
|
||||
|
||||
u64 file_length = wapp_file_get_length(file);
|
||||
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
|
||||
|
||||
u64 count = fread(dst_buf, sizeof(u8), copy_byte_count, file);
|
||||
if (ferror(file)) { return 0; }
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
u64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
|
||||
wapp_debug_assert(src_buf != NULL && file != NULL,
|
||||
"`src_buf` and `file` should not be NULL.");
|
||||
|
||||
return fwrite(src_buf, sizeof(u8), byte_count, file);
|
||||
}
|
||||
|
||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
|
||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||
"`dst_buf` and `file` should not be NULL.");
|
||||
|
||||
@@ -78,15 +100,17 @@ u64 wapp_file_read(GenericArray dst_buf, File *file, u64 item_count) {
|
||||
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
|
||||
}
|
||||
|
||||
u64 count = fread(dst_buf, sizeof(u8), copy_byte_count, file);
|
||||
if (ferror(file)) { return 0; }
|
||||
u64 byte_count = wapp_file_read(dst_buf, file, copy_byte_count);
|
||||
if (byte_count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
wapp_array_set_count(dst_buf, count / item_size);
|
||||
wapp_array_set_count(dst_buf, byte_count / item_size);
|
||||
|
||||
return wapp_array_count(dst_buf);
|
||||
}
|
||||
|
||||
u64 wapp_file_write(const GenericArray src_buf, File *file, u64 item_count) {
|
||||
u64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count) {
|
||||
wapp_debug_assert(src_buf != NULL && file != NULL,
|
||||
"`src_buf` and `file` should not be NULL.");
|
||||
|
||||
@@ -95,15 +119,29 @@ u64 wapp_file_write(const GenericArray src_buf, File *file, u64 item_count) {
|
||||
u64 req_byte_count = item_count * item_size;
|
||||
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
|
||||
|
||||
return fwrite(src_buf, sizeof(u8), to_copy, file);
|
||||
u64 byte_count = wapp_file_write(src_buf, file, to_copy);
|
||||
|
||||
return byte_count / item_size;
|
||||
}
|
||||
|
||||
i32 wapp_file_flush(File *file) {
|
||||
i32 wapp_file_flush(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return fflush(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_close(File *file) {
|
||||
i32 wapp_file_close(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return fclose(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_remove(Str8RO *filepath) {
|
||||
wapp_debug_assert(filepath != NULL, "`filepath` should not be NULL");
|
||||
|
||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||
wapp_debug_assert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
|
||||
|
||||
memset(tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(tmp, filepath->buf, filepath->size);
|
||||
|
||||
return remove((const char *)tmp);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
typedef FILE File;
|
||||
typedef FILE WFile;
|
||||
|
||||
typedef enum {
|
||||
WAPP_FA_MODE_R, // Equivalent to r
|
||||
@@ -40,14 +40,17 @@ typedef enum {
|
||||
WAPP_SEEK_END = SEEK_END,
|
||||
} FileSeekOrigin;
|
||||
|
||||
File *wapp_file_open(Str8RO *filename, FileAccessMode mode);
|
||||
u64 wapp_file_get_current_position(File *file);
|
||||
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin);
|
||||
u64 wapp_file_get_length(File *file);
|
||||
u64 wapp_file_read(GenericArray dst_buf, File *file, u64 item_count);
|
||||
u64 wapp_file_write(const GenericArray src_buf, File *file, u64 item_count);
|
||||
i32 wapp_file_flush(File *file);
|
||||
i32 wapp_file_close(File *file);
|
||||
WFile *wapp_file_open(Str8RO *filename, FileAccessMode mode);
|
||||
i64 wapp_file_get_current_position(WFile *file);
|
||||
i32 wapp_file_seek(WFile *file, u64 offset, FileSeekOrigin origin);
|
||||
i64 wapp_file_get_length(WFile *file);
|
||||
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count);
|
||||
u64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
|
||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
|
||||
u64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
||||
i32 wapp_file_flush(WFile *file);
|
||||
i32 wapp_file_close(WFile *file);
|
||||
i32 wapp_file_remove(Str8RO *filepath);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
Reference in New Issue
Block a user