aboutsummaryrefslogtreecommitdiff
path: root/daemon/XMLOut.cpp
blob: c194fc0042d3d7d7a3d94cee686fb60ebd645ef4 (plain)
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
/**
 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "XMLOut.h"

XMLOut::XMLOut() {
	indent = 0;
	incomplete = false;
	xml_string[0] = 0;
}

XMLOut::~XMLOut() {
}

void XMLOut::writeTabs() {
	for (int i = 0; i < indent; i++) {
		writeData("  ");
	}
}

void XMLOut::encodeAttributeData(const char* data) {
	if (data) {
		while (*data) {
			char ch = *data++;

			if (ch == '<') {
				writeData("&lt;");
			} else if (ch == '>') {
				writeData("&gt;");
			} else if (ch == '&') {
				writeData("&amp;");
			} else if (ch == '"') {
				writeData("&quot;");
			} else if (ch == '\'') {
				writeData("&apos;");
			} else if (ch >= ' ' && ch <= '~') {
				writeData("%c",ch);
			} else {
				writeData("&#%u;",(unsigned int)ch);
			}
		}
	}
}

void XMLOut::writeData(const char *format, ...) {
	va_list	ap;

	va_start(ap, format);
	vsnprintf(temp_buffer, sizeof(temp_buffer), format, ap);
	va_end(ap);

	strncat(xml_string, temp_buffer, sizeof(xml_string) - strlen(xml_string) - 1);
}

const XMLOut & XMLOut::xmlHeader(void) {
	writeData("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	incomplete = false;
	return *this;
}

const XMLOut & XMLOut::comment(const char* text, const bool newline) {
	if (incomplete) {
		writeData(">\n");				
	}
	writeTabs();
	writeData("<!-- %s -->", text);
	if (newline) {
		writeData("\n");
	}
	incomplete = false;
	return *this;
}

const XMLOut & XMLOut::startElement(const char* tag) {
	if (incomplete) {
		writeData(">\n");				
	}
	writeTabs();
	writeData("<%s", tag);
	incomplete = true;
	indent++;
	return *this;
}

const XMLOut & XMLOut::startElement(const char* tag, int index) {
	if (incomplete) {
		writeData(">\n");				
	}
	writeTabs();
	writeData("<!-- %d -->", index);
	writeData("<%s", tag);
	incomplete = true;
	indent++;
	return *this;
}

const XMLOut & XMLOut::endElement(const char* tag) {
	indent--;
	if (indent < 0) {
		indent = 0;	
	}
	if (incomplete) {
		writeData("/>\n");
		incomplete = false;
	} else {
		writeTabs();
		writeData("</%s>\n", tag);
	}
	return *this;
}

const XMLOut & XMLOut::attributeString(const char* name, const char* value) {
	writeData(" %s=\"", name);
	encodeAttributeData(value);
	writeData("\"");
	return *this;
}

const XMLOut & XMLOut::attributeInt(const char* name, int value) {
	writeData(" %s=\"%d\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeUInt(const char* name, unsigned int value) {
	writeData(" %s=\"%u\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeLong(const char* name, long value) {
	writeData(" %s=\"%ld\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeULong(const char* name, unsigned long value) {
	writeData(" %s=\"%lu\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeLongLong(const char* name, long long value) {
	writeData(" %s=\"%lld\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeULongLong(const char* name, unsigned long long value) {
	writeData(" %s=\"%llu\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeDouble(const char* name, double value) {
	writeData(" %s=\"%f\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeBool(const char* name, bool value) {
	writeData(" %s=\"%s\"", name, value ? "yes" : "no");
	return *this;
}

const XMLOut & XMLOut::attributeHex4(const char* name, int value) {
	writeData(" %s=\"0x%04x\"", name, value);
	return *this;
}

const XMLOut & XMLOut::attributeHex8(const char* name, int value) {
	writeData(" %s=\"0x%08x\"", name, value);
	return *this;
}