Skip to content

Commit

Permalink
clean: unify a common idx file read pattern uint32_t size + data
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored Nov 20, 2024
1 parent fa9ad2f commit 112874b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 55 deletions.
6 changes: 1 addition & 5 deletions src/dict/aard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 5 additions & 13 deletions src/dict/bgl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ] );
Expand All @@ -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();

Expand Down
6 changes: 1 addition & 5 deletions src/dict/dictdfiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 2 additions & 10 deletions src/dict/dsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
Expand Down
6 changes: 1 addition & 5 deletions src/dict/gls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 2 additions & 12 deletions src/dict/mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand All @@ -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 );
Expand Down
6 changes: 1 addition & 5 deletions src/dict/sdict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/dict/utils/dictfile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 112874b

Please sign in to comment.