domnode-xml.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. static const char version[] = "$Id$";
  2. /*
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <string.h>
  8. #ifdef HAVE_LANGINFO_H
  9. # include <langinfo.h>
  10. #endif
  11. #include "domnode-xml.h"
  12. /* Generated by bison(1) */
  13. #include "domnode-xml-parser.h"
  14. /* Generated by flex(1) */
  15. #define YY_HEADER_NO_UNDEFS 1
  16. #include "domnode-xml-scanner.h"
  17. extern int __sd_domnode_xml_parse(struct __sd_domnode_xml_maker*);
  18. /******************************************************************************/
  19. static int xml_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream, int a_indent)
  20. {
  21. sd_list_iter_t* iter;
  22. int i;
  23. if (!ptrThis || !ptrThis->name || !a_stream)
  24. return -1;
  25. for (i = 0; i < a_indent; i++)
  26. fprintf(a_stream, " ");
  27. if (ptrThis->name && strcmp(ptrThis->name, "#comment") == 0) {
  28. fprintf(a_stream, "<!-- %s -->\n", ptrThis->value);
  29. return 0;
  30. }
  31. fprintf(a_stream, "<%s", ptrThis->name);
  32. for (iter = sd_list_begin(ptrThis->attrs); iter != sd_list_end(ptrThis->attrs);
  33. iter = sd_list_iter_next(iter)) {
  34. sd_domnode_t* node = (sd_domnode_t*)iter->data;
  35. fprintf(a_stream, " %s=\"%s\"", node->name, node->value);
  36. }
  37. if (ptrThis->value || sd_list_get_nelem(ptrThis->children)) {
  38. fprintf(a_stream, ">\n");
  39. if (ptrThis->value) {
  40. for (i = 0; i < a_indent + 1; i++)
  41. fprintf(a_stream, " ");
  42. fprintf(a_stream, "%s\n", ptrThis->value);
  43. }
  44. for (iter = sd_list_begin(ptrThis->children);
  45. iter != sd_list_end(ptrThis->children);
  46. iter = sd_list_iter_next(iter)) {
  47. sd_domnode_t* node = (sd_domnode_t*)iter->data;
  48. if (xml_fwrite(node, a_stream, a_indent + 1) == -1)
  49. return -1;
  50. }
  51. for (i = 0; i < a_indent; i++)
  52. fprintf(a_stream, " ");
  53. fprintf(a_stream, "</%s>\n", ptrThis->name);
  54. } else {
  55. fprintf(a_stream, "/>\n");
  56. }
  57. return 0;
  58. }
  59. /******************************************************************************/
  60. extern int __sd_domnode_xml_write(const sd_domnode_t* ptrThis, char** a_buffer,
  61. size_t* a_size)
  62. {
  63. /* TODO: to be implemented */
  64. return -1;
  65. }
  66. /******************************************************************************/
  67. extern int __sd_domnode_xml_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream)
  68. {
  69. #ifdef HAVE_NL_LANGINFO
  70. fprintf(a_stream, "<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
  71. nl_langinfo(CODESET));
  72. #else
  73. fprintf(a_stream, "<?xml version=\"1.0\"?>\n\n");
  74. #endif
  75. return xml_fwrite(ptrThis, a_stream, 0);
  76. }
  77. /******************************************************************************/
  78. static int xml_parse(sd_domnode_t** a_node, yyscan_t a_scanner)
  79. {
  80. int r;
  81. struct __sd_domnode_xml_maker maker;
  82. maker.scanner = a_scanner;
  83. maker.elements = sd_stack_new(0);
  84. maker.root = 0;
  85. if (! (r = __sd_domnode_xml_parse(&maker))) *a_node = maker.root;
  86. sd_stack_delete(maker.elements, 0);
  87. return r;
  88. }
  89. /******************************************************************************/
  90. extern int __sd_domnode_xml_fread(sd_domnode_t** a_node, FILE* a_stream)
  91. {
  92. int r;
  93. yyscan_t scanner;
  94. yylex_init(&scanner);
  95. yyset_in(a_stream, scanner);
  96. r = xml_parse(a_node, scanner);
  97. yylex_destroy(scanner);
  98. return r;
  99. }
  100. /******************************************************************************/
  101. extern int __sd_domnode_xml_read(sd_domnode_t** a_node, const char* a_buffer,
  102. size_t a_size)
  103. {
  104. int r;
  105. yyscan_t scanner;
  106. yylex_init(&scanner);
  107. yy_switch_to_buffer(yy_scan_bytes(a_buffer, a_size, scanner), scanner);
  108. r = xml_parse(a_node, scanner);
  109. yylex_destroy(scanner);
  110. return r;
  111. }