libtreestore
changeset 1:a31eae25c0e6
partial implementation of ts_node and ts_attr
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 12 Apr 2014 13:46:00 +0300 |
parents | 740fec9866b1 |
children | e1a825be0eee |
files | src/treestore.c src/treestore.h |
diffstat | 2 files changed, 135 insertions(+), 4 deletions(-) [+] |
line diff
1.1 --- a/src/treestore.c Fri Apr 11 08:56:46 2014 +0300 1.2 +++ b/src/treestore.c Sat Apr 12 13:46:00 2014 +0300 1.3 @@ -240,6 +240,122 @@ 1.4 1.5 int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap) 1.6 { 1.7 + int i; 1.8 + 1.9 if(count <= 1) return -1; 1.10 - return -1; /* TODO */ 1.11 + 1.12 + if((tsv->array = malloc(count * sizeof *tsv->array))) { 1.13 + return -1; 1.14 + } 1.15 + tsv->array_size = count; 1.16 + 1.17 + for(i=0; i<count; i++) { 1.18 + struct ts_value *src = va_arg(ap, struct ts_value*); 1.19 + ts_copy_value(tsv->array + i, src); 1.20 + } 1.21 + return 0; 1.22 } 1.23 + 1.24 + 1.25 +/* ---- ts_attr implementation ---- */ 1.26 + 1.27 +int ts_init_attr(struct ts_attr *attr) 1.28 +{ 1.29 + memset(attr, 0, sizeof *attr); 1.30 + return ts_init_value(&attr->val); 1.31 +} 1.32 + 1.33 +void ts_destroy_attr(struct ts_attr *attr) 1.34 +{ 1.35 + free(attr->name); 1.36 + ts_destroy_value(&attr->val); 1.37 +} 1.38 + 1.39 +struct ts_attr *ts_alloc_attr(void) 1.40 +{ 1.41 + struct ts_attr *attr = malloc(sizeof *attr); 1.42 + if(!attr || ts_init_attr(attr) == -1) { 1.43 + free(attr); 1.44 + return 0; 1.45 + } 1.46 + return attr; 1.47 +} 1.48 + 1.49 +void ts_free_attr(struct ts_attr *attr) 1.50 +{ 1.51 + ts_destroy_attr(attr); 1.52 + free(attr); 1.53 +} 1.54 + 1.55 +int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src) 1.56 +{ 1.57 + if(dest == src) return 0; 1.58 + 1.59 + if(ts_set_attr_name(dest, src->name) == -1) { 1.60 + return -1; 1.61 + } 1.62 + 1.63 + if(ts_copy_value(&dest->val, &src->val) == -1) { 1.64 + ts_destroy_attr(dest); 1.65 + return -1; 1.66 + } 1.67 + return 0; 1.68 +} 1.69 + 1.70 +int ts_set_attr_name(struct ts_attr *attr, const char *name) 1.71 +{ 1.72 + char *n = malloc(strlen(name) + 1); 1.73 + if(!n) return -1; 1.74 + strcpy(n, name); 1.75 + 1.76 + free(attr->name); 1.77 + attr->name = n; 1.78 + return 0; 1.79 +} 1.80 + 1.81 + 1.82 +/* ---- ts_node implementation ---- */ 1.83 + 1.84 +int ts_init_node(struct ts_node *node) 1.85 +{ 1.86 + memset(node, 0, sizeof *node); 1.87 + return 0; 1.88 +} 1.89 + 1.90 +void ts_destroy_node(struct ts_node *node) 1.91 +{ 1.92 + free(node->name); 1.93 + 1.94 + while(node->attr_list) { 1.95 + struct ts_attr *attr = node->attr_list; 1.96 + node->attr_list = node->attr_list->next; 1.97 + ts_free_attr(attr); 1.98 + } 1.99 +} 1.100 + 1.101 +struct ts_node *ts_alloc_node(void) 1.102 +{ 1.103 + struct ts_node *node = malloc(sizeof *node); 1.104 + if(!node || ts_init_node(node) == -1) { 1.105 + free(node); 1.106 + return 0; 1.107 + } 1.108 + return node; 1.109 +} 1.110 + 1.111 +void ts_free_node(struct ts_node *node) 1.112 +{ 1.113 + ts_destroy_node(node); 1.114 + free(node); 1.115 +} 1.116 + 1.117 +void ts_free_tree(struct ts_node *tree) 1.118 +{ 1.119 + while(tree->child_list) { 1.120 + struct ts_tree *child = tree->child_list; 1.121 + tree->child_list = tree->child_list->next; 1.122 + ts_free_tree(child); 1.123 + } 1.124 + 1.125 + ts_free_node(tree); 1.126 +}
2.1 --- a/src/treestore.h Fri Apr 11 08:56:46 2014 +0300 2.2 +++ b/src/treestore.h Sat Apr 12 13:46:00 2014 +0300 2.3 @@ -56,12 +56,23 @@ 2.4 /** treestore node attribute */ 2.5 struct ts_attr { 2.6 char *name; 2.7 - 2.8 struct ts_value val; 2.9 2.10 struct ts_attr *next; 2.11 }; 2.12 2.13 +int ts_init_attr(struct ts_attr *attr); 2.14 +void ts_destroy_attr(struct ts_attr *attr); 2.15 + 2.16 +struct ts_attr *ts_alloc_attr(void); /**< also calls ts_init_attr */ 2.17 +void ts_free_attr(struct ts_attr *attr); /**< also calls ts_destroy_attr */ 2.18 + 2.19 +/** perform a deep-copy of a ts_attr */ 2.20 +int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src); 2.21 + 2.22 +int ts_set_attr_name(struct ts_attr *attr, const char *name); 2.23 + 2.24 + 2.25 2.26 /** treestore node */ 2.27 struct ts_node { 2.28 @@ -77,8 +88,12 @@ 2.29 struct ts_node *next; /* next sibling */ 2.30 }; 2.31 2.32 -struct ts_node *ts_create_node(void); 2.33 -void ts_free_node(struct ts_node *n); 2.34 +int ts_init_node(struct ts_node *node); 2.35 +void ts_destroy_node(struct ts_node *node); 2.36 + 2.37 +struct ts_node *ts_alloc_node(void); /**< also calls ts_init_node */ 2.38 +void ts_free_node(struct ts_node *n); /**< also calls ts_destroy_node */ 2.39 + 2.40 void ts_free_tree(struct ts_node *tree); 2.41 2.42 #ifdef __cplusplus