1 /*
2     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "k3bencodingconverter.h"
7 #include <config-k3b.h>
8 
9 #include <QDebug>
10 
11 #ifdef HAVE_ICONV
12 #include <langinfo.h>
13 #include <iconv.h>
14 #endif
15 
16 
17 class K3b::EncodingConverter::Private
18 {
19 public:
20 #ifdef HAVE_ICONV
21     iconv_t ic;
22 #endif
23 };
24 
25 
EncodingConverter()26 K3b::EncodingConverter::EncodingConverter()
27     : d( new Private )
28 {
29 #ifdef HAVE_ICONV
30     char* codec = nl_langinfo( CODESET );
31     qDebug() << "(K3b::DataUrlAddingDialog) using locale codec: " << codec;
32     d->ic = ::iconv_open( "UCS-2BE", codec );
33 #endif
34 }
35 
36 
~EncodingConverter()37 K3b::EncodingConverter::~EncodingConverter()
38 {
39 #ifdef HAVE_ICONV
40     ::iconv_close( d->ic );
41 #endif
42     delete d;
43 }
44 
45 
encodedLocally(const QByteArray & s)46 bool K3b::EncodingConverter::encodedLocally( const QByteArray& s )
47 {
48 #ifdef HAVE_ICONV
49     QByteArray utf8Encoded( s.length()*2, '\0' );
50 #ifdef ICONV_SECOND_ARGUMENT_IS_CONST
51     const char* in = s.data();
52 #else
53     char* in = const_cast<char*>( s.data() );
54 #endif
55     char* out = utf8Encoded.data();
56     size_t inSize = s.length();
57     size_t outSize = utf8Encoded.size();
58     return( (size_t)-1 != ::iconv( d->ic, &in, &inSize, &out, &outSize ) );
59 #else
60     return true;
61 #endif
62 }
63