diff options
author | Yorhel <git@yorhel.nl> | 2012-03-23 15:49:38 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-03-23 16:14:15 +0100 |
commit | dc8e82571240aa04be8717346b2ac72c59a0c567 (patch) | |
tree | f227962471028d214b3f8ed5b29c26bdef447551 | |
parent | ac17e45262dcce1119b610b286ac074f6dd15b0b (diff) |
Added tn_el_new() and tn_el_copy() functions
-rw-r--r-- | tanja.c | 79 | ||||
-rw-r--r-- | tanja.h | 6 |
2 files changed, 60 insertions, 25 deletions
@@ -165,24 +165,7 @@ void tn_el_free(tn_el el) { } -void tn_tuple_ref(tn_tuple *tup) { - assert(tup != NULL); - atomic_inc(tup->ref); -} - - -void tn_tuple_unref(tn_tuple *tup) { - assert(tup != NULL); - if(!atomic_dec(tup->ref)) { - int i; - for(i=0; i<tup->n; i++) - tn_el_free(tup->e[i]); - free(tup); - } -} - - -static inline tn_el el_new(char t, va_list va) { +static inline tn_el el_newv(char t, va_list va) { if(t == TN_VT_AR || t == TN_VT_MAP) return va_arg(va, tn_el); tn_el a; @@ -197,6 +180,39 @@ static inline tn_el el_new(char t, va_list va) { } +tn_el tn_el_new(char t, ...) { + va_list va; + va_start(va, t); + tn_el e = el_newv(t, va); + va_end(va); + return e; +} + + +tn_el tn_el_copy(tn_el el) { + int i; + tn_el r = el; + switch(el.type) { + case TN_VT_STR: + r.v.s = el.v.s ? strdup(el.v.s) : NULL; + break; + case TN_VT_AR: + r.v.a = malloc(el.count*sizeof(tn_el)); + for(i=0; i<el.count; i++) + r.v.a[i] = tn_el_copy(el.v.a[i]); + break; + case TN_VT_MAP: + r.v.m = malloc(el.count*sizeof(tn_map_el)); + for(i=0; i<el.count; i++) { + r.v.m[i].key = strdup(el.v.m[i].key); + r.v.m[i].val = tn_el_copy(el.v.m[i].val); + } + break; + } + return r; +} + + tn_el tn_array_new(char *lst, ...) { tn_el a; a.count = a.size = strlen(lst); @@ -207,7 +223,7 @@ tn_el tn_array_new(char *lst, ...) { a.v.a = malloc(a.size*sizeof(tn_el)); int i; for(i=0; i<a.count; i++) - a.v.a[i] = el_new(lst[i], va); + a.v.a[i] = el_newv(lst[i], va); } else a.v.a = NULL; va_end(va); @@ -235,7 +251,7 @@ void tn_array_append(tn_el *a, char *lst, ...) { va_start(va, lst); int i; for(i=0; i<n; i++) - a->v.a[a->count++] = el_new(lst[i], va); + a->v.a[a->count++] = el_newv(lst[i], va); va_end(va); } @@ -251,7 +267,7 @@ tn_el tn_map_new(char *lst, ...) { int i; for(i=0; i<a.count; i++) { a.v.m[i].key = va_arg(va, char *); - a.v.m[i].val = el_new(lst[i], va); + a.v.m[i].val = el_newv(lst[i], va); } } else a.v.m = NULL; @@ -283,13 +299,30 @@ void tn_map_set(tn_el *m, char *lst, ...) { int i; for(i=0; i<n; i++) { m->v.m[m->count].key = va_arg(va, char *); - m->v.m[m->count].val = el_new(lst[i], va); + m->v.m[m->count].val = el_newv(lst[i], va); m->count++; } va_end(va); } +void tn_tuple_ref(tn_tuple *tup) { + assert(tup != NULL); + atomic_inc(tup->ref); +} + + +void tn_tuple_unref(tn_tuple *tup) { + assert(tup != NULL); + if(!atomic_dec(tup->ref)) { + int i; + for(i=0; i<tup->n; i++) + tn_el_free(tup->e[i]); + free(tup); + } +} + + tn_tuple *tn_tuple_new(char *lst, ...) { tn_tuple *t = malloc(offsetof(tn_tuple, e) + strlen(lst)*sizeof(tn_el)); t->n = strlen(lst); @@ -298,7 +331,7 @@ tn_tuple *tn_tuple_new(char *lst, ...) { va_start(va, lst); int i; for(i=0; i<t->n; i++) - t->e[i] = el_new(lst[i], va); + t->e[i] = el_newv(lst[i], va); va_end(va); return t; } @@ -94,12 +94,14 @@ typedef void (*tn_link_error_cb)(tn_link *, int, char *); typedef void (*tn_link_ready_cb)(tn_link *); void tn_el_free(tn_el); -void tn_tuple_ref(tn_tuple *); -void tn_tuple_unref(tn_tuple *); +tn_el tn_el_new(char, ...); +tn_el tn_el_copy(tn_el); tn_el tn_array_new(char *, ...); void tn_array_append(tn_el *, char *, ...); tn_el tn_map_new(char *, ...); void tn_map_set(tn_el *, char *, ...); +void tn_tuple_ref(tn_tuple *); +void tn_tuple_unref(tn_tuple *); tn_tuple *tn_tuple_new(char *, ...); int tn_tuple_match(tn_tuple *, tn_tuple *); |