XMLBinaryNode.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <platform.h>
  9. #include "Cry_Color.h"
  10. #include "XMLBinaryNode.h"
  11. //////////////////////////////////////////////////////////////////////////
  12. CBinaryXmlData::~CBinaryXmlData()
  13. {
  14. if (bOwnsFileContentsMemory)
  15. {
  16. delete [] pFileContents;
  17. }
  18. pFileContents = nullptr;
  19. delete [] pBinaryNodes;
  20. pBinaryNodes = nullptr;
  21. }
  22. //////////////////////////////////////////////////////////////////////////
  23. // CBinaryXmlNode implementation.
  24. //////////////////////////////////////////////////////////////////////////
  25. //////////////////////////////////////////////////////////////////////////
  26. XmlNodeRef CBinaryXmlNode::getParent() const
  27. {
  28. const XMLBinary::Node* const pNode = _node();
  29. if (pNode->nParentIndex != (XMLBinary::NodeIndex)-1)
  30. {
  31. return &m_pData->pBinaryNodes[pNode->nParentIndex];
  32. }
  33. return XmlNodeRef();
  34. }
  35. XmlNodeRef CBinaryXmlNode::createNode([[maybe_unused]] const char* tag)
  36. {
  37. assert(0);
  38. return nullptr;
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. bool CBinaryXmlNode::isTag(const char* tag) const
  42. {
  43. return g_pXmlStrCmp(tag, getTag()) == 0;
  44. }
  45. const char* CBinaryXmlNode::getAttr(const char* key) const
  46. {
  47. const char* svalue = GetValue(key);
  48. if (svalue)
  49. {
  50. return svalue;
  51. }
  52. return "";
  53. }
  54. bool CBinaryXmlNode::getAttr(const char* key, const char** value) const
  55. {
  56. const char* svalue = GetValue(key);
  57. if (svalue)
  58. {
  59. *value = svalue;
  60. return true;
  61. }
  62. else
  63. {
  64. *value = "";
  65. return false;
  66. }
  67. }
  68. bool CBinaryXmlNode::haveAttr(const char* key) const
  69. {
  70. return (GetValue(key) != nullptr);
  71. }
  72. //////////////////////////////////////////////////////////////////////////
  73. bool CBinaryXmlNode::getAttr(const char* key, int& value) const
  74. {
  75. const char* svalue = GetValue(key);
  76. if (svalue)
  77. {
  78. value = atoi(svalue);
  79. return true;
  80. }
  81. return false;
  82. }
  83. bool CBinaryXmlNode::getAttr(const char* key, unsigned int& value) const
  84. {
  85. const char* svalue = GetValue(key);
  86. if (svalue)
  87. {
  88. value = static_cast<unsigned int>(strtoul(svalue, nullptr, 10));
  89. return true;
  90. }
  91. return false;
  92. }
  93. //////////////////////////////////////////////////////////////////////////
  94. bool CBinaryXmlNode::getAttr(const char* key, int64& value) const
  95. {
  96. const char* svalue = GetValue(key);
  97. if (svalue)
  98. {
  99. value = strtoll(svalue, nullptr, 10);
  100. return true;
  101. }
  102. return false;
  103. }
  104. //////////////////////////////////////////////////////////////////////////
  105. bool CBinaryXmlNode::getAttr(const char* key, uint64& value, bool useHexFormat) const
  106. {
  107. const char* svalue = GetValue(key);
  108. if (svalue)
  109. {
  110. value = strtoull(svalue, nullptr, useHexFormat ? 16 : 10);
  111. return true;
  112. }
  113. return false;
  114. }
  115. bool CBinaryXmlNode::getAttr(const char* key, bool& value) const
  116. {
  117. const char* svalue = GetValue(key);
  118. if (svalue)
  119. {
  120. value = atoi(svalue) != 0;
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool CBinaryXmlNode::getAttr(const char* key, float& value) const
  126. {
  127. const char* svalue = GetValue(key);
  128. if (svalue)
  129. {
  130. value = (float)atof(svalue);
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool CBinaryXmlNode::getAttr(const char* key, double& value) const
  136. {
  137. const char* svalue = GetValue(key);
  138. if (svalue)
  139. {
  140. value = atof(svalue);
  141. return true;
  142. }
  143. return false;
  144. }
  145. bool CBinaryXmlNode::getAttr(const char* key, Ang3& value) const
  146. {
  147. const char* svalue = GetValue(key);
  148. if (svalue)
  149. {
  150. float x, y, z;
  151. if (azsscanf(svalue, "%f,%f,%f", &x, &y, &z) == 3)
  152. {
  153. value(x, y, z);
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. //////////////////////////////////////////////////////////////////////////
  160. bool CBinaryXmlNode::getAttr(const char* key, Vec3& value) const
  161. {
  162. const char* svalue = GetValue(key);
  163. if (svalue)
  164. {
  165. float x, y, z;
  166. if (azsscanf(svalue, "%f,%f,%f", &x, &y, &z) == 3)
  167. {
  168. value = Vec3(x, y, z);
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. //////////////////////////////////////////////////////////////////////////
  175. bool CBinaryXmlNode::getAttr(const char* key, Vec4& value) const
  176. {
  177. const char* svalue = GetValue(key);
  178. if (svalue)
  179. {
  180. float x, y, z, w;
  181. if (azsscanf(svalue, "%f,%f,%f,%f", &x, &y, &z, &w) == 4)
  182. {
  183. value = Vec4(x, y, z, w);
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. //////////////////////////////////////////////////////////////////////////
  190. bool CBinaryXmlNode::getAttr(const char* key, Vec2& value) const
  191. {
  192. const char* svalue = GetValue(key);
  193. if (svalue)
  194. {
  195. float x, y;
  196. if (azsscanf(svalue, "%f,%f", &x, &y) == 2)
  197. {
  198. value = Vec2(x, y);
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. //////////////////////////////////////////////////////////////////////////
  205. bool CBinaryXmlNode::getAttr(const char* key, Quat& value) const
  206. {
  207. const char* svalue = GetValue(key);
  208. if (svalue)
  209. {
  210. float w, x, y, z;
  211. if (azsscanf(svalue, "%f,%f,%f,%f", &w, &x, &y, &z) == 4)
  212. {
  213. value = Quat(w, x, y, z);
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. //////////////////////////////////////////////////////////////////////////
  220. bool CBinaryXmlNode::getAttr(const char* key, ColorB& value) const
  221. {
  222. const char* svalue = GetValue(key);
  223. if (svalue)
  224. {
  225. unsigned int r, g, b, a = 255;
  226. int numFound = azsscanf(svalue, "%u,%u,%u,%u", &r, &g, &b, &a);
  227. if (numFound == 3 || numFound == 4)
  228. {
  229. // If we only found 3 values, a should be unchanged, and still be 255
  230. if (r < 256 && g < 256 && b < 256 && a < 256)
  231. {
  232. value = ColorB(static_cast<uint8>(r), static_cast<uint8>(g), static_cast<uint8>(b), static_cast<uint8>(a));
  233. return true;
  234. }
  235. }
  236. }
  237. return false;
  238. }
  239. XmlNodeRef CBinaryXmlNode::findChild(const char* tag) const
  240. {
  241. const XMLBinary::Node* const pNode = _node();
  242. const uint32 nFirst = pNode->nFirstChildIndex;
  243. const uint32 nAfterLast = pNode->nFirstChildIndex + pNode->nChildCount;
  244. for (uint32 i = nFirst; i < nAfterLast; ++i)
  245. {
  246. const char* sChildTag = m_pData->pStringData + m_pData->pNodes[m_pData->pChildIndices[i]].nTagStringOffset;
  247. if (g_pXmlStrCmp(tag, sChildTag) == 0)
  248. {
  249. return m_pData->pBinaryNodes + m_pData->pChildIndices[i];
  250. }
  251. }
  252. return nullptr;
  253. }
  254. //! Get XML Node child nodes.
  255. XmlNodeRef CBinaryXmlNode::getChild(int i) const
  256. {
  257. const XMLBinary::Node* const pNode = _node();
  258. assert(i >= 0 && i < (int)pNode->nChildCount);
  259. return m_pData->pBinaryNodes + m_pData->pChildIndices[pNode->nFirstChildIndex + i];
  260. }
  261. //////////////////////////////////////////////////////////////////////////
  262. bool CBinaryXmlNode::getAttributeByIndex(int index, const char** key, const char** value)
  263. {
  264. const XMLBinary::Node* const pNode = _node();
  265. if (index >= 0 && index < pNode->nAttributeCount)
  266. {
  267. const XMLBinary::Attribute& attr = m_pData->pAttributes[pNode->nFirstAttributeIndex + index];
  268. *key = _string(attr.nKeyStringOffset);
  269. *value = _string(attr.nValueStringOffset);
  270. return true;
  271. }
  272. return false;
  273. }
  274. //////////////////////////////////////////////////////////////////////////
  275. bool CBinaryXmlNode::getAttributeByIndex(int index, XmlString& key, XmlString& value)
  276. {
  277. const XMLBinary::Node* const pNode = _node();
  278. if (index >= 0 && index < pNode->nAttributeCount)
  279. {
  280. const XMLBinary::Attribute& attr = m_pData->pAttributes[pNode->nFirstAttributeIndex + index];
  281. key = _string(attr.nKeyStringOffset);
  282. value = _string(attr.nValueStringOffset);
  283. return true;
  284. }
  285. return false;
  286. }
  287. //////////////////////////////////////////////////////////////////////////