File indexing completed on 2024-05-05 04:51:45
0001 /* 0002 SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "k3bencodingconverter.h" 0007 #include <config-k3b.h> 0008 0009 #include <QDebug> 0010 0011 #ifdef HAVE_ICONV 0012 #include <langinfo.h> 0013 #include <iconv.h> 0014 #endif 0015 0016 0017 class K3b::EncodingConverter::Private 0018 { 0019 public: 0020 #ifdef HAVE_ICONV 0021 iconv_t ic; 0022 #endif 0023 }; 0024 0025 0026 K3b::EncodingConverter::EncodingConverter() 0027 : d( new Private ) 0028 { 0029 #ifdef HAVE_ICONV 0030 char* codec = nl_langinfo( CODESET ); 0031 qDebug() << "(K3b::DataUrlAddingDialog) using locale codec: " << codec; 0032 d->ic = ::iconv_open( "UCS-2BE", codec ); 0033 #endif 0034 } 0035 0036 0037 K3b::EncodingConverter::~EncodingConverter() 0038 { 0039 #ifdef HAVE_ICONV 0040 ::iconv_close( d->ic ); 0041 #endif 0042 delete d; 0043 } 0044 0045 0046 bool K3b::EncodingConverter::encodedLocally( const QByteArray& s ) 0047 { 0048 #ifdef HAVE_ICONV 0049 QByteArray utf8Encoded( s.length()*2, '\0' ); 0050 #ifdef ICONV_SECOND_ARGUMENT_IS_CONST 0051 const char* in = s.data(); 0052 #else 0053 char* in = const_cast<char*>( s.data() ); 0054 #endif 0055 char* out = utf8Encoded.data(); 0056 size_t inSize = s.length(); 0057 size_t outSize = utf8Encoded.size(); 0058 return( (size_t)-1 != ::iconv( d->ic, &in, &inSize, &out, &outSize ) ); 0059 #else 0060 return true; 0061 #endif 0062 }