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;
info.setFile( fullName );
if( !info.exists() )
{
QFile f( fullName );
if( f.open( QFile::WriteOnly ) )
{
gd::wstring resourceName = FsEncoding::decode( filename.toStdString() );
vector< char > data;
if( info.exists() )
return fullName;
QFile f( fullName );
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() );
vector< char > data;
// In order to prevent recursive internal redirection...
set< wstring > resourceIncluded;
// In order to prevent recursive internal redirection...
set< wstring > resourceIncluded;
for ( ;; )
{
if( !resourceIncluded.insert( resourceName ).second )
continue;
for( ;; ) {
if( !resourceIncluded.insert( resourceName ).second )
continue;
loadResourceFile( resourceName, data );
loadResourceFile( resourceName, data );
// Check if this file has a redirection
// Always encoded in UTF16-LE
// L"@@@LINK="
static const char pattern[16] =
{
'@', '\0', '@', '\0', '@', '\0', 'L', '\0', 'I', '\0', 'N', '\0', 'K', '\0', '=', '\0'
};
if ( data.size() > sizeof( pattern ) )
{
if ( memcmp( &data.front(), pattern, sizeof( pattern ) ) == 0 )
{
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();
// Check if this file has a redirection
// Always encoded in UTF16-LE
// L"@@@LINK="
if( static const char pattern[ 16 ] =
{ '@', '\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' );
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();
}
return fullName;
}
@ -1236,9 +1220,8 @@ void MdxDictionary::loadResourceFile( const wstring & resourceName, vector< char
newResourceName.insert( 0, 1, '\\' );
}
// local file takes precedence
string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) + FsEncoding::separator() + u8ResourceName;
if( File::exists( fn ) ) {
if( string fn = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) + FsEncoding::separator() + u8ResourceName;
File::exists( fn ) ) {
File::loadFromFile( fn, data );
return;
}