1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#include <fstream>
#include <lib/base/eerror.h>
#include "xmlgenerator.h"
XmlGenerator::XmlGenerator(FILE *f) : m_file(f), m_indent(true), m_level(0)
{
::fprintf(m_file, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
}
XmlGenerator::~XmlGenerator()
{
}
void XmlGenerator::vprint(const char *fmt, va_list ap, bool newline)
{
unsigned int i;
if (m_indent)
for (i = 0; i < m_level; i++)
::fprintf(m_file, "\t");
::vfprintf(m_file, fmt, ap);
if (newline)
::fprintf(m_file, "\n");
}
void XmlGenerator::print(const char *fmt, ...)
{
va_list ap;
::va_start(ap, fmt);
vprint(fmt, ap, false);
::va_end(ap);
}
void XmlGenerator::printLn(const char *fmt, ...)
{
va_list ap;
::va_start(ap, fmt);
vprint(fmt, ap, true);
::va_end(ap);
}
void XmlGenerator::open(const std::string &tag, bool newline)
{
if (newline) {
printLn("<%s>", tag.c_str());
} else {
print("<%s>", tag.c_str());
m_indent = false;
}
m_tags.push(tag);
m_level++;
}
void XmlGenerator::open(const std::string &tag)
{
open(tag, true);
}
void XmlGenerator::close()
{
ASSERT(!m_tags.empty());
ASSERT(m_level > 0);
m_level--;
printLn("</%s>", m_tags.top().c_str());
m_indent = true;
m_tags.pop();
}
void XmlGenerator::comment(const std::string &str)
{
printLn("<!-- %s -->", str.c_str());
}
void XmlGenerator::commentFromErrno(const std::string &tag)
{
open(tag);
comment(strerror(errno));
close();
}
std::string XmlGenerator::cDataEscape(const std::string &str)
{
const std::string search = "]]>";
const std::string replace = "]]]]><![CDATA[>";
std::string ret;
size_t pos = 0, opos;
for (;;) {
opos = pos;
pos = str.find(search, opos);
if (pos == std::string::npos)
break;
ret.append(str, opos, pos - opos);
ret.append(replace);
pos += search.size();
}
ret.append(str, opos, std::string::npos);
return ret;
}
void XmlGenerator::cDataFromCmd(const std::string &tag, const std::string &cmd)
{
FILE *pipe = ::popen(cmd.c_str(), "re");
if (pipe == 0) {
commentFromErrno(tag);
return;
}
std::string result;
char *lineptr = NULL;
size_t n = 0;
for (;;) {
ssize_t ret = ::getline(&lineptr, &n, pipe);
if (ret < 0)
break;
result.append(lineptr, ret);
}
if (lineptr)
::free(lineptr);
::pclose(pipe);
cDataFromString(tag, result);
}
void XmlGenerator::cDataFromFile(const std::string &tag, const std::string &filename, const char *filter)
{
std::ifstream in(filename.c_str());
std::string line;
std::string content;
if (!in.good()) {
commentFromErrno(tag);
return;
}
while (std::getline(in, line))
if (!filter || !line.find(filter))
content += line + '\n';
in.close();
cDataFromString(tag, content);
}
void XmlGenerator::cDataFromString(const std::string &tag, const std::string &str)
{
bool indent = false;
open(tag);
printLn("<![CDATA[");
std::swap(m_indent, indent);
print("%s", cDataEscape(str).c_str());
printLn("]]>");
std::swap(m_indent, indent);
close();
}
void XmlGenerator::string(const std::string &tag, const std::string &str)
{
open(tag, false);
print("%s", str.c_str());
close();
}
void XmlGenerator::stringFromFile(const std::string &tag, const std::string &filename)
{
std::ifstream in(filename.c_str());
std::string line;
if (!in.good()) {
commentFromErrno(tag);
return;
}
std::getline(in, line);
in.close();
string(tag, line);
}
|