|
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
29
30
31
|
uint32_t UTF8CharsetConverter::peek(const char*& it, const char* end) const {
return utf8::peek_next(it, end);
}
|
|
32
33
34
|
uint32_t UTF8CharsetConverter::next(const char*& it, const char* end) const {
return utf8::next(it, end);
}
|
|
35
|
|
|
36
|
void UTF8CharsetConverter::append(uint32_t cp, string& result) const {
|
|
37
|
utf8::append(cp, back_inserter(result));
|
|
38
|
}
|
|
39
|
|
|
40
|
static vector<char> reverseArray(const uint32_t* array) {
|
|
41
42
43
44
|
vector<char> res;
unsigned char c = 0;
do {
uint32_t codepoint = array[c];
|
|
45
46
|
res.resize(max(static_cast<uint32_t> (res.size()), codepoint + 1), DEFAULT_UNDEFINED_CHAR);
res[codepoint] = static_cast<char> (c);
|
|
47
|
c++;
|
|
48
|
} while (c != 255);
|
|
49
|
return res;
|
|
50
51
52
53
|
}
OneByteCharsetConverter::OneByteCharsetConverter(const uint32_t* array)
: array(array),
|
|
54
|
codepoint2Char(reverseArray(array)) {
|
|
55
56
|
}
|
|
57
|
// TODO - sprawdzanie zakresu
|
|
58
|
|
|
59
|
uint32_t OneByteCharsetConverter::peek(const char*& it, const char* end) const {
|
|
60
|
return this->array[static_cast<unsigned char> (*it)];
|
|
61
62
63
|
}
uint32_t OneByteCharsetConverter::next(const char*& it, const char* end) const {
|
|
64
|
return this->array[static_cast<unsigned char> (*(it++))];
|
|
65
66
67
|
}
void OneByteCharsetConverter::append(uint32_t cp, std::string& result) const {
|
|
68
69
|
if (cp < this->codepoint2Char.size()) {
result.push_back(this->codepoint2Char[cp]);
|
|
70
|
} else {
|
|
71
72
|
result.push_back(DEFAULT_UNDEFINED_CHAR);
}
|
|
73
74
|
}
|
|
75
76
|
ISO8859_2_CharsetConverter::ISO8859_2_CharsetConverter()
: OneByteCharsetConverter(ISO_8859_2_TO_CODEPOINT) {
|
|
77
78
|
}
|
|
79
80
|
Windows_1250_CharsetConverter::Windows_1250_CharsetConverter()
: OneByteCharsetConverter(WINDOWS_1250_TO_CODEPOINT) {
|
|
81
|
}
|
|
82
|
|
|
83
84
85
86
|
CP852_CharsetConverter::CP852_CharsetConverter()
: OneByteCharsetConverter(CP852_TO_CODEPOINT) {
}
|
|
87
88
89
90
91
92
93
94
95
96
|
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;
}
|