-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityConverter.cpp
More file actions
177 lines (167 loc) · 4.29 KB
/
EntityConverter.cpp
File metadata and controls
177 lines (167 loc) · 4.29 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
/*
* libiconv++
*
* Copyright (C) 2012-2013 Joachim Wilke <libiconv@joachim-wilke.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "EntityConverter.h"
#include <map>
#include <sstream>
#include <string.h>
#include "CharsetConverter.h"
namespace convert {
const std::map<std::string, std::string> Entities = {
{" ", " "},
{"¡", "¡"},
{"¢", "¢"},
{"£", "£"},
{"¤","€"}, //krazy:exclude=spelling
{"¥", "¥"},
{"¦","Š"},
{"§", "§"},
{"¨", "š"},
{"©", "©"},
{"ª", "ª"},
{"«", "«"},
{"¬", "¬"},
{"­", ""},
{"®", "®"},
{"¯", "¯"},
{"°", "°"},
{"±","±"},
{"²", "²"},
{"³", "³"},
{"´", "Ž"},
{"µ", "µ"},
{"¶", "¶"},
{"·","·"},
{"¸", "ž"},
{"¹", "¹"},
{"º", "º"},
{"»", "»"},
{"¼","Œ"},
{"½","œ"},
{"¾","Ÿ"},
{"¿","¿"},
{"À","À"},
{"Á","Á"},
{"Â", "Â"},
{"Ã","Ã"},
{"Ä", "Ä"},
{"Å", "Å"},
{"Æ", "Æ"},
{"Ç","Ç"},
{"È","È"},
{"É","É"},
{"Ê", "Ê"},
{"Ë", "Ë"},
{"Ì","Ì"},
{"Í","Í"},
{"Î", "Î"},
{"Ï", "Ï"},
{"Ð", "Ð"},
{"Ñ","Ñ"},
{"Ò","Ò"},
{"Ó","Ó"},
{"Ô", "Ô"},
{"Õ","Õ"},
{"Ö", "Ö"},
{"×", "×"},
{"Ø","Ø"},
{"Ù","Ù"},
{"Ú","Ú"},
{"Û", "Û"},
{"Ü", "Ü"},
{"Ý","Ý"},
{"Þ", "Þ"},
{"ß", "ß"},
{"à","à"},
{"á","á"},
{"â", "â"},
{"ã","ã"},
{"ä", "ä"},
{"å", "å"},
{"æ", "æ"},
{"ç","ç"},
{"è","è"},
{"é","é"},
{"ê", "ê"},
{"ë", "ë"},
{"ì","ì"},
{"í","í"},
{"î", "î"},
{"ï", "ï"},
{"ð", "ð"},
{"ñ","ñ"},
{"ò","ò"},
{"ó","ó"},
{"ô", "ô"},
{"õ","õ"},
{"ö", "ö"},
{"÷","÷"},
{"ø","ø"},
{"ù","ù"},
{"ú","ú"},
{"û", "û"},
{"ü", "ü"},
{"ý","ý"},
{"þ", "þ"},
{"ÿ", "ÿ"},
{"&", "&"},
};
std::string EntityConverter::DecodeEntities(const std::string &input) {
std::string output = input;
if (output.find("&") != std::string::npos) {
// convert the entities from UTF-8 to current system character table
CharsetConverter conv("UTF-8");
// convert entities of format ÿ (unicode)
while (output.find("&#x") != std::string::npos) {
size_t pos = output.find("&#x");
size_t end = output.find(";", pos);
// get hex code
std::string unicode = output.substr(pos+3, end - pos - 3);
// convert to int
std::stringstream ss;
ss << std::hex << unicode;
int codepoint;
ss >> codepoint;
// get corresponding char
char out_buffer[8];
memset(out_buffer, 0, 8);
char *out = &(out_buffer[0]);
wchar_t in_buffer = codepoint;
char *in = (char *)&(in_buffer);
size_t inlen = sizeof(in_buffer), outlen = sizeof(out_buffer);
iconv_t cd;
cd = iconv_open("utf-8", "ucs-2");
iconv(cd, &in, &inlen, &out, &outlen);
iconv_close(cd);
// replace it
output.replace(pos, end-pos+1, std::string(out_buffer));
}
// convert other entities with table
for (auto entity : Entities) {
std::string::size_type pos = output.find(entity.first);
while (pos != std::string::npos) {
output.replace(pos, entity.first.length(), conv.convert(entity.second));
pos = output.find(entity.first, pos-1);
}
}
}
return output;
}
} /* namespace convert */