liblcf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
writer_xml.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 liblcf authors
3  * This file is released under the MIT License
4  * http://opensource.org/licenses/MIT
5  */
6 
7 #include <vector>
8 #include "writer_xml.h"
9 
10 XmlWriter::XmlWriter(const char* filename) :
11  filename(filename),
12  indent(0),
13  at_bol(true)
14 {
15  Open();
16 }
17 
18 XmlWriter::XmlWriter(const std::string& filename) :
19  filename(filename),
20  indent(0),
21  at_bol(true)
22 {
23  Open();
24 }
25 
27  Close();
28 }
29 
31  stream = fopen(filename.c_str(), "w");
32  fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", stream);
33 }
34 
36  if (stream != NULL)
37  fclose(stream);
38  stream = NULL;
39 }
40 
41 template <>
42 void XmlWriter::Write<bool>(const bool& val) {
43  Indent();
44  fputs(val ? "T" : "F", stream);
45 }
46 
47 template <>
48 void XmlWriter::Write<int>(const int& val) {
49  Indent();
50  fprintf(stream, "%d", val);
51 }
52 
53 template <>
54 void XmlWriter::Write<uint8_t>(const uint8_t& val) {
55  WriteInt((int) val);
56 }
57 
58 template <>
59 void XmlWriter::Write<int16_t>(const int16_t& val) {
60  WriteInt((int) val);
61 }
62 
63 template <>
64 void XmlWriter::Write<uint32_t>(const uint32_t& val) {
65  Indent();
66  fprintf(stream, "%u", val);
67 }
68 
69 template <>
70 void XmlWriter::Write<double>(const double& val) {
71  Indent();
72  fprintf(stream, "%g", val);
73 }
74 
75 template <>
76 void XmlWriter::Write<std::string>(const std::string& val) {
77  Indent();
78  std::string::const_iterator it;
79  for (it = val.begin(); it != val.end(); it++) {
80  int c = (int) *it;
81  switch (c) {
82  case '<':
83  fputs("&lt;", stream);
84  break;
85  case '>':
86  fputs("&gt;", stream);
87  break;
88  case '&':
89  fputs("&amp;", stream);
90  break;
91  case '\n':
92  fputc(c, stream);
93  at_bol = true;
94  Indent();
95  case '\r':
96  case '\t':
97  fputc(c, stream);
98  break;
99  default:
100  if (c >= 0 && c < 32)
101  fprintf(stream, "&#x%04x;", 0xE000 + c);
102  else
103  fputc(c, stream);
104  break;
105  }
106  }
107 }
108 
109 template <>
110 void XmlWriter::Write<std::vector<int> >(const std::vector<int>& val) {
111  WriteVector<int>(val);
112 }
113 
114 template <>
115 void XmlWriter::Write<std::vector<bool> >(const std::vector<bool>& val) {
116  WriteVector<bool>(val);
117 }
118 
119 template <>
120 void XmlWriter::Write<std::vector<uint8_t> >(const std::vector<uint8_t>& val) {
121  WriteVector<uint8_t>(val);
122 }
123 
124 template <>
125 void XmlWriter::Write<std::vector<int16_t> >(const std::vector<int16_t>& val) {
126  WriteVector<int16_t>(val);
127 }
128 
129 template <>
130 void XmlWriter::Write<std::vector<uint32_t> >(const std::vector<uint32_t>& val) {
131  WriteVector<uint32_t>(val);
132 }
133 
134 template <>
135 void XmlWriter::Write<std::vector<double> >(const std::vector<double>& val) {
136  WriteVector<double>(val);
137 }
138 
139 void XmlWriter::WriteInt(int val) {
140  Write<int>(val);
141 }
142 
143 template <class T>
144 void XmlWriter::WriteVector(const std::vector<T>& val) {
145  Indent();
146  typename std::vector<T>::const_iterator it;
147  bool first = true;
148  for (it = val.begin(); it != val.end(); it++) {
149  if (!first)
150  fputc(' ', stream);
151  first = false;
152  Write<T>(*it);
153  }
154 }
155 
156 template <class T>
157 void XmlWriter::WriteNode(const std::string& name, const T& val) {
158  BeginElement(name);
159  Write<T>(val);
160  EndElement(name);
161 }
162 
163 void XmlWriter::BeginElement(const std::string& name) {
164  NewLine();
165  Indent();
166  fprintf(stream, "<%s>", name.c_str());
167  indent++;
168 }
169 
170 void XmlWriter::BeginElement(const std::string& name, int ID) {
171  NewLine();
172  Indent();
173  fprintf(stream, "<%s id=\"%04d\">", name.c_str(), ID);
174  indent++;
175 }
176 
177 void XmlWriter::EndElement(const std::string& name) {
178  indent--;
179  Indent();
180  fprintf(stream, "</%s>", name.c_str());
181  NewLine();
182 }
183 
185  if (at_bol)
186  return;
187  fputc('\n', stream);
188  at_bol = true;
189 }
190 
192  if (!at_bol)
193  return;
194  for (int i = 0; i < indent; i++)
195  fputc(' ', stream);
196  at_bol = false;
197 }
198 
199 bool XmlWriter::IsOk() const {
200  return (stream != NULL && !ferror(stream));
201 }
202 
203 template void XmlWriter::WriteNode<int>(const std::string& name, const int& val);
204 template void XmlWriter::WriteNode<bool>(const std::string& name, const bool& val);
205 template void XmlWriter::WriteNode<uint8_t>(const std::string& name, const uint8_t& val);
206 template void XmlWriter::WriteNode<int16_t>(const std::string& name, const int16_t& val);
207 template void XmlWriter::WriteNode<uint32_t>(const std::string& name, const uint32_t& val);
208 template void XmlWriter::WriteNode<double>(const std::string& name, const double& val);
209 template void XmlWriter::WriteNode<std::string>(const std::string& name, const std::string& val);
210 
211 template void XmlWriter::WriteNode<std::vector<int> >(const std::string& name, const std::vector<int>& val);
212 template void XmlWriter::WriteNode<std::vector<bool> >(const std::string& name, const std::vector<bool>& val);
213 template void XmlWriter::WriteNode<std::vector<uint8_t> >(const std::string& name, const std::vector<uint8_t>& val);
214 template void XmlWriter::WriteNode<std::vector<int16_t> >(const std::string& name, const std::vector<int16_t>& val);
215 template void XmlWriter::WriteNode<std::vector<uint32_t> >(const std::string& name, const std::vector<uint32_t>& val);
void WriteInt(int val)
Definition: writer_xml.cpp:139
bool IsOk() const
Definition: writer_xml.cpp:199
bool at_bol
Definition: writer_xml.h:118
void BeginElement(const std::string &name)
Definition: writer_xml.cpp:163
void EndElement(const std::string &name)
Definition: writer_xml.cpp:177
FILE * stream
Definition: writer_xml.h:114
void NewLine()
Definition: writer_xml.cpp:184
void Open()
Definition: writer_xml.cpp:30
int indent
Definition: writer_xml.h:116
XmlWriter(const char *filename)
Definition: writer_xml.cpp:10
void Close()
Definition: writer_xml.cpp:35
void WriteVector(const std::vector< T > &val)
Definition: writer_xml.cpp:144
std::string filename
Definition: writer_xml.h:112
void WriteNode(const std::string &name, const T &val)
Definition: writer_xml.cpp:157
void Indent()
Definition: writer_xml.cpp:191