refactor: port StarDict -> Ifo away from File::Index

This commit is contained in:
shenleban tongying 2024-11-19 22:39:36 -05:00 committed by GitHub
parent c892083b00
commit 8f42e2e073
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -80,11 +80,11 @@ struct Ifo
uint32_t wordcount = 0; uint32_t wordcount = 0;
uint32_t synwordcount = 0; uint32_t synwordcount = 0;
uint32_t idxfilesize = 0; uint32_t idxfilesize = 0;
uint32_t idxoffsetbits = 0; uint32_t idxoffsetbits = 32;
string sametypesequence, dicttype, description; string sametypesequence, dicttype, description;
string copyright, author, email, website, date; string copyright, author, email, website, date;
explicit Ifo( File::Index & ); explicit Ifo( const QString & fileName );
}; };
enum { enum {
@ -1087,8 +1087,7 @@ QString const & StardictDictionary::getDescription()
return dictionaryDescription; return dictionaryDescription;
} }
File::Index ifoFile( getDictionaryFilenames()[ 0 ], "r" ); Ifo ifo( QString::fromStdString( getDictionaryFilenames()[ 0 ] ) );
Ifo ifo( ifoFile );
if ( !ifo.copyright.empty() ) { if ( !ifo.copyright.empty() ) {
QString copyright = QString::fromUtf8( ifo.copyright.c_str() ); QString copyright = QString::fromUtf8( ifo.copyright.c_str() );
@ -1456,85 +1455,77 @@ static char const * beginsWith( char const * substr, char const * str )
return strncmp( str, substr, len ) == 0 ? str + len : 0; return strncmp( str, substr, len ) == 0 ? str + len : 0;
} }
Ifo::Ifo( File::Index & f ): Ifo::Ifo( const QString & fileName )
wordcount( 0 ),
synwordcount( 0 ),
idxfilesize( 0 ),
idxoffsetbits( 32 )
{ {
static string const versionEq( "version=" ); QFile f( fileName );
if ( !f.open( QIODevice::ReadOnly ) ) {
throw exCantReadFile( "Cannot open IFO file -> " + fileName.toStdString() );
};
static string const booknameEq( "bookname=" ); if ( !f.readLine().startsWith( "StarDict's dict ifo file" ) || !f.readLine().startsWith( "version=" ) ) {
//GD_DPRINTF( "%s<\n", f.gets().c_str() );
//GD_DPRINTF( "%s<\n", f.gets().c_str() );
if ( QString::fromUtf8( f.gets().c_str() ) != "StarDict's dict ifo file"
|| f.gets().compare( 0, versionEq.size(), versionEq ) ) {
throw exNotAnIfoFile(); throw exNotAnIfoFile();
} }
/// Now go through the file and parse options /// Now go through the file and parse options
{
while ( !f.atEnd() ) {
auto line = f.readLine();
auto option = QByteArrayView( line ).trimmed();
// Empty lines are allowed in .ifo file
try { if ( option.isEmpty() ) {
char option[ 16384 ]; continue;
for ( ;; ) {
if ( !f.gets( option, sizeof( option ), true ) ) {
break;
} }
if ( char const * val = beginsWith( "bookname=", option ) ) { if ( char const * val = beginsWith( "bookname=", option.data() ) ) {
bookname = val; bookname = val;
} }
else if ( char const * val = beginsWith( "wordcount=", option ) ) { else if ( char const * val = beginsWith( "wordcount=", option.data() ) ) {
if ( sscanf( val, "%u", &wordcount ) != 1 ) { if ( sscanf( val, "%u", &wordcount ) != 1 ) {
throw exBadFieldInIfo( option ); throw exBadFieldInIfo( option.data() );
} }
} }
else if ( char const * val = beginsWith( "synwordcount=", option ) ) { else if ( char const * val = beginsWith( "synwordcount=", option.data() ) ) {
if ( sscanf( val, "%u", &synwordcount ) != 1 ) { if ( sscanf( val, "%u", &synwordcount ) != 1 ) {
throw exBadFieldInIfo( option ); throw exBadFieldInIfo( option.data() );
} }
} }
else if ( char const * val = beginsWith( "idxfilesize=", option ) ) { else if ( char const * val = beginsWith( "idxfilesize=", option.data() ) ) {
if ( sscanf( val, "%u", &idxfilesize ) != 1 ) { if ( sscanf( val, "%u", &idxfilesize ) != 1 ) {
throw exBadFieldInIfo( option ); throw exBadFieldInIfo( option.data() );
} }
} }
else if ( char const * val = beginsWith( "idxoffsetbits=", option ) ) { else if ( char const * val = beginsWith( "idxoffsetbits=", option.data() ) ) {
if ( sscanf( val, "%u", &idxoffsetbits ) != 1 || ( idxoffsetbits != 32 && idxoffsetbits != 64 ) ) { if ( sscanf( val, "%u", &idxoffsetbits ) != 1 || ( idxoffsetbits != 32 && idxoffsetbits != 64 ) ) {
throw exBadFieldInIfo( option ); throw exBadFieldInIfo( option.data() );
} }
} }
else if ( char const * val = beginsWith( "sametypesequence=", option ) ) { else if ( char const * val = beginsWith( "sametypesequence=", option.data() ) ) {
sametypesequence = val; sametypesequence = val;
} }
else if ( char const * val = beginsWith( "dicttype=", option ) ) { else if ( char const * val = beginsWith( "dicttype=", option.data() ) ) {
dicttype = val; dicttype = val;
} }
else if ( char const * val = beginsWith( "description=", option ) ) { else if ( char const * val = beginsWith( "description=", option.data() ) ) {
description = val; description = val;
} }
else if ( char const * val = beginsWith( "copyright=", option ) ) { else if ( char const * val = beginsWith( "copyright=", option.data() ) ) {
copyright = val; copyright = val;
} }
else if ( char const * val = beginsWith( "author=", option ) ) { else if ( char const * val = beginsWith( "author=", option.data() ) ) {
author = val; author = val;
} }
else if ( char const * val = beginsWith( "email=", option ) ) { else if ( char const * val = beginsWith( "email=", option.data() ) ) {
email = val; email = val;
} }
else if ( char const * val = beginsWith( "website=", option ) ) { else if ( char const * val = beginsWith( "website=", option.data() ) ) {
website = val; website = val;
} }
else if ( char const * val = beginsWith( "date=", option ) ) { else if ( char const * val = beginsWith( "date=", option.data() ) ) {
date = val; date = val;
} }
} }
} }
catch ( File::exReadError & ) {
}
} }
//// StardictDictionary::getResource() //// StardictDictionary::getResource()
@ -1896,9 +1887,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || indexIsOldOrBad( indexFile ) ) { if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || indexIsOldOrBad( indexFile ) ) {
// Building the index // Building the index
File::Index ifoFile( fileName, "r" ); Ifo ifo( QString::fromStdString( fileName ) );
Ifo ifo( ifoFile );
gdDebug( "Stardict: Building the index for dictionary: %s\n", ifo.bookname.c_str() ); gdDebug( "Stardict: Building the index for dictionary: %s\n", ifo.bookname.c_str() );