Rename file

This commit is contained in:
2026-06-26 17:29:30 +01:00
parent c29466b863
commit c832d5366e
10 changed files with 277 additions and 277 deletions
+15 -15
View File
@@ -16,11 +16,11 @@ wp_intern WpStr8RO R_BRACKET_SPACE = wpStr8LitRo("] ");
wp_intern WpStr8RO R_BRACKET_NEWLINE = wpStr8LitRo("]\n");
typedef struct {
WFile *outlog;
WFile *errlog;
WpFile *outlog;
WpFile *errlog;
WpLogLevel level;
wpMiscUtilsReservePadding(2 * sizeof(WFile *) + sizeof(WpLogLevel));
wpMiscUtilsReservePadding(2 * sizeof(WpFile *) + sizeof(WpLogLevel));
} LogConfig;
wp_intern LogConfig LOG_CONFIG = {
@@ -36,13 +36,13 @@ wp_intern WpStr8RO LOG_LEVEL_STRINGS[COUNT_LOG_LEVEL] = {
};
wp_intern void _get_current_time_string(WpStr8 *dst);
wp_intern void _write_log_line(WFile *fp, const WpLogger *logger, WpStr8 msg, WpLogLevel level);
wp_intern void _write_log_line(WpFile *fp, const WpLogger *logger, WpStr8 msg, WpLogLevel level);
void wpLogSetLevel(WpLogLevel level) {
LOG_CONFIG.level = level;
}
void wpLogConfigure(WFile *outlog, WFile *errlog, WpLogLevel level) {
void wpLogConfigure(WpFile *outlog, WpFile *errlog, WpLogLevel level) {
LOG_CONFIG.outlog = outlog;
LOG_CONFIG.errlog = errlog;
LOG_CONFIG.level = level;
@@ -56,7 +56,7 @@ void wpLogDebug(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_DEBUG) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
WpFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wpFileStdout();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_DEBUG);
}
@@ -64,7 +64,7 @@ void wpLogInfo(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_INFO) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
WpFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wpFileStdout();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_INFO);
}
@@ -72,7 +72,7 @@ void wpLogWarning(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_WARNING) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
WpFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wpFileStdout();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_WARNING);
}
@@ -80,7 +80,7 @@ void wpLogError(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_ERROR) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
WpFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wpFileStderr();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_ERROR);
}
@@ -88,7 +88,7 @@ void wpLogCritical(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_CRITICAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
WpFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wpFileStderr();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_CRITICAL);
}
@@ -96,7 +96,7 @@ void wpLogFatal(const WpLogger *logger, WpStr8 msg) {
wpDebugAssert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WP_LOG_LEVEL_FATAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
WpFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wpFileStderr();
_write_log_line(fp, logger, msg, WP_LOG_LEVEL_FATAL);
}
@@ -110,7 +110,7 @@ wp_intern void _get_current_time_string(WpStr8 *dst) {
wpStr8CopyCstrCapped(dst, buf);
}
wp_intern void _write_log_line(WFile *fp, const WpLogger *logger, WpStr8 msg, WpLogLevel level) {
wp_intern void _write_log_line(WpFile *fp, const WpLogger *logger, WpStr8 msg, WpLogLevel level) {
WpStr8 padding = wpStr8Buf(MIN_LOG_MSG_LENGTH);
u32 padding_size = msg.size < MIN_LOG_MSG_LENGTH ? MIN_LOG_MSG_LENGTH - msg.size + 1 : 0;
wpStr8Format(&padding, "%-*s", padding_size, " ");
@@ -118,7 +118,7 @@ wp_intern void _write_log_line(WFile *fp, const WpLogger *logger, WpStr8 msg, Wp
WpStr8 time_str = wpStr8Buf(TIME_BUF_CAPACITY);
_get_current_time_string(&time_str);
WpStr8RO **strings = wapp_array(
WpStr8RO **strings = wpArray(
WpStr8RO *,
&time_str,
&L_BRACKET,
@@ -131,7 +131,7 @@ wp_intern void _write_log_line(WFile *fp, const WpLogger *logger, WpStr8 msg, Wp
&R_BRACKET_NEWLINE
);
for (u64 i = 0; i < wapp_array_count(strings); ++i) {
wapp_file_write_str8(strings[i], fp);
for (u64 i = 0; i < wpArrayCount(strings); ++i) {
wpFileWriteStr8(strings[i], fp);
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ typedef struct {
} WpLogger;
void wpLogSetLevel(WpLogLevel level);
void wpLogConfigure(WFile *outlog, WFile *errlog, WpLogLevel level);
void wpLogConfigure(WpFile *outlog, WpFile *errlog, WpLogLevel level);
WpLogger wpLogMakeLogger(WpStr8 name);
void wpLogDebug(const WpLogger *logger, WpStr8 msg);
void wpLogInfo(const WpLogger *logger, WpStr8 msg);
+43 -43
View File
@@ -7,77 +7,77 @@
#include "../../base/array/array.h"
#include "../../base/strings/str8/str8.h"
WFile *wapp_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMode mode) {
WpFile *wpFileOpen(const WpAllocator *allocator, WpStr8RO *filepath, WpFileAccessMode mode) {
wpDebugAssert(allocator != NULL && filepath != NULL, "`allocator` and `filepath` should not be NULL");
wpDebugAssert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
return _file_open(allocator, filepath, mode);
wpDebugAssert(filepath->size < WP_PATH_MAX, "`filepath` exceeds max path limit.");
return _fileOpen(allocator, filepath, mode);
}
i64 wapp_file_get_current_position(WFile *file) {
i64 wpFileGetCurrentPosition(WpFile *file) {
wpDebugAssert(file != NULL, "`file` should not be NULL.");
return _file_seek(file, 0, WAPP_SEEK_CURRENT);
return _fileSeek(file, 0, WP_SEEK_CURRENT);
}
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
i64 wpFileSeek(WpFile *file, i64 offset, WpFileSeekOrigin origin) {
wpDebugAssert(file != NULL, "`file` should not be NULL.");
return _file_seek(file, offset, origin);
return _fileSeek(file, offset, origin);
}
i64 wapp_file_get_length(WFile *file) {
i64 wpFileGetLength(WpFile *file) {
wpDebugAssert(file != NULL, "`file` should not be NULL.");
i64 current = wapp_file_get_current_position(file);
i64 current = wpFileGetCurrentPosition(file);
_file_seek(file, 0, WAPP_SEEK_END);
_fileSeek(file, 0, WP_SEEK_END);
i64 output = wapp_file_get_current_position(file);
i64 output = wpFileGetCurrentPosition(file);
// Restore position
_file_seek(file, current, WAPP_SEEK_START);
_fileSeek(file, current, WP_SEEK_START);
return output;
}
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count) {
u64 wpFileRead(void *dst_buf, WpFile *file, u64 byte_count) {
wpDebugAssert(dst_buf != NULL && file != NULL,
"`dst_buf` and `file` should not be NULL.");
i64 file_length = wapp_file_get_length(file);
i64 file_length = wpFileGetLength(file);
if (file_length < 0) {
return 0;
}
return _file_read(dst_buf, byte_count, file, file_length);
return _fileRead(dst_buf, byte_count, file, file_length);
}
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
i64 wpFileWrite(const void *src_buf, WpFile *file, u64 byte_count) {
wpDebugAssert(src_buf != NULL && file != NULL,
"`src_buf` and `file` should not be NULL.");
return _file_write(src_buf, file, byte_count);
return _fileWrite(src_buf, file, byte_count);
}
u64 wapp_file_read_str8(WpStr8 *str, WFile *file) {
u64 wpFileReadStr8(WpStr8 *str, WpFile *file) {
wpDebugAssert(str != NULL, "`str` should not be NULL.");
return wapp_file_read((void *)(str->buf), file, str->size);
return wpFileRead((void *)(str->buf), file, str->size);
}
i64 wapp_file_write_str8(WpStr8RO *str, WFile *file) {
i64 wpFileWriteStr8(WpStr8RO *str, WpFile *file) {
wpDebugAssert(str != NULL, "`str` should not be NULL.");
return wapp_file_write((void *)(str->buf), file, str->size);
return wpFileWrite((void *)(str->buf), file, str->size);
}
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
u64 wpFileReadArray(WpArray dst_buf, WpFile *file, u64 item_count) {
wpDebugAssert(dst_buf != NULL && file != NULL,
"`dst_buf` and `file` should not be NULL.");
i64 _file_length = wapp_file_get_length(file);
i64 _file_length = wpFileGetLength(file);
if (_file_length < 0) {
return 0;
}
u64 file_length = (u64)_file_length;
u64 item_size = wapp_array_item_size(dst_buf);
u64 dst_byte_capacity = wapp_array_capacity(dst_buf) * item_size;
u64 item_size = wpArrayItemSize(dst_buf);
u64 dst_byte_capacity = wpArrayCapacity(dst_buf) * item_size;
u64 req_byte_count = item_count * item_size;
u64 copy_byte_count = 0;
@@ -87,26 +87,26 @@ u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
}
u64 byte_count = _file_read(dst_buf, copy_byte_count, file, file_length);
u64 byte_count = _fileRead(dst_buf, copy_byte_count, file, file_length);
if (byte_count == 0) {
return 0;
}
wapp_array_set_count(dst_buf, byte_count / item_size);
wpArraySetCount(dst_buf, byte_count / item_size);
return wapp_array_count(dst_buf);
return wpArrayCount(dst_buf);
}
i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count) {
i64 wpFileWriteArray(const WpArray src_buf, WpFile *file, u64 item_count) {
wpDebugAssert(src_buf != NULL && file != NULL,
"`src_buf` and `file` should not be NULL.");
u64 item_size = wapp_array_item_size(src_buf);
u64 src_byte_count = wapp_array_count(src_buf) * item_size;
u64 item_size = wpArrayItemSize(src_buf);
u64 src_byte_count = wpArrayCount(src_buf) * item_size;
u64 req_byte_count = item_count * item_size;
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
i64 bytes_written = _file_write(src_buf, file, to_copy);
i64 bytes_written = _fileWrite(src_buf, file, to_copy);
if (bytes_written < 0) {
return 0;
}
@@ -114,26 +114,26 @@ i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_coun
return (u64)bytes_written / item_size;
}
i32 wapp_file_flush(WFile *file) {
i32 wpFileFlush(WpFile *file) {
wpDebugAssert(file != NULL, "`file` should not be NULL.");
return _file_flush(file);
return _fileFlush(file);
}
i32 wapp_file_close(WFile *file) {
i32 wpFileClose(WpFile *file) {
wpDebugAssert(file != NULL, "`file` should not be NULL.");
return _file_close(file);
return _fileClose(file);
}
i32 wapp_file_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
i32 wpFileRename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
wpDebugAssert(old_filepath != NULL && new_filepath != NULL,
"`old_filepath` and `new_filepath` should not be NULL");
wpDebugAssert(old_filepath->size < WAPP_PATH_MAX, "`old_filepath` exceeds max path limit.");
wpDebugAssert(new_filepath->size < WAPP_PATH_MAX, "`new_filepath` exceeds max path limit.");
return _file_rename(old_filepath, new_filepath);
wpDebugAssert(old_filepath->size < WP_PATH_MAX, "`old_filepath` exceeds max path limit.");
wpDebugAssert(new_filepath->size < WP_PATH_MAX, "`new_filepath` exceeds max path limit.");
return _fileRename(old_filepath, new_filepath);
}
i32 wapp_file_remove(WpStr8RO *filepath) {
i32 wpFileRemove(WpStr8RO *filepath) {
wpDebugAssert(filepath != NULL, "`filepath` should not be NULL");
wpDebugAssert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
return _file_remove(filepath);
wpDebugAssert(filepath->size < WP_PATH_MAX, "`filepath` exceeds max path limit.");
return _fileRemove(filepath);
}
+44 -44
View File
@@ -11,64 +11,64 @@
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP
typedef struct WFile WFile;
typedef struct WpFile WpFile;
typedef enum {
WAPP_ACCESS_READ, // Equivalent to r
WAPP_ACCESS_WRITE, // Equivalent to w
WAPP_ACCESS_APPEND, // Equivalent to a
WAPP_ACCESS_READ_EX, // Equivalent to r+
WAPP_ACCESS_WRITE_EX, // Equivalent to w+
WAPP_ACCESS_APPEND_EX, // Equivalent to a+
WAPP_ACCESS_WRITE_FAIL_ON_EXIST, // Equivalent to wx
WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX, // Equivalent to wx+
WP_ACCESS_READ, // Equivalent to r
WP_ACCESS_WRITE, // Equivalent to w
WP_ACCESS_APPEND, // Equivalent to a
WP_ACCESS_READ_EX, // Equivalent to r+
WP_ACCESS_WRITE_EX, // Equivalent to w+
WP_ACCESS_APPEND_EX, // Equivalent to a+
WP_ACCESS_WRITE_FAIL_ON_EXIST, // Equivalent to wx
WP_ACCESS_WRITE_FAIL_ON_EXIST_EX, // Equivalent to wx+
FILE_ACCESS_MODE_COUNT,
} FileAccessMode;
COUNT_FILE_ACCESS_MODE,
} WpFileAccessMode;
typedef enum {
WAPP_SEEK_START,
WAPP_SEEK_CURRENT,
WAPP_SEEK_END,
WP_SEEK_START,
WP_SEEK_CURRENT,
WP_SEEK_END,
FILE_SEEK_ORIGIN_COUNT,
} FileSeekOrigin;
COUNT_FILE_SEEK_ORIGIN,
} WpFileSeekOrigin;
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdin to get the standard input stream
wp_extern WFile *wapp_file_stdin(void);
// wpFileStdin to get the standard input stream
wp_extern WpFile *wpFileStdin(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdout to get the standard output stream
wp_extern WFile *wapp_file_stdout(void);
// wpFileStdout to get the standard output stream
wp_extern WpFile *wpFileStdout(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stderr to get the standard error stream
wp_extern WFile *wapp_file_stderr(void);
// wpFileStderr to get the standard error stream
wp_extern WpFile *wpFileStderr(void);
WFile *wapp_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMode mode);
i64 wapp_file_get_current_position(WFile *file);
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
i64 wapp_file_get_length(WFile *file);
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count);
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
u64 wapp_file_read_str8(WpStr8 *str, WFile *file);
i64 wapp_file_write_str8(WpStr8RO *str, WFile *file);
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
i64 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_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath);
i32 wapp_file_remove(WpStr8RO *filepath);
WpFile *wpFileOpen(const WpAllocator *allocator, WpStr8RO *filepath, WpFileAccessMode mode);
i64 wpFileGetCurrentPosition(WpFile *file);
i64 wpFileSeek(WpFile *file, i64 offset, WpFileSeekOrigin origin);
i64 wpFileGetLength(WpFile *file);
u64 wpFileRead(void *dst_buf, WpFile *file, u64 byte_count);
i64 wpFileWrite(const void *src_buf, WpFile *file, u64 byte_count);
u64 wpFileReadStr8(WpStr8 *str, WpFile *file);
i64 wpFileWriteStr8(WpStr8RO *str, WpFile *file);
u64 wpFileReadArray(WpArray dst_buf, WpFile *file, u64 item_count);
i64 wpFileWriteArray(const WpArray src_buf, WpFile *file, u64 item_count);
i32 wpFileFlush(WpFile *file);
i32 wpFileClose(WpFile *file);
i32 wpFileRename(WpStr8RO *old_filepath, WpStr8RO *new_filepath);
i32 wpFileRemove(WpStr8RO *filepath);
wp_extern WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMode mode);
wp_extern i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
wp_extern u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length);
wp_extern i64 _file_write(const void *src_buf, WFile *file, u64 byte_count);
wp_extern i32 _file_flush(WFile *file);
wp_extern i32 _file_close(WFile *file);
wp_extern i32 _file_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath);
wp_extern i32 _file_remove(WpStr8RO *filepath);
wp_extern WpFile *_fileOpen(const WpAllocator *allocator, WpStr8RO *filepath, WpFileAccessMode mode);
wp_extern i64 _fileSeek(WpFile *file, i64 offset, WpFileSeekOrigin origin);
wp_extern u64 _fileRead(void *dst_buf, u64 byte_count, WpFile *file, u64 file_length);
wp_extern i64 _fileWrite(const void *src_buf, WpFile *file, u64 byte_count);
wp_extern i32 _fileFlush(WpFile *file);
wp_extern i32 _fileClose(WpFile *file);
wp_extern i32 _fileRename(WpStr8RO *old_filepath, WpStr8RO *new_filepath);
wp_extern i32 _fileRemove(WpStr8RO *filepath);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
+46 -46
View File
@@ -21,52 +21,52 @@
#include <unistd.h>
#include <sys/types.h>
wp_intern i32 file_flags[FILE_ACCESS_MODE_COUNT] = {
[WAPP_ACCESS_READ] = O_RDONLY,
[WAPP_ACCESS_WRITE] = O_WRONLY | O_CREAT,
[WAPP_ACCESS_APPEND] = O_WRONLY | O_APPEND | O_CREAT,
[WAPP_ACCESS_READ_EX] = O_RDWR,
[WAPP_ACCESS_WRITE_EX] = O_RDWR | O_CREAT,
[WAPP_ACCESS_APPEND_EX] = O_RDWR | O_APPEND | O_CREAT,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = O_WRONLY | O_CREAT | O_EXCL,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = O_RDWR | O_CREAT | O_EXCL,
wp_intern i32 file_flags[COUNT_FILE_ACCESS_MODE] = {
[WP_ACCESS_READ] = O_RDONLY,
[WP_ACCESS_WRITE] = O_WRONLY | O_CREAT,
[WP_ACCESS_APPEND] = O_WRONLY | O_APPEND | O_CREAT,
[WP_ACCESS_READ_EX] = O_RDWR,
[WP_ACCESS_WRITE_EX] = O_RDWR | O_CREAT,
[WP_ACCESS_APPEND_EX] = O_RDWR | O_APPEND | O_CREAT,
[WP_ACCESS_WRITE_FAIL_ON_EXIST] = O_WRONLY | O_CREAT | O_EXCL,
[WP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = O_RDWR | O_CREAT | O_EXCL,
};
wp_intern mode_t file_modes[FILE_ACCESS_MODE_COUNT] = {
[WAPP_ACCESS_READ] = 0,
[WAPP_ACCESS_WRITE] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WAPP_ACCESS_APPEND] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WAPP_ACCESS_READ_EX] = 0,
[WAPP_ACCESS_WRITE_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WAPP_ACCESS_APPEND_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
wp_intern mode_t file_modes[COUNT_FILE_ACCESS_MODE] = {
[WP_ACCESS_READ] = 0,
[WP_ACCESS_WRITE] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WP_ACCESS_APPEND] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WP_ACCESS_READ_EX] = 0,
[WP_ACCESS_WRITE_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WP_ACCESS_APPEND_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WP_ACCESS_WRITE_FAIL_ON_EXIST] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
[WP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
};
wp_intern i32 file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_START] = SEEK_SET,
[WAPP_SEEK_CURRENT] = SEEK_CUR,
[WAPP_SEEK_END] = SEEK_END,
wp_intern i32 file_seek_origins[COUNT_FILE_SEEK_ORIGIN] = {
[WP_SEEK_START] = SEEK_SET,
[WP_SEEK_CURRENT] = SEEK_CUR,
[WP_SEEK_END] = SEEK_END,
};
WFile *wapp_file_stdin(void) {
wp_persist WFile _stdin = { .fd = STDIN_FILENO };
WpFile *wpFileStdin(void) {
wp_persist WpFile _stdin = { .fd = STDIN_FILENO };
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wp_persist WFile _stdout = { .fd = STDOUT_FILENO };
WpFile *wpFileStdout(void) {
wp_persist WpFile _stdout = { .fd = STDOUT_FILENO };
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wp_persist WFile _stderr = { .fd = STDERR_FILENO };
WpFile *wpFileStderr(void) {
wp_persist WpFile _stderr = { .fd = STDERR_FILENO };
return &_stderr;
}
WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMode mode) {
wp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX);
WpFile *_fileOpen(const WpAllocator *allocator, WpStr8RO *filepath, WpFileAccessMode mode) {
wp_persist c8 tmp[WP_PATH_MAX] = {0};
memset(tmp, 0, WP_PATH_MAX);
memcpy(tmp, filepath->buf, filepath->size);
i32 fd = open((const char *)tmp, file_flags[mode], file_modes[mode]);
@@ -74,7 +74,7 @@ WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMo
return NULL;
}
WFile *output = wpMemAllocatorAlloc(allocator, sizeof(WFile));
WpFile *output = wpMemAllocatorAlloc(allocator, sizeof(WpFile));
if (output) {
output->fd = fd;
}
@@ -82,11 +82,11 @@ WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMo
return output;
}
i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
i64 _fileSeek(WpFile *file, i64 offset, WpFileSeekOrigin origin) {
return lseek64(file->fd, offset, file_seek_origins[origin]);
}
u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length) {
u64 _fileRead(void *dst_buf, u64 byte_count, WpFile *file, u64 file_length) {
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
i64 count = read(file->fd, dst_buf, copy_byte_count);
@@ -95,37 +95,37 @@ u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length) {
return count;
}
i64 _file_write(const void *src_buf, WFile *file, u64 byte_count) {
i64 _fileWrite(const void *src_buf, WpFile *file, u64 byte_count) {
return write(file->fd, src_buf, byte_count);
}
i32 _file_flush(WFile *file) {
i32 _fileFlush(WpFile *file) {
return fsync(file->fd);
}
i32 _file_close(WFile *file) {
i32 _fileClose(WpFile *file) {
return close(file->fd);
}
i32 _file_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
wp_persist c8 old_tmp[WAPP_PATH_MAX] = {0};
wp_persist c8 new_tmp[WAPP_PATH_MAX] = {0};
memset(old_tmp, 0, WAPP_PATH_MAX);
i32 _fileRename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
wp_persist c8 old_tmp[WP_PATH_MAX] = {0};
wp_persist c8 new_tmp[WP_PATH_MAX] = {0};
memset(old_tmp, 0, WP_PATH_MAX);
memcpy(old_tmp, old_filepath->buf, old_filepath->size);
memset(new_tmp, 0, WAPP_PATH_MAX);
memset(new_tmp, 0, WP_PATH_MAX);
memcpy(new_tmp, new_filepath->buf, new_filepath->size);
i32 link_result = link((const char *)old_tmp, (const char *)new_tmp);
if (link_result == 0) {
_file_remove(old_filepath);
_fileRemove(old_filepath);
}
return link_result;
}
i32 _file_remove(WpStr8RO *filepath) {
wp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX);
i32 _fileRemove(WpStr8RO *filepath) {
wp_persist c8 tmp[WP_PATH_MAX] = {0};
memset(tmp, 0, WP_PATH_MAX);
memcpy(tmp, filepath->buf, filepath->size);
return unlink((const char *)tmp);
+2 -2
View File
@@ -12,9 +12,9 @@ BEGIN_C_LINKAGE
#ifdef WP_PLATFORM_POSIX
#define END_OF_LINE "\n"
#define WP_END_OF_LINE "\n"
struct WFile {
struct WpFile {
i32 fd;
};
+54 -54
View File
@@ -15,66 +15,66 @@
#include <fileapi.h>
#include <intsafe.h>
wp_intern DWORD file_accesses[FILE_ACCESS_MODE_COUNT] = {
[WAPP_ACCESS_READ] = FILE_READ_DATA,
[WAPP_ACCESS_WRITE] = FILE_WRITE_DATA,
[WAPP_ACCESS_APPEND] = FILE_APPEND_DATA,
[WAPP_ACCESS_READ_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
[WAPP_ACCESS_WRITE_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
[WAPP_ACCESS_APPEND_EX] = FILE_READ_DATA | FILE_APPEND_DATA,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = FILE_WRITE_DATA,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
wp_intern DWORD file_accesses[COUNT_FILE_ACCESS_MODE] = {
[WP_ACCESS_READ] = FILE_READ_DATA,
[WP_ACCESS_WRITE] = FILE_WRITE_DATA,
[WP_ACCESS_APPEND] = FILE_APPEND_DATA,
[WP_ACCESS_READ_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
[WP_ACCESS_WRITE_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
[WP_ACCESS_APPEND_EX] = FILE_READ_DATA | FILE_APPEND_DATA,
[WP_ACCESS_WRITE_FAIL_ON_EXIST] = FILE_WRITE_DATA,
[WP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = FILE_READ_DATA | FILE_WRITE_DATA,
};
wp_intern DWORD creation_dispositions[FILE_ACCESS_MODE_COUNT] = {
[WAPP_ACCESS_READ] = OPEN_EXISTING,
[WAPP_ACCESS_WRITE] = CREATE_ALWAYS,
[WAPP_ACCESS_APPEND] = OPEN_ALWAYS,
[WAPP_ACCESS_READ_EX] = OPEN_EXISTING,
[WAPP_ACCESS_WRITE_EX] = CREATE_ALWAYS,
[WAPP_ACCESS_APPEND_EX] = OPEN_ALWAYS,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = CREATE_NEW,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = CREATE_NEW,
wp_intern DWORD creation_dispositions[COUNT_FILE_ACCESS_MODE] = {
[WP_ACCESS_READ] = OPEN_EXISTING,
[WP_ACCESS_WRITE] = CREATE_ALWAYS,
[WP_ACCESS_APPEND] = OPEN_ALWAYS,
[WP_ACCESS_READ_EX] = OPEN_EXISTING,
[WP_ACCESS_WRITE_EX] = CREATE_ALWAYS,
[WP_ACCESS_APPEND_EX] = OPEN_ALWAYS,
[WP_ACCESS_WRITE_FAIL_ON_EXIST] = CREATE_NEW,
[WP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = CREATE_NEW,
};
wp_intern DWORD sharing_modes[FILE_ACCESS_MODE_COUNT] = {
[WAPP_ACCESS_READ] = FILE_SHARE_READ | FILE_SHARE_WRITE,
[WAPP_ACCESS_WRITE] = FILE_SHARE_READ,
[WAPP_ACCESS_APPEND] = FILE_SHARE_READ,
[WAPP_ACCESS_READ_EX] = FILE_SHARE_READ | FILE_SHARE_WRITE,
[WAPP_ACCESS_WRITE_EX] = FILE_SHARE_READ,
[WAPP_ACCESS_APPEND_EX] = FILE_SHARE_READ,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = FILE_SHARE_READ,
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = FILE_SHARE_READ,
wp_intern DWORD sharing_modes[COUNT_FILE_ACCESS_MODE] = {
[WP_ACCESS_READ] = FILE_SHARE_READ | FILE_SHARE_WRITE,
[WP_ACCESS_WRITE] = FILE_SHARE_READ,
[WP_ACCESS_APPEND] = FILE_SHARE_READ,
[WP_ACCESS_READ_EX] = FILE_SHARE_READ | FILE_SHARE_WRITE,
[WP_ACCESS_WRITE_EX] = FILE_SHARE_READ,
[WP_ACCESS_APPEND_EX] = FILE_SHARE_READ,
[WP_ACCESS_WRITE_FAIL_ON_EXIST] = FILE_SHARE_READ,
[WP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = FILE_SHARE_READ,
};
wp_intern DWORD file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_START] = FILE_BEGIN,
[WAPP_SEEK_CURRENT] = FILE_CURRENT,
[WAPP_SEEK_END] = FILE_END,
wp_intern DWORD file_seek_origins[COUNT_FILE_SEEK_ORIGIN] = {
[WP_SEEK_START] = FILE_BEGIN,
[WP_SEEK_CURRENT] = FILE_CURRENT,
[WP_SEEK_END] = FILE_END,
};
WFile *wapp_file_stdin(void) {
wp_persist WFile _stdin = { .fh = INVALID_HANDLE_VALUE };
WpFile *wpFileStdin(void) {
wp_persist WpFile _stdin = { .fh = INVALID_HANDLE_VALUE };
_stdin.fh = GetStdHandle(STD_INPUT_HANDLE);
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wp_persist WFile _stdout = { .fh = INVALID_HANDLE_VALUE };
WpFile *wpFileStdout(void) {
wp_persist WpFile _stdout = { .fh = INVALID_HANDLE_VALUE };
_stdout.fh = GetStdHandle(STD_OUTPUT_HANDLE);
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wp_persist WFile _stderr = { .fh = INVALID_HANDLE_VALUE };
WpFile *wpFileStderr(void) {
wp_persist WpFile _stderr = { .fh = INVALID_HANDLE_VALUE };
_stderr.fh = GetStdHandle(STD_ERROR_HANDLE);
return &_stderr;
}
WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMode mode) {
wp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX);
WpFile *_fileOpen(const WpAllocator *allocator, WpStr8RO *filepath, WpFileAccessMode mode) {
wp_persist c8 tmp[WP_PATH_MAX] = {0};
memset(tmp, 0, WP_PATH_MAX);
memcpy(tmp, filepath->buf, filepath->size);
HANDLE fh = CreateFileA((LPCSTR)tmp,
@@ -88,7 +88,7 @@ WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMo
return NULL;
}
WFile *output = wpMemAllocatorAlloc(allocator, sizeof(WFile));
WpFile *output = wpMemAllocatorAlloc(allocator, sizeof(WpFile));
if (output) {
output->fh = fh;
}
@@ -96,7 +96,7 @@ WFile *_file_open(const WpAllocator *allocator, WpStr8RO *filepath, FileAccessMo
return output;
}
i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
i64 _fileSeek(WpFile *file, i64 offset, WpFileSeekOrigin origin) {
LARGE_INTEGER distance = {0};
LARGE_INTEGER output = {0};
@@ -109,7 +109,7 @@ i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
return output.QuadPart;
}
u64 _file_read(void* dst_buf, u64 byte_count, WFile* file, u64 file_length) {
u64 _fileRead(void* dst_buf, u64 byte_count, WpFile* file, u64 file_length) {
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
wpDebugAssert(copy_byte_count <= DWORD_MAX, "Attempting to read large number of bytes at once");
@@ -121,7 +121,7 @@ u64 _file_read(void* dst_buf, u64 byte_count, WFile* file, u64 file_length) {
return (u64)read_count;
}
i64 _file_write(const void *src_buf, WFile *file, u64 byte_count) {
i64 _fileWrite(const void *src_buf, WpFile *file, u64 byte_count) {
wpDebugAssert(byte_count <= DWORD_MAX, "Attempting to write large number of bytes at once");
DWORD write_count = 0;
@@ -131,7 +131,7 @@ i64 _file_write(const void *src_buf, WFile *file, u64 byte_count) {
return (i64)write_count;
}
i32 _file_flush(WFile *file) {
i32 _fileFlush(WpFile *file) {
if (!FlushFileBuffers(file->fh)) {
return -1;
}
@@ -139,7 +139,7 @@ i32 _file_flush(WFile *file) {
return 0;
}
i32 _file_close(WFile *file) {
i32 _fileClose(WpFile *file) {
if (!CloseHandle(file->fh)) {
return -1;
}
@@ -147,12 +147,12 @@ i32 _file_close(WFile *file) {
return 0;
}
i32 _file_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
wp_persist c8 old_tmp[WAPP_PATH_MAX] = {0};
wp_persist c8 new_tmp[WAPP_PATH_MAX] = {0};
memset(old_tmp, 0, WAPP_PATH_MAX);
i32 _fileRename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
wp_persist c8 old_tmp[WP_PATH_MAX] = {0};
wp_persist c8 new_tmp[WP_PATH_MAX] = {0};
memset(old_tmp, 0, WP_PATH_MAX);
memcpy(old_tmp, old_filepath->buf, old_filepath->size);
memset(new_tmp, 0, WAPP_PATH_MAX);
memset(new_tmp, 0, WP_PATH_MAX);
memcpy(new_tmp, new_filepath->buf, new_filepath->size);
if (!MoveFile((LPCSTR)old_tmp, (LPCSTR)new_tmp)) {
@@ -162,9 +162,9 @@ i32 _file_rename(WpStr8RO *old_filepath, WpStr8RO *new_filepath) {
return 0;
}
i32 _file_remove(WpStr8RO *filepath) {
wp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX);
i32 _fileRemove(WpStr8RO *filepath) {
wp_persist c8 tmp[WP_PATH_MAX] = {0};
memset(tmp, 0, WP_PATH_MAX);
memcpy(tmp, filepath->buf, filepath->size);
if (!DeleteFile((LPCSTR)tmp)) {
+2 -2
View File
@@ -12,13 +12,13 @@ BEGIN_C_LINKAGE
#ifdef WP_PLATFORM_WINDOWS
#define END_OF_LINE "\r\n"
#define WP_END_OF_LINE "\r\n"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <fileapi.h>
struct WFile {
struct WpFile {
HANDLE fh;
};
+33 -33
View File
@@ -6,45 +6,45 @@
wp_intern WpAllocator arena = {0};
wp_intern WpStr8RO test_filename = wpStr8LitRoInitialiserList("wapptest.bin");
wp_intern WpStr8RO new_filename = wpStr8LitRoInitialiserList("wapptest2.bin");
wp_intern WFile *test_fp = NULL;
wp_intern I32Array src_array1 = wapp_array(i32, 0, 1, 2, 3, 4);
wp_intern I32Array src_array2 = wapp_array(i32, 5, 6, 7, 8, 9);
wp_intern I32Array src_array3 = wapp_array(i32, 10, 11, 12, 13, 14);
wp_intern I32Array dst_array = wapp_array_with_capacity(i32, DST_CAPACITY, false);
wp_intern WpFile *test_fp = NULL;
wp_intern WpI32Array src_array1 = wpArray(i32, 0, 1, 2, 3, 4);
wp_intern WpI32Array src_array2 = wpArray(i32, 5, 6, 7, 8, 9);
wp_intern WpI32Array src_array3 = wpArray(i32, 10, 11, 12, 13, 14);
wp_intern WpI32Array dst_array = wpArrayWithCapacity(i32, DST_CAPACITY, false);
wp_intern i32 dst_buf[DST_CAPACITY] = {0};
WpTestFuncResult test_wapp_file_open(void) {
arena = wapp_mem_arena_allocator_init(KiB(16));
test_fp = wapp_file_open(&arena, &test_filename, WAPP_ACCESS_WRITE_EX);
arena = wpMemArenaAllocatorInit(KiB(16));
test_fp = wpFileOpen(&arena, &test_filename, WP_ACCESS_WRITE_EX);
return wpTesterResult(test_fp != NULL);
}
WpTestFuncResult test_wapp_file_get_current_position(void) {
i64 pos = wapp_file_get_current_position(test_fp);
i64 pos = wpFileGetCurrentPosition(test_fp);
return wpTesterResult(pos == 0);
}
WpTestFuncResult test_wapp_file_seek(void) {
wapp_file_write_array((GenericArray)src_array1, test_fp, wapp_array_count(src_array1));
wpFileWriteArray((WpArray)src_array1, test_fp, wpArrayCount(src_array1));
i64 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
b8 result = seek_result == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1));
i64 seek_result = wpFileSeek(test_fp, 0, WP_SEEK_END);
b8 result = seek_result == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1));
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
return wpTesterResult(result);
}
WpTestFuncResult test_wapp_file_get_length(void) {
i64 length = wapp_file_get_length(test_fp);
return wpTesterResult(length == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1)));
i64 length = wpFileGetLength(test_fp);
return wpTesterResult(length == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1)));
}
WpTestFuncResult test_wapp_file_read(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
u64 byte_count = DST_CAPACITY * sizeof(i32);
u64 count = wapp_file_read((void *)dst_buf, test_fp, byte_count);
u64 count = wpFileRead((void *)dst_buf, test_fp, byte_count);
b8 result = count == byte_count;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
@@ -52,7 +52,7 @@ WpTestFuncResult test_wapp_file_read(void) {
u64 index = 0;
b8 running = true;
while (running) {
result = result && (dst_buf[index] == *wapp_array_get(i32, src_array1, index));
result = result && (dst_buf[index] == *wpArrayGet(i32, src_array1, index));
++index;
running = index < DST_CAPACITY;
}
@@ -61,58 +61,58 @@ WpTestFuncResult test_wapp_file_read(void) {
}
WpTestFuncResult test_wapp_file_write(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
wpFileSeek(test_fp, 0, WP_SEEK_END);
u64 expected_count = wapp_array_count(src_array2) * wapp_array_item_size(src_array2);
i64 count = wapp_file_write((void *)src_array2, test_fp, expected_count);
u64 expected_count = wpArrayCount(src_array2) * wpArrayItemSize(src_array2);
i64 count = wpFileWrite((void *)src_array2, test_fp, expected_count);
return wpTesterResult(count >= 0 && (u64)count == expected_count);
}
WpTestFuncResult test_wapp_file_read_array(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
u64 count = wapp_file_read_array((GenericArray)dst_array, test_fp, wapp_array_count(src_array1));
b8 result = count == wapp_array_count(src_array1) &&
wapp_array_count(dst_array) == wapp_array_count(src_array1);
u64 count = wpFileReadArray((WpArray)dst_array, test_fp, wpArrayCount(src_array1));
b8 result = count == wpArrayCount(src_array1) &&
wpArrayCount(dst_array) == wpArrayCount(src_array1);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
u64 index = 0;
b8 running = true;
while (running) {
result = result && (*wapp_array_get(i32, dst_array, index) == *wapp_array_get(i32, src_array1, index));
result = result && (*wpArrayGet(i32, dst_array, index) == *wpArrayGet(i32, src_array1, index));
++index;
running = index < wapp_array_count(dst_array);
running = index < wpArrayCount(dst_array);
}
return wpTesterResult(result);
}
WpTestFuncResult test_wapp_file_write_array(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
wpFileSeek(test_fp, 0, WP_SEEK_END);
i64 count = wapp_file_write_array((GenericArray)src_array3, test_fp, wapp_array_count(src_array3));
i64 count = wpFileWriteArray((WpArray)src_array3, test_fp, wpArrayCount(src_array3));
return wpTesterResult(count >= 0 && (u64)count == wapp_array_count(src_array3));
return wpTesterResult(count >= 0 && (u64)count == wpArrayCount(src_array3));
}
WpTestFuncResult test_wapp_file_flush(void) {
i32 flush_result = wapp_file_flush(test_fp);
i32 flush_result = wpFileFlush(test_fp);
return wpTesterResult(flush_result == 0);
}
WpTestFuncResult test_wapp_file_close(void) {
i32 close_result = wapp_file_close(test_fp);
i32 close_result = wpFileClose(test_fp);
return wpTesterResult(close_result == 0);
}
WpTestFuncResult test_wapp_file_rename(void) {
i32 rename_result = wapp_file_rename(&test_filename, &new_filename);
i32 rename_result = wpFileRename(&test_filename, &new_filename);
return wpTesterResult(rename_result == 0);
}
WpTestFuncResult test_wapp_file_remove(void) {
i32 remove_result = wapp_file_remove(&new_filename);
i32 remove_result = wpFileRemove(&new_filename);
return wpTesterResult(remove_result == 0);
}
+37 -37
View File
@@ -6,49 +6,49 @@
wp_intern WpAllocator arena = {};
wp_intern WpStr8RO test_filename = wpStr8LitRoInitialiserList("wapptest.bin");
wp_intern WpStr8RO new_filename = wpStr8LitRoInitialiserList("wapptest2.bin");
wp_intern WFile *test_fp = NULL;
wp_intern WpFile *test_fp = NULL;
wp_intern i32 dst_buf[DST_CAPACITY] = {0};
wp_intern I32Array src_array1;
wp_intern I32Array src_array2;
wp_intern I32Array src_array3;
wp_intern I32Array dst_array ;
wp_intern WpI32Array src_array1;
wp_intern WpI32Array src_array2;
wp_intern WpI32Array src_array3;
wp_intern WpI32Array dst_array ;
WpTestFuncResult test_wapp_file_open(void) {
arena = wapp_mem_arena_allocator_init(KiB(16));
src_array1 = wapp_array(i32, 0, 1, 2, 3, 4);
src_array2 = wapp_array(i32, 5, 6, 7, 8, 9);
src_array3 = wapp_array(i32, 10, 11, 12, 13, 14);
dst_array = wapp_array_with_capacity(i32, DST_CAPACITY, false);
test_fp = wapp_file_open(&arena, &test_filename, WAPP_ACCESS_WRITE_EX);
arena = wpMemArenaAllocatorInit(KiB(16));
src_array1 = wpArray(i32, 0, 1, 2, 3, 4);
src_array2 = wpArray(i32, 5, 6, 7, 8, 9);
src_array3 = wpArray(i32, 10, 11, 12, 13, 14);
dst_array = wpArrayWithCapacity(i32, DST_CAPACITY, false);
test_fp = wpFileOpen(&arena, &test_filename, WP_ACCESS_WRITE_EX);
return wpTesterResult(test_fp != NULL);
}
WpTestFuncResult test_wapp_file_get_current_position(void) {
i64 pos = wapp_file_get_current_position(test_fp);
i64 pos = wpFileGetCurrentPosition(test_fp);
return wpTesterResult(pos == 0);
}
WpTestFuncResult test_wapp_file_seek(void) {
wapp_file_write_array((GenericArray)src_array1, test_fp, wapp_array_count(src_array1));
wpFileWriteArray((WpArray)src_array1, test_fp, wpArrayCount(src_array1));
i64 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
b8 result = seek_result == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1));
i64 seek_result = wpFileSeek(test_fp, 0, WP_SEEK_END);
b8 result = seek_result == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1));
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
return wpTesterResult(result);
}
WpTestFuncResult test_wapp_file_get_length(void) {
i64 length = wapp_file_get_length(test_fp);
return wpTesterResult(length == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1)));
i64 length = wpFileGetLength(test_fp);
return wpTesterResult(length == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1)));
}
WpTestFuncResult test_wapp_file_read(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
u64 byte_count = DST_CAPACITY * sizeof(i32);
u64 count = wapp_file_read((void *)dst_buf, test_fp, byte_count);
u64 count = wpFileRead((void *)dst_buf, test_fp, byte_count);
b8 result = count == byte_count;
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
@@ -56,7 +56,7 @@ WpTestFuncResult test_wapp_file_read(void) {
u64 index = 0;
b8 running = true;
while (running) {
result = result && (dst_buf[index] == *wapp_array_get(i32, src_array1, index));
result = result && (dst_buf[index] == *wpArrayGet(i32, src_array1, index));
++index;
running = index < DST_CAPACITY;
}
@@ -65,58 +65,58 @@ WpTestFuncResult test_wapp_file_read(void) {
}
WpTestFuncResult test_wapp_file_write(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
wpFileSeek(test_fp, 0, WP_SEEK_END);
u64 expected_count = wapp_array_count(src_array2) * wapp_array_item_size(src_array2);
i64 count = wapp_file_write((void *)src_array2, test_fp, expected_count);
u64 expected_count = wpArrayCount(src_array2) * wpArrayItemSize(src_array2);
i64 count = wpFileWrite((void *)src_array2, test_fp, expected_count);
return wpTesterResult(count >= 0 && (u64)count == expected_count);
}
WpTestFuncResult test_wapp_file_read_array(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
wpFileSeek(test_fp, 0, WP_SEEK_START);
u64 count = wapp_file_read_array((GenericArray)dst_array, test_fp, wapp_array_count(src_array1));
b8 result = count == wapp_array_count(src_array1) &&
wapp_array_count(dst_array) == wapp_array_count(src_array1);
u64 count = wpFileReadArray((WpArray)dst_array, test_fp, wpArrayCount(src_array1));
b8 result = count == wpArrayCount(src_array1) &&
wpArrayCount(dst_array) == wpArrayCount(src_array1);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings
u64 index = 0;
b8 running = true;
while (running) {
result = result && (*wapp_array_get(i32, dst_array, index) == *wapp_array_get(i32, src_array1, index));
result = result && (*wpArrayGet(i32, dst_array, index) == *wpArrayGet(i32, src_array1, index));
++index;
running = index < wapp_array_count(dst_array);
running = index < wpArrayCount(dst_array);
}
return wpTesterResult(result);
}
WpTestFuncResult test_wapp_file_write_array(void) {
wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
wpFileSeek(test_fp, 0, WP_SEEK_END);
i64 count = wapp_file_write_array((GenericArray)src_array3, test_fp, wapp_array_count(src_array3));
i64 count = wpFileWriteArray((WpArray)src_array3, test_fp, wpArrayCount(src_array3));
return wpTesterResult(count >= 0 && (u64)count == wapp_array_count(src_array3));
return wpTesterResult(count >= 0 && (u64)count == wpArrayCount(src_array3));
}
WpTestFuncResult test_wapp_file_flush(void) {
i32 flush_result = wapp_file_flush(test_fp);
i32 flush_result = wpFileFlush(test_fp);
return wpTesterResult(flush_result == 0);
}
WpTestFuncResult test_wapp_file_close(void) {
i32 close_result = wapp_file_close(test_fp);
i32 close_result = wpFileClose(test_fp);
return wpTesterResult(close_result == 0);
}
WpTestFuncResult test_wapp_file_rename(void) {
i32 rename_result = wapp_file_rename(&test_filename, &new_filename);
i32 rename_result = wpFileRename(&test_filename, &new_filename);
return wpTesterResult(rename_result == 0);
}
WpTestFuncResult test_wapp_file_remove(void) {
i32 remove_result = wapp_file_remove(&new_filename);
i32 remove_result = wpFileRemove(&new_filename);
return wpTesterResult(remove_result == 0);
}