forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLWriter.cpp
More file actions
194 lines (170 loc) · 3.8 KB
/
Copy pathXMLWriter.cpp
File metadata and controls
194 lines (170 loc) · 3.8 KB
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
189
190
191
192
193
194
//
// FILE: XMLWriter.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// DATE: 2013-11-06
// PURPOSE: Simple XML library
//
// HISTORY:
// 0.1.00 - 2013-11-06 initial version
// 0.1.01 - 2013-11-07 rework interfaces
// 0.1.02 - 2013-11-07 +setIndentSize(), corrected history, +escape support
//
// Released to the public domain
//
#include <XMLWriter.h>
XMLWriter::XMLWriter(Print* stream)
{
_stream = stream;
reset();
}
void XMLWriter::reset()
{
_indent = 0;
_indentStep = 2;
_idx = 0;
}
void XMLWriter::header()
{
_stream->println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
void XMLWriter::comment(char* text, bool multiLine)
{
_stream->println();
if (multiLine)
{
_stream->println("<!-- ");
_stream->println(text);
_stream->println(" -->");
}
else
{
spaces();
_stream->print("<!-- ");
_stream->print(text);
_stream->println(" -->");
}
}
void XMLWriter::tagOpen(char* tag, bool newline)
{
tagOpen(tag, "", newline);
}
void XMLWriter::tagOpen(char* tag, char* name, bool newline)
{
strncpy(tagNames[_idx++], tag, 10);
tagStart(tag);
if (name[0] != 0) tagField("name", name);
tagEnd(newline, NOSLASH);
_indent += _indentStep;
}
void XMLWriter::tagClose(bool indent)
{
_indent -= _indentStep;
if (indent) spaces();
_stream->print("</");
_stream->print(tagNames[--_idx]);
_stream->println(">");
}
void XMLWriter::tagStart(char *tag)
{
spaces();
_stream->write('<');
_stream->print(tag);
}
void XMLWriter::tagField(char *field, char* value)
{
_stream->write(' ');
_stream->print(field);
_stream->print("=\"");
#ifdef XMLWRITER_ESCAPE_SUPPORT
escape(value);
#else
_stream->print(value);
#endif
_stream->print("\"");
}
void XMLWriter::tagEnd(bool newline, bool addSlash)
{
if (addSlash) _stream->write('/');
_stream->print('>');
if (newline) _stream->println();
}
void XMLWriter::writeNode(char* tag, char* value)
{
tagOpen(tag, "", NONEWLINE);
#ifdef XMLWRITER_ESCAPE_SUPPORT
escape(value);
#else
_stream->print(value);
#endif
tagClose(NOINDENT);
}
void XMLWriter::setIndentSize(uint8_t size)
{
_indentStep = size;
}
#ifdef XMLWRITER_EXTENDED
void XMLWriter::tagField(char *field, int value)
{
_stream->write(' ');
_stream->print(field);
_stream->print("=\"");
_stream->print(value);
_stream->print("\"");
}
void XMLWriter::tagField(char *field, long value)
{
_stream->write(' ');
_stream->print(field);
_stream->print("=\"");
_stream->print(value);
_stream->print("\"");
}
void XMLWriter::tagField(char *field, double value, uint8_t decimals)
{
_stream->write(' ');
_stream->print(field);
_stream->print("=\"");
_stream->print(value);
_stream->print("\"");
}
void XMLWriter::writeNode(char* tag, int value)
{
tagOpen(tag, "", NONEWLINE); // one line
_stream->print(value);
tagClose(NOINDENT);
}
void XMLWriter::writeNode(char* tag, long value)
{
tagOpen(tag, "", NONEWLINE);
_stream->print(value);
tagClose(NOINDENT);
}
void XMLWriter::writeNode(char* tag, double value, uint8_t decimals)
{
tagOpen(tag, "", NONEWLINE);
_stream->print(value, decimals);
tagClose(NOINDENT);
}
#endif
////////////////////////////////////////////////////////////////////
void XMLWriter::spaces()
{
for (uint8_t i=_indent; i>0; i--) _stream->write(' ');
}
#ifdef XMLWRITER_ESCAPE_SUPPORT
char c[6] = "\"\'<>&";
char expanded[][7] = { """, "'","<",">","&"}; // todo in flash
void XMLWriter::escape(char* str)
{
char* p = str;
while(*p != 0)
{
char* q = strchr(c, *p);
if (q == NULL) _stream->write(*p);
else _stream->print(expanded[q - c]); // uint8_t idx = q-c;
p++;
}
}
#endif
// END OF FILE