libidav/xml.c

changeset 747
efbd59642577
parent 736
40be8db6fe45
child 756
ea0e059bae72
--- a/libidav/xml.c	Sun Apr 16 14:12:24 2023 +0200
+++ b/libidav/xml.c	Fri Apr 21 21:25:32 2023 +0200
@@ -30,7 +30,8 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <ucx/utils.h>
+#include <cx/utils.h>
+#include <cx/printf.h>
 
 #include "xml.h"
 
@@ -58,23 +59,27 @@
         return NULL;
     }
     
-    UcxMempool *mp = sn->mp;
+    const CxAllocator *a = sn->mp->allocator;
     
-    ConvXmlElm *ce = malloc(sizeof(ConvXmlElm));
-    ce->node = node;
-    ce->parent = NULL;
-    UcxList *stack = ucx_list_prepend(NULL, ce);
+    ConvXmlElm ce;
+    ce.node = node;
+    ce.parent = NULL;
+    CxList *stack = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(ConvXmlElm));
+    if(!stack) {
+        return NULL;
+    }
+    cxListInsert(stack, 0, &ce);
     
     DavXmlNode *ret = NULL;
     
-    while(stack) {
-        ConvXmlElm *c = stack->data;
-        stack = ucx_list_remove(stack, stack);
+    while(stack->size > 0) {
+        ConvXmlElm *c = cxListAt(stack, 0);
+        cxListRemove(stack, 0);
         
         xmlNode *n = c->node;
         DavXmlNode *prev = NULL;
         while(n) {
-            DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode));
+            DavXmlNode *newxn = cxCalloc(a, 1, sizeof(DavXmlNode));
             if(!ret) {
                 ret = newxn;
             }
@@ -98,7 +103,7 @@
                 DavXmlAttr *newattr = NULL;
                 DavXmlAttr *newattr_last = NULL;
                 while(attr) {
-                    DavXmlAttr *na = ucx_mempool_calloc(mp, 1, sizeof(DavXmlAttr));
+                    DavXmlAttr *na = cxCalloc(a, 1, sizeof(DavXmlAttr));
                     na->name = dav_session_strdup(sn, (char*)attr->name);
                     if(attr->children && attr->children->type == XML_TEXT_NODE) {
                         na->value = dav_session_strdup(sn, (char*)attr->children->content);
@@ -115,13 +120,13 @@
                 newxn->attributes = newattr;
                 
                 if(n->children) {
-                    ConvXmlElm *convc = malloc(sizeof(ConvXmlElm));
-                    convc->node = n->children;
-                    convc->parent = newxn;
-                    stack = ucx_list_prepend(stack, convc);
+                    ConvXmlElm convc;
+                    convc.node = n->children;
+                    convc.parent = newxn;
+                    cxListInsert(stack, 0, &convc);
                 }
             } else if(newxn->type == DAV_XML_TEXT) {
-                sstr_t content = sstrdup_a(mp->allocator, sstr((char*)n->content));
+                cxmutstr content = cx_strdup_a(a, cx_str((char*)n->content));
                 newxn->content = content.ptr;
                 newxn->contentlength = content.length;
             }
@@ -129,8 +134,6 @@
             prev = newxn;
             n = n->next;
         }
-        
-        free(c);
     }
     
     return ret;
@@ -161,19 +164,21 @@
     }
 }
 
