-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiconvconverter.cpp
94 lines (80 loc) · 2.57 KB
/
iconvconverter.cpp
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
/*
IconvQtPOC - Integrating iconv() for Unicode Conversion in Qt apps
Copyright (C) 2022 Pedro Lopez-Cabanillas
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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include <QScopedArrayPointer>
#include <QDebug>
#include "iconvconverter.h"
const ::iconv_t ICONV_INVALID = reinterpret_cast< ::iconv_t >(-1);
const ::size_t ICONV_ERROR = static_cast< ::size_t >(-1);
IconvConverter::IconvConverter(const char* source):
m_iconv(ICONV_INVALID)
{
open(source);
}
IconvConverter::IconvConverter(const QByteArray& source):
m_iconv(ICONV_INVALID)
{
open(source);
}
IconvConverter::~IconvConverter()
{
close();
}
void IconvConverter::reset()
{
if (m_iconv != ICONV_INVALID) {
::iconv(m_iconv, nullptr, nullptr, nullptr, nullptr);
}
}
QByteArray IconvConverter::source() const
{
return m_source;
}
void IconvConverter::open(const QByteArray &source)
{
::iconv_t tmp = ::iconv_open("UTF-8", source.data());
if (ICONV_INVALID == tmp) {
throw std::runtime_error("iconv initialization failed");
}
if (ICONV_INVALID != m_iconv) {
::iconv_close(m_iconv);
}
m_iconv = tmp;
m_source = source;
}
void IconvConverter::close()
{
if (ICONV_INVALID != m_iconv) {
::iconv_close(m_iconv);
}
m_iconv = ICONV_INVALID;
m_source.clear();
}
QByteArray IconvConverter::convert(const QByteArray &text)
{
::size_t source_size = text.size();
::size_t dest_size = 6 * source_size;
QByteArray source_buffer(text.data(), source_size);
QByteArray dest_buffer(dest_size, Qt::Uninitialized);
::size_t tmp_source_size = source_size;
::size_t tmp_dest_size = dest_size;
char* source_ptr = source_buffer.data();
char* dest_ptr = dest_buffer.data();
if (ICONV_ERROR == ::iconv(m_iconv, &source_ptr, &tmp_source_size, &dest_ptr, &tmp_dest_size)) {
throw std::runtime_error("iconv conversion failed");
}
dest_buffer.resize( dest_size - tmp_dest_size );
return dest_buffer;
}