src/server/webdav/xml.c

branch
webdav
changeset 319
a9b9344875aa
parent 318
60870dbac94f
child 324
44cf877b3d9f
equal deleted inserted replaced
318:60870dbac94f 319:a9b9344875aa
33 #include <ucx/string.h> 33 #include <ucx/string.h>
34 #include <ucx/map.h> 34 #include <ucx/map.h>
35 #include <ucx/buffer.h> 35 #include <ucx/buffer.h>
36 36
37 #include "../util/util.h" 37 #include "../util/util.h"
38 #include "../util/pool.h"
38 39
39 #include "xml.h" 40 #include "xml.h"
40 41
41 /***************************************************************************** 42 /*****************************************************************************
42 * Utility functions 43 * Utility functions
312 ucx_map_free(nsmap); 313 ucx_map_free(nsmap);
313 return list; 314 return list;
314 } 315 }
315 316
316 317
317 static ssize_t buf_writefunc(void *buf, const void *s, size_t len) { 318 static ssize_t buf_writefunc(void *buf, const char *s, size_t len) {
318 int w = ucx_buffer_write(s, 1, len, buf); 319 int w = ucx_buffer_write(s, 1, len, buf);
319 return w == 0 ? IO_ERROR : w; 320 return w == 0 ? IO_ERROR : w;
320 } 321 }
321 322
322 WSXmlData* wsxml_node2data( 323 WSXmlData* wsxml_node2data(
353 } 354 }
354 355
355 ucx_buffer_free(buf); 356 ucx_buffer_free(buf);
356 357
357 return data; 358 return data;
359 }
360
361 char* wsxml_nslist2string(pool_handle_t *pool, WebdavNSList *nslist) {
362 if(!nslist) return NULL;
363
364 // get required string length
365 size_t len = 0;
366 WebdavNSList *elm = nslist;
367 while(elm) {
368 WSNamespace *ns = elm->namespace;
369 if(ns) {
370 if(ns->prefix) len += strlen((const char*)ns->prefix);
371 if(ns->href) len += strlen((const char*)ns->href);
372 len += 2; // 1 char for ':', 1 char for \n or \0
373 }
374 elm = elm->next;
375 }
376
377 // alloc string
378 char *str = pool_malloc(pool, len);
379 if(!str) {
380 return NULL;
381 }
382 char *pos = str;
383
384 // copy namespace definitions to the string
385 elm = nslist;
386 while(elm) {
387 WSNamespace *ns = elm->namespace;
388 if(ns) {
389 if(ns->prefix) {
390 size_t prefixlen = strlen((const char*)ns->prefix);
391 memcpy(pos, ns->prefix, prefixlen);
392 pos[prefixlen] = ':';
393 pos += prefixlen + 1;
394 } else {
395 pos[0] = ':';
396 pos++;
397 }
398 if(ns->href) {
399 size_t hreflen = strlen((const char*)ns->href);
400 memcpy(pos, ns->href, hreflen);
401 pos[hreflen] = elm->next ? '\n' : '\0';
402 pos += hreflen + 1;
403 } else {
404 pos[0] = elm->next ? '\n' : '\0';
405 pos++;
406 }
407 }
408 elm = elm->next;
409 }
410
411 return str;
412 }
413
414 WebdavNSList* wsxml_string2nslist(pool_handle_t *pool, char *nsliststr) {
415 if(!nsliststr) return NULL;
416 size_t len = strlen(nsliststr);
417 WebdavNSList *list_start = NULL;
418 WebdavNSList *list_current = NULL;
419
420 char *prefix = nsliststr;
421 size_t prefix_start = 0;
422 size_t prefix_len = 0;
423 char *href = NULL;
424 size_t href_start = len;
425 size_t i;
426 for(i=0;i<=len;i++) {
427 char c = nsliststr[i];
428 if(c == '\n' || c == '\0') {
429 if(i > href_start) {
430 WebdavNSList *elm = pool_malloc(pool, sizeof(WebdavNSList));
431 if(!elm) {
432 break;
433 }
434 elm->prev = list_current;
435 elm->next = NULL;
436 WSNamespace *ns = pool_malloc(pool, sizeof(WSNamespace));
437 elm->namespace = ns;
438 if(!ns) {
439 break;
440 }
441 memset(ns, 0, sizeof(WSNamespace));
442 ns->prefix = prefix_len > 0 ? (xmlChar*)sstrdup_pool(pool, sstrn(prefix, prefix_len)).ptr : NULL;
443 ns->href = (xmlChar*)sstrdup_pool(pool, sstrn(href, i-href_start)).ptr;
444 if(list_current) {
445 list_current->next = elm;
446 } else {
447 list_start = elm;
448 }
449 list_current = elm;
450 }
451 prefix_start = i + 1;
452 prefix = nsliststr + prefix_start;
453 prefix_len = 0;
454 href_start = len;
455 href = NULL;
456 } else if(!href && c == ':') {
457 prefix_len = i - prefix_start;
458 href_start = i + 1;
459 href = nsliststr + href_start;
460 }
461 }
462
463 if(i < len) {
464 // error, cleanup
465 while(list_start) {
466 if(list_start->namespace) {
467 WSNamespace *ns = list_start->namespace;
468 if(ns->prefix) {
469 pool_free(pool, (char*)ns->prefix);
470 }
471 if(ns->href) {
472 pool_free(pool, (char*)ns->href);
473 }
474 pool_free(pool, ns);
475 }
476 WebdavNSList *next = list_start->next;
477 pool_free(pool, list_start);
478 list_start = next;
479 }
480 list_start = NULL;
481 }
482
483 return list_start;
358 } 484 }
359 485
360 /***************************************************************************** 486 /*****************************************************************************
361 * Non public functions 487 * Non public functions
362 *****************************************************************************/ 488 *****************************************************************************/

mercurial