You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmltag.cc 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Functions to deal with xml_tags
  3. Copyright (C) 2005 Ron Parker and Robert Lipe.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "cet_util.h"
  18. #include "src/core/xmltag.h"
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. static void
  23. free_xml_tag(xml_tag* tag)
  24. {
  25. xml_tag* next = NULL;
  26. char** ap;
  27. while (tag) {
  28. if (tag->child) {
  29. free_gpx_extras(tag->child);
  30. }
  31. if (tag->attributes) {
  32. ap = tag->attributes;
  33. while (*ap) {
  34. xfree(*ap++);
  35. }
  36. xfree(tag->attributes);
  37. }
  38. next = tag->sibling;
  39. delete tag;
  40. tag = next;
  41. }
  42. }
  43. // FIXME: at some point, this becomes a plain ole copy constructor.
  44. static void
  45. copy_xml_tag(xml_tag** copy, xml_tag* src, xml_tag* parent)
  46. {
  47. xml_tag* res = NULL;
  48. char** ap = NULL;
  49. char** ap2 = NULL;
  50. int count = 0;
  51. if (!src) {
  52. *copy = NULL;
  53. return;
  54. }
  55. res = new xml_tag;
  56. *copy = res;
  57. // memcpy(res, src, sizeof(xml_tag));
  58. res->tagname = (src->tagname);
  59. res->cdata = (src->cdata);
  60. res->parentcdata = (src->parentcdata);
  61. if (src->attributes) {
  62. ap = src->attributes;
  63. while (*ap) {
  64. count++;
  65. ap++;
  66. }
  67. res->attributes = (char**)xcalloc(count+1, sizeof(char*));
  68. ap = src->attributes;
  69. ap2 = res->attributes;
  70. while (*ap) {
  71. *ap2 = xstrdup(*ap);
  72. ap++;
  73. ap2++;
  74. }
  75. }
  76. res->parent = parent;
  77. copy_xml_tag(&(res->sibling), src->sibling, parent);
  78. copy_xml_tag(&(res->child), src->child, res);
  79. }
  80. static void
  81. convert_xml_tag(xml_tag* tag)
  82. {
  83. char** ap = NULL;
  84. if (tag == NULL) {
  85. return;
  86. }
  87. // tag->cdata = cet_convert_string(tag->cdata);
  88. // tag->parentcdata = cet_convert_string(tag->parentcdata);
  89. tag->cdata = tag->cdata;
  90. tag->parentcdata = tag->parentcdata;
  91. ap = tag->attributes;
  92. while (*ap) {
  93. *ap = cet_convert_string(*ap);
  94. ap++;
  95. }
  96. convert_xml_tag(tag->sibling);
  97. convert_xml_tag(tag->child);
  98. }
  99. fs_xml* fs_xml_alloc(long type);
  100. static void
  101. fs_xml_destroy(void* fs)
  102. {
  103. fs_xml* xml = (fs_xml*)fs;
  104. if (xml) {
  105. free_xml_tag(xml->tag);
  106. }
  107. xfree(fs);
  108. }
  109. static void
  110. fs_xml_copy(void** copy, void* source)
  111. {
  112. fs_xml* src = (fs_xml*)source;
  113. if (!source) {
  114. *copy = NULL;
  115. return;
  116. }
  117. *copy = (void*)fs_xml_alloc(src->fs.type);
  118. memcpy(*copy, source, sizeof(fs_xml));
  119. copy_xml_tag(&(((fs_xml*)(*copy))->tag), src->tag, NULL);
  120. }
  121. static void
  122. fs_xml_convert(void* fs)
  123. {
  124. fs_xml* xml = (fs_xml*)fs;
  125. if (xml) {
  126. convert_xml_tag(xml->tag);
  127. }
  128. }
  129. fs_xml* fs_xml_alloc(long type)
  130. {
  131. fs_xml* result = NULL;
  132. result = (fs_xml*)xcalloc(1, sizeof(fs_xml));
  133. result->fs.type = type;
  134. result->fs.copy = fs_xml_copy;
  135. result->fs.destroy = fs_xml_destroy;
  136. result->fs.convert = fs_xml_convert;
  137. return result;
  138. }