Skip to content

Commit

Permalink
Merge branch 'staged' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Dec 29, 2024
2 parents 7f62fc8 + 68522ad commit 4b3d090
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 548 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/auto format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- run: npm ci
- run: |
npm install -g prettier
- run: npx prettier . --write
- uses: autofix-ci/action@ff86a557419858bb967097bfc916833f5647fa8c
1 change: 0 additions & 1 deletion .prettierrc.json

This file was deleted.

28 changes: 0 additions & 28 deletions package-lock.json

This file was deleted.

5 changes: 0 additions & 5 deletions package.json

This file was deleted.

11 changes: 5 additions & 6 deletions src/common/dictionary_icon_name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ QString Icons::DictionaryIconName::getIconName( const QString & dictionaryName )
//get the first character of the dictionary name
QString name = dictionaryName.at( 0 ).toUpper();
auto it1 = _iconDictionaryNames.contains( name );
std::vector< QString > vector = {};
if ( it1 ) {
vector = _iconDictionaryNames.value( name );
vector.emplace_back( dictionaryName );
auto vector = _iconDictionaryNames.value( name );
vector++;
_iconDictionaryNames.insert( name, vector );
}
else {
vector.emplace_back( dictionaryName );
_iconDictionaryNames.insert( name, vector );
_iconDictionaryNames.insert( name, 1 );
}

