Rename cpath

This commit is contained in:
2026-06-26 17:21:41 +01:00
parent 273dbf4535
commit c29466b863
4 changed files with 76 additions and 78 deletions
+18 -20
View File
@@ -8,28 +8,26 @@
#include "../../base/mem/allocator/mem_allocator.h"
#include "../../base/strings/str8/str8.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
u32 wapp_cpath_join_path(WpStr8 *dst, const WpStr8List *parts) {
u32 wpCpathJoinPath(WpStr8 *dst, const WpStr8List *parts) {
if (!dst || !parts) {
return CPATH_JOIN_INVALID_ARGS;
return WP_CPATH_JOIN_RESULT_INVALID_ARGS;
}
if (parts->node_count == 0) {
return CPATH_JOIN_EMPTY_PARTS;
return WP_CPATH_JOIN_RESULT_EMPTY_PARTS;
}
WpStr8 separator = wpStr8Buf(4);
wpStr8PushBack(&separator, WAPP_PATH_SEP);
wpStr8PushBack(&separator, WP_PATH_SEP);
u64 required_capacity = parts->node_count * separator.size + wpStr8ListTotalSize(parts);
if (dst->capacity < required_capacity) {
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
return WP_CPATH_JOIN_RESULT_INSUFFICIENT_DST_CAPACITY;
}
// Handle first node
WpStr8 *first_node = wapp_dbl_list_get(WpStr8, parts, 0);
WpStr8 *first_node = wpDblListGet(WpStr8, parts, 0);
wpStr8CopyStr8Capped(dst, first_node);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
@@ -38,7 +36,7 @@ u32 wapp_cpath_join_path(WpStr8 *dst, const WpStr8List *parts) {
u64 node_index = 1;
b8 running = node_index < parts->node_count;
while (running) {
node = wapp_dbl_list_get(WpStr8, parts, node_index);
node = wpDblListGet(WpStr8, parts, node_index);
if (node->size == 0) {
goto CPATH_JOIN_LOOP_END;
}
@@ -46,7 +44,7 @@ u32 wapp_cpath_join_path(WpStr8 *dst, const WpStr8List *parts) {
if (dst->size > 0) {
char dst_last = wpStr8Get(dst, dst->size - 1);
char node_start = wpStr8Get(node, 0);
b8 add_path_sep = dst_last != WAPP_PATH_SEP && node_start != WAPP_PATH_SEP;
b8 add_path_sep = dst_last != WP_PATH_SEP && node_start != WP_PATH_SEP;
if (add_path_sep) {
wpStr8ConcatCapped(dst, &separator);
@@ -60,18 +58,18 @@ CPATH_JOIN_LOOP_END:
running = node_index < parts->node_count;
}
return CPATH_JOIN_SUCCESS;
return WP_CPATH_JOIN_RESULT_SUCCESS;
}
WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
WpStr8 *_dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
WpStr8 *output = NULL;
if (!allocator || !path) {
goto RETURN_DIRUP;
}
b8 absolute = wpStr8Get(path, 0) == WAPP_PATH_SEP;
b8 absolute = wpStr8Get(path, 0) == WP_PATH_SEP;
WpStr8 separator = wpStr8Buf(4);
wpStr8PushBack(&separator, WAPP_PATH_SEP);
wpStr8PushBack(&separator, WP_PATH_SEP);
if (path->size == 0) {
output = wpStr8AllocBuf(allocator, 16);
@@ -79,7 +77,7 @@ WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
goto RETURN_DIRUP;
}
wpStr8PushBack(output, absolute ? WAPP_PATH_SEP : '.');
wpStr8PushBack(output, absolute ? WP_PATH_SEP : '.');
goto RETURN_DIRUP;
}
@@ -88,7 +86,7 @@ WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
goto RETURN_DIRUP;
}
WpAllocator tmp_arena = wapp_mem_arena_allocator_init(MiB(8));
WpAllocator tmp_arena = wpMemArenaAllocatorInit(MiB(8));
if (wpMemAllocatorInvalid(&tmp_arena)) {
goto RETURN_DIRUP;
}
@@ -104,10 +102,10 @@ WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
goto LIST_CLEANUP_DIRUP;
}
wpStr8PushBack(output, absolute ? WAPP_PATH_SEP : '.');
wpStr8PushBack(output, absolute ? WP_PATH_SEP : '.');
} else {
for (u64 i = 0; i < levels; ++i) {
wapp_dbl_list_pop_back(WpStr8, parts);
wpDblListPopBack(WpStr8, parts);
}
u64 alignment = sizeof(void *) * 2;
@@ -118,7 +116,7 @@ WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
output = wpStr8AllocBuf(allocator, alloc_size);
if (output) {
if (absolute) {
wpStr8PushBack(output, WAPP_PATH_SEP);
wpStr8PushBack(output, WP_PATH_SEP);
}
WpStr8 *joined = wpStr8Join(&tmp_arena, parts, &separator);
@@ -129,7 +127,7 @@ WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels) {
}
LIST_CLEANUP_DIRUP:
wapp_mem_arena_allocator_destroy(&tmp_arena);
wpMemArenaAllocatorDestroy(&tmp_arena);
RETURN_DIRUP:
return output;
+14 -14
View File
@@ -14,29 +14,29 @@ BEGIN_C_LINKAGE
#ifdef WP_PLATFORM_POSIX
#include <limits.h>
#define WAPP_PATH_SEP '/'
#define WAPP_PATH_MAX PATH_MAX
#define WP_PATH_SEP '/'
#define WP_PATH_MAX PATH_MAX
#elif defined(WP_PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define WAPP_PATH_SEP '\\'
#define WAPP_PATH_MAX MAX_PATH
#define WP_PATH_SEP '\\'
#define WP_PATH_MAX MAX_PATH
#else
#error "Unrecognised platform"
#endif
#define wapp_cpath_dirname(ALLOCATOR, PATH) dirup(ALLOCATOR, PATH, 1)
#define wapp_cpath_dirup(ALLOCATOR, PATH, COUNT) dirup(ALLOCATOR, PATH, COUNT)
#define wpCpathDirname(ALLOCATOR, PATH) _dirup(ALLOCATOR, PATH, 1)
#define wpCpathDirup(ALLOCATOR, PATH, COUNT) _dirup(ALLOCATOR, PATH, COUNT)
enum {
CPATH_JOIN_SUCCESS = 0,
CPATH_JOIN_INVALID_ARGS,
CPATH_JOIN_EMPTY_PARTS,
CPATH_JOIN_INSUFFICIENT_DST_CAPACITY,
};
typedef enum {
WP_CPATH_JOIN_RESULT_SUCCESS = 0,
WP_CPATH_JOIN_RESULT_INVALID_ARGS,
WP_CPATH_JOIN_RESULT_EMPTY_PARTS,
WP_CPATH_JOIN_RESULT_INSUFFICIENT_DST_CAPACITY,
} WpCpathJoinResult;
u32 wapp_cpath_join_path(WpStr8 *dst, const WpStr8List *parts);
WpStr8 *dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels);
WpCpathJoinResult wpCpathJoinPath(WpStr8 *dst, const WpStr8List *parts);
WpStr8 *_dirup(const WpAllocator *allocator, WpStr8RO *path, u64 levels);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE