Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
XMLWriter.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_XMLWRITER
19#define INCLUDED_XMLWRITER
20
21/*
22 *
23 *System for writing simple XML files, with human-readable formatting.
24 *
25 *Example usage:
26 *
27 * XMLWriter_File exampleFile;
28 * {
29 * XMLWriter_Element scenarioTag (exampleFile,"Scenario");
30 * {
31 * XMLWriter_Element entitiesTag (exampleFile,"Entities");
32 * for (...)
33 * {
34 * XMLWriter_Element entityTag (exampleFile,"Entity");
35 * {
36 * XMLWriter_Element templateTag (exampleFile,"Template");
37 * templateTag.Text(entity.name);
38 * }
39 * // Or equivalently:
40 * templateTag.Setting("Template", entity.name);
41 * {
42 * XMLWriter_Element positionTag (exampleFile,"Position");
43 * positionTag.Attribute("x", entity.x);
44 * positionTag.Attribute("y", entity.y);
45 * positionTag.Attribute("z", entity.z);
46 * }
47 * {
48 * XMLWriter_Element orientationTag (exampleFile,"Orientation");
49 * orientationTag.Attribute("angle", entity.angle);
50 * }
51 * }
52 * }
53 * }
54 * exampleFile.StoreVFS(g_VFS, "/test.xml");
55 *
56 * In general, "{ XML_Element(name); ... }" means "<name> ... </name>" -- the
57 * scoping braces are important to indicate where an element ends. If you don't put
58 * them the tag won't be closed until the object's destructor is called, usually
59 * when it goes out of scope.
60 * xml_element_.Attribute/xml_element_.Setting are templated. To support more types, alter the
61 * end of XMLWriter.cpp.
62 */
63
64#include "lib/file/vfs/vfs.h"
65#include "ps/CStr.h"
66
67class XMBElement;
68class XMBData;
70
72{
73public:
75
76 void SetPrettyPrint(bool enabled) { m_PrettyPrint = enabled; }
77
78 void Comment(const char* text);
79
80 void XMB(const XMBData& xmb);
81
82 bool StoreVFS(const PIVFS& vfs, const VfsPath& pathname);
83 const CStr8& GetOutput();
84
85private:
86
87 friend class XMLWriter_Element;
88
89 void ElementXMB(const XMBData& xmb, XMBElement el);
90
91 void ElementStart(XMLWriter_Element* element, const char* name);
92 void ElementText(const char* text, bool cdata);
93 template <typename T> void ElementAttribute(const char* name, const T& value, bool newelement);
94 void ElementClose();
95 void ElementEnd(const char* name, int type);
96
97 CStr8 Indent();
98
100
101 CStr m_Data;
104};
105
107{
108public:
109 XMLWriter_Element(XMLWriter_File& file, const char* name);
111
112 template <typename constCharPtr> void Text(constCharPtr text, bool cdata);
113 template <typename T> void Attribute(const char* name, T value) { m_File->ElementAttribute(name, value, false); }
114 template <typename T> void Setting(const char* name, T value) { m_File->ElementAttribute(name, value, true); }
115 void Close(int type);
116
117private:
118
119 friend class XMLWriter_File;
120
122 CStr m_Name;
124};
125
126#endif // INCLUDED_XMLWRITER
Definition: path.h:80
Definition: XMBData.h:96
Definition: XMBData.h:136
Definition: XMLWriter.h:107
void Attribute(const char *name, T value)
Definition: XMLWriter.h:113
int m_Type
Definition: XMLWriter.h:123
~XMLWriter_Element()
Definition: XMLWriter.cpp:219
void Close(int type)
Definition: XMLWriter.cpp:225
XMLWriter_File * m_File
Definition: XMLWriter.h:121
CStr m_Name
Definition: XMLWriter.h:122
XMLWriter_Element(XMLWriter_File &file, const char *name)
Definition: XMLWriter.cpp:212
void Text(constCharPtr text, bool cdata)
void Setting(const char *name, T value)
Definition: XMLWriter.h:114
Definition: XMLWriter.h:72
CStr m_Data
Definition: XMLWriter.h:101
void ElementClose()
Definition: XMLWriter.cpp:162
XMLWriter_Element * m_LastElement
Definition: XMLWriter.h:103
void XMB(const XMBData &xmb)
Definition: XMLWriter.cpp:117
void ElementAttribute(const char *name, const T &value, bool newelement)
bool StoreVFS(const PIVFS &vfs, const VfsPath &pathname)
Definition: XMLWriter.cpp:94
XMLWriter_File()
Definition: XMLWriter.cpp:84
void ElementText(const char *text, bool cdata)
Definition: XMLWriter.cpp:197
void ElementEnd(const char *name, int type)
Definition: XMLWriter.cpp:167
void SetPrettyPrint(bool enabled)
Definition: XMLWriter.h:76
bool m_PrettyPrint
Definition: XMLWriter.h:99
const CStr8 & GetOutput()
Definition: XMLWriter.cpp:111
void Comment(const char *text)
Definition: XMLWriter.cpp:133
void ElementStart(XMLWriter_Element *element, const char *name)
Definition: XMLWriter.cpp:146
void ElementXMB(const XMBData &xmb, XMBElement el)
Definition: XMLWriter.cpp:122
CStr8 Indent()
Definition: XMLWriter.cpp:141
int m_Indent
Definition: XMLWriter.h:102
Definition: vfs_util.cpp:39
#define T(string_literal)
Definition: secure_crt.cpp:77
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:220