refactor(tree,utilities)!: moved the PathBuffer to utilities

This commit is contained in:
lisk77 2025-08-27 02:57:32 +02:00
parent 326a33a3f4
commit e63fb4a0fa
4 changed files with 103 additions and 97 deletions

View file

@ -43,6 +43,87 @@ PathType get_path_type(const char* path) {
return PT_OTHER;
}
PathBuffer* path_buffer_new() {
return list_new(sizeof(char*));
}
void path_buffer_push(PathBuffer* buffer, char* path) {
char* path_copy = strdup(path);
if (!path_copy) {
perror("ERROR: strdup failed in add_path");
return;
}
list_push(buffer, &path_copy);
}
int compare_paths(const void* a, const void* b) {
const char* s1 = *(const char**)a;
const char* s2 = *(const char**)b;
return strcmp(s1,s2);
}
void path_buffer_sort(PathBuffer* buffer) {
if (!buffer || buffer->len <= 1) {
return;
}
qsort(buffer->items, buffer->len, sizeof(char*), compare_paths);
}
void path_buffer_free(PathBuffer* buffer) {
if (buffer) {
for (size_t i = 0; i < buffer->len; i++) {
free(buffer->items[i]);
}
free(buffer->items);
free(buffer);
}
}
static int is_dot_or_dotdot(const char* s) {
return (s[0] == '.' && (s[1] == '\0' || (s[1] == '.' && s[2] == '\0')));
}
static char* join_path(const char* a, const char* b) {
size_t la = strlen(a);
size_t lb = strlen(b);
int need_sep = (la > 0 && a[la - 1] != '/');
char* s = calloc(la + (need_sep ? 1 : 0) + lb + 1, sizeof(char));
if (!s) {
perror("ERROR: calloc failed in join_path!");
exit(1);
}
memcpy(s, a, la);
size_t p = la;
if (need_sep) s[p++] = '/';
memcpy(s + p, b, lb + 1);
return s;
}
void normalize_path(char* path, char* rel) {
if (strlen(rel) == 0) return;
size_t path_len = strlen(path);
size_t rel_len = strlen(rel);
int consumers = rel_len / 3;
int counter = 0;
size_t latest = 0;
for (size_t idx = rel_len; idx < path_len; idx++) {
if (path[idx] == '/') {
counter++;
latest = idx;
if (counter == consumers) break;
}
if (path[idx] == '\0') break;
}
strcpy(path, path+latest+(counter > 0 ? 1 : 0));
}
// 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
@ -177,10 +258,10 @@ void walk(char* base, char* base_rel, char* rel, PathBuffer* paths, int full_rep
free(relative_path);
}
else {
char* real_path = realpath(relative_path, NULL);
cut_path(repo_root_path, real_path);
path_buffer_push(paths, real_path);
char* repo_path = get_repo_path(repo_root_path, relative_path);
path_buffer_push(paths, repo_path);
free(relative_path);
free(repo_path);
}
}
@ -190,6 +271,13 @@ void walk(char* base, char* base_rel, char* rel, PathBuffer* paths, int full_rep
closedir(dir);
}
// We assume that path is in the repo
char* get_repo_path(char* repo_root_path, char* path) {
char* real_path = realpath(path, NULL);
cut_path(repo_root_path, real_path);
return real_path;
}
void visualize_diff(File* old_version, File* new_version, ActionList* actions) {
int* deleted_lines = calloc(old_version->lines, sizeof(int));
int* inserted_lines = calloc(new_version->lines, sizeof(int));