Reformat os
This commit is contained in:
@@ -18,129 +18,129 @@
|
|||||||
#define ARENA_MINIMUM_CAPACITY KiB(16) // Allocate minimum of 4 pages
|
#define ARENA_MINIMUM_CAPACITY KiB(16) // Allocate minimum of 4 pages
|
||||||
|
|
||||||
struct arena {
|
struct arena {
|
||||||
u8 *buf;
|
u8 *buf;
|
||||||
u8 *offset;
|
u8 *offset;
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
b8 committed;
|
b8 committed;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(b8));
|
wapp_misc_utils_padding_size(sizeof(u8 *) * 2 + sizeof(u64) + sizeof(b8));
|
||||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||||
};
|
};
|
||||||
|
|
||||||
b8 wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
b8 wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
||||||
if (!arena || *arena || base_capacity == 0) {
|
if (!arena || *arena || base_capacity == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*arena = (Arena *)calloc(1, sizeof(Arena));
|
*arena = (Arena *)calloc(1, sizeof(Arena));
|
||||||
Arena *arena_ptr = *arena;
|
Arena *arena_ptr = *arena;
|
||||||
if (!arena_ptr) {
|
if (!arena_ptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 arena_capacity = wapp_misc_utils_u64_round_up_pow2(
|
u64 arena_capacity = wapp_misc_utils_u64_round_up_pow2(
|
||||||
base_capacity >= ARENA_MINIMUM_CAPACITY ?
|
base_capacity >= ARENA_MINIMUM_CAPACITY ?
|
||||||
base_capacity :
|
base_capacity :
|
||||||
ARENA_MINIMUM_CAPACITY
|
ARENA_MINIMUM_CAPACITY
|
||||||
);
|
);
|
||||||
|
|
||||||
arena_ptr->buf = (u8 *)wapp_mem_util_alloc(NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags,
|
arena_ptr->buf = (u8 *)wapp_mem_util_alloc(NULL, arena_capacity, WAPP_MEM_ACCESS_READ_WRITE, flags,
|
||||||
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
|
zero_buffer ? WAPP_MEM_INIT_INITIALISED : WAPP_MEM_INIT_UNINITIALISED);
|
||||||
|
|
||||||
if (!(arena_ptr->buf)) {
|
if (!(arena_ptr->buf)) {
|
||||||
wapp_mem_arena_destroy(arena);
|
wapp_mem_arena_destroy(arena);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
arena_ptr->capacity = arena_capacity;
|
arena_ptr->capacity = arena_capacity;
|
||||||
arena_ptr->offset = arena_ptr->buf;
|
arena_ptr->offset = arena_ptr->buf;
|
||||||
arena_ptr->committed = (flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT;
|
arena_ptr->committed = (flags & WAPP_MEM_ALLOC_COMMIT) == WAPP_MEM_ALLOC_COMMIT;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
|
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
|
||||||
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||||
|
|
||||||
u8 *alloc_start = arena->offset;
|
u8 *alloc_start = arena->offset;
|
||||||
|
|
||||||
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||||
if (output + size >= arena->buf + arena->capacity) {
|
if (output + size >= arena->buf + arena->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
arena->offset = output + size;
|
arena->offset = output + size;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
if (!(arena->committed)) {
|
if (!(arena->committed)) {
|
||||||
wapp_mem_util_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
wapp_mem_util_alloc(alloc_start, (uptr)(arena->offset) - (uptr)(alloc_start),
|
||||||
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
WAPP_MEM_ACCESS_READ_WRITE, WAPP_MEM_ALLOC_COMMIT,
|
||||||
WAPP_MEM_INIT_UNINITIALISED);
|
WAPP_MEM_INIT_UNINITIALISED);
|
||||||
}
|
}
|
||||||
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
#endif // ifdef WAPP_PLATFORM_WINDOWS
|
||||||
|
|
||||||
memset(output, 0, size);
|
memset(output, 0, size);
|
||||||
|
|
||||||
return (void *)output;
|
return (void *)output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size) {
|
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size) {
|
||||||
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
||||||
arena->offset + new_size >= arena->buf + arena->capacity) {
|
arena->offset + new_size >= arena->buf + arena->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *new_ptr = wapp_mem_arena_alloc(arena, new_size);
|
void *new_ptr = wapp_mem_arena_alloc(arena, new_size);
|
||||||
if (!new_ptr) {
|
if (!new_ptr) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
||||||
memcpy(new_ptr, ptr, copy_size);
|
memcpy(new_ptr, ptr, copy_size);
|
||||||
|
|
||||||
return new_ptr;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment) {
|
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment) {
|
||||||
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
if ((u8*)ptr < arena->buf || (u8*)ptr > arena->offset ||
|
||||||
arena->offset + new_size >= arena->buf + arena->capacity) {
|
arena->offset + new_size >= arena->buf + arena->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *new_ptr = wapp_mem_arena_alloc_aligned(arena, new_size, alignment);
|
void *new_ptr = wapp_mem_arena_alloc_aligned(arena, new_size, alignment);
|
||||||
if (!new_ptr) {
|
if (!new_ptr) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
u64 copy_size = new_size <= old_size ? new_size : old_size;
|
||||||
memcpy(new_ptr, ptr, copy_size);
|
memcpy(new_ptr, ptr, copy_size);
|
||||||
|
|
||||||
return new_ptr;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_clear(Arena *arena) {
|
void wapp_mem_arena_clear(Arena *arena) {
|
||||||
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
wapp_debug_assert(arena != NULL, "`arena` should not be NULL");
|
||||||
|
|
||||||
memset(arena->buf, 0, arena->offset - arena->buf);
|
memset(arena->buf, 0, arena->offset - arena->buf);
|
||||||
arena->offset = arena->buf;
|
arena->offset = arena->buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_destroy(Arena **arena) {
|
void wapp_mem_arena_destroy(Arena **arena) {
|
||||||
wapp_debug_assert(arena != NULL && (*arena) != NULL, "`arena` double pointer is not valid");
|
wapp_debug_assert(arena != NULL && (*arena) != NULL, "`arena` double pointer is not valid");
|
||||||
|
|
||||||
Arena *arena_ptr = *arena;
|
Arena *arena_ptr = *arena;
|
||||||
if (arena_ptr->buf) {
|
if (arena_ptr->buf) {
|
||||||
wapp_mem_util_free(arena_ptr->buf, arena_ptr->capacity);
|
wapp_mem_util_free(arena_ptr->buf, arena_ptr->capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
arena_ptr->buf = arena_ptr->offset = NULL;
|
arena_ptr->buf = arena_ptr->offset = NULL;
|
||||||
arena_ptr->capacity = 0;
|
arena_ptr->capacity = 0;
|
||||||
|
|
||||||
free(*arena);
|
free(*arena);
|
||||||
*arena = NULL;
|
*arena = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,27 +13,27 @@ BEGIN_C_LINKAGE
|
|||||||
|
|
||||||
typedef struct arena Arena;
|
typedef struct arena Arena;
|
||||||
|
|
||||||
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init(arena_dptr, base_capacity) \
|
||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
||||||
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_commit(arena_dptr, base_capacity) \
|
||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||||
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_zero(arena_dptr, base_capacity) \
|
||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
||||||
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
|
#define wapp_mem_arena_init_commit_and_zero(arena_dptr, base_capacity) \
|
||||||
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
(wapp_mem_arena_init_custom(arena_dptr, base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
|
* Arena initialisation function. `wapp_mem_arena_init_custom` provides the most
|
||||||
* control over how the Arena is initialised. Wrapper macros are provided for
|
* control over how the Arena is initialised. Wrapper macros are provided for
|
||||||
* easier use.
|
* easier use.
|
||||||
*/
|
*/
|
||||||
b8 wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer);
|
b8 wapp_mem_arena_init_custom(Arena **arena, u64 base_capacity, MemAllocFlags flags, b8 zero_buffer);
|
||||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
||||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
||||||
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 old_size, u64 new_size);
|
||||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
|
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
|
||||||
void wapp_mem_arena_clear(Arena *arena);
|
void wapp_mem_arena_clear(Arena *arena);
|
||||||
void wapp_mem_arena_destroy(Arena **arena);
|
void wapp_mem_arena_destroy(Arena **arena);
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -9,51 +9,51 @@ wapp_intern inline void *mem_arena_alloc(u64 size, void *alloc_obj);
|
|||||||
wapp_intern inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
|
wapp_intern inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
|
||||||
wapp_intern inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
|
wapp_intern inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
|
||||||
wapp_intern inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
wapp_intern inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
||||||
void *alloc_obj);
|
void *alloc_obj);
|
||||||
|
|
||||||
|
|
||||||
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags flags, b8 zero_buffer) {
|
||||||
Allocator allocator = {0};
|
Allocator allocator = {0};
|
||||||
b8 initialised = wapp_mem_arena_init_custom((Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
|
b8 initialised = wapp_mem_arena_init_custom((Arena **)(&allocator.obj), base_capacity, flags, zero_buffer);
|
||||||
if (!initialised) {
|
if (!initialised) {
|
||||||
return allocator;
|
return allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
allocator.alloc = mem_arena_alloc;
|
allocator.alloc = mem_arena_alloc;
|
||||||
allocator.alloc_aligned = mem_arena_alloc_aligned;
|
allocator.alloc_aligned = mem_arena_alloc_aligned;
|
||||||
allocator.realloc = mem_arena_realloc;
|
allocator.realloc = mem_arena_realloc;
|
||||||
allocator.realloc_aligned = mem_arena_realloc_aligned;
|
allocator.realloc_aligned = mem_arena_realloc_aligned;
|
||||||
|
|
||||||
return allocator;
|
return allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
|
void wapp_mem_arena_allocator_clear(Allocator *allocator) {
|
||||||
wapp_mem_arena_clear((Arena *)(allocator->obj));
|
wapp_mem_arena_clear((Arena *)(allocator->obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
|
void wapp_mem_arena_allocator_destroy(Allocator *allocator) {
|
||||||
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
|
wapp_mem_arena_destroy((Arena **)(&(allocator->obj)));
|
||||||
*allocator = (Allocator){0};
|
*allocator = (Allocator){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
wapp_intern inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
wapp_intern inline void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
return wapp_mem_arena_alloc(arena, size);
|
return wapp_mem_arena_alloc(arena, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) {
|
wapp_intern inline void *mem_arena_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) {
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) {
|
wapp_intern inline void *mem_arena_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) {
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
return wapp_mem_arena_realloc(arena, ptr, old_size, new_size);
|
return wapp_mem_arena_realloc(arena, ptr, old_size, new_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
wapp_intern inline void *mem_arena_realloc_aligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
|
||||||
void *alloc_obj) {
|
void *alloc_obj) {
|
||||||
Arena *arena = (Arena *)alloc_obj;
|
Arena *arena = (Arena *)alloc_obj;
|
||||||
return wapp_mem_arena_realloc_aligned(arena, ptr, old_size, new_size, alignment);
|
return wapp_mem_arena_realloc_aligned(arena, ptr, old_size, new_size, alignment);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,14 @@
|
|||||||
BEGIN_C_LINKAGE
|
BEGIN_C_LINKAGE
|
||||||
#endif // !WAPP_PLATFORM_CPP
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
#define wapp_mem_arena_allocator_init(base_capacity) \
|
#define wapp_mem_arena_allocator_init(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, false))
|
||||||
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
#define wapp_mem_arena_allocator_init_commit(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, false))
|
||||||
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
#define wapp_mem_arena_allocator_init_zero(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, true))
|
||||||
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
#define wapp_mem_arena_allocator_init_commit_and_zero(base_capacity) \
|
||||||
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
(wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE | WAPP_MEM_ALLOC_COMMIT, true))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
* Wraps an Arena in an Allocator object. It attempts to initialise the Arena
|
||||||
|
|||||||
@@ -12,124 +12,124 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
|
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
|
||||||
if (!dst || !parts) {
|
if (!dst || !parts) {
|
||||||
return CPATH_JOIN_INVALID_ARGS;
|
return CPATH_JOIN_INVALID_ARGS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parts->node_count == 0) {
|
if (parts->node_count == 0) {
|
||||||
return CPATH_JOIN_EMPTY_PARTS;
|
return CPATH_JOIN_EMPTY_PARTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
Str8 separator = wapp_str8_buf(4);
|
Str8 separator = wapp_str8_buf(4);
|
||||||
wapp_str8_push_back(&separator, WAPP_PATH_SEP);
|
wapp_str8_push_back(&separator, WAPP_PATH_SEP);
|
||||||
|
|
||||||
u64 required_capacity = parts->node_count * separator.size + wapp_str8_list_total_size(parts);
|
u64 required_capacity = parts->node_count * separator.size + wapp_str8_list_total_size(parts);
|
||||||
if (dst->capacity < required_capacity) {
|
if (dst->capacity < required_capacity) {
|
||||||
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
|
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle first node
|
// Handle first node
|
||||||
const Str8Node *first_node = wapp_dbl_list_get(Str8, Str8Node, parts, 0);
|
const Str8Node *first_node = wapp_dbl_list_get(Str8, Str8Node, parts, 0);
|
||||||
wapp_str8_copy_str8_capped(dst, first_node->item);
|
wapp_str8_copy_str8_capped(dst, first_node->item);
|
||||||
|
|
||||||
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
|
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
|
||||||
// MSVC Spectre mitigation warnings
|
// MSVC Spectre mitigation warnings
|
||||||
const Str8Node *node = first_node;
|
const Str8Node *node = first_node;
|
||||||
u64 node_index = 1;
|
u64 node_index = 1;
|
||||||
b8 running = node_index < parts->node_count;
|
b8 running = node_index < parts->node_count;
|
||||||
while (running && node->next) {
|
while (running && node->next) {
|
||||||
node = node->next;
|
node = node->next;
|
||||||
if (node->item->size == 0) {
|
if (node->item->size == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dst->size > 0) {
|
if (dst->size > 0) {
|
||||||
char dst_last = wapp_str8_get(dst, dst->size - 1);
|
char dst_last = wapp_str8_get(dst, dst->size - 1);
|
||||||
char node_start = wapp_str8_get(node->item, 0);
|
char node_start = wapp_str8_get(node->item, 0);
|
||||||
b8 add_path_sep = dst_last != WAPP_PATH_SEP && node_start != WAPP_PATH_SEP;
|
b8 add_path_sep = dst_last != WAPP_PATH_SEP && node_start != WAPP_PATH_SEP;
|
||||||
|
|
||||||
if (add_path_sep) {
|
if (add_path_sep) {
|
||||||
wapp_str8_concat_capped(dst, &separator);
|
wapp_str8_concat_capped(dst, &separator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_str8_concat_capped(dst, node->item);
|
wapp_str8_concat_capped(dst, node->item);
|
||||||
|
|
||||||
++node_index;
|
++node_index;
|
||||||
running = node_index < parts->node_count;
|
running = node_index < parts->node_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CPATH_JOIN_SUCCESS;
|
return CPATH_JOIN_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) {
|
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels) {
|
||||||
Str8 *output = NULL;
|
Str8 *output = NULL;
|
||||||
if (!allocator || !path) {
|
if (!allocator || !path) {
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
b8 absolute = wapp_str8_get(path, 0) == WAPP_PATH_SEP;
|
b8 absolute = wapp_str8_get(path, 0) == WAPP_PATH_SEP;
|
||||||
Str8 separator = wapp_str8_buf(4);
|
Str8 separator = wapp_str8_buf(4);
|
||||||
wapp_str8_push_back(&separator, WAPP_PATH_SEP);
|
wapp_str8_push_back(&separator, WAPP_PATH_SEP);
|
||||||
|
|
||||||
if (path->size == 0) {
|
if (path->size == 0) {
|
||||||
output = wapp_str8_alloc_buf(allocator, 16);
|
output = wapp_str8_alloc_buf(allocator, 16);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_str8_push_back(output, absolute ? WAPP_PATH_SEP : '.');
|
wapp_str8_push_back(output, absolute ? WAPP_PATH_SEP : '.');
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (levels < 1) {
|
if (levels < 1) {
|
||||||
output = wapp_str8_alloc_str8(allocator, path);
|
output = wapp_str8_alloc_str8(allocator, path);
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator tmp_arena = wapp_mem_arena_allocator_init(MiB(8));
|
Allocator tmp_arena = wapp_mem_arena_allocator_init(MiB(8));
|
||||||
if (wapp_mem_allocator_invalid(&tmp_arena)) {
|
if (wapp_mem_allocator_invalid(&tmp_arena)) {
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Str8List *parts = wapp_str8_split(&tmp_arena, path, &separator);
|
Str8List *parts = wapp_str8_split(&tmp_arena, path, &separator);
|
||||||
if (!parts) {
|
if (!parts) {
|
||||||
goto RETURN_DIRUP;
|
goto RETURN_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (levels >= parts->node_count) {
|
if (levels >= parts->node_count) {
|
||||||
output = wapp_str8_alloc_buf(allocator, 16);
|
output = wapp_str8_alloc_buf(allocator, 16);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
goto LIST_CLEANUP_DIRUP;
|
goto LIST_CLEANUP_DIRUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_str8_push_back(output, absolute ? WAPP_PATH_SEP : '.');
|
wapp_str8_push_back(output, absolute ? WAPP_PATH_SEP : '.');
|
||||||
} else {
|
} else {
|
||||||
for (u64 i = 0; i < levels; ++i) {
|
for (u64 i = 0; i < levels; ++i) {
|
||||||
wapp_dbl_list_pop_back(Str8, Str8Node, parts);
|
wapp_dbl_list_pop_back(Str8, Str8Node, parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 alignment = sizeof(void *) * 2;
|
u64 alignment = sizeof(void *) * 2;
|
||||||
u64 alloc_size = wapp_str8_list_total_size(parts) + parts->node_count * separator.size;
|
u64 alloc_size = wapp_str8_list_total_size(parts) + parts->node_count * separator.size;
|
||||||
u64 modulo = alloc_size & (alignment - 1);
|
u64 modulo = alloc_size & (alignment - 1);
|
||||||
alloc_size += alignment - modulo;
|
alloc_size += alignment - modulo;
|
||||||
|
|
||||||
output = wapp_str8_alloc_buf(allocator, alloc_size);
|
output = wapp_str8_alloc_buf(allocator, alloc_size);
|
||||||
if (output) {
|
if (output) {
|
||||||
if (absolute) {
|
if (absolute) {
|
||||||
wapp_str8_push_back(output, WAPP_PATH_SEP);
|
wapp_str8_push_back(output, WAPP_PATH_SEP);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str8 *joined = wapp_str8_join(&tmp_arena, parts, &separator);
|
Str8 *joined = wapp_str8_join(&tmp_arena, parts, &separator);
|
||||||
if (joined) {
|
if (joined) {
|
||||||
wapp_str8_concat_capped(output, joined);
|
wapp_str8_concat_capped(output, joined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LIST_CLEANUP_DIRUP:
|
LIST_CLEANUP_DIRUP:
|
||||||
wapp_mem_arena_allocator_destroy(&tmp_arena);
|
wapp_mem_arena_allocator_destroy(&tmp_arena);
|
||||||
|
|
||||||
RETURN_DIRUP:
|
RETURN_DIRUP:
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ BEGIN_C_LINKAGE
|
|||||||
#define wapp_cpath_dirup(ALLOCATOR, PATH, COUNT) dirup(ALLOCATOR, PATH, COUNT)
|
#define wapp_cpath_dirup(ALLOCATOR, PATH, COUNT) dirup(ALLOCATOR, PATH, COUNT)
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
CPATH_JOIN_SUCCESS = 0,
|
CPATH_JOIN_SUCCESS = 0,
|
||||||
CPATH_JOIN_INVALID_ARGS,
|
CPATH_JOIN_INVALID_ARGS,
|
||||||
CPATH_JOIN_EMPTY_PARTS,
|
CPATH_JOIN_EMPTY_PARTS,
|
||||||
CPATH_JOIN_INSUFFICIENT_DST_CAPACITY,
|
CPATH_JOIN_INSUFFICIENT_DST_CAPACITY,
|
||||||
};
|
};
|
||||||
|
|
||||||
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts);
|
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts);
|
||||||
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels);
|
Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels);
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -9,101 +9,101 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
File *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
File *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
||||||
wapp_persist const char *modes[FILE_ACCESS_MODE_COUNT] = {
|
wapp_persist const char *modes[FILE_ACCESS_MODE_COUNT] = {
|
||||||
[WAPP_FA_MODE_R] = "r",
|
[WAPP_FA_MODE_R] = "r",
|
||||||
[WAPP_FA_MODE_W] = "w",
|
[WAPP_FA_MODE_W] = "w",
|
||||||
[WAPP_FA_MODE_A] = "a",
|
[WAPP_FA_MODE_A] = "a",
|
||||||
[WAPP_FA_MODE_R_EX] = "r+",
|
[WAPP_FA_MODE_R_EX] = "r+",
|
||||||
[WAPP_FA_MODE_W_EX] = "w+",
|
[WAPP_FA_MODE_W_EX] = "w+",
|
||||||
[WAPP_FA_MODE_A_EX] = "a+",
|
[WAPP_FA_MODE_A_EX] = "a+",
|
||||||
[WAPP_FA_MODE_RB] = "rb",
|
[WAPP_FA_MODE_RB] = "rb",
|
||||||
[WAPP_FA_MODE_WB] = "wb",
|
[WAPP_FA_MODE_WB] = "wb",
|
||||||
[WAPP_FA_MODE_AB] = "ab",
|
[WAPP_FA_MODE_AB] = "ab",
|
||||||
[WAPP_FA_MODE_RB_EX] = "rb+",
|
[WAPP_FA_MODE_RB_EX] = "rb+",
|
||||||
[WAPP_FA_MODE_WB_EX] = "wb+",
|
[WAPP_FA_MODE_WB_EX] = "wb+",
|
||||||
[WAPP_FA_MODE_AB_EX] = "ab+",
|
[WAPP_FA_MODE_AB_EX] = "ab+",
|
||||||
[WAPP_FA_MODE_WX] = "wx",
|
[WAPP_FA_MODE_WX] = "wx",
|
||||||
[WAPP_FA_MODE_WX_EX] = "wx+",
|
[WAPP_FA_MODE_WX_EX] = "wx+",
|
||||||
[WAPP_FA_MODE_WBX] = "wbx",
|
[WAPP_FA_MODE_WBX] = "wbx",
|
||||||
[WAPP_FA_MODE_WBX_EX] = "wbx+",
|
[WAPP_FA_MODE_WBX_EX] = "wbx+",
|
||||||
};
|
};
|
||||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||||
wapp_debug_assert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
|
wapp_debug_assert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
|
||||||
|
|
||||||
memset(tmp, 0, WAPP_PATH_MAX);
|
memset(tmp, 0, WAPP_PATH_MAX);
|
||||||
memcpy(tmp, filepath->buf, filepath->size);
|
memcpy(tmp, filepath->buf, filepath->size);
|
||||||
|
|
||||||
return fopen((const char *)tmp, modes[mode]);
|
return fopen((const char *)tmp, modes[mode]);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 wapp_file_get_current_position(File *file) {
|
u64 wapp_file_get_current_position(File *file) {
|
||||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||||
return (u64)ftell(file);
|
return (u64)ftell(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin) {
|
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin) {
|
||||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||||
// TODO (Abdelrahman): Revisit conversion to long
|
// TODO (Abdelrahman): Revisit conversion to long
|
||||||
return fseek(file, (long)offset, origin);
|
return fseek(file, (long)offset, origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 wapp_file_get_length(File *file) {
|
u64 wapp_file_get_length(File *file) {
|
||||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||||
|
|
||||||
u64 current = wapp_file_get_current_position(file);
|
u64 current = wapp_file_get_current_position(file);
|
||||||
|
|
||||||
wapp_file_seek(file, 0, WAPP_SEEK_END);
|
wapp_file_seek(file, 0, WAPP_SEEK_END);
|
||||||
|
|
||||||
u64 output = ftell(file);
|
u64 output = ftell(file);
|
||||||
|
|
||||||
// Restore position
|
// Restore position
|
||||||
wapp_file_seek(file, current, WAPP_SEEK_START);
|
wapp_file_seek(file, current, WAPP_SEEK_START);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count) {
|
u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count) {
|
||||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||||
"`dst_buf` and `file` should not be NULL.");
|
"`dst_buf` and `file` should not be NULL.");
|
||||||
|
|
||||||
u64 file_length = wapp_file_get_length(file);
|
u64 file_length = wapp_file_get_length(file);
|
||||||
u64 item_size = dst_buf->item_size;
|
u64 item_size = dst_buf->item_size;
|
||||||
u64 dst_byte_capacity = dst_buf->capacity * item_size;
|
u64 dst_byte_capacity = dst_buf->capacity * item_size;
|
||||||
u64 req_byte_count = item_count * item_size;
|
u64 req_byte_count = item_count * item_size;
|
||||||
u64 copy_byte_count = 0;
|
u64 copy_byte_count = 0;
|
||||||
|
|
||||||
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
|
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
|
||||||
copy_byte_count = req_byte_count;
|
copy_byte_count = req_byte_count;
|
||||||
} else {
|
} else {
|
||||||
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
|
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 count = fread(dst_buf->items, sizeof(u8), copy_byte_count, file);
|
u64 count = fread(dst_buf->items, sizeof(u8), copy_byte_count, file);
|
||||||
if (ferror(file)) { return 0; }
|
if (ferror(file)) { return 0; }
|
||||||
|
|
||||||
dst_buf->count = count / item_size;
|
dst_buf->count = count / item_size;
|
||||||
|
|
||||||
return dst_buf->count;
|
return dst_buf->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 wapp_file_write(const GenericArray *src_buf, File *file, u64 item_count) {
|
u64 wapp_file_write(const GenericArray *src_buf, File *file, u64 item_count) {
|
||||||
wapp_debug_assert(src_buf != NULL && file != NULL,
|
wapp_debug_assert(src_buf != NULL && file != NULL,
|
||||||
"`src_buf` and `file` should not be NULL.");
|
"`src_buf` and `file` should not be NULL.");
|
||||||
|
|
||||||
u64 item_size = src_buf->item_size;
|
u64 item_size = src_buf->item_size;
|
||||||
u64 src_byte_count = src_buf->count * item_size;
|
u64 src_byte_count = src_buf->count * item_size;
|
||||||
u64 req_byte_count = item_count * 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;
|
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
|
||||||
|
|
||||||
return fwrite(src_buf->items, sizeof(u8), to_copy, file);
|
return fwrite(src_buf->items, sizeof(u8), to_copy, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 wapp_file_flush(File *file) {
|
i32 wapp_file_flush(File *file) {
|
||||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||||
return fflush(file);
|
return fflush(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 wapp_file_close(File *file) {
|
i32 wapp_file_close(File *file) {
|
||||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||||
return fclose(file);
|
return fclose(file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,40 +14,40 @@ BEGIN_C_LINKAGE
|
|||||||
typedef FILE File;
|
typedef FILE File;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAPP_FA_MODE_R, // Equivalent to r
|
WAPP_FA_MODE_R, // Equivalent to r
|
||||||
WAPP_FA_MODE_W, // Equivalent to w
|
WAPP_FA_MODE_W, // Equivalent to w
|
||||||
WAPP_FA_MODE_A, // Equivalent to a
|
WAPP_FA_MODE_A, // Equivalent to a
|
||||||
WAPP_FA_MODE_R_EX, // Equivalent to r+
|
WAPP_FA_MODE_R_EX, // Equivalent to r+
|
||||||
WAPP_FA_MODE_W_EX, // Equivalent to w+
|
WAPP_FA_MODE_W_EX, // Equivalent to w+
|
||||||
WAPP_FA_MODE_A_EX, // Equivalent to a+
|
WAPP_FA_MODE_A_EX, // Equivalent to a+
|
||||||
WAPP_FA_MODE_RB, // Equivalent to rb
|
WAPP_FA_MODE_RB, // Equivalent to rb
|
||||||
WAPP_FA_MODE_WB, // Equivalent to wb
|
WAPP_FA_MODE_WB, // Equivalent to wb
|
||||||
WAPP_FA_MODE_AB, // Equivalent to ab
|
WAPP_FA_MODE_AB, // Equivalent to ab
|
||||||
WAPP_FA_MODE_RB_EX, // Equivalent to rb+
|
WAPP_FA_MODE_RB_EX, // Equivalent to rb+
|
||||||
WAPP_FA_MODE_WB_EX, // Equivalent to wb+
|
WAPP_FA_MODE_WB_EX, // Equivalent to wb+
|
||||||
WAPP_FA_MODE_AB_EX, // Equivalent to ab+
|
WAPP_FA_MODE_AB_EX, // Equivalent to ab+
|
||||||
WAPP_FA_MODE_WX, // Equivalent to wx
|
WAPP_FA_MODE_WX, // Equivalent to wx
|
||||||
WAPP_FA_MODE_WX_EX, // Equivalent to wx+
|
WAPP_FA_MODE_WX_EX, // Equivalent to wx+
|
||||||
WAPP_FA_MODE_WBX, // Equivalent to wbx
|
WAPP_FA_MODE_WBX, // Equivalent to wbx
|
||||||
WAPP_FA_MODE_WBX_EX, // Equivalent to wbx+
|
WAPP_FA_MODE_WBX_EX, // Equivalent to wbx+
|
||||||
|
|
||||||
FILE_ACCESS_MODE_COUNT,
|
FILE_ACCESS_MODE_COUNT,
|
||||||
} FileAccessMode;
|
} FileAccessMode;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAPP_SEEK_START = SEEK_SET,
|
WAPP_SEEK_START = SEEK_SET,
|
||||||
WAPP_SEEK_CURRENT = SEEK_CUR,
|
WAPP_SEEK_CURRENT = SEEK_CUR,
|
||||||
WAPP_SEEK_END = SEEK_END,
|
WAPP_SEEK_END = SEEK_END,
|
||||||
} FileSeekOrigin;
|
} FileSeekOrigin;
|
||||||
|
|
||||||
File *wapp_file_open(Str8RO *filename, FileAccessMode mode);
|
File *wapp_file_open(Str8RO *filename, FileAccessMode mode);
|
||||||
u64 wapp_file_get_current_position(File *file);
|
u64 wapp_file_get_current_position(File *file);
|
||||||
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin);
|
i32 wapp_file_seek(File *file, u64 offset, FileSeekOrigin origin);
|
||||||
u64 wapp_file_get_length(File *file);
|
u64 wapp_file_get_length(File *file);
|
||||||
u64 wapp_file_read(GenericArray *dst_buf, File *file, u64 item_count);
|
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);
|
u64 wapp_file_write(const GenericArray *src_buf, File *file, u64 item_count);
|
||||||
i32 wapp_file_flush(File *file);
|
i32 wapp_file_flush(File *file);
|
||||||
i32 wapp_file_close(File *file);
|
i32 wapp_file_close(File *file);
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -21,81 +21,81 @@ wapp_intern inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_han
|
|||||||
wapp_intern inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf);
|
wapp_intern inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf);
|
||||||
|
|
||||||
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd) {
|
CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd) {
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS);
|
return CMD_NO_EXIT(SHELL_ERR_INVALID_ARGS);
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator arena = wapp_mem_arena_allocator_init(KiB(500));
|
Allocator arena = wapp_mem_arena_allocator_init(KiB(500));
|
||||||
|
|
||||||
Str8 *cmd_str = wapp_str8_join(&arena, cmd, &wapp_str8_lit_ro(" "));
|
Str8 *cmd_str = wapp_str8_join(&arena, cmd, &wapp_str8_lit_ro(" "));
|
||||||
if (!cmd_str) {
|
if (!cmd_str) {
|
||||||
wapp_mem_arena_allocator_destroy(&arena);
|
wapp_mem_arena_allocator_destroy(&arena);
|
||||||
return CMD_NO_EXIT(SHELL_ERR_ALLOCATION_FAIL);
|
return CMD_NO_EXIT(SHELL_ERR_ALLOCATION_FAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect output
|
// Redirect output
|
||||||
cmd_str = wapp_str8_alloc_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1"));
|
cmd_str = wapp_str8_alloc_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1"));
|
||||||
|
|
||||||
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
|
CMDResult output = execute_command(cmd_str, out_handling, out_buf);
|
||||||
|
|
||||||
wapp_mem_arena_allocator_destroy(&arena);
|
wapp_mem_arena_allocator_destroy(&arena);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf) {
|
wapp_intern inline CMDResult execute_command(Str8RO *cmd, CMDOutHandling out_handling, Str8 *out_buf) {
|
||||||
char cmd_buf[CMD_BUF_LEN] = {0};
|
char cmd_buf[CMD_BUF_LEN] = {0};
|
||||||
wapp_str8_copy_to_cstr(cmd_buf, cmd, CMD_BUF_LEN);
|
wapp_str8_copy_to_cstr(cmd_buf, cmd, CMD_BUF_LEN);
|
||||||
|
|
||||||
FILE *fp = wapp_shell_utils_popen(cmd_buf, "r");
|
FILE *fp = wapp_shell_utils_popen(cmd_buf, "r");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
|
return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
CMDResult output;
|
CMDResult output;
|
||||||
|
|
||||||
CMDError err = get_command_output(fp, out_handling, out_buf);
|
CMDError err = get_command_output(fp, out_handling, out_buf);
|
||||||
if (err > SHELL_ERR_NO_ERROR) {
|
if (err > SHELL_ERR_NO_ERROR) {
|
||||||
output = CMD_NO_EXIT(err);
|
output = CMD_NO_EXIT(err);
|
||||||
goto EXECUTE_COMMAND_CLOSE;
|
goto EXECUTE_COMMAND_CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 st = EXIT_SUCCESS;
|
i32 st = EXIT_SUCCESS;
|
||||||
err = get_output_status(fp, &st);
|
err = get_output_status(fp, &st);
|
||||||
if (err > SHELL_ERR_NO_ERROR) {
|
if (err > SHELL_ERR_NO_ERROR) {
|
||||||
output = CMD_NO_EXIT(err);
|
output = CMD_NO_EXIT(err);
|
||||||
goto EXECUTE_COMMAND_CLOSE;
|
goto EXECUTE_COMMAND_CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process is already closed in get_output_status
|
// Process is already closed in get_output_status
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
|
|
||||||
output = (CMDResult){
|
output = (CMDResult){
|
||||||
.exited = true,
|
.exited = true,
|
||||||
.exit_code = st,
|
.exit_code = st,
|
||||||
.error = SHELL_ERR_NO_ERROR,
|
.error = SHELL_ERR_NO_ERROR,
|
||||||
};
|
};
|
||||||
|
|
||||||
EXECUTE_COMMAND_CLOSE:
|
EXECUTE_COMMAND_CLOSE:
|
||||||
if (fp) {
|
if (fp) {
|
||||||
wapp_shell_utils_pclose(fp);
|
wapp_shell_utils_pclose(fp);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf) {
|
wapp_intern inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, Str8 *out_buf) {
|
||||||
Str8 out = wapp_str8_buf(OUT_BUF_LEN);
|
Str8 out = wapp_str8_buf(OUT_BUF_LEN);
|
||||||
|
|
||||||
out.size = fread((void *)out.buf, sizeof(u8), out.capacity, fp);
|
out.size = fread((void *)out.buf, sizeof(u8), out.capacity, fp);
|
||||||
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
|
||||||
if (out.size >= out_buf->capacity) {
|
if (out.size >= out_buf->capacity) {
|
||||||
return SHELL_ERR_OUT_BUF_FULL;
|
return SHELL_ERR_OUT_BUF_FULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_str8_concat_capped(out_buf, &out);
|
wapp_str8_concat_capped(out_buf, &out);
|
||||||
} else if (out_handling == SHELL_OUTPUT_PRINT) {
|
} else if (out_handling == SHELL_OUTPUT_PRINT) {
|
||||||
printf(WAPP_STR8_SPEC, wapp_str8_varg(out));
|
printf(WAPP_STR8_SPEC, wapp_str8_varg(out));
|
||||||
}
|
}
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
return SHELL_ERR_NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,29 +11,29 @@ BEGIN_C_LINKAGE
|
|||||||
#endif // !WAPP_PLATFORM_CPP
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SHELL_OUTPUT_DISCARD,
|
SHELL_OUTPUT_DISCARD,
|
||||||
SHELL_OUTPUT_PRINT,
|
SHELL_OUTPUT_PRINT,
|
||||||
SHELL_OUTPUT_CAPTURE,
|
SHELL_OUTPUT_CAPTURE,
|
||||||
} CMDOutHandling;
|
} CMDOutHandling;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SHELL_ERR_NO_ERROR,
|
SHELL_ERR_NO_ERROR,
|
||||||
SHELL_ERR_INVALID_ARGS,
|
SHELL_ERR_INVALID_ARGS,
|
||||||
SHELL_ERR_ALLOCATION_FAIL,
|
SHELL_ERR_ALLOCATION_FAIL,
|
||||||
SHELL_ERR_PROC_START_FAIL,
|
SHELL_ERR_PROC_START_FAIL,
|
||||||
SHELL_ERR_OUT_BUF_FULL,
|
SHELL_ERR_OUT_BUF_FULL,
|
||||||
SHELL_ERR_PROC_EXIT_FAIL,
|
SHELL_ERR_PROC_EXIT_FAIL,
|
||||||
} CMDError;
|
} CMDError;
|
||||||
|
|
||||||
typedef struct commander_result CMDResult;
|
typedef struct commander_result CMDResult;
|
||||||
struct commander_result {
|
struct commander_result {
|
||||||
i32 exit_code;
|
i32 exit_code;
|
||||||
CMDError error;
|
CMDError error;
|
||||||
b8 exited;
|
b8 exited;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
#include "../../../../common/misc/misc_utils.h"
|
#include "../../../../common/misc/misc_utils.h"
|
||||||
wapp_misc_utils_padding_size(sizeof(b8) + sizeof(i32) + sizeof(CMDError));
|
wapp_misc_utils_padding_size(sizeof(b8) + sizeof(i32) + sizeof(CMDError));
|
||||||
#endif // !WAPP_PLATFORM_WINDOWS
|
#endif // !WAPP_PLATFORM_WINDOWS
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,15 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
||||||
*status_out = wapp_shell_utils_pclose(fp);
|
*status_out = wapp_shell_utils_pclose(fp);
|
||||||
|
|
||||||
if (!WIFEXITED(*status_out)) {
|
if (!WIFEXITED(*status_out)) {
|
||||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
return SHELL_ERR_PROC_EXIT_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
*status_out = WEXITSTATUS(*status_out);
|
*status_out = WEXITSTATUS(*status_out);
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
return SHELL_ERR_NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !WAPP_PLATFORM_POSIX
|
#endif // !WAPP_PLATFORM_POSIX
|
||||||
|
|||||||
@@ -10,15 +10,15 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
CMDError get_output_status(FILE *fp, i32 *status_out) {
|
||||||
if (!feof(fp)) {
|
if (!feof(fp)) {
|
||||||
// Ensure process is closed on failure
|
// Ensure process is closed on failure
|
||||||
wapp_shell_utils_pclose(fp);
|
wapp_shell_utils_pclose(fp);
|
||||||
return SHELL_ERR_PROC_EXIT_FAIL;
|
return SHELL_ERR_PROC_EXIT_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
*status_out = wapp_shell_utils_pclose(fp);
|
*status_out = wapp_shell_utils_pclose(fp);
|
||||||
|
|
||||||
return SHELL_ERR_NO_ERROR;
|
return SHELL_ERR_NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !WAPP_PLATFORM_WINDOWS
|
#endif // !WAPP_PLATFORM_WINDOWS
|
||||||
|
|||||||
@@ -10,27 +10,27 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
wapp_intern Str8RO colours[COUNT_TERM_COLOUR] = {
|
wapp_intern Str8RO colours[COUNT_TERM_COLOUR] = {
|
||||||
[WAPP_TERM_COLOUR_FG_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[30m"),
|
[WAPP_TERM_COLOUR_FG_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[30m"),
|
||||||
[WAPP_TERM_COLOUR_FG_RED] = wapp_str8_lit_ro_initialiser_list("\033[31m"),
|
[WAPP_TERM_COLOUR_FG_RED] = wapp_str8_lit_ro_initialiser_list("\033[31m"),
|
||||||
[WAPP_TERM_COLOUR_FG_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[32m"),
|
[WAPP_TERM_COLOUR_FG_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[32m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[34m"),
|
[WAPP_TERM_COLOUR_FG_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[34m"),
|
||||||
[WAPP_TERM_COLOUR_FG_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[36m"),
|
[WAPP_TERM_COLOUR_FG_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[36m"),
|
||||||
[WAPP_TERM_COLOUR_FG_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[35m"),
|
[WAPP_TERM_COLOUR_FG_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[35m"),
|
||||||
[WAPP_TERM_COLOUR_FG_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[33m"),
|
[WAPP_TERM_COLOUR_FG_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[33m"),
|
||||||
[WAPP_TERM_COLOUR_FG_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[37m"),
|
[WAPP_TERM_COLOUR_FG_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[37m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[90m"),
|
[WAPP_TERM_COLOUR_FG_BR_BLACK] = wapp_str8_lit_ro_initialiser_list("\033[90m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_RED] = wapp_str8_lit_ro_initialiser_list("\033[91m"),
|
[WAPP_TERM_COLOUR_FG_BR_RED] = wapp_str8_lit_ro_initialiser_list("\033[91m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[92m"),
|
[WAPP_TERM_COLOUR_FG_BR_GREEN] = wapp_str8_lit_ro_initialiser_list("\033[92m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[94m"),
|
[WAPP_TERM_COLOUR_FG_BR_BLUE] = wapp_str8_lit_ro_initialiser_list("\033[94m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[96m"),
|
[WAPP_TERM_COLOUR_FG_BR_CYAN] = wapp_str8_lit_ro_initialiser_list("\033[96m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[95m"),
|
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = wapp_str8_lit_ro_initialiser_list("\033[95m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[93m"),
|
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = wapp_str8_lit_ro_initialiser_list("\033[93m"),
|
||||||
[WAPP_TERM_COLOUR_FG_BR_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[97m"),
|
[WAPP_TERM_COLOUR_FG_BR_WHITE] = wapp_str8_lit_ro_initialiser_list("\033[97m"),
|
||||||
[WAPP_TERM_COLOUR_CLEAR] = wapp_str8_lit_ro_initialiser_list("\033[0m"),
|
[WAPP_TERM_COLOUR_CLEAR] = wapp_str8_lit_ro_initialiser_list("\033[0m"),
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_coloured_text(Str8RO *text, TerminalColour colour) {
|
void print_coloured_text(Str8RO *text, TerminalColour colour) {
|
||||||
printf(WAPP_STR8_SPEC WAPP_STR8_SPEC, wapp_str8_varg(colours[colour]), wapp_str8_varg((*text)));
|
printf(WAPP_STR8_SPEC WAPP_STR8_SPEC, wapp_str8_varg(colours[colour]), wapp_str8_varg((*text)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !WAPP_PLATFORM_POSIX
|
#endif // !WAPP_PLATFORM_POSIX
|
||||||
|
|||||||
@@ -5,14 +5,14 @@
|
|||||||
#include "../../../base/strings/str8/str8.h"
|
#include "../../../base/strings/str8/str8.h"
|
||||||
|
|
||||||
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour) {
|
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour) {
|
||||||
if (colour < WAPP_TERM_COLOUR_FG_BLACK || colour > WAPP_TERM_COLOUR_FG_BR_WHITE) {
|
if (colour < WAPP_TERM_COLOUR_FG_BLACK || colour > WAPP_TERM_COLOUR_FG_BR_WHITE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_coloured_text(text, colour);
|
print_coloured_text(text, colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_shell_termcolour_clear_colour(void) {
|
void wapp_shell_termcolour_clear_colour(void) {
|
||||||
Str8RO empty = wapp_str8_lit_ro("");
|
Str8RO empty = wapp_str8_lit_ro("");
|
||||||
print_coloured_text(&empty, WAPP_TERM_COLOUR_CLEAR);
|
print_coloured_text(&empty, WAPP_TERM_COLOUR_CLEAR);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,25 +11,25 @@ BEGIN_C_LINKAGE
|
|||||||
#endif // !WAPP_PLATFORM_CPP
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAPP_TERM_COLOUR_FG_BLACK,
|
WAPP_TERM_COLOUR_FG_BLACK,
|
||||||
WAPP_TERM_COLOUR_FG_RED,
|
WAPP_TERM_COLOUR_FG_RED,
|
||||||
WAPP_TERM_COLOUR_FG_GREEN,
|
WAPP_TERM_COLOUR_FG_GREEN,
|
||||||
WAPP_TERM_COLOUR_FG_BLUE,
|
WAPP_TERM_COLOUR_FG_BLUE,
|
||||||
WAPP_TERM_COLOUR_FG_CYAN,
|
WAPP_TERM_COLOUR_FG_CYAN,
|
||||||
WAPP_TERM_COLOUR_FG_MAGENTA,
|
WAPP_TERM_COLOUR_FG_MAGENTA,
|
||||||
WAPP_TERM_COLOUR_FG_YELLOW,
|
WAPP_TERM_COLOUR_FG_YELLOW,
|
||||||
WAPP_TERM_COLOUR_FG_WHITE,
|
WAPP_TERM_COLOUR_FG_WHITE,
|
||||||
WAPP_TERM_COLOUR_FG_BR_BLACK,
|
WAPP_TERM_COLOUR_FG_BR_BLACK,
|
||||||
WAPP_TERM_COLOUR_FG_BR_RED,
|
WAPP_TERM_COLOUR_FG_BR_RED,
|
||||||
WAPP_TERM_COLOUR_FG_BR_GREEN,
|
WAPP_TERM_COLOUR_FG_BR_GREEN,
|
||||||
WAPP_TERM_COLOUR_FG_BR_BLUE,
|
WAPP_TERM_COLOUR_FG_BR_BLUE,
|
||||||
WAPP_TERM_COLOUR_FG_BR_CYAN,
|
WAPP_TERM_COLOUR_FG_BR_CYAN,
|
||||||
WAPP_TERM_COLOUR_FG_BR_MAGENTA,
|
WAPP_TERM_COLOUR_FG_BR_MAGENTA,
|
||||||
WAPP_TERM_COLOUR_FG_BR_YELLOW,
|
WAPP_TERM_COLOUR_FG_BR_YELLOW,
|
||||||
WAPP_TERM_COLOUR_FG_BR_WHITE,
|
WAPP_TERM_COLOUR_FG_BR_WHITE,
|
||||||
WAPP_TERM_COLOUR_CLEAR,
|
WAPP_TERM_COLOUR_CLEAR,
|
||||||
|
|
||||||
COUNT_TERM_COLOUR,
|
COUNT_TERM_COLOUR,
|
||||||
} TerminalColour;
|
} TerminalColour;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
|||||||
@@ -15,59 +15,59 @@
|
|||||||
|
|
||||||
typedef struct termcolour_data TermcolourData;
|
typedef struct termcolour_data TermcolourData;
|
||||||
struct termcolour_data {
|
struct termcolour_data {
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
WORD default_colour;
|
WORD default_colour;
|
||||||
WORD current_colour;
|
WORD current_colour;
|
||||||
|
|
||||||
wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD));
|
wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD));
|
||||||
};
|
};
|
||||||
|
|
||||||
wapp_intern void init_data(TermcolourData *data);
|
wapp_intern void init_data(TermcolourData *data);
|
||||||
|
|
||||||
wapp_intern WORD colours[COUNT_TERM_COLOUR] = {
|
wapp_intern WORD colours[COUNT_TERM_COLOUR] = {
|
||||||
[WAPP_TERM_COLOUR_FG_BLACK] = 0,
|
[WAPP_TERM_COLOUR_FG_BLACK] = 0,
|
||||||
[WAPP_TERM_COLOUR_FG_RED] = FOREGROUND_RED,
|
[WAPP_TERM_COLOUR_FG_RED] = FOREGROUND_RED,
|
||||||
[WAPP_TERM_COLOUR_FG_GREEN] = FOREGROUND_GREEN,
|
[WAPP_TERM_COLOUR_FG_GREEN] = FOREGROUND_GREEN,
|
||||||
[WAPP_TERM_COLOUR_FG_BLUE] = FOREGROUND_BLUE,
|
[WAPP_TERM_COLOUR_FG_BLUE] = FOREGROUND_BLUE,
|
||||||
[WAPP_TERM_COLOUR_FG_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE,
|
[WAPP_TERM_COLOUR_FG_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE,
|
||||||
[WAPP_TERM_COLOUR_FG_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE,
|
[WAPP_TERM_COLOUR_FG_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE,
|
||||||
[WAPP_TERM_COLOUR_FG_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN,
|
[WAPP_TERM_COLOUR_FG_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN,
|
||||||
[WAPP_TERM_COLOUR_FG_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
[WAPP_TERM_COLOUR_FG_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_BLACK] = FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_BLACK] = FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_RED] = FOREGROUND_RED | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_GREEN] = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_BLUE] = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_CYAN] = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_MAGENTA] = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_YELLOW] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
|
||||||
[WAPP_TERM_COLOUR_FG_BR_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
[WAPP_TERM_COLOUR_FG_BR_WHITE] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_coloured_text(Str8RO *text, TerminalColour colour) {
|
void print_coloured_text(Str8RO *text, TerminalColour colour) {
|
||||||
wapp_persist TermcolourData data = {0};
|
wapp_persist TermcolourData data = {0};
|
||||||
if (data.handle == 0) {
|
if (data.handle == 0) {
|
||||||
init_data(&data);
|
init_data(&data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (colour == WAPP_TERM_COLOUR_CLEAR) {
|
if (colour == WAPP_TERM_COLOUR_CLEAR) {
|
||||||
data.current_colour = data.default_colour;
|
data.current_colour = data.default_colour;
|
||||||
} else {
|
} else {
|
||||||
data.current_colour = colours[colour];
|
data.current_colour = colours[colour];
|
||||||
}
|
}
|
||||||
|
|
||||||
SetConsoleTextAttribute(data.handle, data.current_colour);
|
SetConsoleTextAttribute(data.handle, data.current_colour);
|
||||||
printf(WAPP_STR8_SPEC, wapp_str8_varg((*text)));
|
printf(WAPP_STR8_SPEC, wapp_str8_varg((*text)));
|
||||||
}
|
}
|
||||||
|
|
||||||
wapp_intern void init_data(TermcolourData *data) {
|
wapp_intern void init_data(TermcolourData *data) {
|
||||||
// create handle
|
// create handle
|
||||||
data->handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
data->handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
// get console colour information
|
// get console colour information
|
||||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||||
GetConsoleScreenBufferInfo(data->handle, &csbi);
|
GetConsoleScreenBufferInfo(data->handle, &csbi);
|
||||||
data->default_colour = csbi.wAttributes;
|
data->default_colour = csbi.wAttributes;
|
||||||
data->current_colour = data->default_colour;
|
data->current_colour = data->default_colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !WAPP_PLATFORM_WINDOWS
|
#endif // !WAPP_PLATFORM_WINDOWS
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ BEGIN_C_LINKAGE
|
|||||||
#endif // !WAPP_PLATFORM_CPP
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_WINDOWS
|
#ifdef WAPP_PLATFORM_WINDOWS
|
||||||
#define wapp_shell_utils_popen _popen
|
#define wapp_shell_utils_popen _popen
|
||||||
#define wapp_shell_utils_pclose _pclose
|
#define wapp_shell_utils_pclose _pclose
|
||||||
#else
|
#else
|
||||||
#define wapp_shell_utils_popen popen
|
#define wapp_shell_utils_popen popen
|
||||||
#define wapp_shell_utils_pclose pclose
|
#define wapp_shell_utils_pclose pclose
|
||||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
|||||||
Reference in New Issue
Block a user