about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <sternenseemann@systemli.org>2020-08-11 14:38:37 +0200
committersternenseemann <sternenseemann@systemli.org>2020-08-11 14:38:37 +0200
commit5e41e6e81915801da14d0ef582fb7376b77247b4 (patch)
tree0ce4ed2a212a1a5aa34fcfaf85f2cbfd6d6a993b
parentf863b428f301e4b549308c06f2cf14c6ad287fb7 (diff)
refactor(xml): simplify warnings using macro
also point stack head to old stack if malloc fails so del_xml_context
can still free it.
-rw-r--r--xml.c46
1 files changed, 18 insertions, 28 deletions
diff --git a/xml.c b/xml.c
index 5337917..ca319c9 100644
--- a/xml.c
+++ b/xml.c
@@ -7,6 +7,11 @@
 
 #include "xml.h"
 
+#define DEBUG_WARN(ctx, ...) \
+    if(ctx->warn != NULL) { \
+        fprintf(ctx->warn, __VA_ARGS__); \
+    }
+
 void debug_xml_stack(FILE *out, struct xml_stack *stack) {
     if(stack != NULL) {
         fprintf(out, "%s ", stack->tag);
@@ -110,9 +115,7 @@ void output_attrs(FILE *out, va_list attrs, size_t arg_count) {
 
 void xml_empty_tag(struct xml_context *ctx, const char *tag, size_t attr_count, ...) {
     if(tag == NULL || ctx == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Got no tag or ctx\n", ctx->warn);
-        }
+        DEBUG_WARN(ctx, "Got no tag or ctx\n");
         return;
     }
 
@@ -139,9 +142,7 @@ void xml_empty_tag(struct xml_context *ctx, const char *tag, size_t attr_count,
 
 void xml_open_tag_attrs(struct xml_context *ctx, const char *tag, size_t attr_count, ...) {
     if(tag == NULL || ctx == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Got no tag or ctx\n", ctx->warn);
-        }
+        DEBUG_WARN(ctx, "Got no tag or ctx\n");
         return;
     }
 
@@ -167,9 +168,8 @@ void xml_open_tag_attrs(struct xml_context *ctx, const char *tag, size_t attr_co
     ctx->stack = malloc(sizeof(struct xml_context));
 
     if(ctx->stack == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Could not allocate memory for tag stack, now everything will break.\n", ctx->warn);
-        }
+        ctx->stack = old_stack;
+        DEBUG_WARN(ctx, "Could not allocate memory for tag stack, now everything will break.\n")
         return;
     }
 
@@ -186,20 +186,16 @@ void xml_open_tag(struct xml_context *ctx, const char *tag) {
 
 void xml_close_tag(struct xml_context *ctx, const char *tag) {
     if(tag == NULL || ctx == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Got no tag or ctx\n", ctx->warn);
-        }
+        DEBUG_WARN(ctx, "Got no tag or ctx\n");
         return;
     }
 
     if(ctx->stack == NULL || strcmp(tag, ctx->stack->tag) != 0) {
-        if(ctx->warn != NULL) {
-            fprintf(ctx->warn, "Refusing to close tag %s, ", tag);
-            if(ctx->stack == NULL) {
-                fputs("no tags left to be closed\n", ctx->warn);
-            } else {
-                fputs("unclosed tags remaining\n", ctx->warn);
-            }
+        DEBUG_WARN(ctx, "Refusing to close tag %s, ", tag);
+        if(ctx->stack == NULL) {
+            DEBUG_WARN(ctx, "no tags left to be closed\n");
+        } else {
+            DEBUG_WARN(ctx, "unclosed tags remaining\n");
         }
         return;
     }
@@ -218,9 +214,7 @@ void xml_close_tag(struct xml_context *ctx, const char *tag) {
 
 void xml_close_all(struct xml_context *ctx) {
     if(ctx == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Got no ctx\n", ctx->warn);
-        }
+        DEBUG_WARN(ctx, "Got no ctx\n");
         return;
     }
 
@@ -234,16 +228,12 @@ void xml_close_all(struct xml_context *ctx) {
 
 void xml_close_including(struct xml_context *ctx, const char *tag) {
     if(ctx == NULL) {
-        if(ctx->warn != NULL) {
-            fputs("Got no ctx\n", ctx->warn);
-        }
+        DEBUG_WARN(ctx, "Got no ctx\n");
         return;
     }
 
     if(ctx->stack == NULL) {
-        if(ctx->warn != NULL) {
-            fprintf(ctx->warn, "Hit end of tag stack while searching for tag %s to close\n", tag);
-        }
+        DEBUG_WARN(ctx, "Hit end of tag stack while searching for tag %s to close\n", tag);
         return;
     } else {
         int last_tag = strcmp(tag, ctx->stack->tag) == 0;