XMLFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "ArrayPtr.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "Log.h"
  27. #include "Profiler.h"
  28. #include "Serializer.h"
  29. #include "XMLFile.h"
  30. #include <pugixml.hpp>
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. /// XML writer for pugixml.
  35. class XMLWriter : public pugi::xml_writer
  36. {
  37. public:
  38. /// Construct.
  39. XMLWriter(Serializer& dest) :
  40. dest_(dest),
  41. success_(true)
  42. {
  43. }
  44. /// Write bytes to output.
  45. void write(const void* data, size_t size)
  46. {
  47. if (dest_.Write(data, size) != size)
  48. success_ = false;
  49. }
  50. /// Destination serializer.
  51. Serializer& dest_;
  52. /// Success flag.
  53. bool success_;
  54. };
  55. XMLFile::XMLFile(Context* context) :
  56. Resource(context),
  57. document_(new pugi::xml_document())
  58. {
  59. }
  60. XMLFile::~XMLFile()
  61. {
  62. delete document_;
  63. document_ = 0;
  64. }
  65. void XMLFile::RegisterObject(Context* context)
  66. {
  67. context->RegisterFactory<XMLFile>();
  68. }
  69. bool XMLFile::Load(Deserializer& source)
  70. {
  71. PROFILE(LoadXMLFile);
  72. unsigned dataSize = source.GetSize();
  73. if (!dataSize)
  74. return false;
  75. SharedArrayPtr<char> buffer(new char[dataSize]);
  76. if (source.Read(buffer.Get(), dataSize) != dataSize)
  77. return false;
  78. if (!document_->load_buffer(buffer.Get(), dataSize))
  79. {
  80. LOGERROR("Could not parse XML data from " + source.GetName());
  81. return false;
  82. }
  83. // Note: this probably does not reflect internal data structure size accurately
  84. SetMemoryUse(dataSize);
  85. return true;
  86. }
  87. bool XMLFile::Save(Serializer& dest) const
  88. {
  89. XMLWriter writer(dest);
  90. document_->save(writer);
  91. return writer.success_;
  92. }
  93. XMLElement XMLFile::CreateRoot(const String& name)
  94. {
  95. document_->reset();
  96. pugi::xml_node root = document_->append_child(name.CString());
  97. return XMLElement(this, root.internal_object());
  98. }
  99. XMLElement XMLFile::GetRoot(const String& name)
  100. {
  101. pugi::xml_node root = document_->first_child();
  102. if (root.empty())
  103. return XMLElement();
  104. if (!name.Empty() && name != root.name())
  105. return XMLElement();
  106. else
  107. return XMLElement(this, root.internal_object());
  108. }
  109. }