From 483cb08a83ae5103ba4a9d02c279624445d7a746 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Fri, 22 Aug 2025 23:54:29 +0200 Subject: [PATCH] feat(utilities): added cut_path and combine_path to modify paths for storage and later usage in the file system --- include/utilities.h | 35 +++++++++++++++++++++++++++++++++++ src/tree.c | 5 +++++ 2 files changed, 40 insertions(+) diff --git a/include/utilities.h b/include/utilities.h index 795707c..d2aaaa1 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -89,4 +89,39 @@ void visualize_diff(File* old_version, File* new_version, ActionList* actions) { free(inserted_lines); } +// In this function we assume that base is a prefix of path +// Thus we just need the length of the base to jump ahead in the path +void cut_path(char* base, char* path) { + size_t base_len = strlen(base); + if (strlen(path) < base_len) perror("ERROR: the provided path is smaller than the base path!"); + int add = strlen(path) != base_len ? 1 : 0; + + printf("base: %li\n", base_len); + strcpy(path, path+base_len+add); + + if (add) { + char tmp[PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%s/", path); + strcpy(path, tmp); + } +} + +// In this function we assume that the end of the base and the beginning of the +// path are the same so we can concat them together at that part +// The path gets mutated in place +void combine_path(char* base, char* path) { + size_t base_len = 0; + + for (size_t idx = strlen(base); idx > 0; idx--) { + if (base[idx] == '/') { + base_len = idx; + break; + } + } + + char tmp[PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%*s/%s", base_len, base, path); + strcpy(path, tmp); +} + #endif // UTILITIES_H diff --git a/src/tree.c b/src/tree.c index 4d6ea72..2b6b674 100644 --- a/src/tree.c +++ b/src/tree.c @@ -73,6 +73,11 @@ static char* join_path(const char* a, const char* b) { return s; } +// A function that returns the system path of the where the closest merk +// directory to the current working directory is located up in direction to +// the filesystem root +// Mutates relative in place to get the correct amount of ../ in relation to +// the directory which the software was called in char* find_root(char* relative) { char* current_dir = calloc(PATH_MAX, sizeof(char)); if (!current_dir) {