From f2454ebf3d01d5f279e037c2b4e354d2ee6a12ea Mon Sep 17 00:00:00 2001 From: garhve Date: Thu, 15 Dec 2022 09:40:15 +0800 Subject: basically working --- c/tree/basic.c | 45 ++++++++++++++++++++++++++++++++++++++++----- c/tree/main.c | 38 +++++++++++++------------------------- c/tree/tree.h | 10 ++++++---- 3 files changed, 59 insertions(+), 34 deletions(-) (limited to 'c') diff --git a/c/tree/basic.c b/c/tree/basic.c index 8ce0daa..aa95497 100644 --- a/c/tree/basic.c +++ b/c/tree/basic.c @@ -27,13 +27,48 @@ void empty(regfile * head) while (head->next != NULL) { head->next = scan->next; + free(scan->node->text); free(scan->node); free(scan); scan = head->next; } } -void addreg(regfile * head, const char * d_name) +bool checkdir(const char * d_name) +{ + struct stat st; + stat(d_name,&st); + + return S_ISDIR(st.st_mode); +} + +void allocText(char ** text, const char * str, size_t len) +{ + if (*text == NULL) { + *text = (char *) calloc (len, sizeof(char)); + if (!*text) + error("newly allocation failed"); + memmove(*text,str,len+1); + } +} + +nodes * addnodes(const char * d_name, int level) +{ + nodes * new = (nodes *) calloc (1,sizeof(nodes)); + if (!new) + error("allocation for new node failed"); + + new->len = strlen(d_name); + new->level = level; + new->fod = checkdir(d_name); + + allocText(&new->text, d_name, new->len); + allocText(&new->text, '\0', 1); + + return new; +} + +void addreg(regfile * head, const char * d_name, int level) { regfile * new = head; @@ -43,11 +78,11 @@ void addreg(regfile * head, const char * d_name) if (!new->next) error("allocation for new regfile failed"); - new->next->node = addnodes(d_name); + new->next->node = addnodes(d_name, level); new->next->next = NULL; } -void extractContent(regfile * head, const char * parent) +void extractContent(regfile * head, const char * parent, int level) { DIR * d = opendir(parent); struct dirent * dir; @@ -58,7 +93,7 @@ void extractContent(regfile * head, const char * parent) while ((dir = readdir(d))) { if (dir->d_name[1] == '.' || (dir->d_name[0] == '.' && dir->d_name[1] == '\0')) continue; - addreg(head, dir->d_name); + addreg(head, dir->d_name, 0); } } @@ -66,6 +101,6 @@ void getfile(regfile * head, const char * parent) { init(head); - extractContent(head, parent); + extractContent(head, parent, 0); } diff --git a/c/tree/main.c b/c/tree/main.c index 86d2c30..b4b3374 100644 --- a/c/tree/main.c +++ b/c/tree/main.c @@ -6,32 +6,18 @@ #include "tree.h" -bool checkdir(const char * dir) +void printChild(const regfile * head) { - struct stat st; - stat(dir,&st); - - return S_ISDIR(st.st_mode); -} - -void printChild(const char * cur) -{ - DIR * d = opendir(cur); - - struct dirent * dir; - - if (!d) - error("Failed to open directory"); - - while ((dir = readdir(d))) { - //if (dir->d_name[1] == '.' || strcmp(".",dir->d_name) == 0) - if (dir->d_name[1] == '.' || dir->d_name[0] == '.' && dir->d_name[1] == '\0') - continue; - printf("%s\n", dir->d_name); - - if (checkdir(dir->d_name) == true) { - printChild(dir->d_name); - } + regfile * scan = head->next; + + while (scan != NULL) { + printf("%s\tlevel: %d\tname length: %u\tfile or dir: ",scan->node->text, + scan->node->level, scan->node->len); + if (scan->node->fod) + printf("dir\n"); + else + printf("file\n"); + scan = scan->next; } } @@ -41,6 +27,8 @@ int main() getfile(&head,"."); + printChild(&head); + empty(&head); return 0; diff --git a/c/tree/tree.h b/c/tree/tree.h index 4ccc687..82eb201 100644 --- a/c/tree/tree.h +++ b/c/tree/tree.h @@ -17,9 +17,10 @@ #include typedef struct inodes { - char * text; - int level; - bool fod; // file or dir + char * text; + int level; + size_t len; + bool fod; // file or dir } nodes; typedef struct regular { @@ -28,7 +29,8 @@ typedef struct regular { } regfile; void error(const char * str); -void init(regfile * st); +void init(regfile * head); +void empty(regfile * head); void getfile(regfile * head, const char * str); #endif /* end of include guard TREE_H */ -- cgit v1.2.3-70-g09d2