liblcf
Loading...
Searching...
No Matches
writer_xml.cpp
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) liblcf authors.
3 * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4 *
5 * liblcf is Free/Libre Open Source Software, released under the MIT License.
6 * For the full copyright and license information, please view the COPYING
7 * file that was distributed with this source code.
8 */
9
10#include <ostream>
11#include <vector>
12
13#include "lcf/saveopt.h"
14#include "lcf/writer_xml.h"
15#include "lcf/dbstring.h"
16#include "lcf/dbarray.h"
17#include "lcf/dbbitarray.h"
18
19namespace lcf {
20
21XmlWriter::XmlWriter(std::ostream& filestream, EngineVersion engine) :
22 stream(filestream),
23 indent(0),
24 at_bol(true),
25 engine(engine)
26{
27 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
28}
29
30
31XmlWriter::~XmlWriter() {
32}
33
34template <>
35void XmlWriter::Write<bool>(const bool& val) {
36 Indent();
37 stream << (val ? "T" : "F");
38}
39
40template <>
41void XmlWriter::Write<int32_t>(const int32_t& val) {
42 Indent();
43 stream << val;
44}
45
46template <>
47void XmlWriter::Write<int8_t>(const int8_t& val) {
48 WriteInt((int) val);
49}
50
51template <>
52void XmlWriter::Write<uint8_t>(const uint8_t& val) {
53 WriteInt((int) val);
54}
55
56template <>
57void XmlWriter::Write<int16_t>(const int16_t& val) {
58 WriteInt((int) val);
59}
60
61template <>
62void XmlWriter::Write<uint32_t>(const uint32_t& val) {
63 Indent();
64 stream << val;
65}
66
67template <>
68void XmlWriter::Write<double>(const double& val) {
69 Indent();
70 stream << val;
71}
72
73void XmlWriter::WriteString(StringView val) {
74 Indent();
75 for (auto c: val) {
76 switch (c) {
77 case '<':
78 stream << "&lt;";
79 break;
80 case '>':
81 stream << "&gt;";
82 break;
83 case '&':
84 stream << "&amp;";
85 break;
86 case '\n':
87 stream.put(c);
88 break;
89 case '\r':
90 case '\t':
91 stream.put(c);
92 break;
93 default:
94 if (c >= 0 && c < 32) {
95 char temp[10];
96 snprintf(temp,10, "&#x%04x;", 0xE000 + c);
97 stream << temp;
98 }
99 else
100 stream.put(c);
101 break;
102 }
103 }
104}
105
106template <>
107void XmlWriter::Write<std::string>(const std::string& val) {
109}
110
111template <>
112void XmlWriter::Write<DBString>(const DBString& val) {
113 WriteString(val);
114}
115
116template <>
117void XmlWriter::Write<std::vector<int32_t>>(const std::vector<int32_t>& val) {
119}
120
121template <>
122void XmlWriter::Write<std::vector<bool>>(const std::vector<bool>& val) {
124}
125
126template <>
127void XmlWriter::Write<std::vector<uint8_t>>(const std::vector<uint8_t>& val) {
129}
130
131template <>
132void XmlWriter::Write<std::vector<int16_t>>(const std::vector<int16_t>& val) {
134}
135
136template <>
137void XmlWriter::Write<std::vector<uint32_t>>(const std::vector<uint32_t>& val) {
139}
140
141template <>
142void XmlWriter::Write<std::vector<double>>(const std::vector<double>& val) {
144}
145
146template <>
147void XmlWriter::Write<DBArray<int32_t>>(const DBArray<int32_t>& val) {
148 WriteVector(val);
149}
150
151template <>
152void XmlWriter::Write<DBArray<bool>>(const DBArray<bool>& val) {
153 WriteVector(val);
154}
155
156template <>
157void XmlWriter::Write<DBArray<uint8_t>>(const DBArray<uint8_t>& val) {
158 WriteVector(val);
159}
160
161template <>
162void XmlWriter::Write<DBArray<int16_t>>(const DBArray<int16_t>& val) {
163 WriteVector(val);
164}
165
166template <>
167void XmlWriter::Write<DBArray<uint32_t>>(const DBArray<uint32_t>& val) {
168 WriteVector(val);
169}
170
171template <>
172void XmlWriter::Write<DBArray<double>>(const DBArray<double>& val) {
173 WriteVector(val);
174}
175
176void XmlWriter::WriteInt(int val) {
177 Write<int32_t>(val);
178}
179
180template <>
181void XmlWriter::Write(const DBBitArray& val) {
182 WriteVector(val);
183}
184
185template <typename ArrayType>
186void XmlWriter::WriteVector(const ArrayType& val) {
187 Indent();
188 bool first = true;
189 for (auto&& e: val) {
190 if (!first)
191 stream.put(' ');
192 first = false;
193 Write<typename ArrayType::value_type>(e);
194 }
195}
196
197template <class T>
198void XmlWriter::WriteNode(const std::string& name, const T& val) {
199 BeginElement(name);
200 Write<T>(val);
201 EndElement(name);
202}
203
204void XmlWriter::BeginElement(const std::string& name) {
205 NewLine();
206 Indent();
207 stream << "<" << name << ">";
208 indent++;
209}
210
211void XmlWriter::BeginElement(const std::string& name, int ID) {
212 NewLine();
213 Indent();
214 char temp[6];
215 snprintf(temp, 6, "%04d", ID);
216 stream << "<" << name << " id=\"" << temp << "\">";
217 indent++;
218}
219
220void XmlWriter::EndElement(const std::string& name) {
221 indent--;
222 Indent();
223 stream << "</" << name << ">";
224 NewLine();
225}
226
227void XmlWriter::NewLine() {
228 if (at_bol)
229 return;
230 stream.put('\n');
231 at_bol = true;
232}
233
234void XmlWriter::Indent() {
235 if (!at_bol)
236 return;
237 for (int i = 0; i < indent; i++)
238 stream.put(' ');
239 at_bol = false;
240}
241
242bool XmlWriter::IsOk() const {
243 return (stream.good());
244}
245
246template void XmlWriter::WriteNode<bool>(const std::string& name, const bool& val);
247template void XmlWriter::WriteNode<uint8_t>(const std::string& name, const uint8_t& val);
248template void XmlWriter::WriteNode<int16_t>(const std::string& name, const int16_t& val);
249template void XmlWriter::WriteNode<uint32_t>(const std::string& name, const uint32_t& val);
250template void XmlWriter::WriteNode<int32_t>(const std::string& name, const int32_t& val);
251template void XmlWriter::WriteNode<double>(const std::string& name, const double& val);
252template void XmlWriter::WriteNode<std::string>(const std::string& name, const std::string& val);
253template void XmlWriter::WriteNode<DBString>(const std::string& name, const DBString& val);
254
255template void XmlWriter::WriteNode<std::vector<bool>>(const std::string& name, const std::vector<bool>& val);
256template void XmlWriter::WriteNode<std::vector<uint8_t>>(const std::string& name, const std::vector<uint8_t>& val);
257template void XmlWriter::WriteNode<std::vector<int16_t>>(const std::string& name, const std::vector<int16_t>& val);
258template void XmlWriter::WriteNode<std::vector<uint32_t>>(const std::string& name, const std::vector<uint32_t>& val);
259template void XmlWriter::WriteNode<std::vector<int32_t>>(const std::string& name, const std::vector<int32_t>& val);
260
261template void XmlWriter::WriteNode<DBArray<bool>>(const std::string& name, const DBArray<bool>& val);
262template void XmlWriter::WriteNode<DBArray<uint8_t>>(const std::string& name, const DBArray<uint8_t>& val);
263template void XmlWriter::WriteNode<DBArray<int16_t>>(const std::string& name, const DBArray<int16_t>& val);
264template void XmlWriter::WriteNode<DBArray<uint32_t>>(const std::string& name, const DBArray<uint32_t>& val);
265template void XmlWriter::WriteNode<DBArray<int32_t>>(const std::string& name, const DBArray<int32_t>& val);
266
267} //namespace lcf