Move path utils to os, fix bugs with it and write tests
This commit is contained in:
@@ -1,18 +1,9 @@
|
||||
#include "cpath.h"
|
||||
#include "aliases.h"
|
||||
#include "platform.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef WAPP_PLATFORM_POSIX
|
||||
internal char path_sep = '/';
|
||||
#elif defined(WAPP_PLATFORM_WINDOWS)
|
||||
internal char path_sep = '\\';
|
||||
#else
|
||||
#error "Unrecognised platform"
|
||||
#endif
|
||||
|
||||
void join_root_and_leaf(const char *root, const char *leaf, char *dst);
|
||||
|
||||
void join_path(char *dst, u64 count, ...) {
|
||||
@@ -32,21 +23,21 @@ void dirup(char *dst, u64 levels, const char *path) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 end_index = 0;
|
||||
u64 copy_count = 0;
|
||||
u64 sep_count = 0;
|
||||
|
||||
u64 full_length;
|
||||
u64 length;
|
||||
length = full_length = strlen(path);
|
||||
|
||||
if (path[length - 1] == path_sep) {
|
||||
if (length > 1 && path[length - 1] == PATH_SEP) {
|
||||
--length;
|
||||
}
|
||||
|
||||
for (i64 i = length - 1; i >= 0; --i) {
|
||||
if (path[i] == path_sep) {
|
||||
if (path[i] == PATH_SEP) {
|
||||
++sep_count;
|
||||
end_index = i;
|
||||
copy_count = i + 1;
|
||||
|
||||
if (sep_count == levels) {
|
||||
break;
|
||||
@@ -54,37 +45,44 @@ void dirup(char *dst, u64 levels, const char *path) {
|
||||
}
|
||||
}
|
||||
|
||||
if (sep_count < levels) {
|
||||
end_index = 0;
|
||||
char tmp[256];
|
||||
sprintf(tmp, "%c", PATH_SEP);
|
||||
if (sep_count < levels && strncmp(path, tmp, copy_count) != 0) {
|
||||
copy_count = 0;
|
||||
}
|
||||
|
||||
if (dst == path) {
|
||||
memset(&dst[end_index], 0, full_length - end_index);
|
||||
memset(&dst[copy_count], 0, full_length - copy_count);
|
||||
} else {
|
||||
u64 dst_length = strlen(dst);
|
||||
memset(dst, 0, dst_length);
|
||||
strncpy(dst, path, end_index);
|
||||
strncpy(dst, path, copy_count);
|
||||
}
|
||||
|
||||
u64 final_length = strlen(dst);
|
||||
if (final_length > 1) {
|
||||
dst[final_length - 1] = '\0';
|
||||
} else if (final_length == 0) {
|
||||
dst[0] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
memcpy(dst, root, root_length);
|
||||
|
||||
if (leaf_length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (leaf[leaf_start] == path_sep) {
|
||||
++leaf_start;
|
||||
u64 copy_start = root_length;
|
||||
|
||||
if (root_length > 0 && root[root_length - 1] != PATH_SEP && leaf[0] != PATH_SEP) {
|
||||
dst[root_length] = PATH_SEP;
|
||||
++copy_start;
|
||||
}
|
||||
|
||||
memcpy(dst, root, ++root_end);
|
||||
|
||||
dst[root_end] = path_sep;
|
||||
|
||||
memcpy(&(dst[++root_end]), &(leaf[leaf_start]), leaf_length - leaf_start);
|
||||
memcpy(&(dst[copy_start]), leaf, leaf_length);
|
||||
}
|
||||
|
@@ -1,12 +1,21 @@
|
||||
#ifndef PATH_UTILS_H
|
||||
#define PATH_UTILS_H
|
||||
#ifndef CPATH_H
|
||||
#define CPATH_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // __cplusplus
|
||||
|
||||
#ifdef WAPP_PLATFORM_POSIX
|
||||
#define PATH_SEP '/'
|
||||
#elif defined(WAPP_PLATFORM_WINDOWS)
|
||||
#define PATH_SEP '\\'
|
||||
#else
|
||||
#error "Unrecognised platform"
|
||||
#endif
|
||||
|
||||
#define NUMPARTS(...) (sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
||||
|
||||
#define wapp_cpath_join_path(DST, ...) join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
|
||||
|
Reference in New Issue
Block a user