2012-09-28 12:39:52 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "zipsounds.hh"
|
|
|
|
#include "file.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "btreeidx.hh"
|
2023-04-28 16:09:45 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
#include "audiolink.hh"
|
|
|
|
#include "indexedzip.hh"
|
2013-01-22 12:53:19 +00:00
|
|
|
#include "filetype.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2014-02-06 20:16:44 +00:00
|
|
|
#include "chunkedstorage.hh"
|
2014-02-07 13:02:30 +00:00
|
|
|
#include "htmlescape.hh"
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stub_msvc.h>
|
|
|
|
#endif
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
namespace ZipSounds {
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using gd::wstring;
|
|
|
|
using std::map;
|
|
|
|
using std::multimap;
|
|
|
|
using std::set;
|
|
|
|
using BtreeIndexing::WordArticleLink;
|
|
|
|
using BtreeIndexing::IndexedWords;
|
|
|
|
using BtreeIndexing::IndexInfo;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
DEF_EX( exInvalidData, "Invalid data encountered", Dictionary::Ex )
|
|
|
|
|
|
|
|
enum {
|
|
|
|
Signature = 0x5350495a, // ZIPS on little-endian, SPIZ on big-endian
|
2022-05-10 15:34:42 +00:00
|
|
|
CurrentFormatVersion = 6 + BtreeIndexing::FormatVersion
|
2012-09-28 12:39:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, ZIPS
|
|
|
|
uint32_t formatVersion; // File format version, currently 1.
|
|
|
|
uint32_t soundsCount; // Total number of sounds, for informative purposes only
|
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
2014-02-06 20:16:44 +00:00
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__( ( packed ) )
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
bool indexIsOldOrBad( string const & indexFile )
|
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 || header.signature != Signature
|
|
|
|
|| header.formatVersion != CurrentFormatVersion;
|
|
|
|
}
|
|
|
|
|
2013-01-22 12:53:19 +00:00
|
|
|
wstring stripExtension( string const & str )
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
2013-01-22 12:53:19 +00:00
|
|
|
wstring name;
|
|
|
|
try {
|
|
|
|
name = Utf8::decode( str );
|
|
|
|
}
|
2018-05-22 14:48:14 +00:00
|
|
|
catch ( Utf8::exCantDecode & ) {
|
2013-01-22 12:53:19 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Filetype::isNameOfSound( str ) ) {
|
|
|
|
wstring::size_type pos = name.rfind( L'.' );
|
|
|
|
if ( pos != wstring::npos )
|
|
|
|
name.erase( pos );
|
2014-02-06 16:34:34 +00:00
|
|
|
|
|
|
|
// Strip spaces at the end of name
|
|
|
|
string::size_type n = name.length();
|
|
|
|
while ( n && name.at( n - 1 ) == L' ' )
|
|
|
|
n--;
|
|
|
|
|
|
|
|
if ( n != name.length() )
|
|
|
|
name.erase( n );
|
2013-01-22 12:53:19 +00:00
|
|
|
}
|
|
|
|
return name;
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ZipSoundsDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutex idxMutex;
|
2012-09-28 12:39:52 +00:00
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
2014-02-06 20:16:44 +00:00
|
|
|
sptr< ChunkedStorage::Reader > chunks;
|
2012-09-28 12:39:52 +00:00
|
|
|
IndexedZip zipsFile;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
ZipSoundsDictionary( string const & id, string const & indexFile, vector< string > const & dictionaryFiles );
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
string getName() noexcept override;
|
2012-09-28 12:39:52 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
map< Dictionary::Property, string > getProperties() noexcept override
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
|
|
|
return map< Dictionary::Property, string >();
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getArticleCount() noexcept override
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
|
|
|
return idxHeader.soundsCount;
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getWordCount() noexcept override
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
|
|
|
return getArticleCount();
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< Dictionary::DataRequest >
|
|
|
|
getArticle( wstring const &, vector< wstring > const & alts, wstring const &, bool ignoreDiacritics ) override;
|
2012-09-28 12:39:52 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< Dictionary::DataRequest > getResource( string const & name ) override;
|
2012-12-03 12:47:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void loadIcon() noexcept override;
|
2012-09-28 12:39:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ZipSoundsDictionary::ZipSoundsDictionary( string const & id,
|
|
|
|
string const & indexFile,
|
|
|
|
vector< string > const & dictionaryFiles ):
|
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() )
|
|
|
|
{
|
2022-11-29 03:54:31 +00:00
|
|
|
chunks = std::shared_ptr< ChunkedStorage::Reader >( new ChunkedStorage::Reader( idx, idxHeader.chunksOffset ) );
|
2014-02-06 20:16:44 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
// Initialize the index
|
|
|
|
|
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements, idxHeader.indexRootOffset ), idx, idxMutex );
|
|
|
|
|
2023-04-13 10:08:32 +00:00
|
|
|
QString zipName = QDir::fromNativeSeparators( getDictionaryFilenames()[ 0 ].c_str() );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
zipsFile.openZipFile( zipName );
|
|
|
|
zipsFile.openIndex( IndexInfo( idxHeader.indexBtreeMaxElements, idxHeader.indexRootOffset ), idx, idxMutex );
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
string ZipSoundsDictionary::getName() noexcept
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
2023-04-28 16:09:45 +00:00
|
|
|
string result = Utils::Fs::basename( getDictionaryFilenames()[ 0 ] );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
// Strip the extension
|
|
|
|
result.erase( result.rfind( '.' ) );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > ZipSoundsDictionary::getArticle( wstring const & word,
|
|
|
|
vector< wstring > const & alts,
|
2018-06-13 16:00:42 +00:00
|
|
|
wstring const &,
|
|
|
|
bool ignoreDiacritics )
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
2018-06-13 16:00:42 +00:00
|
|
|
vector< WordArticleLink > chain = findArticles( word, ignoreDiacritics );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
for ( unsigned x = 0; x < alts.size(); ++x ) {
|
|
|
|
/// Make an additional query for each alt
|
|
|
|
|
2018-06-13 16:00:42 +00:00
|
|
|
vector< WordArticleLink > altChain = findArticles( alts[ x ], ignoreDiacritics );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
multimap< wstring, uint32_t > mainArticles, alternateArticles;
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
set< uint32_t > articlesIncluded; // Some synonims make it that the articles
|
|
|
|
// appear several times. We combat this
|
|
|
|
// by only allowing them to appear once.
|
|
|
|
|
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
2018-06-13 16:00:42 +00:00
|
|
|
if ( ignoreDiacritics )
|
|
|
|
wordCaseFolded = Folding::applyDiacriticsOnly( wordCaseFolded );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
for ( unsigned x = 0; x < chain.size(); ++x ) {
|
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
|
|
|
|
|
|
|
// Ok. Now, does it go to main articles, or to alternate ones? We list
|
|
|
|
// main ones first, and alternates after.
|
|
|
|
|
|
|
|
// We do the case-folded comparison here.
|
|
|
|
|
2023-04-29 02:35:56 +00:00
|
|
|
wstring headwordStripped = Folding::applySimpleCaseOnly( chain[ x ].word );
|
2018-06-13 16:00:42 +00:00
|
|
|
if ( ignoreDiacritics )
|
|
|
|
headwordStripped = Folding::applyDiacriticsOnly( headwordStripped );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
multimap< wstring, uint32_t > & mapToUse =
|
2012-09-28 12:39:52 +00:00
|
|
|
( wordCaseFolded == headwordStripped ) ? mainArticles : alternateArticles;
|
|
|
|
|
2023-04-29 02:35:56 +00:00
|
|
|
mapToUse.insert( std::pair( Folding::applySimpleCaseOnly( chain[ x ].word ), chain[ x ].articleOffset ) );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() )
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< Dictionary::DataRequestInstant >( false ); // No such word
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
string result;
|
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
multimap< wstring, uint32_t >::const_iterator i;
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
result += "<table class=\"lsa_play\">";
|
2014-02-06 20:16:44 +00:00
|
|
|
|
2014-02-07 13:02:30 +00:00
|
|
|
vector< char > chunk;
|
|
|
|
char * nameBlock;
|
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
for ( i = mainArticles.begin(); i != mainArticles.end(); ++i ) {
|
2014-02-07 13:02:30 +00:00
|
|
|
try {
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &idxMutex );
|
2014-02-07 13:02:30 +00:00
|
|
|
nameBlock = chunks->getBlock( i->second, chunk );
|
|
|
|
|
|
|
|
if ( nameBlock >= &chunk.front() + chunk.size() ) {
|
|
|
|
// chunks reader thinks it's okay since zero-sized records can exist,
|
|
|
|
// but we don't allow that.
|
|
|
|
throw ChunkedStorage::exAddressOutOfRange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( ChunkedStorage::exAddressOutOfRange & ) {
|
|
|
|
// Bad address
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-06 20:16:44 +00:00
|
|
|
|
|
|
|
uint16_t sz;
|
|
|
|
memcpy( &sz, nameBlock, sizeof( uint16_t ) );
|
|
|
|
nameBlock += sizeof( uint16_t );
|
|
|
|
|
|
|
|
string name( nameBlock, sz );
|
|
|
|
nameBlock += sz;
|
|
|
|
|
|
|
|
string displayedName =
|
|
|
|
mainArticles.size() + alternateArticles.size() > 1 ? name : Utf8::encode( stripExtension( name ) );
|
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
result += "<tr>";
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2012-09-28 12:39:52 +00:00
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
2021-11-27 07:17:33 +00:00
|
|
|
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( name.c_str() ) ) );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
|
|
|
|
|
|
|
result += addAudioLink( ref, getId() );
|
|
|
|
|
2023-03-05 20:20:05 +00:00
|
|
|
result += "<td><a href=" + ref + R"(><img src="qrc:///icons/playsound.png" border="0" alt="Play"/></a></td>)";
|
2014-02-07 13:02:30 +00:00
|
|
|
result += "<td><a href=" + ref + ">" + Html::escape( displayedName ) + "</a></td>";
|
2012-09-28 12:39:52 +00:00
|
|
|
result += "</tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = alternateArticles.begin(); i != alternateArticles.end(); ++i ) {
|
2014-02-07 13:02:30 +00:00
|
|
|
try {
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &idxMutex );
|
2014-02-07 13:02:30 +00:00
|
|
|
nameBlock = chunks->getBlock( i->second, chunk );
|
|
|
|
|
|
|
|
if ( nameBlock >= &chunk.front() + chunk.size() ) {
|
|
|
|
// chunks reader thinks it's okay since zero-sized records can exist,
|
|
|
|
// but we don't allow that.
|
|
|
|
throw ChunkedStorage::exAddressOutOfRange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( ChunkedStorage::exAddressOutOfRange & ) {
|
|
|
|
// Bad address
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-06 20:16:44 +00:00
|
|
|
|
|
|
|
uint16_t sz;
|
|
|
|
memcpy( &sz, nameBlock, sizeof( uint16_t ) );
|
|
|
|
nameBlock += sizeof( uint16_t );
|
|
|
|
|
|
|
|
string name( nameBlock, sz );
|
|
|
|
nameBlock += sz;
|
|
|
|
|
|
|
|
string displayedName =
|
|
|
|
mainArticles.size() + alternateArticles.size() > 1 ? name : Utf8::encode( stripExtension( name ) );
|
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
result += "<tr>";
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2012-09-28 12:39:52 +00:00
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
2021-11-27 07:17:33 +00:00
|
|
|
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( name.c_str() ) ) );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
|
|
|
|
|
|
|
result += addAudioLink( ref, getId() );
|
|
|
|
|
2023-03-05 20:20:05 +00:00
|
|
|
result += "<td><a href=" + ref + R"(><img src="qrc:///icons/playsound.png" border="0" alt="Play"/></a></td>)";
|
2014-02-07 13:02:30 +00:00
|
|
|
result += "<td><a href=" + ref + ">" + Html::escape( displayedName ) + "</a></td>";
|
2012-09-28 12:39:52 +00:00
|
|
|
result += "</tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += "</table>";
|
|
|
|
|
2023-06-24 21:36:10 +00:00
|
|
|
auto ret = std::make_shared< Dictionary::DataRequestInstant >( true );
|
|
|
|
ret->appendString( result );
|
|
|
|
return ret;
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > ZipSoundsDictionary::getResource( string const & name )
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
{
|
2013-01-22 12:53:19 +00:00
|
|
|
// Remove extension for sound files (like in sound dirs)
|
2012-10-04 14:33:26 +00:00
|
|
|
|
2013-01-22 12:53:19 +00:00
|
|
|
wstring strippedName = stripExtension( name );
|
2012-10-04 14:33:26 +00:00
|
|
|
|
2013-01-22 12:53:19 +00:00
|
|
|
vector< WordArticleLink > chain = findArticles( strippedName );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
if ( chain.empty() )
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< Dictionary::DataRequestInstant >( false ); // No such resource
|
2012-09-28 12:39:52 +00:00
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
// Find sound
|
|
|
|
|
|
|
|
uint32_t dataOffset = 0;
|
|
|
|
for ( int x = chain.size() - 1; x >= 0; x-- ) {
|
|
|
|
vector< char > chunk;
|
|
|
|
char * nameBlock = chunks->getBlock( chain[ x ].articleOffset, chunk );
|
|
|
|
|
|
|
|
uint16_t sz;
|
|
|
|
memcpy( &sz, nameBlock, sizeof( uint16_t ) );
|
|
|
|
nameBlock += sizeof( uint16_t );
|
|
|
|
|
|
|
|
string fileName( nameBlock, sz );
|
|
|
|
nameBlock += sz;
|
|
|
|
|
|
|
|
memcpy( &dataOffset, nameBlock, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
if ( name.compare( fileName ) == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
sptr< Dictionary::DataRequestInstant > dr = std::make_shared< Dictionary::DataRequestInstant >( true );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
if ( zipsFile.loadFile( dataOffset, dr->getData() ) )
|
2012-09-28 12:39:52 +00:00
|
|
|
return dr;
|
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< Dictionary::DataRequestInstant >( false );
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
void ZipSoundsDictionary::loadIcon() noexcept
|
2012-12-03 12:48:06 +00:00
|
|
|
{
|
2013-01-31 22:30:11 +00:00
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
2023-04-13 10:08:32 +00:00
|
|
|
QString fileName = QDir::fromNativeSeparators( getDictionaryFilenames()[ 0 ].c_str() );
|
2012-12-03 12:48:06 +00:00
|
|
|
|
|
|
|
// Remove the extension
|
|
|
|
fileName.chop( 4 );
|
|
|
|
|
|
|
|
if ( !loadIconFromFile( fileName ) ) {
|
|
|
|
// Load failed -- use default icons
|
2023-07-21 06:34:09 +00:00
|
|
|
dictionaryIcon = QIcon( ":/icons/zipsound.svg" );
|
2012-12-03 12:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
} // namespace
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
|
|
|
Dictionary::Initializing & initializing )
|
|
|
|
|
|
|
|
{
|
2012-10-31 13:58:35 +00:00
|
|
|
(void)initializing;
|
2012-09-28 12:39:52 +00:00
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for ( vector< string >::const_iterator i = fileNames.begin(); i != fileNames.end(); ++i ) {
|
|
|
|
/// Only allow .zips extension
|
|
|
|
if ( i->size() < 5 || strcasecmp( i->c_str() + ( i->size() - 5 ), ".zips" ) != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
vector< string > dictFiles( 1, *i );
|
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || indexIsOldOrBad( indexFile ) ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdDebug( "Zips: Building the index for dictionary: %s\n", i->c_str() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
File::Class idx( indexFile, "wb" );
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
|
|
|
|
memset( &idxHeader, 0, sizeof( idxHeader ) );
|
|
|
|
|
|
|
|
// We write a dummy header first. At the end of the process the header
|
|
|
|
// will be rewritten with the right values.
|
|
|
|
|
|
|
|
idx.write( idxHeader );
|
|
|
|
|
|
|
|
IndexedWords names, zipFileNames;
|
2014-02-06 20:16:44 +00:00
|
|
|
ChunkedStorage::Writer chunks( idx );
|
2014-10-10 12:52:22 +00:00
|
|
|
quint32 namesCount;
|
2014-02-06 20:16:44 +00:00
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
IndexedZip zipFile;
|
2023-04-13 10:08:32 +00:00
|
|
|
if ( zipFile.openZipFile( QDir::fromNativeSeparators( i->c_str() ) ) )
|
2014-10-10 12:52:22 +00:00
|
|
|
zipFile.indexFile( zipFileNames, &namesCount );
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
if ( !zipFileNames.empty() ) {
|
|
|
|
for ( IndexedWords::iterator i = zipFileNames.begin(); i != zipFileNames.end(); ++i ) {
|
|
|
|
vector< WordArticleLink > links = i->second;
|
|
|
|
for ( unsigned x = 0; x < links.size(); x++ ) {
|
2014-02-06 20:16:44 +00:00
|
|
|
// Save original name
|
|
|
|
|
|
|
|
uint32_t offset = chunks.startNewBlock();
|
|
|
|
uint16_t sz = links[ x ].word.size();
|
|
|
|
chunks.addToBlock( &sz, sizeof( uint16_t ) );
|
|
|
|
chunks.addToBlock( links[ x ].word.c_str(), sz );
|
|
|
|
chunks.addToBlock( &links[ x ].articleOffset, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
// Remove extension for sound files (like in sound dirs)
|
|
|
|
|
2013-01-22 12:53:19 +00:00
|
|
|
wstring word = stripExtension( links[ x ].word );
|
|
|
|
if ( !word.empty() )
|
2022-05-10 15:34:42 +00:00
|
|
|
names.addWord( word, offset );
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 20:16:44 +00:00
|
|
|
// Finish with the chunks
|
|
|
|
|
|
|
|
idxHeader.chunksOffset = chunks.finish();
|
|
|
|
|
2012-09-28 12:39:52 +00:00
|
|
|
// Build the resulting zip file index
|
|
|
|
|
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( names, idx );
|
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.indexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.indexRootOffset = idxInfo.rootOffset;
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
|
2014-10-10 12:52:22 +00:00
|
|
|
idxHeader.soundsCount = namesCount;
|
2012-09-28 12:39:52 +00:00
|
|
|
|
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
idx.close();
|
2023-04-13 10:08:32 +00:00
|
|
|
QFile::remove( QDir::fromNativeSeparators( indexFile.c_str() ) );
|
2012-09-28 12:39:52 +00:00
|
|
|
throw exInvalidData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
dictionaries.push_back( std::make_shared< ZipSoundsDictionary >( dictId, indexFile, dictFiles ) );
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Zipped sounds pack reading failed: %s, error: %s\n", i->c_str(), e.what() );
|
2012-09-28 12:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZipSounds
|