commit d3cb1b256b354de09b0d26a233a1574166de9c7e
parent 08ee99629d7750bf3811d2e7f9f8907be149ac4a
Author: rpa <rpa@grass>
Date: Mon, 20 Nov 2023 23:07:09 +0000
src/tablist: some herlper functions
Diffstat:
2 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c
@@ -225,8 +225,9 @@ void *
_encnext(TLencoder *w, char *c)
{
TLnode *n = ENCTOS(w);
- TLnode *fc = nil;
- if (n->children->len > 0) fc = *(TLnode **)slicegetp(n->children, 0);
+ // TODO: delete? don't remember what this was for
+ // TLnode *fc = nil;
+ // if (n->children->len > 0) fc = *(TLnode **)slicegetp(n->children, 0);
if (n->children->len == 0) {
w->cs[w->sp]++;
@@ -295,3 +296,54 @@ TLencode(TLencoder *w, char *buf, usize count)
}
return i;
}
+
+TLnode *
+TLgetnode(Slice *list, char *name, char *value)
+{
+ if (list == nil) sysfatal("TLgetnode: list is nil");
+ if (list->arr->type != &TLnodeType) sysfatal("TLgetnode: list is not TLnodeType");
+ if (list->len == 0) return nil;
+ int i;
+ for (i = 0; i < list->len; i++) {
+ TLnode **p = slicegetp(list, i);
+ TLnode *n = *p;
+ if (name != nil) {
+ if (strlen(name) != n->name->len) continue;
+ if (memcmp(name, n->name->p, n->name->len) == 0)
+ return n;
+ } else if (value != nil) {
+ if (strlen(value) != n->value->len) continue;
+ if (memcmp(value, n->value->p, n->value->len) == 0)
+ return n;
+ } else return nil;
+ }
+ return nil;
+}
+
+char *
+TLval2str(TLnode *n)
+{
+ usize len = 0;
+ int i;
+ for(i = 0; i < n->children->len; i++) {
+ TLnode **v = slicegetp(n->children, i);
+ TLnode *nn = *v;
+ if (nn->value == nil) continue;
+ len += nn->value->len + 1;
+ }
+ if (len == 0) len = 1;
+ char *buf = malloc(len);
+ char *p = buf;
+ for(i = 0; i < n->children->len; i++) {
+ TLnode **v = slicegetp(n->children, i);
+ TLnode *nn = *v;
+ if (nn->value == nil) continue;
+ memcpy(p, nn->value->p, nn->value->len);
+ p += nn->value->len;
+ *p = '\n';
+ p++;
+ }
+ buf[len - 1] = '\0';
+ // fprint(2, "TLval2str: %s\n", buf);
+ return buf;
+}
diff --git a/src/tablist/tablist.h b/src/tablist/tablist.h
@@ -40,3 +40,6 @@ int TLdecode(TLdecoder *, char *, usize);
TLencoder * initTLencoder(Slice *);
int TLencode(TLencoder *, char *, usize);
+
+TLnode * TLgetnode(Slice *, char *, char *);
+char * TLval2str(TLnode *);
+\ No newline at end of file