From a0ca7ab5421d001f50fabbc6c139ded380f597d8 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 27 May 2022 16:12:46 -0700 Subject: web: refactor svg drawing and related code --- web/web_dom.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'web/web_dom.c') diff --git a/web/web_dom.c b/web/web_dom.c index 54a6c8d..2467fb6 100644 --- a/web/web_dom.c +++ b/web/web_dom.c @@ -1,5 +1,6 @@ #include "web_dom.h" +#include #include #include #include @@ -56,7 +57,9 @@ void dom_elem_free(struct dom_elem *e) free(e); } -void dom_elem_add_attr(struct dom_elem *e, char *name, char *val) +// this function takes ownership of the passed val string, freeing it when the +// dom_elem is freed. +static void dom_elem_add_attrp(struct dom_elem *e, char *name, char *val) { if (e->n_attrs == e->size_attrs) { e->size_attrs += e->size_attrs ? e->size_attrs : 1; @@ -67,14 +70,31 @@ void dom_elem_add_attr(struct dom_elem *e, char *name, char *val) } e->attr_names[e->n_attrs] = strdup(name); - e->attr_vals[e->n_attrs++] = strdup(val); + e->attr_vals[e->n_attrs++] = val; +} + +void dom_elem_add_attr(struct dom_elem *e, char *name, char *val) +{ + dom_elem_add_attrp(e, name, strdup(val)); } void dom_elem_add_attrd(struct dom_elem *e, char *name, double val) { - char str[512]; - sprintf(str, "%f", val); - dom_elem_add_attr(e, name, str); + char *str; + asprintf(&str, "%f", val); + dom_elem_add_attrp(e, name, str); +} + +void dom_elem_add_attrf(struct dom_elem *e, char *name, char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + char *str; + vasprintf(&str, fmt, args); + dom_elem_add_attrp(e, name, str); + + va_end(args); } void dom_elem_add_child(struct dom_elem *e, struct dom_elem *child) -- cgit v1.2.3-70-g09d2