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
+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;
};