int Add_Entry_In_Tree (char *entry, struct path_s *root, struct path_s **path, struct file_list_s **file) { int entries; char **centries, **cp; int i; struct path_s *add_path,*p,*pb; struct path_list_s *pl,*plb; struct file_list_s *f,*fp; int found = 0; if (path == NULL && file == NULL) return -1; if (entry == NULL) return -1; if (root == NULL) return -1; centries = split_dir(entry, &entries); if (entries == 0) return -1; if (entries == 1) { add_path = root; cp = centries; } else { p = root; cp = centries; for (i=0;i<entries-1;i++) { found = 0; if (p->entries == NULL) { pb = p; p->entries = malloc(sizeof(struct path_list_s)); memset(p->entries, 0, (sizeof(struct path_list_s))); p->entries->next = NULL; p->entries->previous = NULL; p->entries->path = malloc(sizeof(struct path_s)); memset(p->entries->path, 0, (sizeof(struct path_s))); p->entries->path->name = strdup(*cp); p->entries->path->parent = p; p->entries->path->files = NULL; p->entries->path->entries = NULL; p = p->entries->path; } else { pl = p->entries; plb = pl; pb = p; while (pl) { if (!strcmp(pl->path->name, *cp)) { p = pl->path; found = 1; break; } plb = pl; pl = pl->next; } if (pb == p && found == 0) { plb->next = malloc(sizeof(struct path_list_s)); memset(plb->next, 0, (sizeof(struct path_list_s))); pl = plb->next; pl->next = NULL; pl->previous = plb; pl->path = malloc(sizeof(struct path_s)); memset(pl->path, 0, (sizeof(struct path_s))); pl->path->parent = p; pl->path->entries = NULL; pl->path->name = strdup(*cp); pl->path->files = NULL; p = pl->path; } } cp++; } add_path = p; } p = add_path; if (file) if (*file) { if (p->files == NULL) { p->files = *file; (*file)->dir = p; (*file)->entry_name = strdup(entry); split_dir_cleanup(centries,entries); return 0; } f = p->files; while (f) { if (!strcmp(f->name, *cp)) { printf("file already existed %s in path %s\n", f->name, p->name); split_dir_cleanup(centries,entries); return -1; } fp = f; f = f->next; } fp->next = *file; (*file)->next = NULL; (*file)->previous = fp; (*file)->dir = p; (*file)->entry_name = strdup(entry); split_dir_cleanup(centries,entries); return 0; } if (path) if (*path) { plb = pl = p->entries; while (pl) { if (!strcmp(pl->path->name, *cp)) { printf("path already existed\n"); split_dir_cleanup(centries,entries); return -1; } plb = pl; pl = pl->next; } if (plb == NULL) { p->entries = malloc(sizeof(struct path_list_s)); memset(p->entries, 0, (sizeof(struct path_list_s))); pl = p->entries; plb = NULL; } else { plb->next = malloc(sizeof(struct path_list_s)); memset(plb->next, 0, (sizeof(struct path_list_s))); pl = plb->next; } pl->next = NULL; pl->previous = plb; pl->path = (*path); (*path)->entries = NULL; (*path)->files = NULL; (*path)->parent = p; } split_dir_cleanup(centries,entries); return 0; }
void Get_Entry_From_Tree (char *entry, struct path_s *root, struct path_s **path, struct file_list_s **file) { int entries; char **centries, **cp; int i; struct path_s *p,*pb; struct path_list_s *pl; struct file_list_s *fl; int found; if (path == NULL && file == NULL) return; if (path) *path = NULL; if (file) *file = NULL; if (entry == NULL) return; if (entry[0] == '/' && strlen(entry) == 1) { *path = root; return; } centries = split_dir(entry, &entries); if (entries == 0) { *path = root; return; } p = root; cp = centries; for (i=0 ; i<entries ; i++) { found = 0; if (i+1 == entries && file) { fl = p->files; while (fl) { if (!strcmp(fl->name, *cp)) { *file = fl; break; } fl=fl->next; } } pl = p->entries; while (pl) { found = 0; if (!strcmp(pl->path->name, *cp)) { found = 1; p = pl->path; break; } pl = pl->next; } cp++; } if (path && found) { *path = p; } split_dir_cleanup(centries, entries); }
/// Splits file path -> (directory, file_base_name, extension) inline std::tuple<string, string, string> split_path(string const& file_path) { string dir, base_name, ext; std::tie(dir, base_name) = split_dir(file_path); std::tie(base_name, ext) = split_extension(base_name); return std::make_tuple(dir, base_name, ext); }