mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
fix: dsl ann file codec detection
This commit is contained in:
parent
5b70a7e081
commit
0f3336977f
|
@ -47,6 +47,17 @@ QString unescapeAmps( QString const & str )
|
|||
result.replace( "&&", "&" );
|
||||
return result;
|
||||
}
|
||||
|
||||
QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 )
|
||||
{
|
||||
QStringConverter::Encoding detectedEncoding = QStringConverter::encodingForData( data, expectedFirstCharacter );
|
||||
// mapping the encoding
|
||||
if ( detectedEncoding.has_value() && encodingMap.contains( detectedEncoding.value() ) ) {
|
||||
return encodingMap[ encoding ];
|
||||
}
|
||||
// default utf8
|
||||
return QTextCodec::Utf8;
|
||||
}
|
||||
} // namespace Utils
|
||||
|
||||
QString Utils::Path::combine( const QString & path1, const QString & path2 )
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QWidget>
|
||||
#include <QStringConverter>
|
||||
#include <QTextCodec>
|
||||
#include <QMap>
|
||||
#include "filetype.hh"
|
||||
#include <string>
|
||||
using std::string;
|
||||
|
@ -353,4 +356,21 @@ QString escapeAmps( QString const & str );
|
|||
|
||||
QString unescapeAmps( QString const & str );
|
||||
|
||||
//restrict scope to this very file.
|
||||
namespace{
|
||||
// encoding mappings between QStringConverter::Encoding and QTextCodec::Encoding
|
||||
const QMap< QStringConverter::Encoding, QTextCodec::Encoding > encodingMap = {
|
||||
{ QStringConverter::Latin1, QTextCodec::Latin1 },
|
||||
{ QStringConverter::Utf8, QTextCodec::Utf8 },
|
||||
{ QStringConverter::Utf16, QTextCodec::Utf16 },
|
||||
{ QStringConverter::Utf16LE , QTextCodec::Utf16LE },
|
||||
{ QStringConverter::Utf16BE , QTextCodec::Utf16BE },
|
||||
{ QStringConverter::Utf32LE , QTextCodec::Utf32LE },
|
||||
{ QStringConverter::Utf32BE , QTextCodec::Utf32BE },
|
||||
// others
|
||||
};
|
||||
}
|
||||
|
||||
QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 );
|
||||
|
||||
} // namespace Utils
|
||||
|
|
|
@ -1042,6 +1042,8 @@ QString const & DslDictionary::getDescription()
|
|||
QString data, str;
|
||||
|
||||
str = annStream.readLine();
|
||||
auto codec = Utils::detectEncoding( str );
|
||||
annStream.setCodec( codec );
|
||||
|
||||
if ( str.left( 10 ).compare( "#LANGUAGE " ) != 0 ) {
|
||||
annStream.seek( 0 );
|
||||
|
|
|
@ -872,30 +872,8 @@ DslScanner::DslScanner( string const & fileName ):
|
|||
bool needExactEncoding = false;
|
||||
|
||||
// Note that .dsl format always starts with "#NAME"
|
||||
if ( auto guessedEncoding = QStringConverter::encodingForData( { firstBytes, firstBytesSize }, '#' );
|
||||
guessedEncoding.has_value() ) {
|
||||
switch ( guessedEncoding.value() ) {
|
||||
case QStringConverter::Utf8:
|
||||
encoding = Utf8::Utf8;
|
||||
break;
|
||||
case QStringConverter::Utf16LE:
|
||||
encoding = Utf8::Utf16LE;
|
||||
break;
|
||||
case QStringConverter::Utf16BE:
|
||||
encoding = Utf8::Utf16BE;
|
||||
break;
|
||||
case QStringConverter::Utf32LE:
|
||||
encoding = Utf8::Utf16LE;
|
||||
break;
|
||||
case QStringConverter::Utf32BE:
|
||||
encoding = Utf8::Utf32BE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
codec = QTextCodec::codecForName( getEncodingNameFor( encoding ) );
|
||||
codec = Utils::detectEncoding( { firstBytes, firstBytesSize }, '#' );
|
||||
|
||||
qDebug() << "DSL encoding ->" << codec->name();
|
||||
|
||||
|
|
Loading…
Reference in a new issue