XMLNode.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "XMLNode.h"
  2. static int CompareStuff(const wchar_t *const &str1, const wchar_t *const &str2)
  3. {
  4. return CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), 0, str1, -1, str2, -1)-2;
  5. }
  6. XMLNode::XMLNode() : content(0), content_len(0), parent(0)
  7. {
  8. }
  9. XMLNode::~XMLNode()
  10. {
  11. for (PropMap::iterator mapItr=properties.begin();mapItr != properties.end();mapItr++)
  12. {
  13. free((wchar_t *)mapItr->first);
  14. free(mapItr->second);
  15. }
  16. for (NodeMap::iterator mapItr=nodes.begin();mapItr != nodes.end();mapItr++)
  17. {
  18. NodeList * const nodeList = mapItr->second;
  19. if (nodeList)
  20. {
  21. for (NodeList::iterator itr=nodeList->begin(); itr!= nodeList->end(); itr++)
  22. {
  23. delete static_cast<XMLNode *>(*itr);
  24. }
  25. }
  26. free((wchar_t *)mapItr->first);
  27. }
  28. if (content)
  29. {
  30. free(content);
  31. content = 0;
  32. content_len = 0;
  33. }
  34. }
  35. const XMLNode *XMLNode::Get(const wchar_t *tagName) const
  36. {
  37. NodeMap::const_iterator itr = nodes.find(tagName);
  38. if (itr == nodes.end())
  39. return 0;
  40. else
  41. {
  42. NodeList *list = itr->second;
  43. return list->at(0);
  44. }
  45. }
  46. const XMLNode::NodeList *XMLNode::GetList(const wchar_t *tagName) const
  47. {
  48. NodeMap::const_iterator itr = nodes.find(tagName);
  49. if (itr == nodes.end())
  50. {
  51. return 0;
  52. }
  53. else
  54. {
  55. NodeList *list = itr->second;
  56. return list;
  57. }
  58. }
  59. const bool XMLNode::Present(const wchar_t *tagName) const
  60. {
  61. return nodes.find(tagName) != nodes.end();
  62. }
  63. // LEGACY implementaions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  64. //void XMLNode::SetProperty(const wchar_t *prop, const wchar_t *value)
  65. //{
  66. // PropMap::MapPair &pair = properties.getItem(prop);
  67. // if (pair.first == prop) // replace with a copy if we made a new entry
  68. // pair.first = _wcsdup(prop);
  69. // free(pair.second);
  70. // pair.second = _wcsdup(value);
  71. //}
  72. void XMLNode::SetProperty(const wchar_t* prop, const wchar_t* value)
  73. {
  74. auto it = properties.find(prop);
  75. if (properties.end() == it)
  76. {
  77. properties.insert({ _wcsdup(prop), _wcsdup(value) });
  78. }
  79. else
  80. {
  81. if (nullptr != it->second)
  82. {
  83. free(it->second);
  84. }
  85. properties[prop] = _wcsdup(value);
  86. }
  87. }
  88. void XMLNode::SetContent_Own(wchar_t *new_content)
  89. {
  90. if (content)
  91. {
  92. free(content);
  93. content = 0;
  94. content_len = 0;
  95. }
  96. if (new_content && *new_content)
  97. {
  98. content = new_content;
  99. content_len = wcslen(content);
  100. }
  101. }
  102. void XMLNode::AppendContent(wchar_t *append)
  103. {
  104. if (append && *append)
  105. {
  106. if (content)
  107. {
  108. size_t len = wcslen(append), new_len = len + content_len;
  109. wchar_t *new_content = (wchar_t *)realloc(content, (new_len+1)*sizeof(wchar_t));
  110. if (new_content)
  111. {
  112. content = new_content;
  113. wcsncpy(content+content_len, append, len);
  114. *(content+content_len+len) = 0;
  115. content_len = new_len;
  116. }
  117. else
  118. {
  119. new_content = (wchar_t *)malloc((new_len+1)*sizeof(wchar_t));
  120. if (new_content)
  121. {
  122. memcpy(new_content, content, content_len*sizeof(wchar_t));
  123. free(content);
  124. content = new_content;
  125. wcsncpy(content+content_len, append, len);
  126. *(content+content_len+len) = 0;
  127. content_len = new_len;
  128. }
  129. }
  130. }
  131. else
  132. {
  133. content_len = wcslen(append);
  134. content = (wchar_t *)malloc((content_len+1)*sizeof(wchar_t));
  135. if (content)
  136. memcpy(content, append, (content_len+1)*sizeof(wchar_t));
  137. }
  138. }
  139. }
  140. // LEGACY implementaions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  141. //void XMLNode::AddNode(const wchar_t *name, XMLNode *new_node)
  142. //{
  143. // // first, add entry in nodes map
  144. // NodeMap::MapPair &pair = nodes.getItem(name);
  145. // if (pair.first == name) // replace with a copy if we made a new entry
  146. // pair.first = _wcsdup(name);
  147. //
  148. // // make the node list if we need it
  149. // if (!pair.second)
  150. // pair.second = new NodeList;
  151. //
  152. // pair.second->push_back(new_node);
  153. //}
  154. void XMLNode::AddNode(const wchar_t* name, XMLNode* new_node)
  155. {
  156. auto it = nodes.find(name);
  157. if (nodes.end() == it)
  158. {
  159. nodes.insert({ _wcsdup(name), new NodeList() });
  160. }
  161. else
  162. {
  163. if (nullptr == it->second)
  164. {
  165. nodes[name] = new NodeList();
  166. }
  167. }
  168. nodes[name]->push_back(new_node);
  169. }
  170. const wchar_t *XMLNode::GetContent() const
  171. {
  172. return content;
  173. }
  174. const wchar_t *XMLNode::GetProperty(const wchar_t *prop) const
  175. {
  176. for (PropMap::const_iterator mapItr = properties.begin(); mapItr != properties.end(); mapItr++)
  177. {
  178. if (CompareStuff(mapItr->first, prop) == 0)
  179. {
  180. return mapItr->second;
  181. }
  182. }
  183. return 0;
  184. }