xml.h
1.02 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
#ifndef NVDAHELPER_XML_H
#define NVDAHELPER_XML_H
#include <string>
#include <sstream>
#include <algorithm>
inline void appendCharToXML(const wchar_t c, std::wstring& xml, bool isAttribute=false) {
switch(c) {
case L'"':
xml+=L""";
break;
case L'<':
xml+=L"<";
break;
case L'>':
xml+=L">";
break;
case L'&':
xml+=L"&";
break;
default:
if (c == 0x9 || c == 0xA || c == 0xD
|| (c >= 0x20 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD)
) {
// Valid XML character.
xml+=c;
} else {
// Invalid XML character.
if (isAttribute)
xml += 0xfffd; // Unicode replacement character
else {
std::wostringstream s;
s<<L"<unich value=\""<<((unsigned short)c)<<L"\" />";
xml += s.str();
}
}
}
}
inline std::wstring sanitizeXMLAttribName(std::wstring attribName) {
// #6249: Attribute names can sometimes contain spaces,
// but this isn't valid in XML, so filter it out.
std::replace(attribName.begin(), attribName.end(), L' ', L'_');
return attribName;
}
#endif