Move cpath to os
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
#include "cpath.h"
|
||||
#include "aliases.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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 copy_count = 0;
|
||||
u64 sep_count = 0;
|
||||
|
||||
u64 full_length;
|
||||
u64 length;
|
||||
length = full_length = strlen(path);
|
||||
|
||||
if (length > 1 && path[length - 1] == PATH_SEP) {
|
||||
--length;
|
||||
}
|
||||
|
||||
for (i64 i = length - 1; i >= 0; --i) {
|
||||
if (path[i] == PATH_SEP) {
|
||||
++sep_count;
|
||||
copy_count = i + 1;
|
||||
|
||||
if (sep_count == levels) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char tmp[256];
|
||||
sprintf(tmp, "%c", PATH_SEP);
|
||||
// NOTE (Abdelrahman): Conditions stored in variables to silence MSVC warning C5045
|
||||
bool insufficient_levels = sep_count < levels;
|
||||
bool path_to_copy_is_separator = strncmp(path, tmp, copy_count) != 0;
|
||||
if (insufficient_levels && path_to_copy_is_separator) {
|
||||
copy_count = 0;
|
||||
}
|
||||
|
||||
if (dst == path) {
|
||||
memset(&dst[copy_count], 0, full_length - copy_count);
|
||||
} else {
|
||||
u64 dst_length = strlen(dst);
|
||||
memset(dst, 0, dst_length);
|
||||
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 leaf_length = strlen(leaf);
|
||||
|
||||
memcpy(dst, root, root_length);
|
||||
|
||||
if (leaf_length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
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[copy_start]), leaf, leaf_length);
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
#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__)
|
||||
#define wapp_cpath_dirname(DST, PATH) dirup(DST, 1, PATH)
|
||||
#define wapp_cpath_dirup(DST, COUNT, PATH) dirup(DST, COUNT, PATH)
|
||||
|
||||
void join_path(char *dst, u64 count, ...);
|
||||
void dirup(char *dst, u64 levels, const char *path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
END_C_LINKAGE
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !CPATH_H
|
Reference in New Issue
Block a user