domnode-xml.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  3. *
  4. * See the COPYING file for the terms of usage and distribution.
  5. */
  6. #include <tchar.h>
  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. static const TCHAR version[] = _T("$Id$");
  18. extern int __sd_domnode_xml_parse(struct __sd_domnode_xml_maker*);
  19. /******************************************************************************/
  20. static int xml_fwrite(const sd_domnode_t* ptrThis, FILE* a_stream, int a_indent)
  21. {
  22. sd_list_iter_t* iter;
  23. int i;
  24. if (!ptrThis || !ptrThis->name || !a_stream)
  25. return -1;
  26. for (i = 0; i < a_indent; i++)
  27. _ftprintf(a_stream, _T(" "));
  28. if (ptrThis->name && _tcscmp(ptrThis->name, _T("#comment")) == 0) {
  29. _ftprintf(a_stream, _T("<!-- %s -->\n"), ptrThis->value);
  30. return 0;
  31. }
  32. _ftprintf(a_stream, _T("<%s"), ptrThis->name);
  33. for (iter = sd_list_begin(ptrThis->attrs); iter != sd_list_end(ptrThis->attrs);
  34. iter = sd_list_iter_next(iter)) {
  35. sd_domnode_t* node = (sd_domnode_t*)iter->data;
  36. _ftprintf(a_stream, _T(" %s=\"%s\""), node->name, node->value);
  37. }
  38. if (ptrThis->value || sd_list_get_nelem(ptrThis->children)) {
  39. _ftprintf(a_stream, _T(">\n"));
  40. if (ptrThis->value) {
  41. for (i = 0; i < a_indent + 1; i++)
  42. _ftprintf(a_stream, _T(" "));
  43. _ftprintf(a_stream, _T("%s\n"), ptrThis->value);
  44. }
  45. for (iter = sd_list_begin(ptrThis->children);
  46. iter != sd_list_end(ptrThis->children);
  47. iter = sd_list_iter_next(iter)) {
  48. sd_domnode_t* node = (sd_domnode_t*)iter->data;
  49. if (xml_fwrite(node, a_stream, a_indent + 1) == -1)
  50. return -1;
  51. }
  52. for (i = 0; i < a_indent; i++)
  53. _ftprintf(a_stream, _T(" "));
  54. _ftprintf(a_stream, _T("</%s>\n"), ptrThis->name);
  55. } else {
  56. _ftprintf(a_stream, _T("/>\n"));
  57. }
  58. return 0;
  59. }
  60. /******************************************************************************/
  61. extern int __sd_domnode_xml_write(const sd_domnode_t* ptrThis, TCHAR** a_buffer, 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. _ftprintf(a_stream, _T("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n"),nl_langinfo(CODESET));
  71. #else
  72. _ftprintf(a_stream, _T("<?xml version=\"1.0\"?>\n\n"));
  73. #endif
  74. return xml_fwrite(ptrThis, a_stream, 0);
  75. }
  76. /******************************************************************************/
  77. static int xml_parse(sd_domnode_t** a_node, yyscan_t a_scanner)
  78. {
  79. int r;
  80. struct __sd_domnode_xml_maker maker;
  81. maker.scanner = a_scanner;
  82. maker.elements = sd_stack_new(0);
  83. maker.root = 0;
  84. if (! (r = __sd_domnode_xml_parse(&maker))) *a_node = maker.root;
  85. sd_stack_delete(maker.elements, 0);
  86. return r;
  87. }
  88. /******************************************************************************/
  89. extern int __sd_domnode_xml_fread(sd_domnode_t** a_node, FILE* a_stream)
  90. {
  91. int r;
  92. yyscan_t scanner;
  93. yylex_init(&scanner);
  94. yyset_in(a_stream, scanner);
  95. r = xml_parse(a_node, scanner);
  96. yylex_destroy(scanner);
  97. return r;
  98. }
  99. /******************************************************************************/
  100. extern int __sd_domnode_xml_read(sd_domnode_t** a_node, const TCHAR* a_buffer,size_t a_size)
  101. {
  102. int r;
  103. yyscan_t scanner;
  104. yylex_init(&scanner);
  105. yy_switch_to_buffer(yy_scan_bytes(a_buffer, a_size, scanner), scanner);
  106. r = xml_parse(a_node, scanner);
  107. yylex_destroy(scanner);
  108. return r;
  109. }