26 int errno; /* External symbol */
28 char *node_to_abspath(const struct node *node)
32 const struct node *tmp;
36 while (tmp && node_name(tmp)) {
37 len += strlen(node_name(tmp)) + 1; /* trail '/' */
38 tmp = node_parent(tmp);
41 path = malloc(len + 3); /* '/' and '\0' */
54 while (tmp && node_name(tmp)) {
55 ptr -= (strlen(node_name(tmp)) + 1);
57 strncpy(ptr + 1, node_name(tmp), strlen(node_name(tmp)));
58 tmp = node_parent(tmp);
65 static inline int next_state(int from, char ch)
73 if ( from == 1 ) return 2;
74 if ( from == 2 ) return 3;
80 static inline void abspath(const char* pBuffer, char* pRet)
86 int src_len = strlen(pBuffer);
90 while (src_idx <= src_len) {
92 state = next_state(from, pBuffer[src_idx]);
97 pRet[idx] = pBuffer[src_idx];
103 if ( idx > 1 ) idx --;
105 pRet[idx] = pBuffer[src_idx];
110 // Only can go to the 1 or 4
113 if ( idx < 1 ) idx = 1;
115 while ( idx > 1 && pRet[idx] != '/' ) idx --; /* Remove .. */
116 if ( idx > 1 && pRet[idx] == '/' ) idx --;
117 while ( idx > 1 && pRet[idx] != '/' ) idx --; /* Remove parent folder */
120 pRet[idx] = pBuffer[src_idx];
130 struct node *node_find(const struct node *node, const char *path)
137 while (node->parent && path[0] == '.' && path[1] == '.') {
138 if (path[2] != '/' && path[2] != '\0')
142 path += (path[2] == '/');
147 buffer = malloc(strlen(path) + 3); /* add 2 more bytes */
149 printf("Error: %s\n", strerror(errno));
153 abspath(path, buffer);
157 ptr += (*ptr == '/');
158 for (len = 0; ptr[len] && ptr[len] != '/'; len++);
162 if (!strncmp("..", ptr, len)) {
164 node = node->parent ? node->parent : node;
168 if (!strncmp(".", ptr, len)) {
178 if (!strncmp(node->name, ptr, len) && node->name[len] == '\0') {
183 node = node->sibling.next;
185 } while (*ptr && node);
188 return (struct node *)node;
191 struct node *node_create(struct node *parent, const char *name, enum node_type type)
195 node = malloc(sizeof(*node));
197 printf("Error: %s\n", strerror(errno));
201 node->parent = parent;
204 node->name = strdup(name);
206 printf("Error: %s\n", strerror(errno));
215 node->sibling.next = NULL;
216 node->sibling.prev = NULL;
225 while (tmp->sibling.next)
226 tmp = tmp->sibling.next;
228 tmp->sibling.next = node;
229 node->sibling.prev = tmp;
231 parent->child = node;
237 void *node_destroy(struct node *node)
248 void node_delete(struct node *node, void (del_cb)(struct node *node))
254 if (node->sibling.prev)
255 node->sibling.prev->sibling.next = node->sibling.next;
257 if (node->sibling.next)
258 node->sibling.next->sibling.prev = node->sibling.prev;
261 node->parent->child = NULL;
267 while (tmp->child) tmp = tmp->child;
269 parent = tmp->parent;
270 next = tmp->sibling.next;
286 struct node * const node_next_sibling(const struct node *node)
288 return node->sibling.next;
291 struct node * const node_prev_sibling(const struct node *node)
293 return node->sibling.prev;
296 void node_set_mode(struct node *node, int mode)
301 void node_set_data(struct node *node, void *data)
306 void node_set_type(struct node *node, enum node_type type)
311 struct node * const node_child(const struct node *node)
316 struct node * const node_parent(const struct node *node)
321 const int const node_mode(const struct node *node)
326 void * const node_data(const struct node *node)
331 const enum node_type const node_type(const struct node *node)
336 const char * const node_name(const struct node *node)
341 void node_set_age(struct node *node, int age)
346 int node_age(struct node *node)