2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2019-08-02 06:14:46 +08:00
|
|
|
// Copyright (C) 2019 Igara Studio S.A.
|
2015-02-12 23:16:25 +08:00
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/xml_document.h"
|
|
|
|
|
|
|
|
#include "app/xml_exception.h"
|
|
|
|
#include "base/file_handle.h"
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
#include "tinyxml.h"
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
namespace app {
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
using namespace base;
|
|
|
|
|
2014-04-21 06:53:27 +08:00
|
|
|
XmlDocumentRef open_xml(const std::string& filename)
|
2012-05-03 11:32:40 +08:00
|
|
|
{
|
2013-10-15 06:58:11 +08:00
|
|
|
FileHandle file(open_file(filename, "rb"));
|
|
|
|
if (!file)
|
|
|
|
throw Exception("Error loading file: " + filename);
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
// Try to load the XML file
|
2019-08-02 06:14:46 +08:00
|
|
|
auto doc = std::make_shared<TiXmlDocument>();
|
2013-10-15 06:58:11 +08:00
|
|
|
doc->SetValue(filename.c_str());
|
2015-04-03 07:42:43 +08:00
|
|
|
if (!doc->LoadFile(file.get()))
|
|
|
|
throw XmlException(doc.get());
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
return doc;
|
|
|
|
}
|
2012-05-03 11:32:40 +08:00
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
void save_xml(XmlDocumentRef doc, const std::string& filename)
|
|
|
|
{
|
|
|
|
FileHandle file(open_file(filename, "wb"));
|
|
|
|
if (!file)
|
|
|
|
throw Exception("Error loading file: " + filename);
|
|
|
|
|
2015-04-03 07:42:43 +08:00
|
|
|
if (!doc->SaveFile(file.get()))
|
|
|
|
throw XmlException(doc.get());
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 00:20:20 +08:00
|
|
|
bool bool_attr(const TiXmlElement* elem, const char* attrName, bool defaultVal)
|
2015-03-11 03:44:18 +08:00
|
|
|
{
|
|
|
|
const char* value = elem->Attribute(attrName);
|
|
|
|
|
2021-09-07 00:20:20 +08:00
|
|
|
return value == NULL ? defaultVal : strcmp(value, "true") == 0;
|
2015-03-11 03:44:18 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
} // namespace app
|