Line data Source code
1 : /* Copyright (C) 2026 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 : #include "precompiled.h"
19 :
20 : #include "XMLWriter.h"
21 :
22 : #include "lib/alignment.h"
23 : #include "lib/allocators/shared_ptr.h"
24 : #include "lib/debug.h"
25 : #include "lib/path.h"
26 : #include "lib/status.h"
27 : #include "lib/utf8.h"
28 : #include "maths/Fixed.h"
29 : #include "ps/CLogger.h"
30 : #include "ps/XMB/XMBData.h"
31 : #include "ps/XMB/XMBStorage.h"
32 : #include "ps/XML/Xeromyces.h"
33 : #include "ps/algorithm.h"
34 :
35 : #include <cstring>
36 : #include <memory>
37 : #include <sstream>
38 : #include <string>
39 :
40 : // TODO (maybe): Write to the file frequently, instead of buffering
41 : // the entire file, so that large files get written faster.
42 :
43 : namespace
44 : {
45 5 : CStr escapeAttributeValue(const char* input)
46 : {
47 : // Spec says:
48 : // AttValue ::= '"' ([^<&"] | Reference)* '"'
49 : // so > is allowed in attribute values, so we don't bother escaping it.
50 :
51 5 : CStr ret = input;
52 5 : PS::ReplaceSubrange(ret, "&", "&");
53 5 : PS::ReplaceSubrange(ret, "<", "<");
54 5 : PS::ReplaceSubrange(ret, "\"", """);
55 5 : return ret;
56 0 : }
57 :
58 12 : CStr escapeCharacterData(const char* input)
59 : {
60 : // CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
61 :
62 12 : CStr ret = input;
63 12 : PS::ReplaceSubrange(ret, "&", "&");
64 12 : PS::ReplaceSubrange(ret, "<", "<");
65 12 : PS::ReplaceSubrange(ret, "]]>", "]]>");
66 12 : return ret;
67 0 : }
68 :
69 1 : CStr escapeCDATA(const char* input)
70 : {
71 1 : CStr ret = input;
72 1 : PS::ReplaceSubrange(ret, "]]>", "]]>]]><![CDATA[");
73 1 : return ret;
74 0 : }
75 :
76 3 : CStr escapeComment(const char* input)
77 : {
78 : // Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
79 : // This just avoids double-hyphens, and doesn't enforce the no-hyphen-at-end
80 : // rule, since it's only used in contexts where there's already a space
81 : // between this data and the -->.
82 3 : CStr ret = input;
83 3 : PS::ReplaceSubrange(ret, "--", "\xE2\x80\x90\xE2\x80\x90");
84 : // replace with U+2010 HYPHEN, because it's close enough and it's
85 : // probably nicer than inserting spaces or deleting hyphens or
86 : // any alternative
87 3 : return ret;
88 0 : }
89 : }
90 :
91 : enum { EL_ATTR, EL_TEXT, EL_SUBEL };
92 :
93 9 : XMLWriter_File::XMLWriter_File()
94 9 : : m_Indent(0), m_LastElement(NULL),
95 9 : m_PrettyPrint(true)
96 : {
97 : // Encoding is always UTF-8 - that's one of the only two guaranteed to be
98 : // supported by XML parsers (along with UTF-16), and there's not much need
99 : // to let people choose another.
100 9 : m_Data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
101 9 : }
102 :
103 0 : bool XMLWriter_File::StoreVFS(const PIVFS& vfs, const VfsPath& pathname)
104 : {
105 0 : if (m_LastElement) debug_warn(L"ERROR: Saving XML while an element is still open");
106 :
107 0 : const size_t size = m_Data.length();
108 0 : std::shared_ptr<std::uint8_t> data;
109 0 : AllocateAligned(data, size, maxSectorSize);
110 0 : memcpy(data.get(), m_Data.data(), size);
111 0 : Status ret = vfs->CreateFile(pathname, {data.get(), size});
112 0 : if (ret < 0)
113 : {
114 0 : LOGERROR("Error saving XML data through VFS: {} '{}'", ret, pathname.string8());
115 0 : return false;
116 : }
117 : return true;
118 : }
119 :
120 9 : const CStr& XMLWriter_File::GetOutput()
121 : {
122 9 : return m_Data;
123 : }
124 :
125 :
126 0 : void XMLWriter_File::XMB(const XMBData& xmb)
127 : {
128 0 : ElementXMB(xmb, xmb.GetRoot());
129 0 : }
130 :
131 0 : void XMLWriter_File::ElementXMB(const XMBData& xmb, XMBElement el)
132 : {
133 0 : XMLWriter_Element writer(*this, xmb.GetElementString(el.GetNodeName()));
134 :
135 0 : for (XMBAttribute attr : el.GetAttributes())
136 0 : writer.Attribute(xmb.GetAttributeString(attr.Name), attr.Value);
137 :
138 0 : for (XMBElement child : el.GetChildNodes())
139 0 : ElementXMB(xmb, child);
140 0 : }
141 :
142 3 : void XMLWriter_File::Comment(const char* text)
143 : {
144 3 : ElementStart(NULL, "!-- ");
145 6 : m_Data += escapeComment(text);
146 3 : m_Data += " -->";
147 3 : --m_Indent;
148 3 : }
149 :
150 27 : CStr XMLWriter_File::Indent()
151 : {
152 54 : return std::string(m_Indent, '\t');
153 : }
154 :
155 24 : void XMLWriter_File::ElementStart(XMLWriter_Element* element, const char* name)
156 : {
157 24 : if (m_LastElement) m_LastElement->Close(EL_SUBEL);
158 24 : m_LastElement = element;
159 :
160 24 : if (m_PrettyPrint)
161 : {
162 21 : m_Data += "\n";
163 42 : m_Data += Indent();
164 : }
165 24 : m_Data += "<";
166 24 : m_Data += name;
167 :
168 24 : ++m_Indent;
169 24 : }
170 :
171 18 : void XMLWriter_File::ElementClose()
172 : {
173 0 : m_Data += ">";
174 0 : }
175 :
176 21 : void XMLWriter_File::ElementEnd(const char* name, int type)
177 : {
178 21 : --m_Indent;
179 21 : m_LastElement = NULL;
180 :
181 21 : switch (type)
182 : {
183 2 : case EL_ATTR:
184 2 : m_Data += "/>";
185 : break;
186 11 : case EL_TEXT:
187 11 : m_Data += "</";
188 11 : m_Data += name;
189 11 : m_Data += ">";
190 : break;
191 8 : case EL_SUBEL:
192 8 : if (m_PrettyPrint)
193 : {
194 6 : m_Data += "\n";
195 12 : m_Data += Indent();
196 : }
197 8 : m_Data += "</";
198 8 : m_Data += name;
199 8 : m_Data += ">";
200 : break;
201 0 : default:
202 0 : DEBUG_WARN_ERR(ERR::LOGIC);
203 : }
204 21 : }
205 :
206 13 : void XMLWriter_File::ElementText(const char* text, bool cdata)
207 : {
208 13 : if (cdata)
209 : {
210 1 : m_Data += "<![CDATA[";
211 2 : m_Data += escapeCDATA(text);
212 1 : m_Data += "]]>";
213 : }
214 : else
215 : {
216 25 : m_Data += escapeCharacterData(text);
217 : }
218 13 : }
219 :
220 :
221 20 : XMLWriter_Element::XMLWriter_Element(XMLWriter_File& file, const char* name)
222 20 : : m_File(&file), m_Name(name), m_Type(EL_ATTR)
223 : {
224 20 : m_File->ElementStart(this, name);
225 20 : }
226 :
227 :
228 20 : XMLWriter_Element::~XMLWriter_Element()
229 : {
230 20 : m_File->ElementEnd(m_Name.c_str(), m_Type);
231 20 : }
232 :
233 :
234 20 : void XMLWriter_Element::Close(int type)
235 : {
236 8 : if (m_Type == type)
237 : return;
238 :
239 18 : m_File->ElementClose();
240 18 : m_Type = type;
241 : }
242 :
243 :
244 : // Template specialisations for various string types:
245 :
246 12 : template <> void XMLWriter_Element::Text<const char*>(const char* text, bool cdata)
247 : {
248 12 : Close(EL_TEXT);
249 12 : m_File->ElementText(text, cdata);
250 12 : }
251 :
252 1 : template <> void XMLWriter_Element::Text<const wchar_t*>(const wchar_t* text, bool cdata)
253 : {
254 2 : Text( CStrW(text).ToUTF8().c_str(), cdata );
255 1 : }
256 :
257 : //
258 :
259 6 : template <> void XMLWriter_File::ElementAttribute<const char*>(const char* name, const char* const& value, bool newelement)
260 : {
261 6 : if (newelement)
262 : {
263 1 : ElementStart(NULL, name);
264 1 : m_Data += ">";
265 1 : ElementText(value, false);
266 1 : ElementEnd(name, EL_TEXT);
267 : }
268 : else
269 : {
270 5 : ENSURE(m_LastElement && m_LastElement->m_Type == EL_ATTR);
271 5 : m_Data += " ";
272 5 : m_Data += name;
273 5 : m_Data += "=\"";
274 10 : m_Data += escapeAttributeValue(value);
275 5 : m_Data += "\"";
276 : }
277 6 : }
278 :
279 : // Attribute/setting value-to-string template specialisations.
280 : //
281 : // These only deal with basic types. Anything more complicated should
282 : // be converted into a basic type by whatever is making use of XMLWriter,
283 : // to keep game-related logic out of the not-directly-game-related code here.
284 :
285 0 : template <> void XMLWriter_File::ElementAttribute<CStr>(const char* name, const CStr& value, bool newelement)
286 : {
287 0 : ElementAttribute(name, value.c_str(), newelement);
288 0 : }
289 0 : template <> void XMLWriter_File::ElementAttribute<std::string>(const char* name, const std::string& value, bool newelement)
290 : {
291 0 : ElementAttribute(name, value.c_str(), newelement);
292 0 : }
293 : // Encode Unicode strings as UTF-8
294 0 : template <> void XMLWriter_File::ElementAttribute<CStrW>(const char* name, const CStrW& value, bool newelement)
295 : {
296 0 : ElementAttribute(name, value.ToUTF8(), newelement);
297 0 : }
298 0 : template <> void XMLWriter_File::ElementAttribute<std::wstring>(const char* name, const std::wstring& value, bool newelement)
299 : {
300 0 : ElementAttribute(name, utf8_from_wstring(value).c_str(), newelement);
301 0 : }
302 :
303 0 : template <> void XMLWriter_File::ElementAttribute<fixed>(const char* name, const fixed& value, bool newelement)
304 : {
305 0 : ElementAttribute(name, value.ToString().c_str(), newelement);
306 0 : }
307 :
308 1 : template <> void XMLWriter_File::ElementAttribute<int>(const char* name, const int& value, bool newelement)
309 : {
310 1 : std::stringstream ss;
311 1 : ss << value;
312 1 : ElementAttribute(name, ss.str().c_str(), newelement);
313 1 : }
314 :
315 0 : template <> void XMLWriter_File::ElementAttribute<unsigned int>(const char* name, const unsigned int& value, bool newelement)
316 : {
317 0 : std::stringstream ss;
318 0 : ss << value;
319 0 : ElementAttribute(name, ss.str().c_str(), newelement);
320 0 : }
321 :
322 0 : template <> void XMLWriter_File::ElementAttribute<float>(const char* name, const float& value, bool newelement)
323 : {
324 0 : std::stringstream ss;
325 0 : ss << value;
326 0 : ElementAttribute(name, ss.str().c_str(), newelement);
327 0 : }
328 :
329 2 : template <> void XMLWriter_File::ElementAttribute<double>(const char* name, const double& value, bool newelement)
330 : {
331 2 : std::stringstream ss;
332 2 : ss << value;
333 2 : ElementAttribute(name, ss.str().c_str(), newelement);
334 2 : }
|