From ac43018f6824eddbf830bad57a60ef4221eb9f2c Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Fri, 30 Aug 2024 04:09:05 -0400 Subject: [PATCH] clean: port away from QTextCodec in slob.cc --- src/common/iconv.cc | 10 ++++++++++ src/common/iconv.hh | 2 ++ src/dict/slob.cc | 47 +++++++++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/common/iconv.cc b/src/common/iconv.cc index 206cdbd7..76e36cd5 100644 --- a/src/common/iconv.cc +++ b/src/common/iconv.cc @@ -126,3 +126,13 @@ std::string Iconv::toUtf8( char const * fromEncoding, void const * fromData, siz const QString outStr = ic.convert( fromData, dataSize ); return outStr.toStdString(); } + +QString Iconv::toQString( char const * fromEncoding, void const * fromData, size_t dataSize ) +{ + if ( dataSize == 0 ) { + return {}; + } + + Iconv ic( fromEncoding ); + return ic.convert( fromData, dataSize ); +} diff --git a/src/common/iconv.hh b/src/common/iconv.hh index ec2312eb..3f8fa227 100644 --- a/src/common/iconv.hh +++ b/src/common/iconv.hh @@ -47,6 +47,8 @@ public: // string. static std::string toUtf8( char const * fromEncoding, void const * fromData, size_t dataSize ); + static QString toQString( char const * fromEncoding, void const * fromData, size_t dataSize ); + private: // Copying/assigning not supported diff --git a/src/dict/slob.cc b/src/dict/slob.cc index 3ccdc3ce..72c11437 100644 --- a/src/dict/slob.cc +++ b/src/dict/slob.cc @@ -21,11 +21,12 @@ #include #endif +#include "iconv.hh" + #include #include #include #include -#include #include #include #include @@ -127,9 +128,8 @@ private: QFile file; QString fileName, dictionaryName; Compressions compression; - QString encoding; + std::string encoding; unsigned char uuid[ 16 ]; - QTextCodec * codec; QMap< QString, QString > tags; QVector< QString > contentTypes; quint32 blobCount; @@ -149,7 +149,6 @@ private: public: SlobFile(): compression( UNKNOWN ), - codec( 0 ), blobCount( 0 ), storeOffset( 0 ), fileSize( 0 ), @@ -170,7 +169,7 @@ public: return compression; } - QString const & getEncoding() const + std::string const & getEncoding() const { return encoding; } @@ -200,11 +199,6 @@ public: return contentTypesCount; } - QTextCodec * getCodec() const - { - return codec; - } - const RefOffsetsVector & getSortedRefOffsets(); void clearRefOffsets() @@ -241,10 +235,17 @@ QString SlobFile::readString( unsigned length ) QByteArray data = file.read( length ); QString str; - if ( codec != 0 && !data.isEmpty() ) - str = codec->toUnicode( data ); - else + if ( !encoding.empty() && !data.isEmpty() ) { + try { + str = Iconv::toQString( encoding.c_str(), data.data(), data.size() ); + } + catch ( Iconv::Ex & e ) { + qDebug() << QString( R"(slob decoding failed: %1)" ).arg( e.what() ); + } + } + else { str = QString( data ); + } char term = 0; int n = str.indexOf( term ); @@ -317,13 +318,7 @@ void SlobFile::open( const QString & name ) // Read encoding - encoding = readTinyText(); - - codec = QTextCodec::codecForName( encoding.toLatin1() ); - if ( codec == nullptr ) { - error = QString( R"(for encoding "%1")" ).arg( encoding ); - throw exNoCodecFound( string( error.toUtf8().data() ) ); - } + encoding = readTinyText().toStdString(); // Read compression type @@ -865,9 +860,15 @@ quint32 SlobDictionary::readArticle( quint32 articleNumber, std::string & result || contentType.contains( "/css", Qt::CaseInsensitive ) || contentType.contains( "/javascript", Qt::CaseInsensitive ) || contentType.contains( "/json", Qt::CaseInsensitive ) ) { - QTextCodec * codec = sf.getCodec(); - QString content = codec->toUnicode( data.c_str(), data.size() ); - result = string( content.toUtf8().data() ); + QString content; + try { + content = Iconv::toQString( sf.getEncoding().c_str(), data.data(), data.size() ); + } + catch ( Iconv::Ex & e ) { + qDebug() << QString( R"(slob decoding failed: %1)" ).arg( e.what() ); + } + + result = string( content.toUtf8().data() ); } else result = data;