XMLFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. OBJECTTYPESTATIC(XMLFile);
  56. XMLFile::XMLFile(Context* context) :
  57. Resource(context),
  58. document_(new pugi::xml_document())
  59. {
  60. }
  61. XMLFile::~XMLFile()
  62. {
  63. delete document_;
  64. document_ = 0;
  65. }
  66. void XMLFile::RegisterObject(Context* context)
  67. {
  68. context->RegisterFactory<XMLFile>();
  69. }
  70. bool XMLFile::Load(Deserializer& source)
  71. {
  72. PROFILE(LoadXMLFile);
  73. unsigned dataSize = source.GetSize();
  74. if (!dataSize)
  75. return false;
  76. SharedArrayPtr<char> buffer(new char[dataSize]);
  77. if (source.Read(buffer.Get(), dataSize) != dataSize)
  78. return false;
  79. if (!document_->load_buffer(buffer.Get(), dataSize))
  80. {
  81. LOGERROR("Could not parse XML data from " + source.GetName());
  82. return false;
  83. }
  84. // Note: this probably does not reflect internal data structure size accurately
  85. SetMemoryUse(dataSize);
  86. return true;
  87. }
  88. bool XMLFile::Save(Serializer& dest) const
  89. {
  90. XMLWriter writer(dest);
  91. document_->save(writer);
  92. return writer.success_;
  93. }
  94. XMLElement XMLFile::CreateRoot(const String& name)
  95. {
  96. document_->reset();
  97. pugi::xml_node root = document_->append_child(name.CString());
  98. return XMLElement(this, root.internal_object());
  99. }
  100. XMLElement XMLFile::GetRoot(const String& name)
  101. {
  102. pugi::xml_node root = document_->first_child();
  103. if (root.empty())
  104. return XMLElement();
  105. if (!name.Empty() && name != root.name())
  106. return XMLElement();
  107. else
  108. return XMLElement(this, root.internal_object());
  109. }
  110. }