refactor(tree,utilities)!: moved the PathBuffer to utilities
This commit is contained in:
parent
326a33a3f4
commit
e63fb4a0fa
4 changed files with 103 additions and 97 deletions
|
|
@ -1,26 +1,13 @@
|
||||||
#ifndef TREE_H
|
#ifndef TREE_H
|
||||||
#define TREE_H
|
#define TREE_H
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "file.h"
|
|
||||||
|
|
||||||
typedef List PathBuffer;
|
|
||||||
typedef List FileInfoBuffer;
|
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*);
|
void save_tree(PathBuffer*);
|
||||||
|
|
||||||
#endif // TREE_H
|
#endif // TREE_H
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,19 @@ typedef struct {
|
||||||
size_t item_size;
|
size_t item_size;
|
||||||
} List;
|
} List;
|
||||||
|
|
||||||
|
typedef List PathBuffer;
|
||||||
|
|
||||||
List* list_new(size_t);
|
List* list_new(size_t);
|
||||||
int list_push(List*, void*);
|
int list_push(List*, void*);
|
||||||
PathType get_path_type(const char*);
|
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*);
|
char* find_root(char*);
|
||||||
void walk(char*, char*, char*, PathBuffer*, int, 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 visualize_diff(File*, File*, ActionList*);
|
||||||
void cut_path(char* base, char* path);
|
void cut_path(char* base, char* path);
|
||||||
void combine_path(char* base, char* path);
|
void combine_path(char* base, char* path);
|
||||||
|
|
|
||||||
85
src/tree.c
85
src/tree.c
|
|
@ -1,86 +1,5 @@
|
||||||
#include "tree.h"
|
#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) {
|
void save_tree(PathBuffer* tree) {
|
||||||
PathBuffer* files = path_buffer_new();
|
PathBuffer* files = path_buffer_new();
|
||||||
char relative[PATH_MAX] = "";
|
char relative[PATH_MAX] = "";
|
||||||
|
|
@ -94,6 +13,10 @@ void save_tree(PathBuffer* tree) {
|
||||||
walk(root, relative, relative, files, 1, root);
|
walk(root, relative, relative, files, 1, root);
|
||||||
path_buffer_sort(files);
|
path_buffer_sort(files);
|
||||||
|
|
||||||
|
for (int i = 0; i < files->len; i++) {
|
||||||
|
printf("%s\n", files->items[i]);
|
||||||
|
}
|
||||||
|
|
||||||
char tmp[1024];
|
char tmp[1024];
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,87 @@ PathType get_path_type(const char* path) {
|
||||||
return PT_OTHER;
|
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
|
// 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
|
// directory to the current working directory is located up in direction to
|
||||||
// the filesystem root
|
// the filesystem root
|
||||||
|
|
@ -177,10 +258,10 @@ void walk(char* base, char* base_rel, char* rel, PathBuffer* paths, int full_rep
|
||||||
free(relative_path);
|
free(relative_path);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
char* real_path = realpath(relative_path, NULL);
|
char* repo_path = get_repo_path(repo_root_path, relative_path);
|
||||||
cut_path(repo_root_path, real_path);
|
path_buffer_push(paths, repo_path);
|
||||||
path_buffer_push(paths, real_path);
|
|
||||||
free(relative_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);
|
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) {
|
void visualize_diff(File* old_version, File* new_version, ActionList* actions) {
|
||||||
int* deleted_lines = calloc(old_version->lines, sizeof(int));
|
int* deleted_lines = calloc(old_version->lines, sizeof(int));
|
||||||
int* inserted_lines = calloc(new_version->lines, sizeof(int));
|
int* inserted_lines = calloc(new_version->lines, sizeof(int));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue