Switch wapp_cpath_join_path to use Str8

This commit is contained in:
2025-02-22 17:48:25 +00:00
parent ba5e902a1d
commit ed4ec54c7a
4 changed files with 107 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
#include "cpath.h"
#include "aliases.h"
#include "str8.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
@@ -7,16 +8,48 @@
void join_root_and_leaf(const char *root, const char *leaf, char *dst);
void join_path(char *dst, u64 count, ...) {
va_list args;
va_start(args, count);
for (u64 i = 0; i < count; ++i) {
join_root_and_leaf(dst, va_arg(args, const char *), dst);
u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts) {
if (!dst || !parts) {
return CPATH_JOIN_INVALID_ARGS;
}
va_end(args);
if (parts->node_count == 0) {
return CPATH_JOIN_EMPTY_PARTS;
}
Str8 separator = wapp_str8_buf(4);
wapp_str8_push_back(&separator, PATH_SEP);
u64 required_capacity = parts->node_count * separator.size + parts->total_size;
if (dst->capacity < required_capacity) {
return CPATH_JOIN_INSUFFICIENT_DST_CAPACITY;
}
// Handle first node
const Str8Node *first_node = wapp_str8_list_get(parts, 0);
wapp_str8_copy_str8_capped(dst, first_node->string);
const Str8Node *node = first_node;
for (u64 i = 1; i < parts->node_count; ++i) {
node = node->next;
if (node->string->size == 0) {
continue;
}
if (dst->size > 0) {
char dst_last = wapp_str8_get(dst, dst->size - 1);
char node_start = wapp_str8_get(node->string, 0);
bool add_path_sep = dst_last != PATH_SEP && node_start != PATH_SEP;
if (add_path_sep) {
wapp_str8_concat_capped(dst, &separator);
}
}
wapp_str8_concat_capped(dst, node->string);
}
return CPATH_JOIN_SUCCESS;
}
void dirup(char *dst, u64 levels, const char *path) {