feat(utilities): added cut_path and combine_path to modify paths for storage and later usage in the file system
This commit is contained in:
parent
25480fc92f
commit
483cb08a83
2 changed files with 40 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue