libidav/xml.c

changeset 747
efbd59642577
parent 736
40be8db6fe45
child 756
ea0e059bae72
equal deleted inserted replaced
746:a569148841ff 747:efbd59642577
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 32
33 #include <ucx/utils.h> 33 #include <cx/utils.h>
34 #include <cx/printf.h>
34 35
35 #include "xml.h" 36 #include "xml.h"
36 37
37 static DavXmlNodeType convert_type(xmlElementType type) { 38 static DavXmlNodeType convert_type(xmlElementType type) {
38 DavXmlNodeType ct; 39 DavXmlNodeType ct;
56 DavXmlNodeType newnt = convert_type(node->type); 57 DavXmlNodeType newnt = convert_type(node->type);
57 if(newnt == DAV_XML_NONE) { 58 if(newnt == DAV_XML_NONE) {
58 return NULL; 59 return NULL;
59 } 60 }
60 61
61 UcxMempool *mp = sn->mp; 62 const CxAllocator *a = sn->mp->allocator;
62 63
63 ConvXmlElm *ce = malloc(sizeof(ConvXmlElm)); 64 ConvXmlElm ce;
64 ce->node = node; 65 ce.node = node;
65 ce->parent = NULL; 66 ce.parent = NULL;
66 UcxList *stack = ucx_list_prepend(NULL, ce); 67 CxList *stack = cxLinkedListCreate(cxDefaultAllocator, NULL, sizeof(ConvXmlElm));
68 if(!stack) {
69 return NULL;
70 }
71 cxListInsert(stack, 0, &ce);
67 72
68 DavXmlNode *ret = NULL; 73 DavXmlNode *ret = NULL;
69 74
70 while(stack) { 75 while(stack->size > 0) {
71 ConvXmlElm *c = stack->data; 76 ConvXmlElm *c = cxListAt(stack, 0);
72 stack = ucx_list_remove(stack, stack); 77 cxListRemove(stack, 0);
73 78
74 xmlNode *n = c->node; 79 xmlNode *n = c->node;
75 DavXmlNode *prev = NULL; 80 DavXmlNode *prev = NULL;
76 while(n) { 81 while(n) {
77 DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode)); 82 DavXmlNode *newxn = cxCalloc(a, 1, sizeof(DavXmlNode));
78 if(!ret) { 83 if(!ret) {
79 ret = newxn; 84 ret = newxn;
80 } 85 }
81 newxn->type = convert_type(n->type); 86 newxn->type = convert_type(n->type);
82 newxn->parent = c->parent; 87 newxn->parent = c->parent;
96 101
97 xmlAttr *attr = n->properties; 102 xmlAttr *attr = n->properties;
98 DavXmlAttr *newattr = NULL; 103 DavXmlAttr *newattr = NULL;
99 DavXmlAttr *newattr_last = NULL; 104 DavXmlAttr *newattr_last = NULL;
100 while(attr) { 105 while(attr) {
101 DavXmlAttr *na = ucx_mempool_calloc(mp, 1, sizeof(DavXmlAttr)); 106 DavXmlAttr *na = cxCalloc(a, 1, sizeof(DavXmlAttr));
102 na->name = dav_session_strdup(sn, (char*)attr->name); 107 na->name = dav_session_strdup(sn, (char*)attr->name);
103 if(attr->children && attr->children->type == XML_TEXT_NODE) { 108 if(attr->children && attr->children->type == XML_TEXT_NODE) {
104 na->value = dav_session_strdup(sn, (char*)attr->children->content); 109 na->value = dav_session_strdup(sn, (char*)attr->children->content);
105 } 110 }
106 if(!newattr) { 111 if(!newattr) {
113 attr = attr->next; 118 attr = attr->next;
114 } 119 }
115 newxn->attributes = newattr; 120 newxn->attributes = newattr;
116 121
117 if(n->children) { 122 if(n->children) {
118 ConvXmlElm *convc = malloc(sizeof(ConvXmlElm)); 123 ConvXmlElm convc;
119 convc->node = n->children; 124 convc.node = n->children;
120 convc->parent = newxn; 125 convc.parent = newxn;
121 stack = ucx_list_prepend(stack, convc); 126 cxListInsert(stack, 0, &convc);
122 } 127 }
123 } else if(newxn->type == DAV_XML_TEXT) { 128 } else if(newxn->type == DAV_XML_TEXT) {
124 sstr_t content = sstrdup_a(mp->allocator, sstr((char*)n->content)); 129 cxmutstr content = cx_strdup_a(a, cx_str((char*)n->content));
125 newxn->content = content.ptr; 130 newxn->content = content.ptr;
126 newxn->contentlength = content.length; 131 newxn->contentlength = content.length;
127 } 132 }
128 133
129 prev = newxn; 134 prev = newxn;
130 n = n->next; 135 n = n->next;
131 } 136 }
132
133 free(c);
134 } 137 }
135 138
136 return ret; 139 return ret;
137 } 140 }
138 141
159 if(node->next) { 162 if(node->next) {
160 dav_print_xml(node->next); 163 dav_print_xml(node->next);
161 } 164 }
162 } 165 }
163 166
164 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) { 167 void dav_print_node(void *stream, cx_write_func writef, CxMap *nsmap, DavXmlNode *node) {
165 while(node) { 168 while(node) {
166 if(node->type == DAV_XML_ELEMENT) { 169 if(node->type == DAV_XML_ELEMENT) {
167 char *tagend = node->children ? ">" : " />"; 170 char *tagend = node->children ? ">" : " />";
168 char *prefix = NULL; 171 char *prefix = NULL;
172 char *prefix_fr = NULL;
169 if(node->namespace) { 173 if(node->namespace) {
170 prefix = ucx_map_cstr_get(nsmap, node->namespace); 174 prefix = cxMapGet(nsmap, cx_hash_key_str(node->namespace));
171 if(!prefix) { 175 if(!prefix) {
172 sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count+1); 176 cxmutstr newpre = cx_asprintf("x%d", (int)nsmap->size+1);
173 // TODO: fix namespace declaration 177 // TODO: fix namespace declaration
174 //ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr); 178 //ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
175 prefix = newpre.ptr; 179 prefix = newpre.ptr;
176 ucx_fprintf( 180 prefix_fr = prefix;
181 cx_fprintf(
177 stream, 182 stream,
178 writef, 183 writef,
179 "<%s:%s xmlns:%s=\"%s\"", 184 "<%s:%s xmlns:%s=\"%s\"",
180 prefix, 185 prefix,
181 node->name, 186 node->name,
182 prefix, 187 prefix,
183 node->namespace); 188 node->namespace);
184 } else { 189 } else {
185 ucx_fprintf(stream, writef, "<%s:%s", prefix, node->name); 190 cx_fprintf(stream, writef, "<%s:%s", prefix, node->name);
186 } 191 }
187 } else { 192 } else {
188 ucx_fprintf(stream, writef, "<%s", node->name); 193 cx_fprintf(stream, writef, "<%s", node->name);
189 } 194 }
190 195
191 DavXmlAttr *attr = node->attributes; 196 DavXmlAttr *attr = node->attributes;
192 while(attr) { 197 while(attr) {
193 ucx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value); 198 cx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value);
194 attr = attr->next; 199 attr = attr->next;
195 } 200 }
196 writef(tagend, 1, strlen(tagend), stream); // end xml tag 201 writef(tagend, 1, strlen(tagend), stream); // end xml tag
197 202
198 if(node->children) { 203 if(node->children) {
199 dav_print_node(stream, writef, nsmap, node->children); 204 dav_print_node(stream, writef, nsmap, node->children);
200 if(prefix) { 205 if(prefix) {
201 ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name); 206 cx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
202 } else { 207 } else {
203 ucx_fprintf(stream, writef, "</%s>", node->name); 208 cx_fprintf(stream, writef, "</%s>", node->name);
204 } 209 }
210 }
211
212 if(prefix_fr) {
213 free(prefix_fr);
205 } 214 }
206 } else if(node->type == DAV_XML_TEXT) { 215 } else if(node->type == DAV_XML_TEXT) {
207 writef(node->content, 1, node->contentlength, stream); 216 writef(node->content, 1, node->contentlength, stream);
208 } 217 }
209 218
239 } 248 }
240 return NULL; 249 return NULL;
241 } 250 }
242 251
243 DavXmlNode* dav_text_node(DavSession *sn, const char *text) { 252 DavXmlNode* dav_text_node(DavSession *sn, const char *text) {
244 UcxMempool *mp = sn->mp; 253 const CxAllocator *a = sn->mp->allocator;
245 DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode)); 254 DavXmlNode *newxn = cxCalloc(a, 1, sizeof(DavXmlNode));
246 newxn->type = DAV_XML_TEXT; 255 newxn->type = DAV_XML_TEXT;
247 sstr_t content = scstrdup_a(mp->allocator, scstr(text)); 256 cxmutstr content = cx_strdup_a(a, cx_str(text));
248 newxn->content = content.ptr; 257 newxn->content = content.ptr;
249 newxn->contentlength = content.length; 258 newxn->contentlength = content.length;
250 return newxn; 259 return newxn;
251 } 260 }
252 261
253 DavXmlNode* dav_text_element(DavSession *sn, const char *ns, const char *name, const char *text) { 262 DavXmlNode* dav_text_element(DavSession *sn, const char *ns, const char *name, const char *text) {
254 UcxMempool *mp = sn->mp; 263 const CxAllocator *a = sn->mp->allocator;
255 DavXmlNode *newelm = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode)); 264 DavXmlNode *newelm = cxCalloc(a, 1, sizeof(DavXmlNode));
256 newelm->type = DAV_XML_ELEMENT; 265 newelm->type = DAV_XML_ELEMENT;
257 newelm->namespace = scstrdup_a(mp->allocator, scstr(ns)).ptr; 266 newelm->namespace = cx_strdup_a(a, cx_str(ns)).ptr;
258 newelm->name = scstrdup_a(mp->allocator, scstr(name)).ptr; 267 newelm->name = cx_strdup_a(a, cx_str(name)).ptr;
259 newelm->children = dav_text_node(sn, text); 268 newelm->children = dav_text_node(sn, text);
260 return newelm; 269 return newelm;
261 } 270 }
262 271
263 static void dav_free_xml_node_a(UcxAllocator *a, DavXmlNode *node) { 272 static void dav_free_xml_node_a(const CxAllocator *a, DavXmlNode *node) {
264 if(node->name) alfree(a, node->name); 273 if(node->name) cxFree(a, node->name);
265 if(node->namespace) alfree(a, node->namespace); 274 if(node->namespace) cxFree(a, node->namespace);
266 if(node->content) alfree(a, node->content); 275 if(node->content) cxFree(a, node->content);
267 DavXmlAttr *attr = node->attributes; 276 DavXmlAttr *attr = node->attributes;
268 while(attr) { 277 while(attr) {
269 if(attr->name) alfree(a, attr->name); 278 if(attr->name) cxFree(a, attr->name);
270 if(attr->value) alfree(a, attr->value); 279 if(attr->value) cxFree(a, attr->value);
271 attr = attr->next; 280 attr = attr->next;
272 } 281 }
273 DavXmlNode *children = node->children; 282 DavXmlNode *children = node->children;
274 while(children) { 283 while(children) {
275 DavXmlNode *next_ch = children->next; 284 DavXmlNode *next_ch = children->next;
276 dav_free_xml_node_a(a, children); 285 dav_free_xml_node_a(a, children);
277 children = next_ch; 286 children = next_ch;
278 } 287 }
279 alfree(a, node); 288 cxFree(a, node);
280 } 289 }
281 290
282 void dav_free_xml_node_sn(DavSession *sn, DavXmlNode *node) { 291 void dav_free_xml_node_sn(DavSession *sn, DavXmlNode *node) {
283 dav_free_xml_node_a(sn->mp->allocator, node); 292 dav_free_xml_node_a(sn->mp->allocator, node);
284 } 293 }
285 294
286 void dav_free_xml_node(DavXmlNode *node) { 295 void dav_free_xml_node(DavXmlNode *node) {
287 dav_free_xml_node_a(ucx_default_allocator(), node); 296 dav_free_xml_node_a(cxDefaultAllocator, node);
288 } 297 }
289 298
290 DavXmlAttr* dav_copy_xml_attr(DavXmlAttr *attr) { 299 DavXmlAttr* dav_copy_xml_attr(DavXmlAttr *attr) {
291 if(!attr) { 300 if(!attr) {
292 return NULL; 301 return NULL;
360 } 369 }
361 370
362 DavXmlNode* dav_xml_createtextnode(const char *text) { 371 DavXmlNode* dav_xml_createtextnode(const char *text) {
363 DavXmlNode *node = calloc(1, sizeof(DavXmlNode)); 372 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
364 node->type = DAV_XML_TEXT; 373 node->type = DAV_XML_TEXT;
365 sstr_t content = sstrdup(sstr((char*)text)); 374 cxmutstr content = cx_strdup(cx_str((char*)text));
366 node->content = content.ptr; 375 node->content = content.ptr;
367 node->contentlength = content.length; 376 node->contentlength = content.length;
368 return node; 377 return node;
369 } 378 }
370 379

mercurial