#include "cpath.h" #include "aliases.h" #include #include #include #if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__) internal char path_sep = '/'; #elif defined(_WIN32) || defined(_WIN64) internal char path_sep = '\\'; #endif 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); } va_end(args); } void dirup(char *dst, u64 levels, const char *path) { if (levels < 1) { return; } u64 end_index = 0; u64 sep_count = 0; u64 full_length; u64 length; length = full_length = strlen(path); if (path[length - 1] == path_sep) { --length; } for (u64 i = length - 1; i >= 0; --i) { if (path[i] == path_sep) { ++sep_count; end_index = i; if (sep_count == levels) { break; } } } if (sep_count < levels) { end_index = 0; } if (dst == path) { memset(&dst[end_index], 0, full_length - end_index); } else { u64 dst_length = strlen(dst); memset(dst, 0, dst_length); strncpy(dst, path, end_index); } } void join_root_and_leaf(const char *root, const char *leaf, char *dst) { u64 root_length = strlen(root); u64 root_end = root_length - 1; u64 leaf_length = strlen(leaf); u64 leaf_start = 0; if (root[root_end] == path_sep) { --root_end; } if (leaf[leaf_start] == path_sep) { ++leaf_start; } memcpy(dst, root, ++root_end); dst[root_end] = path_sep; memcpy(&(dst[++root_end]), &(leaf[leaf_start]), leaf_length - leaf_start); }