XMLBinaryNode.cpp 9.4 KB

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