diff options
Diffstat (limited to 'c/tree/basic.c')
-rw-r--r-- | c/tree/basic.c | 45 |
1 files changed, 40 insertions, 5 deletions
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); } |