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

49
mdx.cc
View file

@ -1158,19 +1158,20 @@ 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() );
return QString();
}
gd::wstring resourceName = FsEncoding::decode( filename.toStdString() ); gd::wstring resourceName = FsEncoding::decode( filename.toStdString() );
vector< char > data; 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;
@ -1179,23 +1180,16 @@ QString MdxDictionary::getCachedFileName( QString filename )
// 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 ) {
};
if ( data.size() > sizeof( pattern ) )
{
if ( memcmp( &data.front(), pattern, sizeof( pattern ) ) == 0 )
{
data.push_back( '\0' ); data.push_back( '\0' );
data.push_back( '\0' ); data.push_back( '\0' );
QString target = MdictParser::toUtf16( "UTF-16LE", &data.front() + sizeof( pattern ), QString target =
data.size() - sizeof( pattern ) ); MdictParser::toUtf16( "UTF-16LE", &data.front() + sizeof( pattern ), data.size() - sizeof( pattern ) );
resourceName = gd::toWString( target.trimmed() ); resourceName = gd::toWString( target.trimmed() );
continue; continue;
} }
}
break; break;
} }
@ -1205,20 +1199,10 @@ QString MdxDictionary::getCachedFileName( QString filename )
f.close(); f.close();
if( n < (qint64)data.size() ) if( n < (qint64) data.size() ) {
{ gdWarning( R"(Mdx: file "%s" writing error: "%s")", fullName.toUtf8().data(), f.errorString().toUtf8().data() );
gdWarning( R"(Mdx: file "%s" writing error: "%s")", fullName.toUtf8().data(),
f.errorString().toUtf8().data() );
return QString(); return QString();
} }
}
else
{
gdWarning( R"(Mdx: file "%s" creating 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;
} }