-void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) {
+void dav_print_node(void *stream, cx_write_func writef, CxMap *nsmap, DavXmlNode *node) {
     while(node) {
         if(node->type == DAV_XML_ELEMENT) {
             char *tagend = node->children ? ">" : " />";          
             char *prefix = NULL;
+            char *prefix_fr = NULL;
             if(node->namespace) {
-                prefix = ucx_map_cstr_get(nsmap, node->namespace);
+                prefix = cxMapGet(nsmap, cx_hash_key_str(node->namespace));
                 if(!prefix) {
-                    sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count+1);
+                    cxmutstr newpre = cx_asprintf("x%d", (int)nsmap->size+1);
                     // TODO: fix namespace declaration
                     //ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
                     prefix = newpre.ptr;
-                    ucx_fprintf(
+                    prefix_fr = prefix;
+                    cx_fprintf(
                             stream,
                             writef,
                             "<%s:%s xmlns:%s=\"%s\"",
@@ -182,15 +187,15 @@
                             prefix,
                             node->namespace);
                 } else {
-                    ucx_fprintf(stream, writef, "<%s:%s", prefix, node->name);
+                    cx_fprintf(stream, writef, "<%s:%s", prefix, node->name);
                 }
             } else {
-                ucx_fprintf(stream, writef, "<%s", node->name);
+                cx_fprintf(stream, writef, "<%s", node->name);
             }
             
             DavXmlAttr *attr = node->attributes;
             while(attr) {
-                ucx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value);
+                cx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value);
                 attr = attr->next;
             }
             writef(tagend, 1, strlen(tagend), stream); // end xml tag
@@ -198,11 +203,15 @@
             if(node->children) {
                 dav_print_node(stream, writef, nsmap, node->children);
                 if(prefix) {
-                    ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
+                    cx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
                 } else {
-                    ucx_fprintf(stream, writef, "</%s>", node->name);
+                    cx_fprintf(stream, writef, "</%s>", node->name);
                 }
             }
+            
+            if(prefix_fr) {
+                free(prefix_fr);
+            }
         } else if(node->type == DAV_XML_TEXT) {
             writef(node->content, 1, node->contentlength, stream);
         }
@@ -241,33 +250,33 @@
 }
 
 DavXmlNode* dav_text_node(DavSession *sn, const char *text) {
-    UcxMempool *mp = sn->mp; 
-    DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode));
+    const CxAllocator *a = sn->mp->allocator; 
+    DavXmlNode *newxn = cxCalloc(a, 1, sizeof(DavXmlNode));
     newxn->type = DAV_XML_TEXT;
-    sstr_t content = scstrdup_a(mp->allocator, scstr(text));
+    cxmutstr content = cx_strdup_a(a, cx_str(text));
     newxn->content = content.ptr;
     newxn->contentlength = content.length;
     return newxn;
 }
 
 DavXmlNode* dav_text_element(DavSession *sn, const char *ns, const char *name, const char *text) {
-    UcxMempool *mp = sn->mp; 
-    DavXmlNode *newelm = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode));
+    const CxAllocator *a = sn->mp->allocator; 
+    DavXmlNode *newelm = cxCalloc(a, 1, sizeof(DavXmlNode));
     newelm->type = DAV_XML_ELEMENT;
-    newelm->namespace = scstrdup_a(mp->allocator, scstr(ns)).ptr;
-    newelm->name = scstrdup_a(mp->allocator, scstr(name)).ptr;
+    newelm->namespace = cx_strdup_a(a, cx_str(ns)).ptr;
+    newelm->name = cx_strdup_a(a, cx_str(name)).ptr;
     newelm->children = dav_text_node(sn, text);
     return newelm;
 }
 
-static void dav_free_xml_node_a(UcxAllocator *a, DavXmlNode *node) {
-    if(node->name) alfree(a, node->name);
-    if(node->namespace) alfree(a, node->namespace);
-    if(node->content) alfree(a, node->content);
+static void dav_free_xml_node_a(const CxAllocator *a, DavXmlNode *node) {
+    if(node->name) cxFree(a, node->name);
+    if(node->namespace) cxFree(a, node->namespace);
+    if(node->content) cxFree(a, node->content);
     DavXmlAttr *attr = node->attributes;
     while(attr) {
-        if(attr->name) alfree(a, attr->name);
-        if(attr->value) alfree(a, attr->value);
+        if(attr->name) cxFree(a, attr->name);
+        if(attr->value) cxFree(a, attr->value);
         attr = attr->next;
     }
     DavXmlNode *children = node->children;
@@ -276,7 +285,7 @@
         dav_free_xml_node_a(a, children);
         children = next_ch;
     }
-    alfree(a, node);
+    cxFree(a, node);
 }
 
 void dav_free_xml_node_sn(DavSession *sn, DavXmlNode *node) {
@@ -284,7 +293,7 @@
 }
 
 void dav_free_xml_node(DavXmlNode *node) {
-    dav_free_xml_node_a(ucx_default_allocator(), node);
+    dav_free_xml_node_a(cxDefaultAllocator, node);
 }
 
 DavXmlAttr* dav_copy_xml_attr(DavXmlAttr *attr) {
@@ -362,7 +371,7 @@
 DavXmlNode* dav_xml_createtextnode(const char *text) {
     DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
     node->type = DAV_XML_TEXT;
-    sstr_t content = sstrdup(sstr((char*)text));
+    cxmutstr content = cx_strdup(cx_str((char*)text));
     node->content = content.ptr;
     node->contentlength = content.length;
     return node;

mercurial