Skip to content

Commit

Permalink
opt: 1
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Dec 26, 2024
1 parent 2e7ead0 commit c5db920
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
18 changes: 18 additions & 0 deletions src/common/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ inline std::string findFirstExistingFile( std::initializer_list< std::string > f
}
return {};
}

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

// 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() ) );
}

} // namespace Fs

namespace WebSite {
Expand Down
4 changes: 2 additions & 2 deletions src/dict/mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ 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 +1342,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
32 changes: 23 additions & 9 deletions src/dict/stardict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1658,20 +1658,34 @@ 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 ) ) ) {
throw exNoIdxFile( ifo );
vector<string> idxFiles = {
base + "idx", base + "idx.gz", base + "idx.dz",
base + "IDX", base + "IDX.GZ", base + "IDX.DZ"
};

auto ret = Utils::Fs::anyExistingFile(idxFiles);
if(!ret) {
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 ) ) ) {
vector<string> dictFiles = {
base + "dict", base + "dict.dz",
base + "DICT", base + "dict.DZ"
};

ret = Utils::Fs::findFirstExistingFile(dictFiles);
if(!ret) {
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 ) ) ) {
vector<string> synFiles = {
base + "syn", base + "syn.gz", base + "syn.dz",
base + "SYN", base + "SYN.GZ", base + "SYN.DZ"
};

ret = Utils::Fs::findFirstExistingFile(synFiles);

if ( !ret ) {
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
6 changes: 1 addition & 5 deletions src/dict/utils/dictfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ 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

0 comments on commit c5db920

Please sign in to comment.