]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 16 Oct 2021 00:07:43 +0000 (17:07 -0700)
committeralex <[email protected]>
Sat, 16 Oct 2021 00:07:43 +0000 (17:07 -0700)
inc/tree.h
src/tree.c

index a27273ba95155cd49e17c8e0359b4ef9815052c4..ca7a4153c315a58245e01207e731352acb609cf8 100644 (file)
@@ -5,11 +5,16 @@
 
 #include<file.h>
 
+struct tree_entry {
+       struct file *file;
+       struct tree_entry *next;
+};
+
 struct tree {
-       struct file **files;
-       size_t file_count;
-       struct tree **directories;
-       size_t directory_count;
+       const char *name;
+       struct tree_entry *files;
+       struct tree *directories;
+       struct tree *next;
 };
 
 int tree_add(struct tree*,struct file*);
index 9b826a08197b40432f05518ed88709b1afe77408..aaaf7aeb4fd06ff45e4ebb4be6d5e386c0a3d3cf 100644 (file)
@@ -1,32 +1,27 @@
 #include<tree.h>
 
-static int tree_add_directory(struct tree*,const char*);
-static int tree_add_file(struct tree*,struct file*);
+static struct tree* tree_add_directory(struct tree*,const char*);
+static int tree_add_file(struct tree_entry*,struct file*);
 
-int tree_add(struct tree *root, struct file *p) {
-       char *str, *p, *prev;
+int tree_add(struct tree *root, struct file *to_add) {
+       char *str, *p;
        struct tree *tree;
-       int index;
 
        if(NULL==root) { return -1; }
        tree = root;
 
-       str = strdup(p->path);
+       str = strdup(to_add->path);
        if(NULL==str) { return -1; }
 
-       prev = str;
        p = strtok(str,"/");
        while(p) {
-               if((index = tree_find_directory(tree,p))<0) {
-                       if((index = tree_add_directory(p))<0) { goto clean; }
-               }
-               tree = tree->directories[index];
+               tree = tree_add_directory(tree,p);
+               if(NULL==tree) { goto clean; }
 
-               prev = p;
                p = strtok(NULL,"/");
        }
 
-       if(tree_add_file(tree,prev)<0) { goto clean; }
+       if(tree_add_file(tree->files,to_add)<0) { goto clean; }
 
        free(str);
        
@@ -36,36 +31,100 @@ int tree_add(struct tree *root, struct file *p) {
                return -1;
 }
 
-static int tree_add_directory(struct tree *p, const char *dirname) {
-       return -1;
-}
+static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
+       struct tree *prev, *next;
+       int i;
 
-static int tree_add_file(struct tree *tree, struct file *p) {
-       return -1;
-}
+       prev = NULL;
+       while(p!=NULL) {
+               if((i = strcmp(p->name,dirname))>=0) {
+                       if(i==0) { return p; }
+                       break;
+               }
+               
+               prev = p;
+               p = p->next;
+       }
+
+       if(tree_init(&next)<0) { return NULL; }
 
-int tree_find_directory(struct tree *p, const char *dirname) {
-       return -1;
+       if(prev!=NULL) {
+               prev->next = next;
+       }
+
+       next->next = p;
+       
+       return next;
 }
 
-int tree_find_entry(struct tree *p, const char *filename) {
-       return -1;
+static int tree_add_file(struct tree_entry *p, struct file *to_add) {
+       struct tree_entry *prev, *next;
+       int i;
+
+       prev = NULL;
+       while(p!=NULL) {
+               if((i = strcmp(p->file->name,to_add->name))>=0) {
+                       if(i==0) { return 0; }
+                       break;
+               }
+
+               prev = p;
+               p = p->next;
+       }
+
+       next = malloc(sizeof(struct tree_entry));
+       if(NULL==next) { return -1; }
+
+       next->file = to_add;
+       next->next = NULL;
+
+       if(prev!=NULL) {
+               prev->next = next;
+       }
+
+       next->next = p;
+
+       return 1;
 }
 
 void tree_free(struct tree *p) {
-       while(p->file_count>0) {
-               file_free(p->files[p->file_count-1]);
-               p->file_count--;
+       struct tree_entry *entry;
+       struct tree *subdir;
+
+       while(p->files!=NULL) {
+               entry = p->files;
+               p->files = p->files->next;
+               file_free(entry->file);
+               free(entry);
+       }
+
+       while(p->directories!=NULL) {
+               subdir = p->directories;
+               p->directories = p->directories->next;
+               tree_free(subdir);
+               free(subdir);
        }
        
-       while(p->directory_count>0) {
-               tree_free(p->directories[p->directory_count-1]);
-               p->directory_count--;
+       while(p->next!=NULL) {
+               subdir = p->next;
+               p->next = p->next->next;
+               tree_free(subdir);
+               free(subdir);
        }
 
        free(p);
 }
 
 int tree_init(struct tree **p) {
-       return -1;
+       (*p) = malloc(sizeof(struct tree));
+       if(NULL==(*p)) {
+               perror("malloc");
+               return -1;
+       }
+
+       (*p)->files = NULL;
+       (*p)->directories = NULL;
+       (*p)->next = NULL;
+
+       return 1;
 }