From 112874b0e34405859c52f7058497fed11be7601c Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Wed, 20 Nov 2024 03:22:08 -0500 Subject: [PATCH] clean: unify a common idx file read pattern uint32_t size + data --- src/dict/aard.cc | 6 +----- src/dict/bgl.cc | 18 +++++------------- src/dict/dictdfiles.cc | 6 +----- src/dict/dsl.cc | 12 ++---------- src/dict/gls.cc | 6 +----- src/dict/mdx.cc | 14 ++------------ src/dict/sdict.cc | 6 +----- src/dict/utils/dictfile.hh | 12 ++++++++++++ 8 files changed, 25 insertions(+), 55 deletions(-) diff --git a/src/dict/aard.cc b/src/dict/aard.cc index 5e68cbcd..bcf982d2 100644 --- a/src/dict/aard.cc +++ b/src/dict/aard.cc @@ -295,11 +295,7 @@ AardDictionary::AardDictionary( string const & id, string const & indexFile, vec // Read dictionary name idx.seek( sizeof( idxHeader ) ); - vector< char > dName( idx.read< quint32 >() ); - if ( dName.size() ) { - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); - } + idx.readU32SizeAndData<>( dictionaryName ); // Initialize the index diff --git a/src/dict/bgl.cc b/src/dict/bgl.cc index 1ca71c55..3632d9df 100644 --- a/src/dict/bgl.cc +++ b/src/dict/bgl.cc @@ -258,15 +258,7 @@ BglDictionary::BglDictionary( string const & id, string const & indexFile, strin // Read the dictionary's name - size_t len = idx.read< uint32_t >(); - - if ( len ) { - vector< char > nameBuf( len ); - - idx.read( &nameBuf.front(), len ); - - dictionaryName = string( &nameBuf.front(), len ); - } + idx.readU32SizeAndData<>( dictionaryName ); // Initialize the index @@ -899,8 +891,8 @@ void BglResourceRequest::run() break; } - vector< char > nameData( idx.read< uint32_t >() ); - idx.read( &nameData.front(), nameData.size() ); + vector< char > nameData; + idx.readU32SizeAndData<>( nameData ); for ( size_t x = nameData.size(); x--; ) { nameData[ x ] = tolower( nameData[ x ] ); @@ -917,9 +909,9 @@ void BglResourceRequest::run() 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(); diff --git a/src/dict/dictdfiles.cc b/src/dict/dictdfiles.cc index cc9bcbc4..8a52fc63 100644 --- a/src/dict/dictdfiles.cc +++ b/src/dict/dictdfiles.cc @@ -163,11 +163,7 @@ DictdDictionary::DictdDictionary( string const & id, // Read the dictionary name idx.seek( sizeof( idxHeader ) ); - vector< char > dName( idx.read< uint32_t >() ); - if ( dName.size() > 0 ) { - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); - } + idx.readU32SizeAndData<>( dictionaryName ); // Open the .dict file diff --git a/src/dict/dsl.cc b/src/dict/dsl.cc index 16579f9f..3eedafa7 100644 --- a/src/dict/dsl.cc +++ b/src/dict/dsl.cc @@ -303,17 +303,9 @@ DslDictionary::DslDictionary( string const & id, string const & indexFile, vecto idx.seek( sizeof( idxHeader ) ); - vector< char > dName( idx.read< uint32_t >() ); - if ( dName.size() > 0 ) { - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); - } + idx.readU32SizeAndData<>( dictionaryName ); + idx.readU32SizeAndData<>( preferredSoundDictionary ); - 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(); QString s = QString::fromStdString( getDictionaryFilenames()[ 0 ] ); diff --git a/src/dict/gls.cc b/src/dict/gls.cc index dc28abfc..3bf734b1 100644 --- a/src/dict/gls.cc +++ b/src/dict/gls.cc @@ -461,11 +461,7 @@ GlsDictionary::GlsDictionary( string const & id, string const & indexFile, vecto idx.seek( sizeof( idxHeader ) ); - vector< char > dName( idx.read< uint32_t >() ); - if ( dName.size() > 0 ) { - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); - } + idx.readU32SizeAndData<>( dictionaryName ); // Initialize the index diff --git a/src/dict/mdx.cc b/src/dict/mdx.cc index a7b436dd..c849f7ae 100644 --- a/src/dict/mdx.cc +++ b/src/dict/mdx.cc @@ -310,12 +310,7 @@ MdxDictionary::MdxDictionary( string const & id, string const & indexFile, vecto { // Read the dictionary's name idx.seek( sizeof( idxHeader ) ); - size_t len = idx.read< uint32_t >(); - vector< char > buf( len ); - if ( len > 0 ) { - idx.read( &buf.front(), len ); - dictionaryName = string( &buf.front(), len ); - } + idx.readU32SizeAndData<>( dictionaryName ); //fallback, use filename as dictionary name if ( dictionaryName.empty() ) { @@ -324,12 +319,7 @@ MdxDictionary::MdxDictionary( string const & id, string const & indexFile, vecto } // then read the dictionary's encoding - len = idx.read< uint32_t >(); - if ( len > 0 ) { - buf.resize( len ); - idx.read( &buf.front(), len ); - encoding = string( &buf.front(), len ); - } + idx.readU32SizeAndData<>( encoding ); dictFile.setFileName( QString::fromUtf8( dictionaryFiles[ 0 ].c_str() ) ); dictFile.open( QIODevice::ReadOnly ); diff --git a/src/dict/sdict.cc b/src/dict/sdict.cc index f03dd76f..837c85ae 100644 --- a/src/dict/sdict.cc +++ b/src/dict/sdict.cc @@ -196,11 +196,7 @@ SdictDictionary::SdictDictionary( string const & id, // Read dictionary name idx.seek( sizeof( idxHeader ) ); - vector< char > dName( idx.read< uint32_t >() ); - if ( dName.size() > 0 ) { - idx.read( &dName.front(), dName.size() ); - dictionaryName = string( &dName.front(), dName.size() ); - } + idx.readU32SizeAndData<>( dictionaryName ); // Initialize the index diff --git a/src/dict/utils/dictfile.hh b/src/dict/utils/dictfile.hh index 06ac9c4c..c91056da 100644 --- a/src/dict/utils/dictfile.hh +++ b/src/dict/utils/dictfile.hh @@ -81,6 +81,18 @@ public: /// Like the above, but uses its own local internal buffer and strips newlines by default. 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 QByteArray readall();