1 | // Aseprite |
---|---|
2 | // Copyright (C) 2019 Igara Studio S.A. |
3 | // Copyright (C) 2001-2015 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifdef HAVE_CONFIG_H |
9 | #include "config.h" |
10 | #endif |
11 | |
12 | #include "app/xml_document.h" |
13 | |
14 | #include "app/xml_exception.h" |
15 | #include "base/file_handle.h" |
16 | |
17 | #include "tinyxml.h" |
18 | |
19 | namespace app { |
20 | |
21 | using namespace base; |
22 | |
23 | XmlDocumentRef open_xml(const std::string& filename) |
24 | { |
25 | FileHandle file(open_file(filename, "rb")); |
26 | if (!file) |
27 | throw Exception("Error loading file: "+ filename); |
28 | |
29 | // Try to load the XML file |
30 | auto doc = std::make_shared<TiXmlDocument>(); |
31 | doc->SetValue(filename.c_str()); |
32 | if (!doc->LoadFile(file.get())) |
33 | throw XmlException(doc.get()); |
34 | |
35 | return doc; |
36 | } |
37 | |
38 | void save_xml(XmlDocumentRef doc, const std::string& filename) |
39 | { |
40 | FileHandle file(open_file(filename, "wb")); |
41 | if (!file) |
42 | throw Exception("Error loading file: "+ filename); |
43 | |
44 | if (!doc->SaveFile(file.get())) |
45 | throw XmlException(doc.get()); |
46 | } |
47 | |
48 | bool bool_attr(const TiXmlElement* elem, const char* attrName, bool defaultVal) |
49 | { |
50 | const char* value = elem->Attribute(attrName); |
51 | |
52 | return value == NULL ? defaultVal : strcmp(value, "true") == 0; |
53 | } |
54 | |
55 | } // namespace app |
56 |