diff --git a/mdx.cc b/mdx.cc index cc4da83b..983a1107 100644 --- a/mdx.cc +++ b/mdx.cc @@ -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; }