clean: unify a common idx file read pattern uint32_t size + data
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run

This commit is contained in:
shenleban tongying 2024-11-20 03:22:08 -05:00 committed by GitHub
parent fa9ad2fdf7
commit 112874b0e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 25 additions and 55 deletions

View file

@ -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

View file

@ -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();

View file

@ -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

View file

@ -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 ] );

View file

@ -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

View file

@ -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 );

View file

@ -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

View file

@ -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();