libtreestore
diff src/treestore.c @ 3:48d75df3ef04
picking this up again, converted to cmake, and started a text format reader/writer
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 10 Nov 2016 16:19:44 +0200 |
parents | e1a825be0eee |
children | bb873449cf59 |
line diff
1.1 --- a/src/treestore.c Sat Apr 12 13:50:01 2014 +0300 1.2 +++ b/src/treestore.c Thu Nov 10 16:19:44 2016 +0200 1.3 @@ -1,8 +1,12 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <string.h> 1.7 +#include <errno.h> 1.8 #include "treestore.h" 1.9 1.10 +struct ts_node *ts_text_load(FILE *fp); 1.11 +int ts_text_save(struct ts_node *tree, FILE *fp); 1.12 + 1.13 /* ---- ts_value implementation ---- */ 1.14 1.15 int ts_init_value(struct ts_value *tsv) 1.16 @@ -359,3 +363,106 @@ 1.17 1.18 ts_free_node(tree); 1.19 } 1.20 + 1.21 +void ts_add_attr(struct ts_node *node, struct ts_attr *attr) 1.22 +{ 1.23 + attr->next = 0; 1.24 + if(node->attr_list) { 1.25 + node->attr_tail->next = attr; 1.26 + node->attr_tail = attr; 1.27 + } else { 1.28 + node->attr_list = node->attr_tail = attr; 1.29 + } 1.30 +} 1.31 + 1.32 +struct ts_attr *ts_get_attr(struct ts_node *node, const char *name) 1.33 +{ 1.34 + struct ts_attr *attr = node->attr_list; 1.35 + while(attr) { 1.36 + if(strcmp(attr->name, name) == 0) { 1.37 + return attr; 1.38 + } 1.39 + attr = attr->next; 1.40 + } 1.41 + return 0; 1.42 +} 1.43 + 1.44 +void ts_add_child(struct ts_node *node, struct ts_node *child) 1.45 +{ 1.46 + if(child->parent) { 1.47 + if(child->parent == node) return; 1.48 + ts_remove_child(child->parent, child); 1.49 + } 1.50 + child->parent = node; 1.51 + child->next = 0; 1.52 + 1.53 + if(node->child_list) { 1.54 + node->child_tail->next = child; 1.55 + node->child_tail = child; 1.56 + } else { 1.57 + node->child_list = node->child_tail = child; 1.58 + } 1.59 +} 1.60 + 1.61 +int ts_remove_child(struct ts_node *node, struct ts_node *child) 1.62 +{ 1.63 + struct ts_node dummy, *iter = &dummy; 1.64 + dummy.next = node->child_list; 1.65 + 1.66 + while(iter->next && iter->next != child) { 1.67 + iter = iter->next; 1.68 + } 1.69 + if(!iter->next) { 1.70 + return -1; 1.71 + } 1.72 + 1.73 + child->parent = 0; 1.74 + 1.75 + iter->next = child->next; 1.76 + if(!iter->next) { 1.77 + node->child_tail = iter; 1.78 + } 1.79 + node->child_list = dummy.next; 1.80 + return 0; 1.81 +} 1.82 + 1.83 +struct ts_node *ts_get_child(struct ts_node *node, const char *name) 1.84 +{ 1.85 + struct ts_node *res = node->child_list; 1.86 + while(res) { 1.87 + if(strcmp(res->name, name) == 0) { 1.88 + return res; 1.89 + } 1.90 + res = res->next; 1.91 + } 1.92 + return 0; 1.93 +} 1.94 + 1.95 +struct ts_node *ts_load(const char *fname) 1.96 +{ 1.97 + FILE *fp; 1.98 + struct ts_node *root; 1.99 + 1.100 + if(!(fp = fopen(fname, "rb"))) { 1.101 + fprintf(stderr, "ts_load: failed to open file: %s: %s\n", fname, strerror(errno)); 1.102 + return 0; 1.103 + } 1.104 + 1.105 + root = ts_text_load(fp); 1.106 + fclose(fp); 1.107 + return root; 1.108 +} 1.109 + 1.110 +int ts_save(struct ts_node *tree, const char *fname) 1.111 +{ 1.112 + FILE *fp; 1.113 + int res; 1.114 + 1.115 + if(!(fp = fopen(fname, "wb"))) { 1.116 + fprintf(stderr, "ts_save: failed to open file: %s: %s\n", fname, strerror(errno)); 1.117 + return 0; 1.118 + } 1.119 + res = ts_text_save(tree, fp); 1.120 + fclose(fp); 1.121 + return res; 1.122 +}