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 );
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.
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

View file

@ -21,11 +21,12 @@
#include <stub_msvc.h>
#endif
#include "iconv.hh"
#include <QString>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextCodec>
#include <QMap>
#include <QPair>
#include <QProcess>
@ -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,8 +860,14 @@ 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() );
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