forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.cpp
More file actions
340 lines (320 loc) · 7.21 KB
/
Copy pathLanguage.cpp
File metadata and controls
340 lines (320 loc) · 7.21 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright 2010-2012 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Language.h"
#include <iostream>
#include <fstream>
#include "CrossPlatform.h"
#include "Exception.h"
#include "Options.h"
#include "../Interface/TextList.h"
namespace OpenXcom
{
/**
* Initializes an empty language file.
*/
Language::Language() : _name(L""), _strings()
{
}
/**
*
*/
Language::~Language()
{
}
/**
* Takes an 8-bit string encoded in UTF-8 and converts it
* to a wide-character string.
* @note Adapted from http://www.linuxquestions.org/questions/programming-9/wstring-utf8-conversion-in-pure-c-701084/
* @param src UTF-8 string.
* @return Wide-character string.
*/
std::wstring Language::utf8ToWstr(const std::string& src)
{
std::wstring dest;
wchar_t w = 0;
int bytes = 0;
wchar_t err = 0xfffd;
for (size_t i = 0; i < src.size(); ++i)
{
unsigned char c = (unsigned char)src[i];
if (c <= 0x7f) //first byte
{
if (bytes)
{
dest.push_back(err);
bytes = 0;
}
dest.push_back((wchar_t)c);
}
else if (c <= 0xbf) //second/third/etc byte
{
if (bytes)
{
w = ((w << 6)|(c & 0x3f));
bytes--;
if (bytes == 0)
dest.push_back(w);
}
else
dest.push_back(err);
}
else if (c <= 0xdf) //2byte sequence start
{
bytes = 1;
w = c & 0x1f;
}
else if (c <= 0xef) //3byte sequence start
{
bytes = 2;
w = c & 0x0f;
}
else if (c <= 0xf7) //4byte sequence start
{
bytes = 3;
w = c & 0x07;
}
else
{
dest.push_back(err);
bytes = 0;
}
}
if (bytes)
dest.push_back(err);
return dest;
}
/**
* Takes a wide-character string and converts it
* to a 8-bit string encoded in UTF-8.
* @note Adapted from http://www.linuxquestions.org/questions/programming-9/wstring-utf8-conversion-in-pure-c-701084/
* @param src Wide-character string.
* @return UTF-8 string.
*/
std::string Language::wstrToUtf8(const std::wstring& src)
{
std::string dest;
for (size_t i = 0; i < src.size(); ++i)
{
wchar_t w = src[i];
if (w <= 0x7f)
{
dest.push_back((char)w);
}
else if (w <= 0x7ff)
{
dest.push_back(0xc0 | ((w >> 6)& 0x1f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (w <= 0xffff)
{
dest.push_back(0xe0 | ((w >> 12)& 0x0f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (w <= 0x10ffff)
{
dest.push_back(0xf0 | ((w >> 18)& 0x07));
dest.push_back(0x80 | ((w >> 12) & 0x3f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else
dest.push_back('?');
}
return dest;
}
/**
* Replaces every instance of a substring.
* @param str The string to modify.
* @param find The substring to find.
* @param replace The substring to replace it with.
*/
void Language::replace(std::string &str, const std::string &find, const std::string &replace)
{
for (size_t i = str.find(find); i != std::string::npos;)
{
str.replace(i, find.length(), replace);
++i;
i = str.find(find, i);
}
}
/**
* Gets all the languages found in the
* data folder and adds them to a text list.
* @param list Text list.
* @return List of language filenames.
*/
std::vector<std::string> Language::getList(TextList *list)
{
std::vector<std::string> langs = CrossPlatform::getFolderContents(Options::getDataFolder() + "Language/", "lng");
for (std::vector<std::string>::iterator i = langs.begin(); i != langs.end();)
{
std::string file = (*i);
std::string fullname = Options::getDataFolder() + "Language/" + file;
std::ifstream fin(fullname.c_str(), std::ios::in | std::ios::binary);
try
{
if (!fin)
{
throw Exception("Failed to load language");
}
char value;
std::string langname;
while (fin.read(&value, 1))
{
if (value != '\n')
{
langname += value;
}
else
{
break;
}
}
fin.close();
list->addRow(1, Language::utf8ToWstr(langname).c_str());
(*i) = file.substr(0, file.length()-4);
++i;
}
catch (Exception &e)
{
std::cerr << e.what() << std::endl;
i = langs.erase(i);
continue;
}
}
return langs;
}
/**
* Loads pairs of strings separated by linebreaks contained
* in a text file into the Language. Each pair is made of
* an ID and a localized string.
* @param filename Filename of the LNG file.
* @sa <a href="../language_id.html">Reference Table</a>
*/
void Language::loadLng(const std::string &filename)
{
_strings.clear();
std::ifstream txtFile (filename.c_str(), std::ios::in | std::ios::binary);
if (!txtFile)
{
throw Exception("Failed to load LNG");
}
char value;
std::string buffer, bufid;
std::wstring bufstr;
bool first = true, id = true;
while (txtFile.read(&value, 1))
{
if (value != '\n')
{
buffer += value;
}
else
{
// Get language name
if (first)
{
_name = utf8ToWstr(buffer);
first = false;
}
else
{
// Get ID
if (id)
{
bufid = buffer;
}
// Get string
else
{
replace(buffer, "{NEWLINE}", "\n");
replace(buffer, "{SMALLLINE}", "\x02");
bufstr = utf8ToWstr(buffer);
_strings[bufid] = bufstr;
}
id = !id;
}
buffer.clear();
}
}
if (!txtFile.eof())
{
throw Exception("Invalid data from file");
}
txtFile.close();
}
/**
* Returns the language's name in its native language.
* @return Language name.
*/
std::wstring Language::getName() const
{
return _name;
}
/**
* Returns the localizable string with the specified ID.
* If it's not found, just returns the ID.
* @param id ID of the string.
* @return String with the requested ID.
*/
std::wstring Language::getString(const std::string &id) const
{
if (id.empty())
return L"";
std::map<std::string, std::wstring>::const_iterator s = _strings.find(id);
if (s == _strings.end())
{
std::wcout << "WARNING: " << utf8ToWstr(id) << " not found in " << _name << std::endl;
return utf8ToWstr(id);
}
else
{
return s->second;
}
}
/**
* Outputs all the language IDs and strings
* to an HTML table.
*/
void Language::toHtml() const
{
std::ofstream htmlFile ("lang.html", std::ios::out);
htmlFile << "<table border=\"1\" width=\"100%\">" << std::endl;
htmlFile << "<tr><th>ID String</th><th>English String</th></tr>" << std::endl;
for (std::map<std::string, std::wstring>::const_iterator i = _strings.begin(); i != _strings.end(); ++i)
{
htmlFile << "<tr><td>" << i->first << "</td><td>";
for (std::wstring::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
{
if (*j == 2 || *j == '\n')
{
htmlFile << "<br />";
}
else
{
htmlFile << *j;
}
}
htmlFile << "</td></tr>" << std::endl;
}
htmlFile << "</table>" << std::endl;
htmlFile.close();
}
}