summaryrefslogtreecommitdiff
path: root/web/web_dom.c
diff options
context:
space:
mode:
Diffstat (limited to 'web/web_dom.c')
-rw-r--r--web/web_dom.c30
1 files changed, 25 insertions, 5 deletions
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 <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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)