|
1
|
|
|
2
3
|
#include <vector>
#include <iterator>
|
|
4
|
#include <algorithm>
|
|
5
|
#include <inttypes.h>
|
|
6
|
#include <iostream>
|
|
7
|
#include "../endianness.hpp"
|
|
8
9
|
#include "utf8.h"
#include "CharsetConverter.hpp"
|
|
10
|
#include "conversion_tables.hpp"
|
|
11
|
|
|
12
13
|
using namespace std;
|
|
14
15
|
const char DEFAULT_UNDEFINED_CHAR = static_cast<char>(0xF7);
|
|
16
17
|
string CharsetConverter::toString(const vector<uint32_t>& codepoints) const {
string res;
|
|
18
19
|
for (unsigned int i = 0; i < codepoints.size(); i++) {
this->append(codepoints[i], res);
|
|
20
21
22
23
|
}
return res;
}
|
|
24
25
26
27
|
CharsetConverter::~CharsetConverter() {
}
|
|
28
|
uint32_t UTF8CharsetConverter::peek(const char*& it, const char* end) const {
|
|
29
|
return utf8::unchecked::peek_next(it);
|
|
30
31
|
}
|
|
32
|
uint32_t UTF8CharsetConverter::next(const char*& it, const char* end) const {
|
|
33
34
35
36
37
38
39
40
|
return utf8::unchecked::next(it);
// catch (utf8::exception ex) {
// cerr << "WARNING: Replacing invalid character: " << hex << (uint16_t) *it << dec << " with replacement char: 0xFFFD" << endl;
// while (it != end && peek(it, end) == 0xFFFD) {
// utf8::unchecked::next(it);
// }
// return 0xFFFD;
// }
|
|
41
|
}
|
|
42
|
|
|
43
|
void UTF8CharsetConverter::append(uint32_t cp, string& result) const {
|
|
44
|
utf8::append(cp, back_inserter(result));
|
|
45
|
}
|
|
46
|
|
|
47
|
static vector<char> reverseArray(const uint32_t* array) {
|
|
48
49
50
51
|
vector<char> res;
unsigned char c = 0;
do {
uint32_t codepoint = array[c];
|
|
52
53
|
res.resize(max(static_cast<uint32_t> (res.size()), codepoint + 1), DEFAULT_UNDEFINED_CHAR);
res[codepoint] = static_cast<char> (c);
|
|
54
|
c++;
|
|
55
|
} while (c != 255);
|
|
56
|
return res;
|
|
57
58
59
60
|
}
OneByteCharsetConverter::OneByteCharsetConverter(const uint32_t* array)
: array(array),
|
|
61
|
codepoint2Char(reverseArray(array)) {
|
|
62
63
|
}
|
|
64
|
// TODO - sprawdzanie zakresu
|
|
65
|
|
|
66
|
uint32_t OneByteCharsetConverter::peek(const char*& it, const char* end) const {
|
|
67
|
return this->array[static_cast<unsigned char> (*it)];
|
|
68
69
70
|
}
uint32_t OneByteCharsetConverter::next(const char*& it, const char* end) const {
|
|
71
|
return this->array[static_cast<unsigned char> (*(it++))];
|
|
72
73
74
|
}
void OneByteCharsetConverter::append(uint32_t cp, std::string& result) const {
|
|
75
76
|
if (cp < this->codepoint2Char.size()) {
result.push_back(this->codepoint2Char[cp]);
|
|
77
|
} else {
|
|
78
79
|
result.push_back(DEFAULT_UNDEFINED_CHAR);
}
|
|
80
81
|
}
|
|
82
83
|
ISO8859_2_CharsetConverter::ISO8859_2_CharsetConverter()
: OneByteCharsetConverter(ISO_8859_2_TO_CODEPOINT) {
|
|
84
85
|
}
|
|
86
87
|
Windows_1250_CharsetConverter::Windows_1250_CharsetConverter()
: OneByteCharsetConverter(WINDOWS_1250_TO_CODEPOINT) {
|
|
88
|
}
|
|
89
|
|
|
90
91
92
93
|
CP852_CharsetConverter::CP852_CharsetConverter()
: OneByteCharsetConverter(CP852_TO_CODEPOINT) {
}
|
|
94
95
96
97
98
99
100
101
102
103
|
string CharsetConverter::fromUTF8(const string& input) const {
string res;
const char* currInput = input.c_str();
const char* inputEnd = input.c_str() + input.length();
while (currInput != inputEnd) {
uint32_t cp = utf8::next(currInput, inputEnd);
this->append(cp, res);
}
return res;
}
|