name = name + QString::number( vector.size() );
name = name + QString::number( _iconDictionaryNames.value( name ) );
_dictionaryIconNames.insert( dictionaryName, name );
return name;
}
2 changes: 1 addition & 1 deletion src/common/dictionary_icon_name.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Icons {
class DictionaryIconName
{
//map icon name to dictionary names;
QMap< QString, std::vector< QString > > _iconDictionaryNames;
QMap< QString, int > _iconDictionaryNames;
//map dictionary name to icon name;
QMap< QString, QString > _dictionaryIconNames;

Expand Down
40 changes: 40 additions & 0 deletions src/common/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QTextDocument>
#include <QUrl>
#include <QUrlQuery>
#include <QFileInfo>
#include <QWidget>
#include "filetype.hh"
#include <string>
Expand Down Expand Up @@ -356,6 +357,45 @@ string basename( string const & );
void removeDirectory( QString const & directory );

void removeDirectory( string const & directory );

inline QString findFirstExistingFile( std::initializer_list< QString > filePaths )
{
for ( const QString & filePath : filePaths ) {
if ( QFileInfo::exists( filePath ) ) {
return filePath;
}
}
return QString();
}

inline std::string findFirstExistingFile( std::initializer_list< std::string > filePaths )
{
for ( const std::string & filePath : filePaths ) {
auto fp = QString::fromStdString( filePath );
if ( QFileInfo::exists( fp ) ) {
return filePath;
}
}
return {};
}

inline bool anyExistingFile( std::initializer_list< std::string > filePaths )
{
for ( const std::string & filePath : filePaths ) {
auto fp = QString::fromStdString( filePath );
if ( QFileInfo::exists( fp ) ) {
return true;
}
}
return false;
}

// used for std::string and char*
inline bool exists( std::string_view filename ) noexcept
{
return QFileInfo::exists( QString::fromUtf8( filename.data(), filename.size() ) );
}

} // namespace Fs

namespace WebSite {
Expand Down
7 changes: 2 additions & 5 deletions src/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ bool Class::loadIconFromText( const QString & iconUrl, QString const & text )

QFont font = painter.font();
//the orderNum should be a little smaller than the icon
font.setPixelSize( iconSize * 0.6 );
font.setPixelSize( iconSize * 0.8 );
font.setWeight( QFont::Bold );
painter.setFont( font );

Expand All @@ -328,12 +328,9 @@ bool Class::loadIconFromText( const QString & iconUrl, QString const & text )
font.setPixelSize( iconSize * 0.4 );
QFontMetrics fm1( font );
const QString & orderNum = abbrName.mid( 1 );
int orderNumberWidth = fm1.horizontalAdvance( orderNum );

painter.setFont( font );
painter.drawText( rectangle.x() + rectangle.width() - orderNumberWidth * 1.2,
rectangle.y() + rectangle.height(),
orderNum );
painter.drawText( rectangle, Qt::AlignRight | Qt::AlignBottom, orderNum );

painter.end();

Expand Down
73 changes: 24 additions & 49 deletions src/dict/dsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1604,55 +1604,29 @@ void DslResourceRequest::run()

string n = dict.getContainingFolder().toStdString() + Utils::Fs::separator() + resourceName;

qDebug( "dsl resource name is %s", n.c_str() );

auto fp = Utils::Fs::findFirstExistingFile(
{ n, dict.getResourceDir1() + resourceName, dict.getResourceDir2() + resourceName } );
qDebug( "found dsl resource name is %s", fp.c_str() );
try {
try {
QMutexLocker _( &dataMutex );
QMutexLocker _( &dataMutex );

File::loadFromFile( n, data );
if ( !fp.empty() ) {
File::loadFromFile( fp, data );
}
catch ( File::exCantOpen & ) {
n = dict.getResourceDir1() + resourceName;
try {
QMutexLocker _( &dataMutex );

File::loadFromFile( n, data );
}
catch ( File::exCantOpen & ) {
n = dict.getResourceDir2() + resourceName;

try {
QMutexLocker _( &dataMutex );

File::loadFromFile( n, data );
}
catch ( File::exCantOpen & ) {
// Try reading from zip file

if ( dict.resourceZip.isOpen() ) {
QMutexLocker _( &dataMutex );

if ( !dict.resourceZip.loadFile( Text::toUtf32( resourceName ), data ) ) {
throw; // Make it fail since we couldn't read the archive
}
}
else {
throw;
}
}
else if ( dict.resourceZip.isOpen() ) {
if ( !dict.resourceZip.loadFile( Text::toUtf32( resourceName ), data ) ) {
throw std::runtime_error( "Failed to load file from resource zip" );
}
}
else {
throw std::runtime_error( "Resource zip not opened" );
}

if ( Filetype::isNameOfTiff( resourceName ) ) {
// Convert it

QMutexLocker _( &dataMutex );
GdTiff::tiff2img( data );
}

QMutexLocker _( &dataMutex );

hasAnyData = true;
}
catch ( std::exception & ex ) {
Expand Down Expand Up @@ -1723,13 +1697,14 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
string baseName = ( fileName[ fileName.size() - 4 ] == '.' ) ? string( fileName, 0, fileName.size() - 4 ) :
string( fileName, 0, fileName.size() - 7 );

string abrvFileName;
string abrvFileName = Utils::Fs::findFirstExistingFile( { baseName + "_abrv.dsl",
baseName + "_abrv.dsl.dz",
baseName + "_ABRV.DSL",
baseName + "_ABRV.DSL.DZ",
baseName + "_ABRV.DSL.dz" } );

if ( File::tryPossibleName( baseName + "_abrv.dsl", abrvFileName )
|| File::tryPossibleName( baseName + "_abrv.dsl.dz", abrvFileName )
|| File::tryPossibleName( baseName + "_ABRV.DSL", abrvFileName )
|| File::tryPossibleName( baseName + "_ABRV.DSL.DZ", abrvFileName )
|| File::tryPossibleName( baseName + "_ABRV.DSL.dz", abrvFileName ) ) {
//check empty string
if ( abrvFileName.size() ) {
dictFiles.push_back( abrvFileName );
}

Expand All @@ -1739,12 +1714,12 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f

// See if there's a zip file with resources present. If so, include it.

string zipFileName;
string zipFileName = Utils::Fs::findFirstExistingFile( { baseName + ".dsl.files.zip",
baseName + ".dsl.dz.files.zip",
baseName + ".DSL.FILES.ZIP",
baseName + ".DSL.DZ.FILES.ZIP" } );

if ( File::tryPossibleZipName( baseName + ".dsl.files.zip", zipFileName )
|| File::tryPossibleZipName( baseName + ".dsl.dz.files.zip", zipFileName )
|| File::tryPossibleZipName( baseName + ".DSL.FILES.ZIP", zipFileName )
|| File::tryPossibleZipName( baseName + ".DSL.DZ.FILES.ZIP", zipFileName ) ) {
if ( !zipFileName.empty() ) {
dictFiles.push_back( zipFileName );
}

Expand Down
5 changes: 3 additions & 2 deletions src/dict/mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,8 @@ void MdxDictionary::loadResourceFile( const std::u32string & resourceName, vecto
newResourceName.insert( 0, 1, '\\' );
}
// local file takes precedence
if ( string fn = getContainingFolder().toStdString() + Utils::Fs::separator() + u8ResourceName; File::exists( fn ) ) {
if ( string fn = getContainingFolder().toStdString() + Utils::Fs::separator() + u8ResourceName;
Utils::Fs::exists( fn ) ) {
File::loadFromFile( fn, data );
return;
}
Expand Down Expand Up @@ -1342,7 +1343,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
initializing.indexingDictionary( title );

for ( vector< string >::const_iterator mddIter = dictFiles.begin() + 1; mddIter != dictFiles.end(); ++mddIter ) {
if ( File::exists( *mddIter ) ) {
if ( Utils::Fs::exists( *mddIter ) ) {
sptr< MdictParser > mddParser = std::make_shared< MdictParser >();
if ( !mddParser->open( mddIter->c_str() ) ) {
qWarning( "Broken mdd (resource) file: %s", mddIter->c_str() );
Expand Down
17 changes: 9 additions & 8 deletions src/dict/stardict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1658,20 +1658,21 @@ static void findCorrespondingFiles( string const & ifo, string & idx, string & d
{
string base( ifo, 0, ifo.size() - 3 );

if ( !( File::tryPossibleName( base + "idx", idx ) || File::tryPossibleName( base + "idx.gz", idx )
|| File::tryPossibleName( base + "idx.dz", idx ) || File::tryPossibleName( base + "IDX", idx )
|| File::tryPossibleName( base + "IDX.GZ", idx ) || File::tryPossibleName( base + "IDX.DZ", idx ) ) ) {
idx = Utils::Fs::findFirstExistingFile(
{ base + "idx", base + "idx.gz", base + "idx.dz", base + "IDX", base + "IDX.GZ", base + "IDX.DZ" } );
if ( idx.empty() ) {
throw exNoIdxFile( ifo );
}

if ( !( File::tryPossibleName( base + "dict", dict ) || File::tryPossibleName( base + "dict.dz", dict )
|| File::tryPossibleName( base + "DICT", dict ) || File::tryPossibleName( base + "dict.DZ", dict ) ) ) {
dict = Utils::Fs::findFirstExistingFile( { base + "dict", base + "dict.dz", base + "DICT", base + "dict.DZ" } );
if ( dict.empty() ) {
throw exNoDictFile( ifo );
}

if ( !( File::tryPossibleName( base + "syn", syn ) || File::tryPossibleName( base + "syn.gz", syn )
|| File::tryPossibleName( base + "syn.dz", syn ) || File::tryPossibleName( base + "SYN", syn )
|| File::tryPossibleName( base + "SYN.GZ", syn ) || File::tryPossibleName( base + "SYN.DZ", syn ) ) ) {
syn = Utils::Fs::findFirstExistingFile(
{ base + "syn", base + "syn.gz", base + "syn.dz", base + "SYN", base + "SYN.GZ", base + "SYN.DZ" } );

if ( syn.empty() ) {
syn.clear();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dict/utils/dictfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
#include "zipfile.hh"

#include <string>
#include <QFileInfo>
#ifdef __WIN32
#include <windows.h>
#endif
#include "utils.hh"

namespace File {

bool tryPossibleName( std::string const & name, std::string & copyTo )
{
if ( File::exists( name ) ) {
if ( Utils::Fs::exists( name ) ) {
copyTo = name;
return true;
}
Expand Down
5 changes: 0 additions & 5 deletions src/dict/utils/dictfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ bool tryPossibleZipName( std::string const & name, std::string & copyTo );

void loadFromFile( std::string const & filename, std::vector< char > & data );

// QFileInfo::exists but used for std::string and char*
inline bool exists( std::string_view filename ) noexcept
{
return QFileInfo::exists( QString::fromUtf8( filename.data(), filename.size() ) );
};

/// Exclusivly used for processing GD's index files
class Index
Expand Down
26 changes: 18 additions & 8 deletions src/dict/utils/indexedzip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
return (size_t)zip.read( &data.front(), data.size() ) == data.size();

case ZipFile::Deflated: {
// Now do the deflation
// Decompress the data using the zlib library

QByteArray compressedData = zip.read( header.compressedSize );

Expand All @@ -94,9 +94,7 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )

data.resize( header.uncompressedSize );

z_stream stream;

memset( &stream, 0, sizeof( stream ) );
z_stream stream = {};

stream.next_in = (Bytef *)compressedData.data();
stream.avail_in = compressedData.size();
Expand All @@ -108,17 +106,29 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
return false;
}

if ( inflate( &stream, Z_FINISH ) != Z_STREAM_END ) {
qDebug( "Not zstream end!" );
int ret = inflate( &stream, Z_FINISH );
if ( ret != Z_STREAM_END ) {
qDebug() << "Not zstream end! Stream total_in:" << stream.total_in << "total_out:" << stream.total_out
<< "msg:" << ( stream.msg ? stream.msg : "none" );

data.clear();

inflateEnd( &stream );
int endRet = inflateEnd( &stream );
if ( endRet != Z_OK ) {
qDebug() << "inflateEnd failed after inflate! msg:" << ( stream.msg ? stream.msg : "none" );
}

return false;
}

inflateEnd( &stream );
ret = inflateEnd( &stream );
if ( ret != Z_OK ) {
qDebug() << "inflateEnd failed! msg:" << ( stream.msg ? stream.msg : "none" );

data.clear();

return false;
}

return true;
}
Expand Down
Loading

0 comments on commit 4b3d090

Please sign in to comment.