From e63fb4a0fad651cdfa8fc46e8c96f35fde0f3992 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Wed, 27 Aug 2025 02:57:32 +0200 Subject: [PATCH] refactor(tree,utilities)!: moved the PathBuffer to utilities --- include/tree.h | 13 ------- include/utilities.h | 8 ++++ src/tree.c | 85 ++-------------------------------------- src/utilities.c | 94 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 103 insertions(+), 97 deletions(-) diff --git a/include/tree.h b/include/tree.h index 40c7c32..2547d4b 100644 --- a/include/tree.h +++ b/include/tree.h @@ -1,26 +1,13 @@ #ifndef TREE_H #define TREE_H -#include -#include -#include -#include -#include -#include -#include #include #include "utilities.h" #include "hash.h" -#include "file.h" -typedef List PathBuffer; typedef List FileInfoBuffer; -PathBuffer* path_buffer_new(); -void path_buffer_push(PathBuffer*, char*); -void path_buffer_sort(PathBuffer*); -void path_buffer_free(PathBuffer*); void save_tree(PathBuffer*); #endif // TREE_H diff --git a/include/utilities.h b/include/utilities.h index 8d475a0..ebbd114 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -35,11 +35,19 @@ typedef struct { size_t item_size; } List; +typedef List PathBuffer; + List* list_new(size_t); int list_push(List*, void*); PathType get_path_type(const char*); +PathBuffer* path_buffer_new(); +void path_buffer_push(PathBuffer*, char*); +void path_buffer_sort(PathBuffer*); +void path_buffer_free(PathBuffer*); char* find_root(char*); void walk(char*, char*, char*, PathBuffer*, int, char*); +char* get_repo_path(char*, char*); +int is_in_repo(char*, char*); void visualize_diff(File*, File*, ActionList*); void cut_path(char* base, char* path); void combine_path(char* base, char* path); diff --git a/src/tree.c b/src/tree.c index 46a2240..93334a9 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,86 +1,5 @@ #include "tree.h" -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)); -} - void save_tree(PathBuffer* tree) { PathBuffer* files = path_buffer_new(); char relative[PATH_MAX] = ""; @@ -94,6 +13,10 @@ void save_tree(PathBuffer* tree) { walk(root, relative, relative, files, 1, root); path_buffer_sort(files); + for (int i = 0; i < files->len; i++) { + printf("%s\n", files->items[i]); + } + char tmp[1024]; size_t offset = 0; diff --git a/src/utilities.c b/src/utilities.c index f73ddcc..30187ca 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -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));