XMLFile.cpp 3.4 KB

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