void file_free(struct file *p) {
free(p->name);
+ free(p->path);
+ free(p);
}
int file_init(struct file **p, const char *path) {
char *b;
if(NULL==p) { return -1; }
- if(0==strlen(path)) { return -1; }
+ if((NULL==path)||(0==strlen(path))) { return -1; }
*p = malloc(sizeof(struct file));
if(NULL==(*p)) {
#include<tree.h>
static struct tree* tree_add_directory(struct tree*,const char*);
-static int tree_add_file(struct tree_entry*,struct file*);
+static int tree_add_file(struct tree*,struct file*);
int tree_add(struct tree *root, struct file *to_add) {
- char *str, *p;
+ char *str, *p, *prev;
struct tree *tree;
if(NULL==root) { return -1; }
str = strdup(to_add->path);
if(NULL==str) { return -1; }
+ prev = NULL;
p = strtok(str,"/");
while(p) {
- tree = tree_add_directory(tree->directories,p);
- if(NULL==tree) { goto clean; }
+ if(prev!=NULL) {
+ tree = tree_add_directory(tree,prev);
+ if(NULL==tree) { goto clean; }
+ }
+ prev = p;
p = strtok(NULL,"/");
}
- if(tree_add_file(tree->files,to_add)<0) { goto clean; }
+ if(tree_add_file(tree,to_add)<0) { goto clean; }
free(str);
return -1;
}
-static struct tree* tree_add_directory(struct tree *p, const char *dirname) {
- struct tree *prev, *next;
+static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
+ struct tree *p, *prev, *next;
int i;
+ if(NULL==root) { return NULL; }
+
+ if(NULL==dirname) { return NULL; }
+ if(0==strlen(dirname)) { return NULL; }
+
+ p = root->directories;
+
prev = NULL;
while(p!=NULL) {
if((i = strcmp(p->name,dirname))>=0) {
if(prev!=NULL) {
prev->next = next;
+ } else {
+ root->directories = next;
}
next->next = p;
return next;
}
-static int tree_add_file(struct tree_entry *p, struct file *to_add) {
- struct tree_entry *prev, *next;
+static int tree_add_file(struct tree *root, struct file *to_add) {
+ struct tree_entry *p, *prev, *next;
int i;
+ if(NULL==root) { return -1; }
+ if(NULL==to_add) { return -1; }
+
+ p = root->files;
+
prev = NULL;
while(p!=NULL) {
if((i = strcmp(p->file->name,to_add->name))>=0) {
if(prev!=NULL) {
prev->next = next;
+ } else {
+ root->files = next;
}
next->next = p;
subdir = p->directories;
p->directories = p->directories->next;
tree_free(subdir);
- free(subdir);
}
while(p->next!=NULL) {
subdir = p->next;
p->next = p->next->next;
tree_free(subdir);
- free(subdir);
}
free(p);
-DNDEBUG
endif
-check_PROGRAMS = hashmap.tests tree.tests util.filter.tests
+check_PROGRAMS = file.tests hashmap.tests tree.tests util.filter.tests
TESTS = $(check_PROGRAMS)
if ENABLE_MEMCHECK
common_SOURCES = test_utils.c
+file_tests_SOURCES = \
+ $(common_SOURCES) \
+ file.tests.c \
+ $(top_srcdir)/src/file.c
+
hashmap_tests_SOURCES = \
$(common_SOURCES) \
hashmap.tests.c \
--- /dev/null
+#include<test_utils.h>
+
+#include<file.h>
+
+int main();
+static void file_init_basic_test();
+
+int main() {
+ setup_env();
+
+ file_init_basic_test();
+
+ clean_env();
+
+ return EXIT_SUCCESS;
+}
+
+static void file_init_basic_test() {
+ struct file *p;
+
+ assert(-1==file_init(NULL,NULL));
+ assert(-1==file_init(&p,NULL));
+ assert(-1==file_init(&p,""));
+
+ assert(1==file_init(&p,"testdir/test"));
+
+ assert(strcmp(p->name,"test")==0);
+ assert(strcmp(p->path,"testdir/test")==0);
+
+ file_free(p);
+}
int main();
static void tree_add_basic_test();
+static void tree_add_extended_test();
static void tree_init_basic_test();
int main() {
setup_env();
tree_add_basic_test();
+ tree_add_extended_test();
tree_init_basic_test();
clean_env();
assert(tree->directories->directories->next==NULL);
tree_free(tree);
+}
+
+static void tree_add_extended_test() {
+ struct tree *tree;
+ struct file *file;
+
+ char valid[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
+
- file_free(file1);
- file_free(file2);
+ char files[7][100] = {
+ "test/test1",
+ "hello/test2",
+ "/notalsdkfj asdfkkl /asdlfkj /asdflkjasdf/klaasdlfjasd",
+ "src/asldfkak/a/aslkdfj//a/lsd/as/alskdf/lasdfjasdfasdlfkajsdfa/sdf",
+ "//./asd//sd/f/sadfasldklfasld///s/d/flskdfkl/asdk",
+ "..",
+ "."
+ };
+
+ assert(1==tree_init(&tree,NULL));
+
+ for(int i = 0;i<3;i++) {
+ assert(1==file_init(&file,files[i]));
+ assert(1==tree_add(tree,file));
+ }
+
+ tree_free(tree);
}
static void tree_init_basic_test() {