XMLFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "Context.h"
  25. #include "File.h"
  26. #include "Log.h"
  27. #include "SharedArrayPtr.h"
  28. #include "StringUtils.h"
  29. #include "XMLFile.h"
  30. #include <tinyxml.h>
  31. #include "DebugNew.h"
  32. OBJECTTYPESTATIC(XMLFile);
  33. XMLFile::XMLFile(Context* context) :
  34. Resource(context),
  35. document_(new TiXmlDocument())
  36. {
  37. }
  38. XMLFile::~XMLFile()
  39. {
  40. delete document_;
  41. document_ = 0;
  42. }
  43. void XMLFile::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<XMLFile>();
  46. }
  47. bool XMLFile::Load(Deserializer& source)
  48. {
  49. unsigned dataSize = source.GetSize();
  50. if (!dataSize)
  51. return false;
  52. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  53. if (source.Read(buffer.GetPtr(), dataSize) != dataSize)
  54. return false;
  55. buffer[dataSize] = 0;
  56. document_->Clear();
  57. document_->Parse(buffer.GetPtr());
  58. if (document_->Error())
  59. {
  60. document_->ClearError();
  61. LOGERROR("Could not parse XML data from " + source.GetName());
  62. return false;
  63. }
  64. // Note: this probably does not reflect internal data structure size accurately
  65. SetMemoryUse(dataSize);
  66. return true;
  67. }
  68. bool XMLFile::Save(Serializer& dest)
  69. {
  70. // Only support saving to a File
  71. File* file = dynamic_cast<File*>(&dest);
  72. if (!file)
  73. {
  74. LOGERROR("XML data destination is not a File");
  75. return false;
  76. }
  77. if (!file->IsOpen())
  78. return false;
  79. if (!document_->SaveFile((FILE*)file->GetHandle()))
  80. {
  81. LOGERROR("Failed to save XML data to " + file->GetName());
  82. return false;
  83. }
  84. return true;
  85. }
  86. XMLElement XMLFile::CreateRootElement(const std::string& name)
  87. {
  88. TiXmlElement newRoot(name.c_str());
  89. document_->Clear();
  90. document_->InsertEndChild(newRoot);
  91. return GetRootElement();
  92. }
  93. XMLElement XMLFile::GetRootElement(const std::string& name)
  94. {
  95. XMLElement rootElem = XMLElement(this, document_->RootElement());
  96. if ((rootElem.IsNull()) || ((!name.empty()) && (rootElem.GetName() != name)))
  97. return XMLElement();
  98. return rootElem;
  99. }