XMLFile.cpp 3.2 KB

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