From 3f49d92011d71b7249deea31d5e1a4328999ca95 Mon Sep 17 00:00:00 2001 From: xiaoyifang Date: Fri, 22 Nov 2024 11:32:50 +0800 Subject: [PATCH] fix: dsl ann file codec detection --- src/common/utils.cc | 13 ++++++++++++- src/common/utils.hh | 3 ++- src/dict/dsl.cc | 4 ++-- src/dict/dsl_details.cc | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/common/utils.cc b/src/common/utils.cc index f27ced15..ccc6a8f2 100644 --- a/src/common/utils.cc +++ b/src/common/utils.cc @@ -48,7 +48,7 @@ QString unescapeAmps( QString const & str ) return result; } -QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 ) +QTextCodec::Encoding detectCodec( QByteArray & ba, char16_t expectedFirstCharacter = 0 ) { QStringConverter::Encoding detectedEncoding = QStringConverter::encodingForData( data, expectedFirstCharacter ); // mapping the encoding @@ -58,6 +58,17 @@ QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstChar // default utf8 return QTextCodec::Utf8; } + +QStringConverter::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 ) +{ + QStringConverter::Encoding detectedEncoding = QStringConverter::encodingForData( data, expectedFirstCharacter ); + // mapping the encoding + if ( detectedEncoding.has_value() ) { + return detectedEncoding.value(); + } + // default utf8 + return QStringConverter::Utf8; +} } // namespace Utils QString Utils::Path::combine( const QString & path1, const QString & path2 ) diff --git a/src/common/utils.hh b/src/common/utils.hh index 8f014ce3..5d62166f 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -371,6 +371,7 @@ const QMap< QStringConverter::Encoding, QTextCodec::Encoding > encodingMap = { }; } -QTextCodec::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 ); +QTextCodec::Encoding detectCodec( QByteArray & ba, char16_t expectedFirstCharacter = 0 ); +QStringConverter::Encoding detectEncoding( QByteArray & ba, char16_t expectedFirstCharacter = 0 ); } // namespace Utils diff --git a/src/dict/dsl.cc b/src/dict/dsl.cc index caebf7b6..c91101ae 100644 --- a/src/dict/dsl.cc +++ b/src/dict/dsl.cc @@ -1042,8 +1042,8 @@ QString const & DslDictionary::getDescription() QString data, str; str = annStream.readLine(); - auto codec = Utils::detectEncoding( str ); - annStream.setCodec( codec ); + auto encoding = Utils::detectEncoding( str ); + annStream.setEncoding( encoding ); if ( str.left( 10 ).compare( "#LANGUAGE " ) != 0 ) { annStream.seek( 0 ); diff --git a/src/dict/dsl_details.cc b/src/dict/dsl_details.cc index 3ab8a718..8afd4fe2 100644 --- a/src/dict/dsl_details.cc +++ b/src/dict/dsl_details.cc @@ -873,7 +873,7 @@ DslScanner::DslScanner( string const & fileName ): // Note that .dsl format always starts with "#NAME" - codec = Utils::detectEncoding( { firstBytes, firstBytesSize }, '#' ); + codec = Utils::detectCodec( { firstBytes, firstBytesSize }, '#' ); qDebug() << "DSL encoding ->" << codec->name();