aseprite/src/app/xml_document.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

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.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/xml_document.h"
#include "app/xml_exception.h"
#include "base/file_handle.h"
2024-04-23 05:28:03 +08:00
#include "tinyxml2.h"
namespace app {
using namespace base;
2024-04-23 05:28:03 +08:00
using namespace tinyxml2;
2024-04-23 05:28:03 +08:00
XMLDocumentRef open_xml(const std::string& filename)
{
FileHandle file(open_file(filename, "rb"));
if (!file)
throw Exception("Error loading file: " + filename);
// 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());
return doc;
}
2024-04-23 05:28:03 +08:00
void save_xml(XMLDocument* doc, const std::string& filename)
{
FileHandle file(open_file(filename, "wb"));
if (!file) {
// TODO add information about why the file couldn't be opened (errno?, win32?)
throw Exception("Error loading file: " + filename);
}
2024-04-23 05:28:03 +08:00
if (doc->SaveFile(file.get()) != XML_SUCCESS)
throw XmlException(filename, doc);
}
2024-04-23 05:28:03 +08:00
bool bool_attr(const XMLElement* elem, const char* attrName, bool defaultVal)
{
const char* value = elem->Attribute(attrName);
2021-09-07 00:20:20 +08:00
return value == NULL ? defaultVal : strcmp(value, "true") == 0;
}
} // namespace app