clean: port away from QTextCodec in slob.cc

This commit is contained in:
shenleban tongying 2024-08-30 04:09:05 -04:00
parent 5cdac6be4d
commit ac43018f68
No known key found for this signature in database
3 changed files with 36 additions and 23 deletions

View file

@ -126,3 +126,13 @@ std::string Iconv::toUtf8( char const * fromEncoding, void const * fromData, siz
const QString outStr = ic.convert( fromData, dataSize ); const QString outStr = ic.convert( fromData, dataSize );
return outStr.toStdString(); 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 );
}

View file

@ -47,6 +47,8 @@ public:
// string. // string.
static std::string toUtf8( char const * fromEncoding, void const * fromData, size_t dataSize ); 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: private:
// Copying/assigning not supported // Copying/assigning not supported

View file

@ -21,11 +21,12 @@
#include <stub_msvc.h> #include <stub_msvc.h>
#endif #endif
#include "iconv.hh"
#include <QString> #include <QString>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
#include <QTextCodec>
#include <QMap> #include <QMap>
#include <QPair> #include <QPair>
#include <QProcess> #include <QProcess>
@ -127,9 +128,8 @@ private:
QFile file; QFile file;
QString fileName, dictionaryName; QString fileName, dictionaryName;
Compressions compression; Compressions compression;
QString encoding; std::string encoding;
unsigned char uuid[ 16 ]; unsigned char uuid[ 16 ];
QTextCodec * codec;
QMap< QString, QString > tags; QMap< QString, QString > tags;
QVector< QString > contentTypes; QVector< QString > contentTypes;
quint32 blobCount; quint32 blobCount;
@ -149,7 +149,6 @@ private:
public: public:
SlobFile(): SlobFile():
compression( UNKNOWN ), compression( UNKNOWN ),
codec( 0 ),
blobCount( 0 ), blobCount( 0 ),
storeOffset( 0 ), storeOffset( 0 ),
fileSize( 0 ), fileSize( 0 ),
@ -170,7 +169,7 @@ public:
return compression; return compression;
} }
QString const & getEncoding() const std::string const & getEncoding() const
{ {
return encoding; return encoding;
} }
@ -200,11 +199,6 @@ public:
return contentTypesCount; return contentTypesCount;
} }
QTextCodec * getCodec() const
{
return codec;
}
const RefOffsetsVector & getSortedRefOffsets(); const RefOffsetsVector & getSortedRefOffsets();
void clearRefOffsets() void clearRefOffsets()
@ -241,10 +235,17 @@ QString SlobFile::readString( unsigned length )
QByteArray data = file.read( length ); QByteArray data = file.read( length );
QString str; QString str;
if ( codec != 0 && !data.isEmpty() ) if ( !encoding.empty() && !data.isEmpty() ) {
str = codec->toUnicode( data ); try {
else 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 ); str = QString( data );
}
char term = 0; char term = 0;
int n = str.indexOf( term ); int n = str.indexOf( term );
@ -317,13 +318,7 @@ void SlobFile::open( const QString & name )
// Read encoding // Read encoding
encoding = readTinyText(); encoding = readTinyText().toStdString();
codec = QTextCodec::codecForName( encoding.toLatin1() );
if ( codec == nullptr ) {
error = QString( R"(for encoding "%1")" ).arg( encoding );
throw exNoCodecFound( string( error.toUtf8().data() ) );
}
// Read compression type // Read compression type
@ -865,9 +860,15 @@ quint32 SlobDictionary::readArticle( quint32 articleNumber, std::string & result
|| contentType.contains( "/css", Qt::CaseInsensitive ) || contentType.contains( "/css", Qt::CaseInsensitive )
|| contentType.contains( "/javascript", Qt::CaseInsensitive ) || contentType.contains( "/javascript", Qt::CaseInsensitive )
|| contentType.contains( "/json", Qt::CaseInsensitive ) ) { || contentType.contains( "/json", Qt::CaseInsensitive ) ) {
QTextCodec * codec = sf.getCodec(); QString content;
QString content = codec->toUnicode( data.c_str(), data.size() ); try {
result = string( content.toUtf8().data() ); 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 else
result = data; result = data;