From e091cdb3187b60a9901bd14d475eebc481c9a299 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 3 May 2022 15:40:09 -0700 Subject: web: move elem construction to shared dom file --- web/web_dom.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 web/web_dom.c (limited to 'web/web_dom.c') diff --git a/web/web_dom.c b/web/web_dom.c new file mode 100644 index 0000000..ddb59ad --- /dev/null +++ b/web/web_dom.c @@ -0,0 +1,67 @@ +#include "web_dom.h" + +#include +#include +#include + +#include + +struct elem *elem_init(char *ns, char *name, int size_attrs) +{ + struct elem *e = malloc(sizeof(*e)); + + e->ns = ns ? strdup(ns) : NULL; + e->name = strdup(name); + + e->n_attrs = 0; + e->size_attrs = size_attrs; + e->attr_names = malloc(size_attrs * sizeof(*(e->attr_names))); + e->attr_vals = malloc(size_attrs * sizeof(*(e->attr_vals))); + e->content = NULL; + + return e; +} + +void elem_free(struct elem *e) +{ + for(int i = 0; i < e->n_attrs; ++i) { + free(e->attr_names[i]); + free(e->attr_vals[i]); + } + + free(e->content); + free(e->attr_names); + free(e->attr_vals); + free(e->name); + free(e); +} + +void elem_add_attr(struct elem *e, char *name, char *val) +{ + if (e->n_attrs == e->size_attrs) { + e->size_attrs *= 2; + e->attr_names = realloc(e->attr_names, + e->size_attrs * sizeof(*(e->attr_names))); + e->attr_vals = realloc(e->attr_vals, + e->size_attrs * sizeof(*(e->attr_vals))); + } + + e->attr_names[e->n_attrs] = strdup(name); + e->attr_vals[e->n_attrs++] = strdup(val); +} + +void elem_add_attr_double(struct elem *e, char *name, double val) +{ + char str[512]; + sprintf(str, "%f", val); + elem_add_attr(e, name, str); +} + +void append_elemv(char *sel, int elemc, struct elem **elemv) +{ + EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8)}, + sel, elemc, elemv, + offsetof(struct elem, ns), offsetof(struct elem, name), + offsetof(struct elem, content), offsetof(struct elem, n_attrs), + offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals)); +} -- cgit v1.2.3-70-g09d2