XMLBinaryNode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #pragma once
  9. #include "IXml.h"
  10. #include "XMLBinaryHeaders.h"
  11. // Compare function for string comparison, can be strcmp or _stricmp
  12. typedef int (__cdecl * XmlStrCmpFunc)(const char* str1, const char* str2);
  13. extern XmlStrCmpFunc g_pXmlStrCmp;
  14. class CBinaryXmlNode;
  15. //////////////////////////////////////////////////////////////////////////
  16. class CBinaryXmlData
  17. {
  18. public:
  19. const XMLBinary::Node* pNodes = nullptr;
  20. const XMLBinary::Attribute* pAttributes = nullptr;
  21. const XMLBinary::NodeIndex* pChildIndices = nullptr;
  22. const char* pStringData = nullptr;
  23. const char* pFileContents = nullptr;
  24. size_t nFileSize = 0;
  25. bool bOwnsFileContentsMemory = true;
  26. CBinaryXmlNode* pBinaryNodes = nullptr;
  27. int nRefCount = 0;
  28. CBinaryXmlData() = default;
  29. ~CBinaryXmlData();
  30. };
  31. // forward declaration
  32. namespace XMLBinary
  33. {
  34. class XMLBinaryReader;
  35. };
  36. //////////////////////////////////////////////////////////////////////////
  37. // CBinaryXmlNode class only used for fast read only binary XML import
  38. //////////////////////////////////////////////////////////////////////////
  39. class CBinaryXmlNode
  40. : public IXmlNode
  41. {
  42. public:
  43. //////////////////////////////////////////////////////////////////////////
  44. // Custom new/delete with pool allocator.
  45. //////////////////////////////////////////////////////////////////////////
  46. //void* operator new( size_t nSize );
  47. //void operator delete( void *ptr );
  48. void DeleteThis() override { }
  49. //! Create new XML node.
  50. XmlNodeRef createNode(const char* tag) override;
  51. // Summary:
  52. // Reference counting.
  53. void AddRef() override { ++m_pData->nRefCount; };
  54. // Notes:
  55. // When ref count reach zero XML node dies.
  56. void Release() override
  57. {
  58. if (--m_pData->nRefCount <= 0)
  59. {
  60. delete m_pData;
  61. }
  62. };
  63. //! Get XML node tag.
  64. const char* getTag() const override { return _string(_node()->nTagStringOffset); };
  65. void setTag([[maybe_unused]] const char* tag) override { assert(0); };
  66. //! Return true if given tag is equal to node tag.
  67. bool isTag(const char* tag) const override;
  68. //! Get XML Node attributes.
  69. int getNumAttributes() const override { return (int)_node()->nAttributeCount; };
  70. //! Return attribute key and value by attribute index.
  71. bool getAttributeByIndex(int index, const char** key, const char** value) override;
  72. //! Return attribute key and value by attribute index, string version.
  73. virtual bool getAttributeByIndex(int index, XmlString& key, XmlString& value);
  74. void copyAttributes(XmlNodeRef fromNode) override { assert(0); };
  75. //! Get XML Node attribute for specified key.
  76. const char* getAttr(const char* key) const override;
  77. //! Get XML Node attribute for specified key.
  78. // Returns true if the attribute exists, false otherwise.
  79. bool getAttr(const char* key, const char** value) const override;
  80. //! Check if attributes with specified key exist.
  81. bool haveAttr(const char* key) const override;
  82. XmlNodeRef newChild([[maybe_unused]] const char* tagName) override { assert(0); return 0; };
  83. void addChild([[maybe_unused]] const XmlNodeRef& node) override { assert(0); };
  84. void removeChild([[maybe_unused]] const XmlNodeRef& node) override { assert(0); };
  85. //! Remove all child nodes.
  86. void removeAllChilds() override { assert(0); };
  87. //! Get number of child XML nodes.
  88. int getChildCount() const override { return (int)_node()->nChildCount; };
  89. //! Get XML Node child nodes.
  90. XmlNodeRef getChild(int i) const override;
  91. //! Find node with specified tag.
  92. XmlNodeRef findChild(const char* tag) const override;
  93. void deleteChild([[maybe_unused]] const char* tag) { assert(0); };
  94. //! Get parent XML node.
  95. XmlNodeRef getParent() const override;
  96. //! Returns content of this node.
  97. const char* getContent() const override { return _string(_node()->nContentStringOffset); };
  98. void setContent([[maybe_unused]] const char* str) override { assert(0); };
  99. //! Set line number in xml.
  100. void setLine([[maybe_unused]] int line) override { assert(0); };
  101. //! Returns XML of this node and sub nodes.
  102. IXmlStringData* getXMLData([[maybe_unused]] int nReserveMem = 0) const override { assert(0); return 0; };
  103. XmlString getXML([[maybe_unused]] int level = 0) const override { assert(0); return ""; };
  104. bool saveToFile([[maybe_unused]] const char* fileName) override { assert(0); return false; }; // saves in one huge chunk
  105. bool saveToFile([[maybe_unused]] const char* fileName, [[maybe_unused]] size_t chunkSizeBytes, [[maybe_unused]] AZ::IO::HandleType fileHandle = AZ::IO::InvalidHandle) override { assert(0); return false; }; // save in small memory chunks
  106. //! Set new XML Node attribute (or override attribute with same key).
  107. using IXmlNode::setAttr;
  108. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const char* value) override { assert(0); };
  109. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] int value) override { assert(0); };
  110. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] unsigned int value) override { assert(0); };
  111. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] int64 value) override { assert(0); };
  112. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] uint64 value, [[maybe_unused]] bool useHexFormat = true /* ignored */) override { assert(0); };
  113. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] float value) override { assert(0); };
  114. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] f64 value) override { assert(0); };
  115. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const Vec2& value) override { assert(0); };
  116. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const Ang3& value) override { assert(0); };
  117. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const Vec3& value) override { assert(0); };
  118. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const Vec4& value) override { assert(0); };
  119. void setAttr([[maybe_unused]] const char* key, [[maybe_unused]] const Quat& value) override { assert(0); };
  120. void delAttr([[maybe_unused]] const char* key) override { assert(0); };
  121. void removeAllAttributes() override { assert(0); };
  122. //! Get attribute value of node.
  123. bool getAttr(const char* key, int& value) const override;
  124. bool getAttr(const char* key, unsigned int& value) const override;
  125. bool getAttr(const char* key, int64& value) const override;
  126. bool getAttr(const char* key, uint64& value, bool useHexFormat = true /* ignored */) const override;
  127. bool getAttr(const char* key, float& value) const override;
  128. bool getAttr(const char* key, f64& value) const override;
  129. bool getAttr(const char* key, bool& value) const override;
  130. bool getAttr(const char* key, XmlString& value) const override {const char* v(NULL); bool boHasAttribute(getAttr(key, &v)); value = v; return boHasAttribute; }
  131. bool getAttr(const char* key, Vec2& value) const override;
  132. bool getAttr(const char* key, Ang3& value) const override;
  133. bool getAttr(const char* key, Vec3& value) const override;
  134. bool getAttr(const char* key, Vec4& value) const override;
  135. bool getAttr(const char* key, Quat& value) const override;
  136. bool getAttr(const char* key, ColorB& value) const override;
  137. private:
  138. //////////////////////////////////////////////////////////////////////////
  139. // INTERNAL METHODS
  140. //////////////////////////////////////////////////////////////////////////
  141. const char* GetValue(const char* key) const
  142. {
  143. const XMLBinary::Attribute* const pAttributes = m_pData->pAttributes;
  144. const char* const pStringData = m_pData->pStringData;
  145. const int nFirst = _node()->nFirstAttributeIndex;
  146. const int nLast = nFirst + _node()->nAttributeCount;
  147. for (int i = nFirst; i < nLast; i++)
  148. {
  149. const char* const attrKey = pStringData + pAttributes[i].nKeyStringOffset;
  150. if (g_pXmlStrCmp(key, attrKey) == 0)
  151. {
  152. const char* attrValue = pStringData + pAttributes[i].nValueStringOffset;
  153. return attrValue;
  154. }
  155. }
  156. return 0;
  157. }
  158. // Return current node in binary data.
  159. const XMLBinary::Node* _node() const
  160. {
  161. return &m_pData->pNodes[this - m_pData->pBinaryNodes];
  162. }
  163. const char* _string(int nIndex) const
  164. {
  165. return m_pData->pStringData + nIndex;
  166. }
  167. protected:
  168. void setParent([[maybe_unused]] const XmlNodeRef& inRef) override { assert(0); }
  169. //////////////////////////////////////////////////////////////////////////
  170. private:
  171. CBinaryXmlData* m_pData;
  172. friend class XMLBinary::XMLBinaryReader;
  173. };