2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2024-04-23 05:28:03 +08:00
|
|
|
// Copyright (C) 2019-2024 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
|
|
|
|
2024-04-23 05:28:03 +08:00
|
|
|
#include "tinyxml2.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;
|
2024-04-23 05:28:03 +08:00
|
|
|
using namespace tinyxml2;
|
2013-10-15 06:58:11 +08:00
|
|
|
|
2024-04-23 05:28:03 +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
|
2024-04-23 05:28:03 +08:00
|
|
|
auto doc = std::make_unique<XMLDocument>();
|
|
|
|
if (doc->LoadFile(file.get()) != XML_SUCCESS)
|
|
|
|
throw XmlException(filename, 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
|
|
|
|
2024-04-23 05:28:03 +08:00
|
|
|
void save_xml(XMLDocument* doc, const std::string& filename)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
|
|
|
FileHandle file(open_file(filename, "wb"));
|
2023-05-01 21:53:44 +08:00
|
|
|
if (!file) {
|
|
|
|
// TODO add information about why the file couldn't be opened (errno?, win32?)
|
2014-10-29 22:58:03 +08:00
|
|
|
throw Exception("Error loading file: " + filename);
|
2023-05-01 21:53:44 +08:00
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2024-04-23 05:28:03 +08:00
|
|
|
if (doc->SaveFile(file.get()) != XML_SUCCESS)
|
|
|
|
throw XmlException(filename, doc);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2024-04-23 05:28:03 +08:00
|
|
|
bool bool_attr(const XMLElement* 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
|