PlistDictionary.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "PlistDictionary.h"
  9. namespace ProjectSettingsTool
  10. {
  11. PlistDictionary::PlistDictionary(XmlDocument* plist)
  12. : m_document(plist)
  13. , m_dict(plist->first_node("plist")->first_node("dict"))
  14. {
  15. }
  16. XmlNode* PlistDictionary::MakeNode(const char* name, const char* value)
  17. {
  18. return m_document->allocate_node
  19. (
  20. AZ::rapidxml::node_element,
  21. m_document->allocate_string(name),
  22. m_document->allocate_string(value)
  23. );
  24. }
  25. XmlNode* PlistDictionary::MakeNode()
  26. {
  27. return m_document->allocate_node(AZ::rapidxml::node_element);
  28. }
  29. XmlNode* PlistDictionary::GetPropertyKeyNode(const char* key)
  30. {
  31. XmlNode* keyNode = m_dict->first_node("key");
  32. // Look for the key in pList's interesting structure
  33. while (keyNode)
  34. {
  35. if (strcmp(keyNode->value(), key) == 0)
  36. {
  37. break;
  38. }
  39. keyNode = keyNode->next_sibling("key");
  40. }
  41. // Returns key if found otherwise nullptr
  42. return keyNode;
  43. }
  44. XmlNode* PlistDictionary::GetPropertyValueNode(const char* key)
  45. {
  46. XmlNode* keyNode = GetPropertyKeyNode(key);
  47. // Key found return data node
  48. if (keyNode != nullptr)
  49. {
  50. return keyNode->next_sibling();
  51. }
  52. // Failed to find key return nullptr
  53. return nullptr;
  54. }
  55. XmlNode* PlistDictionary::AddProperty(const char* key)
  56. {
  57. m_dict->append_node(MakeNode("key", key));
  58. XmlNode* data = MakeNode();
  59. m_dict->append_node(data);
  60. return data;
  61. }
  62. void PlistDictionary::RemoveProperty(const char* key)
  63. {
  64. XmlNode* keyNode = GetPropertyKeyNode(key);
  65. if (keyNode != nullptr)
  66. {
  67. XmlNode* dataNode = keyNode->next_sibling();
  68. m_dict->remove_node(keyNode);
  69. m_dict->remove_node(dataNode);
  70. }
  71. }
  72. const char* PlistDictionary::GetPropertyValue(const char* key)
  73. {
  74. XmlNode* valueNode = GetPropertyValueNode(key);
  75. return valueNode != nullptr ? GetPropertyValue(valueNode) : nullptr;
  76. }
  77. const char* PlistDictionary::GetPropertyValue(XmlNode* node)
  78. {
  79. return node->value_size() > 0 ? node->value() : nullptr;
  80. }
  81. const char* PlistDictionary::GetPropertyValueName(const char* key)
  82. {
  83. XmlNode* valueNode = GetPropertyValueNode(key);
  84. return valueNode != nullptr ? GetPropertyValueName(valueNode) : nullptr;
  85. }
  86. const char* PlistDictionary::GetPropertyValueName(XmlNode* node)
  87. {
  88. return node->name_size() > 0 ? node->name() : nullptr;
  89. }
  90. XmlNode* PlistDictionary::SetPropertyValue(const char* key, const char* newValue)
  91. {
  92. XmlNode* dataNode = GetPropertyValueNode(key);
  93. if (dataNode == nullptr)
  94. {
  95. dataNode = AddProperty(key);
  96. SetPropertyValueName(dataNode, "string");
  97. }
  98. SetPropertyValue(dataNode, newValue);
  99. return dataNode;
  100. }
  101. void PlistDictionary::SetPropertyValue(XmlNode* node, const char* newValue)
  102. {
  103. node->value(m_document->allocate_string(newValue));
  104. }
  105. XmlNode* PlistDictionary::SetPropertyValueName(const char* key, const char* newName)
  106. {
  107. XmlNode* dataNode = GetPropertyValueNode(key);
  108. if (dataNode == nullptr)
  109. {
  110. dataNode = AddProperty(key);
  111. }
  112. SetPropertyValueName(dataNode, newName);
  113. return dataNode;
  114. }
  115. void PlistDictionary::SetPropertyValueName(XmlNode* node, const char* newName)
  116. {
  117. node->name(m_document->allocate_string(newName));
  118. }
  119. bool PlistDictionary::ContainsValidDict(XmlDocument* plist)
  120. {
  121. XmlNode* node = plist->first_node("plist");
  122. if (node != nullptr)
  123. {
  124. node = node->first_node("dict");
  125. if (node != nullptr)
  126. {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. } // namespace ProjectSettingsTool