fix:remove code smell

This commit is contained in:
Xiao YiFang 2023-03-25 23:38:48 +08:00 committed by xiaoyifang
parent 6abdaab5d3
commit da406e1e02

99
mdx.cc
View file

@ -1158,66 +1158,50 @@ QString MdxDictionary::getCachedFileName( QString filename )
QString fullName = cacheDirName + QDir::separator() + filename; QString fullName = cacheDirName + QDir::separator() + filename;
info.setFile( fullName ); info.setFile( fullName );
if( !info.exists() ) if( info.exists() )
{ return fullName;
QFile f( fullName ); QFile f( fullName );
if( f.open( QFile::WriteOnly ) ) if( !f.open( QFile::WriteOnly ) ) {
{ gdWarning( R"(Mdx: file "%s" creating error: "%s")", fullName.toUtf8().data(), f.errorString().toUtf8().data() );
gd::wstring resourceName = FsEncoding::decode( filename.toStdString() ); return QString();
vector< char > data; }
gd::wstring resourceName = FsEncoding::decode( filename.toStdString() );
vector< char > data;
// In order to prevent recursive internal redirection... // In order to prevent recursive internal redirection...
set< wstring > resourceIncluded; set< wstring > resourceIncluded;
for ( ;; ) for( ;; ) {
{ if( !resourceIncluded.insert( resourceName ).second )
if( !resourceIncluded.insert( resourceName ).second ) continue;
continue;
loadResourceFile( resourceName, data ); loadResourceFile( resourceName, data );
// Check if this file has a redirection // Check if this file has a redirection
// Always encoded in UTF16-LE // Always encoded in UTF16-LE
// L"@@@LINK=" // L"@@@LINK="
static const char pattern[16] = if( static const char pattern[ 16 ] =
{ { '@', '\0', '@', '\0', '@', '\0', 'L', '\0', 'I', '\0', 'N', '\0', 'K', '\0', '=', '\0' };
'@', '\0', '@', '\0', '@', '\0', 'L', '\0', 'I', '\0', 'N', '\0', 'K', '\0', '=', '\0' data.size() > sizeof( pattern ) && memcmp( &data.front(), pattern, sizeof( pattern ) ) == 0 ) {
}; data.push_back( '\0' );
data.push_back( '\0' );
if ( data.size() > sizeof( pattern ) ) QString target =
{ MdictParser::toUtf16( "UTF-16LE", &data.front() + sizeof( pattern ), data.size() - sizeof( pattern ) );
if ( memcmp( &data.front(), pattern, sizeof( pattern ) ) == 0 ) resourceName = gd::toWString( target.trimmed() );
{ continue;
data.push_back( '\0' );
data.push_back( '\0' );
QString target = MdictParser::toUtf16( "UTF-16LE", &data.front() + sizeof( pattern ),
data.size() - sizeof( pattern ) );
resourceName = gd::toWString( target.trimmed() );
continue;
}
}
break;
}
qint64 n = 0;
if( !data.empty() )
n = f.write( data.data(), data.size() );
f.close();
if( n < (qint64)data.size() )
{
gdWarning( R"(Mdx: file "%s" writing error: "%s")", fullName.toUtf8().data(),
f.errorString().toUtf8().data() );
return QString();
}
}
else
{
gdWarning( R"(Mdx: file "%s" creating error: "%s")", fullName.toUtf8().data(),
f.errorString().toUtf8().data() );
return QString();
} }
break;
}
qint64 n = 0;
if( !data.empty() )
n = f.write( data.data(), data.size() );
f.close();
if( n < (qint64) data.size() ) {
gdWarning( R"(Mdx: file "%s" writing error: "%s")", fullName.toUtf8().data(), f.errorString().toUtf8().data() );
return QString();
} }
return fullName; return fullName;
} }
@ -1236,9 +1220,8 @@ void MdxDictionary::loadResourceFile( const wstring & resourceName, vector< char
newResourceName.insert( 0, 1, '\\' ); newResourceName.insert( 0, 1, '\\' );
} }
// local file takes precedence // local file takes precedence
string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) + FsEncoding::separator() + u8ResourceName; if( string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) + FsEncoding::separator() + u8ResourceName;
File::exists( fn ) ) {
if( File::exists( fn ) ) {
File::loadFromFile( fn, data ); File::loadFromFile( fn, data );
return; return;
} }