mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 16:04:06 +00:00
clean: unify a common idx file read pattern uint32_t size + data
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
This commit is contained in:
parent
fa9ad2fdf7
commit
112874b0e3
|
@ -295,11 +295,7 @@ AardDictionary::AardDictionary( string const & id, string const & indexFile, vec
|
||||||
// Read dictionary name
|
// Read dictionary name
|
||||||
|
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
vector< char > dName( idx.read< quint32 >() );
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
if ( dName.size() ) {
|
|
||||||
idx.read( &dName.front(), dName.size() );
|
|
||||||
dictionaryName = string( &dName.front(), dName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the index
|
// Initialize the index
|
||||||
|
|
||||||
|
|
|
@ -258,15 +258,7 @@ BglDictionary::BglDictionary( string const & id, string const & indexFile, strin
|
||||||
|
|
||||||
// Read the dictionary's name
|
// Read the dictionary's name
|
||||||
|
|
||||||
size_t len = idx.read< uint32_t >();
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
|
|
||||||
if ( len ) {
|
|
||||||
vector< char > nameBuf( len );
|
|
||||||
|
|
||||||
idx.read( &nameBuf.front(), len );
|
|
||||||
|
|
||||||
dictionaryName = string( &nameBuf.front(), len );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the index
|
// Initialize the index
|
||||||
|
|
||||||
|
@ -899,8 +891,8 @@ void BglResourceRequest::run()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector< char > nameData( idx.read< uint32_t >() );
|
vector< char > nameData;
|
||||||
idx.read( &nameData.front(), nameData.size() );
|
idx.readU32SizeAndData<>( nameData );
|
||||||
|
|
||||||
for ( size_t x = nameData.size(); x--; ) {
|
for ( size_t x = nameData.size(); x--; ) {
|
||||||
nameData[ x ] = tolower( nameData[ x ] );
|
nameData[ x ] = tolower( nameData[ x ] );
|
||||||
|
@ -917,9 +909,9 @@ void BglResourceRequest::run()
|
||||||
|
|
||||||
data.resize( idx.read< uint32_t >() );
|
data.resize( idx.read< uint32_t >() );
|
||||||
|
|
||||||
vector< unsigned char > compressedData( idx.read< uint32_t >() );
|
vector< unsigned char > compressedData;
|
||||||
|
|
||||||
idx.read( &compressedData.front(), compressedData.size() );
|
idx.readU32SizeAndData<>( compressedData );
|
||||||
|
|
||||||
unsigned long decompressedLength = data.size();
|
unsigned long decompressedLength = data.size();
|
||||||
|
|
||||||
|
|
|
@ -163,11 +163,7 @@ DictdDictionary::DictdDictionary( string const & id,
|
||||||
// Read the dictionary name
|
// Read the dictionary name
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
|
|
||||||
vector< char > dName( idx.read< uint32_t >() );
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
if ( dName.size() > 0 ) {
|
|
||||||
idx.read( &dName.front(), dName.size() );
|
|
||||||
dictionaryName = string( &dName.front(), dName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open the .dict file
|
// Open the .dict file
|
||||||
|
|
||||||
|
|
|
@ -303,17 +303,9 @@ DslDictionary::DslDictionary( string const & id, string const & indexFile, vecto
|
||||||
|
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
|
|
||||||
vector< char > dName( idx.read< uint32_t >() );
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
if ( dName.size() > 0 ) {
|
idx.readU32SizeAndData<>( preferredSoundDictionary );
|
||||||
idx.read( &dName.front(), dName.size() );
|
|
||||||
dictionaryName = string( &dName.front(), dName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
vector< char > sName( idx.read< uint32_t >() );
|
|
||||||
if ( sName.size() > 0 ) {
|
|
||||||
idx.read( &sName.front(), sName.size() );
|
|
||||||
preferredSoundDictionary = string( &sName.front(), sName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
resourceDir1 = getDictionaryFilenames()[ 0 ] + ".files" + Utils::Fs::separator();
|
resourceDir1 = getDictionaryFilenames()[ 0 ] + ".files" + Utils::Fs::separator();
|
||||||
QString s = QString::fromStdString( getDictionaryFilenames()[ 0 ] );
|
QString s = QString::fromStdString( getDictionaryFilenames()[ 0 ] );
|
||||||
|
|
|
@ -461,11 +461,7 @@ GlsDictionary::GlsDictionary( string const & id, string const & indexFile, vecto
|
||||||
|
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
|
|
||||||
vector< char > dName( idx.read< uint32_t >() );
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
if ( dName.size() > 0 ) {
|
|
||||||
idx.read( &dName.front(), dName.size() );
|
|
||||||
dictionaryName = string( &dName.front(), dName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the index
|
// Initialize the index
|
||||||
|
|
||||||
|
|
|
@ -310,12 +310,7 @@ MdxDictionary::MdxDictionary( string const & id, string const & indexFile, vecto
|
||||||
{
|
{
|
||||||
// Read the dictionary's name
|
// Read the dictionary's name
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
size_t len = idx.read< uint32_t >();
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
vector< char > buf( len );
|
|
||||||
if ( len > 0 ) {
|
|
||||||
idx.read( &buf.front(), len );
|
|
||||||
dictionaryName = string( &buf.front(), len );
|
|
||||||
}
|
|
||||||
|
|
||||||
//fallback, use filename as dictionary name
|
//fallback, use filename as dictionary name
|
||||||
if ( dictionaryName.empty() ) {
|
if ( dictionaryName.empty() ) {
|
||||||
|
@ -324,12 +319,7 @@ MdxDictionary::MdxDictionary( string const & id, string const & indexFile, vecto
|
||||||
}
|
}
|
||||||
|
|
||||||
// then read the dictionary's encoding
|
// then read the dictionary's encoding
|
||||||
len = idx.read< uint32_t >();
|
idx.readU32SizeAndData<>( encoding );
|
||||||
if ( len > 0 ) {
|
|
||||||
buf.resize( len );
|
|
||||||
idx.read( &buf.front(), len );
|
|
||||||
encoding = string( &buf.front(), len );
|
|
||||||
}
|
|
||||||
|
|
||||||
dictFile.setFileName( QString::fromUtf8( dictionaryFiles[ 0 ].c_str() ) );
|
dictFile.setFileName( QString::fromUtf8( dictionaryFiles[ 0 ].c_str() ) );
|
||||||
dictFile.open( QIODevice::ReadOnly );
|
dictFile.open( QIODevice::ReadOnly );
|
||||||
|
|
|
@ -196,11 +196,7 @@ SdictDictionary::SdictDictionary( string const & id,
|
||||||
// Read dictionary name
|
// Read dictionary name
|
||||||
|
|
||||||
idx.seek( sizeof( idxHeader ) );
|
idx.seek( sizeof( idxHeader ) );
|
||||||
vector< char > dName( idx.read< uint32_t >() );
|
idx.readU32SizeAndData<>( dictionaryName );
|
||||||
if ( dName.size() > 0 ) {
|
|
||||||
idx.read( &dName.front(), dName.size() );
|
|
||||||
dictionaryName = string( &dName.front(), dName.size() );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the index
|
// Initialize the index
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,18 @@ public:
|
||||||
/// Like the above, but uses its own local internal buffer and strips newlines by default.
|
/// Like the above, but uses its own local internal buffer and strips newlines by default.
|
||||||
std::string gets( bool stripNl = true );
|
std::string gets( bool stripNl = true );
|
||||||
|
|
||||||
|
/// Read 32bit as uint, then reading the subsequent data into a container
|
||||||
|
template< typename T >
|
||||||
|
void readU32SizeAndData( T & container )
|
||||||
|
{
|
||||||
|
uint32_t size = 0;
|
||||||
|
read( &size, sizeof( uint32_t ) );
|
||||||
|
if ( size > 0 ) {
|
||||||
|
container.resize( size );
|
||||||
|
read( container.data(), size );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// export QFile::readall
|
/// export QFile::readall
|
||||||
QByteArray readall();
|
QByteArray readall();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue