2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "stardict.hh"
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "chunkedstorage.hh"
|
2023-04-17 20:12:27 +00:00
|
|
|
#include "dictzip.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include "xdxf2html.hh"
|
|
|
|
#include "htmlescape.hh"
|
2009-04-23 19:57:39 +00:00
|
|
|
#include "langcoder.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2023-04-28 16:09:45 +00:00
|
|
|
|
2012-02-29 13:00:38 +00:00
|
|
|
#include "filetype.hh"
|
2013-03-25 13:13:25 +00:00
|
|
|
#include "indexedzip.hh"
|
2014-02-11 14:02:00 +00:00
|
|
|
#include "tiff.hh"
|
2014-04-16 16:18:28 +00:00
|
|
|
#include "ftshelpers.hh"
|
2017-10-17 14:51:27 +00:00
|
|
|
#include "audiolink.hh"
|
2009-04-23 19:57:39 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2023-05-07 10:02:22 +00:00
|
|
|
|
|
|
|
#ifndef Q_OS_WIN
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <arpa/inet.h>
|
2009-02-02 00:59:14 +00:00
|
|
|
#else
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
2009-01-30 01:20:37 +00:00
|
|
|
#include <stdlib.h>
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-29 23:18:26 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stub_msvc.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QString>
|
2009-04-16 11:33:12 +00:00
|
|
|
#include <QSemaphore>
|
|
|
|
#include <QAtomicInt>
|
2022-02-27 05:17:37 +00:00
|
|
|
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
|
2022-02-27 14:42:40 +00:00
|
|
|
#include <QtCore5Compat/QRegExp>
|
|
|
|
#else
|
|
|
|
#include <QRegExp>
|
2022-02-27 05:17:37 +00:00
|
|
|
#endif
|
2015-02-16 14:57:10 +00:00
|
|
|
#include <QStringList>
|
2017-08-23 13:21:50 +00:00
|
|
|
#include <QDomDocument>
|
2011-09-09 12:05:28 +00:00
|
|
|
#include "ufile.hh"
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2009-04-29 23:18:26 +00:00
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
#include <QRegularExpression>
|
2023-04-25 13:33:36 +00:00
|
|
|
#include "globalregex.hh"
|
2018-02-27 16:42:21 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
namespace Stardict {
|
|
|
|
|
|
|
|
using std::map;
|
|
|
|
using std::multimap;
|
|
|
|
using std::pair;
|
|
|
|
using std::set;
|
|
|
|
using std::string;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
using BtreeIndexing::WordArticleLink;
|
|
|
|
using BtreeIndexing::IndexedWords;
|
2009-04-14 16:35:47 +00:00
|
|
|
using BtreeIndexing::IndexInfo;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
DEF_EX( exNotAnIfoFile, "Not an .ifo file", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exBadFieldInIfo, "Bad field in .ifo file encountered:", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exNoIdxFile, "No corresponding .idx file was found for", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exNoDictFile, "No corresponding .dict file was found for", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exNoSynFile, "No corresponding .syn file was found for", Dictionary::Ex )
|
|
|
|
|
|
|
|
DEF_EX( ex64BitsNotSupported, "64-bit indices are not presently supported, sorry", Dictionary::Ex )
|
|
|
|
DEF_EX( exDicttypeNotSupported, "Dictionaries with dicttypes are not supported, sorry", Dictionary::Ex )
|
|
|
|
|
2023-06-19 22:36:43 +00:00
|
|
|
using Dictionary::exCantReadFile;
|
2009-01-28 20:55:45 +00:00
|
|
|
DEF_EX_STR( exWordIsTooLarge, "Enountered a word that is too large:", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exSuddenEndOfFile, "Sudden end of file", Dictionary::Ex )
|
2014-04-24 18:50:47 +00:00
|
|
|
DEF_EX_STR( exDictzipError, "DICTZIP error", Dictionary::Ex )
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
DEF_EX_STR( exIncorrectOffset, "Incorrect offset encountered in file", Dictionary::Ex )
|
|
|
|
|
|
|
|
/// Contents of an ifo file
|
|
|
|
struct Ifo
|
|
|
|
{
|
|
|
|
string version;
|
|
|
|
string bookname;
|
|
|
|
uint32_t wordcount, synwordcount, idxfilesize, idxoffsetbits;
|
2012-09-07 13:58:45 +00:00
|
|
|
string sametypesequence, dicttype, description;
|
2017-01-03 11:02:39 +00:00
|
|
|
string copyright, author, email, website, date;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-05-07 10:02:22 +00:00
|
|
|
explicit Ifo( File::Class & );
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
Signature = 0x58444953, // SIDX on little-endian, XDIS on big-endian
|
2013-03-25 13:13:25 +00:00
|
|
|
CurrentFormatVersion = 9 + BtreeIndexing::FormatVersion + Folding::Version
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, SIDX
|
|
|
|
uint32_t formatVersion; // File format version (CurrentFormatVersion)
|
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
|
|
|
uint32_t wordCount; // Saved from Ifo::wordcount
|
|
|
|
uint32_t synWordCount; // Saved from Ifo::synwordcount
|
|
|
|
uint32_t bookNameSize; // Book name's length. Used to read it then.
|
|
|
|
uint32_t sameTypeSequenceSize; // That string's size. Used to read it then.
|
2009-04-23 19:57:39 +00:00
|
|
|
uint32_t langFrom; // Source language
|
|
|
|
uint32_t langTo; // Target language
|
2013-03-25 13:13:25 +00:00
|
|
|
uint32_t hasZipFile; // Non-zero means there's a zip file with resources present
|
|
|
|
uint32_t zipIndexBtreeMaxElements; // Two fields from IndexInfo of the zip
|
|
|
|
// resource index.
|
|
|
|
uint32_t zipIndexRootOffset;
|
2009-05-05 21:51:21 +00:00
|
|
|
}
|
2009-04-29 23:18:26 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__( ( packed ) )
|
|
|
|
#endif
|
|
|
|
;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
class StardictDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutex idxMutex;
|
2009-01-28 20:55:45 +00:00
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
2009-04-14 16:35:47 +00:00
|
|
|
string bookName;
|
|
|
|
string sameTypeSequence;
|
2009-01-28 20:55:45 +00:00
|
|
|
ChunkedStorage::Reader chunks;
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutex dzMutex;
|
2009-01-28 20:55:45 +00:00
|
|
|
dictData * dz;
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutex resourceZipMutex;
|
2013-03-25 13:13:25 +00:00
|
|
|
IndexedZip resourceZip;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
StardictDictionary( string const & id, string const & indexFile, vector< string > const & dictionaryFiles );
|
|
|
|
|
|
|
|
~StardictDictionary();
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
string getName() noexcept override
|
2009-04-14 16:35:47 +00:00
|
|
|
{
|
|
|
|
return bookName;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-05-20 14:33:20 +00:00
|
|
|
void setName( string _name ) noexcept override
|
|
|
|
{
|
|
|
|
bookName = _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
map< Dictionary::Property, string > getProperties() noexcept override
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
return map< Dictionary::Property, string >();
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getArticleCount() noexcept override
|
2009-04-14 16:35:47 +00:00
|
|
|
{
|
|
|
|
return idxHeader.wordCount;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getWordCount() noexcept override
|
2009-04-14 16:35:47 +00:00
|
|
|
{
|
|
|
|
return idxHeader.wordCount + idxHeader.synWordCount;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
inline quint32 getLangFrom() const override
|
2009-04-23 19:57:39 +00:00
|
|
|
{
|
|
|
|
return idxHeader.langFrom;
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
inline quint32 getLangTo() const override
|
2009-04-23 19:57:39 +00:00
|
|
|
{
|
|
|
|
return idxHeader.langTo;
|
|
|
|
}
|
|
|
|
|
2023-05-07 05:59:01 +00:00
|
|
|
sptr< Dictionary::WordSearchRequest > findHeadwordsForSynonym( wstring const & ) override;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-05-07 05:59:01 +00:00
|
|
|
sptr< Dictionary::DataRequest >
|
|
|
|
getArticle( wstring const &, vector< wstring > const & alts, wstring const &, bool ignoreDiacritics ) override;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-05-07 05:59:01 +00:00
|
|
|
sptr< Dictionary::DataRequest > getResource( string const & name ) override;
|
2012-02-29 13:00:38 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
QString const & getDescription() override;
|
2012-09-07 13:58:45 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
QString getMainFilename() override;
|
2013-06-11 18:55:29 +00:00
|
|
|
|
2023-05-30 23:42:31 +00:00
|
|
|
sptr< Dictionary::DataRequest >
|
|
|
|
getSearchResults( QString const & searchString, int searchMode, bool matchCase, bool ignoreDiacritics ) override;
|
2022-12-29 07:07:40 +00:00
|
|
|
void getArticleText( uint32_t articleAddress, QString & headword, QString & text ) override;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void makeFTSIndex( QAtomicInt & isCancelled, bool firstIteration ) override;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void setFTSParameters( Config::FullTextSearch const & fts ) override
|
2014-04-17 14:31:51 +00:00
|
|
|
{
|
|
|
|
can_FTS = fts.enabled && !fts.disabledTypes.contains( "STARDICT", Qt::CaseInsensitive )
|
|
|
|
&& ( fts.maxDictionarySize == 0 || getArticleCount() <= fts.maxDictionarySize );
|
|
|
|
}
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
protected:
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void loadIcon() noexcept override;
|
2012-12-03 12:47:43 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-01 13:02:02 +00:00
|
|
|
|
2018-07-07 09:33:15 +00:00
|
|
|
/// Retrieves the article's offset/size in .dict file, and its headword.
|
2009-01-28 20:55:45 +00:00
|
|
|
void getArticleProps( uint32_t articleAddress, string & headword, uint32_t & offset, uint32_t & size );
|
|
|
|
|
|
|
|
/// Loads the article, storing its headword and formatting the data it has
|
|
|
|
/// into an html.
|
|
|
|
void loadArticle( uint32_t address, string & headword, string & articleText );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
string loadString( size_t size );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2012-02-29 13:00:38 +00:00
|
|
|
string handleResource( char type, char const * resource, size_t size );
|
|
|
|
|
2015-02-16 14:57:10 +00:00
|
|
|
void pangoToHtml( QString & text );
|
|
|
|
|
2013-03-25 13:13:25 +00:00
|
|
|
friend class StardictResourceRequest;
|
2009-04-16 11:33:12 +00:00
|
|
|
friend class StardictArticleRequest;
|
|
|
|
friend class StardictHeadwordsRequest;
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
StardictDictionary::StardictDictionary( string const & id,
|
|
|
|
string const & indexFile,
|
2009-04-14 16:35:47 +00:00
|
|
|
vector< string > const & dictionaryFiles ):
|
2009-01-28 20:55:45 +00:00
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
2009-04-14 16:35:47 +00:00
|
|
|
bookName( loadString( idxHeader.bookNameSize ) ),
|
|
|
|
sameTypeSequence( loadString( idxHeader.sameTypeSequenceSize ) ),
|
2012-12-03 12:47:43 +00:00
|
|
|
chunks( idx, idxHeader.chunksOffset )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Open the .dict file
|
|
|
|
|
2014-04-24 18:50:47 +00:00
|
|
|
DZ_ERRORS error;
|
|
|
|
dz = dict_data_open( dictionaryFiles[ 2 ].c_str(), &error, 0 );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( !dz )
|
2014-04-24 18:50:47 +00:00
|
|
|
throw exDictzipError( string( dz_error_str( error ) ) + "(" + dictionaryFiles[ 2 ] + ")" );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// Initialize the index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements, idxHeader.indexRootOffset ), idx, idxMutex );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
// Open a resource zip file, if there's one
|
|
|
|
|
|
|
|
if ( idxHeader.hasZipFile && ( idxHeader.zipIndexBtreeMaxElements || idxHeader.zipIndexRootOffset ) ) {
|
|
|
|
resourceZip.openIndex( IndexInfo( idxHeader.zipIndexBtreeMaxElements, idxHeader.zipIndexRootOffset ),
|
|
|
|
idx,
|
|
|
|
idxMutex );
|
|
|
|
|
2023-04-13 10:08:32 +00:00
|
|
|
QString zipName = QDir::fromNativeSeparators( getDictionaryFilenames().back().c_str() );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
if ( zipName.endsWith( ".zip", Qt::CaseInsensitive ) ) // Sanity check
|
|
|
|
resourceZip.openZipFile( zipName );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
// Full-text search parameters
|
|
|
|
|
|
|
|
can_FTS = true;
|
|
|
|
|
2022-10-06 03:04:48 +00:00
|
|
|
ftsIdxName = indexFile + Dictionary::getFtsSuffix();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2023-06-03 00:29:19 +00:00
|
|
|
if ( !Dictionary::needToRebuildIndex( dictionaryFiles, ftsIdxName ) && !FtsHelpers::ftsIndexIsOldOrBad( this ) )
|
2014-05-08 12:38:00 +00:00
|
|
|
FTS_index_completed.ref();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StardictDictionary::~StardictDictionary()
|
|
|
|
{
|
|
|
|
if ( dz )
|
|
|
|
dict_data_close( dz );
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
void StardictDictionary::loadIcon() noexcept
|
2012-03-01 13:02:02 +00:00
|
|
|
{
|
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
2023-04-13 10:08:32 +00:00
|
|
|
QString fileName = QDir::fromNativeSeparators( getDictionaryFilenames()[ 0 ].c_str() );
|
2012-03-01 13:02:02 +00:00
|
|
|
|
|
|
|
// Remove the extension
|
|
|
|
fileName.chop( 3 );
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
if ( !loadIconFromFile( fileName ) ) {
|
2012-03-01 13:02:02 +00:00
|
|
|
// Load failed -- use default icons
|
2023-06-19 02:34:08 +00:00
|
|
|
dictionaryIcon = QIcon( ":/icons/icon32_stardict.png" );
|
2012-03-01 13:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
string StardictDictionary::loadString( size_t size )
|
|
|
|
{
|
2019-01-17 14:53:13 +00:00
|
|
|
if ( size == 0 )
|
|
|
|
return string();
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
vector< char > data( size );
|
|
|
|
|
|
|
|
idx.read( &data.front(), data.size() );
|
|
|
|
|
|
|
|
return string( &data.front(), data.size() );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void StardictDictionary::getArticleProps( uint32_t articleAddress,
|
|
|
|
string & headword,
|
|
|
|
uint32_t & offset,
|
|
|
|
uint32_t & size )
|
|
|
|
{
|
|
|
|
vector< char > chunk;
|
|
|
|
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &idxMutex );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
char * articleData = chunks.getBlock( articleAddress, chunk );
|
|
|
|
|
|
|
|
memcpy( &offset, articleData, sizeof( uint32_t ) );
|
|
|
|
articleData += sizeof( uint32_t );
|
|
|
|
memcpy( &size, articleData, sizeof( uint32_t ) );
|
|
|
|
articleData += sizeof( uint32_t );
|
|
|
|
|
|
|
|
headword = articleData;
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
class PowerWordDataProcessor
|
|
|
|
{
|
|
|
|
class PWSyntaxTranslate
|
2023-07-20 08:02:22 +00:00
|
|
|
{
|
2017-08-23 13:21:50 +00:00
|
|
|
public:
|
|
|
|
PWSyntaxTranslate( const char * re, const char * replacement ):
|
2018-02-27 16:42:21 +00:00
|
|
|
_re( re, QRegularExpression::UseUnicodePropertiesOption )
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2023-07-20 08:02:22 +00:00
|
|
|
,
|
2017-08-23 13:21:50 +00:00
|
|
|
_replacement( replacement )
|
|
|
|
{
|
|
|
|
}
|
2018-02-27 16:42:21 +00:00
|
|
|
const QRegularExpression & re() const
|
2023-07-20 08:02:22 +00:00
|
|
|
{
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
return _re;
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
2017-08-23 13:21:50 +00:00
|
|
|
const QString & replacement() const
|
|
|
|
{
|
|
|
|
return _replacement;
|
|
|
|
}
|
|
|
|
|
2023-07-20 08:02:22 +00:00
|
|
|
private:
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression _re;
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
QString _replacement;
|
2023-07-20 08:02:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2017-08-23 13:21:50 +00:00
|
|
|
PowerWordDataProcessor( const char * resource, size_t size ):
|
2016-03-17 14:41:43 +00:00
|
|
|
_data( QString::fromUtf8( resource, size ) )
|
2017-08-23 13:21:50 +00:00
|
|
|
{
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
string process()
|
2023-07-20 08:02:22 +00:00
|
|
|
{
|
2017-08-23 13:21:50 +00:00
|
|
|
QDomDocument doc;
|
|
|
|
QString ss;
|
|
|
|
ss = "<div class=\"sdct_k\">";
|
|
|
|
if ( !doc.setContent( _data ) ) {
|
|
|
|
ss += _data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QStringList sl;
|
|
|
|
walkNode( doc.firstChild(), sl );
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
QStringListIterator itr( sl );
|
|
|
|
while ( itr.hasNext() ) {
|
|
|
|
QString s = itr.next();
|
|
|
|
translatePW( s );
|
|
|
|
ss += s;
|
|
|
|
ss += "<br>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss += "</div>";
|
|
|
|
QByteArray ba = ss.toUtf8();
|
|
|
|
return string( ba.data(), ba.size() );
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
private:
|
|
|
|
void walkNode( const QDomNode & e, QStringList & sl )
|
|
|
|
{
|
|
|
|
if ( e.isNull() ) {
|
|
|
|
return;
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
2017-08-23 13:21:50 +00:00
|
|
|
if ( e.isText() ) {
|
|
|
|
sl.append( e.toText().data() );
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-08-23 13:21:50 +00:00
|
|
|
QDomNodeList l = e.childNodes();
|
|
|
|
for ( int i = 0; i < l.size(); ++i ) {
|
|
|
|
QDomNode n = l.at( i );
|
|
|
|
if ( n.isText() ) {
|
|
|
|
sl.append( n.toText().data() );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
walkNode( n, sl );
|
|
|
|
}
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
2017-08-23 13:21:50 +00:00
|
|
|
}
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
2017-08-23 13:21:50 +00:00
|
|
|
|
|
|
|
void translatePW( QString & s )
|
|
|
|
{
|
|
|
|
const int TRANSLATE_TBL_SIZE = 5;
|
|
|
|
static PWSyntaxTranslate t[ TRANSLATE_TBL_SIZE ] = {
|
2022-12-24 22:01:50 +00:00
|
|
|
PWSyntaxTranslate( R"(&[bB]\s*\{([^\{}&]+)\})", "<B>\\1</B>" ),
|
|
|
|
PWSyntaxTranslate( R"(&[iI]\s*\{([^\{}&]+)\})", "<I>\\1</I>" ),
|
|
|
|
PWSyntaxTranslate( R"(&[uU]\s*\{([^\{}&]+)\})", "<U>\\1</U>" ),
|
|
|
|
PWSyntaxTranslate( R"(&[lL]\s*\{([^\{}&]+)\})", R"(<SPAN style="color:#0000ff">\1</SPAN>)" ),
|
|
|
|
PWSyntaxTranslate( R"(&[2]\s*\{([^\{}&]+)\})", R"(<SPAN style="color:#0000ff">\1</SPAN>)" ) };
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
QString old;
|
|
|
|
while ( s.compare( old ) != 0 ) {
|
|
|
|
for ( int i = 0; i < TRANSLATE_TBL_SIZE; ++i ) {
|
|
|
|
PWSyntaxTranslate & a = t[ i ];
|
|
|
|
s.replace( a.re(), a.replacement() );
|
|
|
|
}
|
|
|
|
old = s;
|
|
|
|
}
|
2018-02-27 16:42:21 +00:00
|
|
|
s.replace( QRegularExpression( "&.\\s*\\{",
|
|
|
|
QRegularExpression::UseUnicodePropertiesOption
|
|
|
|
| QRegularExpression::DotMatchesEverythingOption ),
|
2023-07-20 08:02:22 +00:00
|
|
|
"" );
|
2017-08-23 13:21:50 +00:00
|
|
|
s.replace( "}", "" );
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 13:21:50 +00:00
|
|
|
private:
|
|
|
|
QString _data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
/// This function tries to make an html of the Stardict's resource typed
|
|
|
|
/// 'type', contained in a block pointed to by 'resource', 'size' bytes long.
|
2012-02-29 13:00:38 +00:00
|
|
|
string StardictDictionary::handleResource( char type, char const * resource, size_t size )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2015-02-16 14:57:10 +00:00
|
|
|
QString text;
|
2023-04-15 08:45:57 +00:00
|
|
|
|
|
|
|
// See "Type identifiers" at http://www.huzheng.org/stardict/StarDictFileFormat
|
2009-01-28 20:55:45 +00:00
|
|
|
switch ( type ) {
|
|
|
|
case 'x': // Xdxf content
|
2015-08-01 10:38:39 +00:00
|
|
|
return Xdxf2Html::convert( string( resource, size ), Xdxf2Html::STARDICT, NULL, this, &resourceZip );
|
2009-01-28 20:55:45 +00:00
|
|
|
case 'h': // Html content
|
2012-02-29 13:00:38 +00:00
|
|
|
{
|
2016-03-17 14:41:43 +00:00
|
|
|
QString articleText = QString( "<div class=\"sdct_h\">" ) + QString::fromUtf8( resource, size ) + "</div>";
|
|
|
|
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression imgRe( R"((<\s*img\s+[^>]*src\s*=\s*["']+)(?!(?:data|https?|ftp):))",
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption
|
|
|
|
| QRegularExpression::InvertedGreedinessOption );
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']+)(?!(?:data|https?|ftp):))",
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption
|
|
|
|
| QRegularExpression::InvertedGreedinessOption );
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2017-03-13 14:36:23 +00:00
|
|
|
|
|
|
|
articleText.replace( imgRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" )
|
|
|
|
.replace( linkRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" );
|
2016-03-17 14:41:43 +00:00
|
|
|
|
|
|
|
// Handle links to articles
|
|
|
|
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression linksReg( R"(<a(\s*[^>]*)href\s*=\s*['"](bword://)?([^'"]+)['"])",
|
2018-02-28 14:15:27 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption );
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2016-03-17 14:41:43 +00:00
|
|
|
|
|
|
|
int pos = 0;
|
2018-02-27 16:42:21 +00:00
|
|
|
QString articleNewText;
|
|
|
|
QRegularExpressionMatchIterator it = linksReg.globalMatch( articleText );
|
|
|
|
while ( it.hasNext() ) {
|
|
|
|
QRegularExpressionMatch match = it.next();
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewText += articleText.mid( pos, match.capturedStart() - pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = match.capturedEnd();
|
|
|
|
|
|
|
|
QString link = match.captured( 3 );
|
2016-03-17 14:41:43 +00:00
|
|
|
|
2023-04-25 13:33:36 +00:00
|
|
|
if ( link.indexOf( ':' ) < 0 ) {
|
|
|
|
//compatible with issue #567
|
|
|
|
//such as bword://flȅk
|
|
|
|
if ( link.contains( RX::Html::htmlEntity ) ) {
|
|
|
|
link = Html::unescape( link );
|
|
|
|
}
|
|
|
|
|
2016-03-17 14:41:43 +00:00
|
|
|
QString newLink;
|
|
|
|
if ( link.indexOf( '#' ) < 0 )
|
2018-02-27 16:42:21 +00:00
|
|
|
newLink = QString( "<a" ) + match.captured( 1 ) + "href=\"bword:" + link + "\"";
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2016-03-17 14:41:43 +00:00
|
|
|
|
|
|
|
// Anchors
|
2023-04-25 13:33:36 +00:00
|
|
|
if ( link.indexOf( '#' ) > 0 && link.indexOf( "&#" ) < 0 ) {
|
2018-02-27 16:42:21 +00:00
|
|
|
newLink = QString( "<a" ) + match.captured( 1 ) + "href=\"gdlookup://localhost/" + link + "\"";
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2016-03-17 14:41:43 +00:00
|
|
|
newLink.replace( "#", "?gdanchor=" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !newLink.isEmpty() ) {
|
2018-02-27 16:42:21 +00:00
|
|
|
articleNewText += newLink;
|
2016-03-17 14:41:43 +00:00
|
|
|
}
|
|
|
|
else
|
2018-02-27 16:42:21 +00:00
|
|
|
articleNewText += match.captured();
|
2016-03-17 14:41:43 +00:00
|
|
|
}
|
|
|
|
else
|
2018-02-27 16:42:21 +00:00
|
|
|
articleNewText += match.captured();
|
|
|
|
}
|
|
|
|
if ( pos ) {
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewText += articleText.mid( pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
articleText = articleNewText;
|
|
|
|
articleNewText.clear();
|
|
|
|
}
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2017-10-17 14:51:27 +00:00
|
|
|
// Handle "audio" tags
|
|
|
|
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*)</audio>)",
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption
|
2018-03-11 21:06:09 +00:00
|
|
|
| QRegularExpression::DotMatchesEverythingOption
|
2018-02-27 16:42:21 +00:00
|
|
|
| QRegularExpression::InvertedGreedinessOption );
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2017-10-17 14:51:27 +00:00
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
it = audioRe.globalMatch( articleText );
|
|
|
|
while ( it.hasNext() ) {
|
|
|
|
QRegularExpressionMatch match = it.next();
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewText += articleText.mid( pos, match.capturedStart() - pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = match.capturedEnd();
|
|
|
|
|
|
|
|
QString src = match.captured( 2 );
|
2017-10-17 14:51:27 +00:00
|
|
|
|
|
|
|
if ( src.indexOf( "://" ) >= 0 )
|
2018-02-27 16:42:21 +00:00
|
|
|
articleNewText += match.captured();
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2017-10-17 14:51:27 +00:00
|
|
|
else {
|
|
|
|
std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\"";
|
|
|
|
QString newTag = QString::fromUtf8(
|
|
|
|
( addAudioLink( href, getId() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">" ).c_str() );
|
2018-02-27 16:42:21 +00:00
|
|
|
newTag += match.captured( 4 );
|
|
|
|
if ( match.captured( 4 ).indexOf( "<img " ) < 0 )
|
2021-11-19 13:47:22 +00:00
|
|
|
|
2023-03-05 20:20:05 +00:00
|
|
|
newTag += R"( <img src="qrc:///icons/playsound.png" border="0" alt="Play">)";
|
2017-10-17 14:51:27 +00:00
|
|
|
newTag += "</a></span>";
|
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
articleNewText += newTag;
|
2017-10-17 14:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 16:42:21 +00:00
|
|
|
if ( pos ) {
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewText += articleText.mid( pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
articleText = articleNewText;
|
|
|
|
articleNewText.clear();
|
|
|
|
}
|
2017-10-17 14:51:27 +00:00
|
|
|
|
2016-03-17 14:41:43 +00:00
|
|
|
return ( articleText.toUtf8().data() );
|
2012-02-29 13:00:38 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
case 'm': // Pure meaning, usually means preformatted text
|
2013-07-13 11:24:58 +00:00
|
|
|
return "<div class=\"sdct_m\">" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "</div>";
|
2009-01-28 20:55:45 +00:00
|
|
|
case 'l': // Same as 'm', but not in utf8, instead in current locale's
|
|
|
|
// encoding.
|
|
|
|
// We just use Qt here, it should know better about system's
|
|
|
|
// locale.
|
2013-07-13 11:24:58 +00:00
|
|
|
return "<div class=\"sdct_l\">"
|
|
|
|
+ Html::preformat( QString::fromLocal8Bit( resource, size ).toUtf8().data(), isToLanguageRTL() ) + "</div>";
|
2009-01-28 20:55:45 +00:00
|
|
|
case 'g': // Pango markup.
|
2015-02-16 14:57:10 +00:00
|
|
|
text = QString::fromUtf8( resource, size );
|
|
|
|
pangoToHtml( text );
|
|
|
|
return "<div class=\"sdct_g\">" + string( text.toUtf8().data() ) + "</div>";
|
2009-01-28 20:55:45 +00:00
|
|
|
case 't': // Transcription
|
|
|
|
return "<div class=\"sdct_t\">" + Html::escape( string( resource, size ) ) + "</div>";
|
|
|
|
case 'y': // Chinese YinBiao or Japanese KANA. Examples are needed. For now,
|
|
|
|
// just output as pure escaped utf8.
|
|
|
|
return "<div class=\"sdct_y\">" + Html::escape( string( resource, size ) ) + "</div>";
|
2018-02-27 16:42:21 +00:00
|
|
|
case 'k': // KingSoft PowerWord data.
|
2017-08-23 13:21:50 +00:00
|
|
|
{
|
|
|
|
PowerWordDataProcessor pwdp( resource, size );
|
|
|
|
return pwdp.process();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
case 'w': // MediaWiki markup. We don't handle this right now.
|
|
|
|
return "<div class=\"sdct_w\">" + Html::escape( string( resource, size ) ) + "</div>";
|
|
|
|
case 'n': // WordNet data. We don't know anything about it.
|
|
|
|
return "<div class=\"sdct_n\">" + Html::escape( string( resource, size ) ) + "</div>";
|
|
|
|
|
2023-04-15 08:45:57 +00:00
|
|
|
case 'r': // Resource file list. For now, only img: is handled.
|
|
|
|
{
|
|
|
|
string result = R"(<div class="sdct_r">)";
|
|
|
|
|
|
|
|
// Handle img:example.jpg
|
|
|
|
QString imgTemplate( R"(<img src="bres://)" + QString::fromStdString( getId() ) + R"(/%1">)" );
|
|
|
|
|
|
|
|
for ( const auto & file : QString::fromUtf8( resource, size ).simplified().split( " " ) ) {
|
|
|
|
if ( file.startsWith( "img:" ) ) {
|
|
|
|
result += imgTemplate.arg( file.right( file.size() - file.indexOf( ":" ) - 1 ) ).toStdString();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result += Html::escape( file.toStdString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result + "</div>";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
case 'W': // An embedded Wav file. Unhandled yet.
|
|
|
|
return "<div class=\"sdct_W\">(an embedded .wav file)</div>";
|
|
|
|
case 'P': // An embedded picture file. Unhandled yet.
|
|
|
|
return "<div class=\"sdct_P\">(an embedded picture file)</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( islower( type ) ) {
|
|
|
|
return string( "<b>Unknown textual entry type " ) + string( 1, type ) + ":</b> "
|
|
|
|
+ Html::escape( string( resource, size ) ) + "<br>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return string( "<b>Unknown blob entry type " ) + string( 1, type ) + "</b><br>";
|
|
|
|
}
|
|
|
|
|
2015-02-16 14:57:10 +00:00
|
|
|
void StardictDictionary::pangoToHtml( QString & text )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Partially support for Pango Markup Language
|
|
|
|
* Attributes "fallback", "lang", "gravity", "gravity_hint" just ignored
|
|
|
|
*/
|
|
|
|
|
|
|
|
QRegExp spanRegex( "<span\\s*([^>]*)>", Qt::CaseInsensitive );
|
|
|
|
QRegExp styleRegex( "(\\w+)=\"([^\"]*)\"" );
|
|
|
|
|
|
|
|
text.replace( "\n", "<br>" );
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
do {
|
|
|
|
pos = spanRegex.indexIn( text, pos );
|
|
|
|
if ( pos >= 0 ) {
|
|
|
|
QString styles = spanRegex.cap( 1 );
|
|
|
|
QString newSpan( "<span style=\"" );
|
|
|
|
int stylePos = 0;
|
|
|
|
do {
|
|
|
|
stylePos = styleRegex.indexIn( styles, stylePos );
|
|
|
|
QString style = styleRegex.cap( 1 );
|
|
|
|
if ( stylePos >= 0 ) {
|
|
|
|
if ( style.compare( "font_desc", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "font", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
// Parse font description
|
|
|
|
|
2021-12-29 14:29:06 +00:00
|
|
|
QStringList list = styleRegex.cap( 2 ).split( " ", Qt::SkipEmptyParts );
|
2015-02-16 14:57:10 +00:00
|
|
|
int n;
|
|
|
|
QString sizeStr, stylesStr, familiesStr;
|
|
|
|
for ( n = list.size() - 1; n >= 0; n-- ) {
|
|
|
|
QString str = list.at( n );
|
|
|
|
|
|
|
|
// font size
|
|
|
|
if ( str[ 0 ].isNumber() ) {
|
|
|
|
sizeStr = QString( "font-size:" ) + str + ";";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// font style
|
|
|
|
if ( str.compare( "normal", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "oblique", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "italic", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( !stylesStr.contains( "font-style:" ) )
|
|
|
|
stylesStr += QString( "font-style:" ) + str + ";";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// font variant
|
|
|
|
if ( str.compare( "smallcaps", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-variant:small-caps" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// font weight
|
|
|
|
if ( str.compare( "ultralight", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-weight:100;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "light", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-weight:200;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "bold", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-weight:bold;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "ultrabold", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-weight:800;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "heavy", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-weight:900" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// font stretch
|
|
|
|
if ( str.compare( "ultracondensed", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:ultra-condensed;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "extracondensed", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:extra-condensed;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "semicondensed", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:semi-condensed;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "semiexpanded", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:semi-expanded;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "extraexpanded", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:extra-expanded;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "ultraexpanded", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:ultra-expanded;" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( str.compare( "condensed", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "expanded", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
stylesStr += QString( "font-stretch:" ) + str + ";";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gravity
|
|
|
|
if ( str.compare( "south", Qt::CaseInsensitive ) == 0 || str.compare( "east", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "north", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "west", Qt::CaseInsensitive ) == 0
|
|
|
|
|| str.compare( "auto", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// last words is families list
|
|
|
|
if ( n >= 0 ) {
|
|
|
|
familiesStr = QString( "font-family:" );
|
|
|
|
for ( int i = 0; i <= n; i++ ) {
|
|
|
|
if ( i > 0 && !familiesStr.endsWith( ',' ) )
|
|
|
|
familiesStr += ",";
|
|
|
|
familiesStr += list.at( i );
|
|
|
|
}
|
|
|
|
familiesStr += ";";
|
|
|
|
}
|
|
|
|
|
|
|
|
newSpan += familiesStr + stylesStr + sizeStr;
|
|
|
|
}
|
|
|
|
else if ( style.compare( "font_family", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "face", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-family:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else if ( style.compare( "font_size", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "size", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 )[ 0 ].isLetter() || styleRegex.cap( 2 ).endsWith( "px", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "pt", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "em", Qt::CaseInsensitive ) || styleRegex.cap( 2 ).endsWith( "%" ) )
|
|
|
|
newSpan += QString( "font-size:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else {
|
|
|
|
int size = styleRegex.cap( 2 ).toInt();
|
|
|
|
if ( size )
|
|
|
|
newSpan += QString( "font-size:%1pt;" ).arg( size / 1024.0, 0, 'f', 3 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( style.compare( "font_style", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "style", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-style:" ) + styleRegex.cap( 2 ) + ";";
|
2023-05-07 10:02:22 +00:00
|
|
|
else if ( style.compare( "font_weight", Qt::CaseInsensitive ) == 0
|
2015-02-16 14:57:10 +00:00
|
|
|
|| style.compare( "weight", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
QString str = styleRegex.cap( 2 );
|
|
|
|
if ( str.compare( "ultralight", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-weight:100;" );
|
|
|
|
else if ( str.compare( "light", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-weight:200;" );
|
|
|
|
else if ( str.compare( "ultrabold", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-weight:800;" );
|
|
|
|
else if ( str.compare( "heavy", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-weight:900" );
|
|
|
|
else
|
|
|
|
newSpan += QString( "font-weight:" ) + str + ";";
|
|
|
|
}
|
|
|
|
else if ( style.compare( "font_variant", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "variant", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 ).compare( "smallcaps", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-variant:small-caps" );
|
|
|
|
else
|
|
|
|
newSpan += QString( "font-variant:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
}
|
|
|
|
else if ( style.compare( "font_stretch", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "stretch", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
QString str = styleRegex.cap( 2 );
|
|
|
|
if ( str.compare( "ultracondensed", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:ultra-condensed;" );
|
|
|
|
else if ( str.compare( "extracondensed", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:extra-condensed;" );
|
|
|
|
else if ( str.compare( "semicondensed", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:semi-condensed;" );
|
|
|
|
else if ( str.compare( "semiexpanded", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:semi-expanded;" );
|
|
|
|
else if ( str.compare( "extraexpanded", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:extra-expanded;" );
|
|
|
|
else if ( str.compare( "ultraexpanded", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "font-stretch:ultra-expanded;" );
|
|
|
|
else
|
|
|
|
newSpan += QString( "font-stretch:" ) + str + ";";
|
|
|
|
}
|
|
|
|
else if ( style.compare( "foreground", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "fgcolor", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "color", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "color:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else if ( style.compare( "background", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "bgcolor", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "background-color:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else if ( style.compare( "underline_color", Qt::CaseInsensitive ) == 0
|
|
|
|
|| style.compare( "strikethrough_color", Qt::CaseInsensitive ) == 0 )
|
|
|
|
newSpan += QString( "text-decoration-color:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else if ( style.compare( "underline", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 ).compare( "none", Qt::CaseInsensitive ) )
|
|
|
|
newSpan += QString( "text-decoration-line:none;" );
|
|
|
|
else {
|
|
|
|
newSpan += QString( "text-decoration-line:underline; " );
|
|
|
|
if ( styleRegex.cap( 2 ).compare( "low", Qt::CaseInsensitive ) )
|
|
|
|
newSpan += QString( "text-decoration-style:dotted;" );
|
|
|
|
else if ( styleRegex.cap( 2 ).compare( "single", Qt::CaseInsensitive ) )
|
|
|
|
newSpan += QString( "text-decoration-style:solid;" );
|
|
|
|
else if ( styleRegex.cap( 2 ).compare( "error", Qt::CaseInsensitive ) )
|
|
|
|
newSpan += QString( "text-decoration-style:wavy;" );
|
|
|
|
else
|
|
|
|
newSpan += QString( "text-decoration-style:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( style.compare( "strikethrough", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 ).compare( "true", Qt::CaseInsensitive ) )
|
|
|
|
newSpan += QString( "text-decoration-line:line-through;" );
|
|
|
|
else
|
|
|
|
newSpan += QString( "text-decoration-line:none;" );
|
|
|
|
}
|
|
|
|
else if ( style.compare( "rise", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 ).endsWith( "px", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "pt", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "em", Qt::CaseInsensitive ) || styleRegex.cap( 2 ).endsWith( "%" ) )
|
|
|
|
newSpan += QString( "vertical-align:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else {
|
|
|
|
int riseValue = styleRegex.cap( 2 ).toInt();
|
|
|
|
if ( riseValue )
|
|
|
|
newSpan += QString( "vertical-align:%1pt;" ).arg( riseValue / 1024.0, 0, 'f', 3 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( style.compare( "letter_spacing", Qt::CaseInsensitive ) == 0 ) {
|
|
|
|
if ( styleRegex.cap( 2 ).endsWith( "px", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "pt", Qt::CaseInsensitive )
|
|
|
|
|| styleRegex.cap( 2 ).endsWith( "em", Qt::CaseInsensitive ) || styleRegex.cap( 2 ).endsWith( "%" ) )
|
|
|
|
newSpan += QString( "letter-spacing:" ) + styleRegex.cap( 2 ) + ";";
|
|
|
|
else {
|
|
|
|
int spacing = styleRegex.cap( 2 ).toInt();
|
|
|
|
if ( spacing )
|
|
|
|
newSpan += QString( "letter-spacing:%1pt;" ).arg( spacing / 1024.0, 0, 'f', 3 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stylePos += styleRegex.matchedLength();
|
|
|
|
}
|
|
|
|
} while ( stylePos >= 0 );
|
|
|
|
|
|
|
|
newSpan += "\">";
|
|
|
|
text.replace( pos, spanRegex.matchedLength(), newSpan );
|
|
|
|
pos += newSpan.size();
|
|
|
|
}
|
|
|
|
} while ( pos >= 0 );
|
|
|
|
|
|
|
|
text.replace( " ", " " );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void StardictDictionary::loadArticle( uint32_t address, string & headword, string & articleText )
|
|
|
|
{
|
|
|
|
uint32_t offset, size;
|
|
|
|
|
|
|
|
getArticleProps( address, headword, offset, size );
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
char * articleBody;
|
|
|
|
|
|
|
|
{
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dzMutex );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
// Note that the function always zero-pads the result.
|
|
|
|
articleBody = dict_data_read_( dz, offset, size, 0, 0 );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( !articleBody ) {
|
2013-03-15 12:27:32 +00:00
|
|
|
// throw exCantReadFile( getDictionaryFilenames()[ 2 ] );
|
|
|
|
articleText = string( "<div class=\"sdict_m\">DICTZIP error: " ) + dict_error_str( dz ) + "</div>";
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
articleText.clear();
|
|
|
|
|
|
|
|
char * ptr = articleBody;
|
|
|
|
|
2023-05-07 05:59:01 +00:00
|
|
|
if ( !sameTypeSequence.empty() ) {
|
2009-01-28 20:55:45 +00:00
|
|
|
/// The sequence is known, it's not stored in the article itself
|
2009-04-14 16:35:47 +00:00
|
|
|
for ( unsigned seq = 0; seq < sameTypeSequence.size(); ++seq ) {
|
2009-01-28 20:55:45 +00:00
|
|
|
// Last entry doesn't have size info -- it is inferred from
|
|
|
|
// the bytes left
|
2009-04-14 16:35:47 +00:00
|
|
|
bool entrySizeKnown = ( seq == sameTypeSequence.size() - 1 );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-02-29 13:00:38 +00:00
|
|
|
uint32_t entrySize = 0;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( entrySizeKnown )
|
|
|
|
entrySize = size;
|
|
|
|
else if ( !size ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: short entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
char type = sameTypeSequence[ seq ];
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( islower( type ) ) {
|
|
|
|
// Zero-terminated entry, unless it's the last one
|
|
|
|
if ( !entrySizeKnown )
|
|
|
|
entrySize = strlen( ptr );
|
|
|
|
|
|
|
|
if ( size < entrySize ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
articleText += handleResource( type, ptr, entrySize );
|
|
|
|
|
|
|
|
if ( !entrySizeKnown )
|
|
|
|
++entrySize; // Need to skip the zero byte
|
|
|
|
|
|
|
|
ptr += entrySize;
|
|
|
|
size -= entrySize;
|
|
|
|
}
|
|
|
|
else if ( isupper( *ptr ) ) {
|
|
|
|
// An entry which has its size before contents, unless it's the last one
|
|
|
|
|
|
|
|
if ( !entrySizeKnown ) {
|
|
|
|
if ( size < sizeof( uint32_t ) ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( &entrySize, ptr, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
entrySize = ntohl( entrySize );
|
|
|
|
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
size -= sizeof( uint32_t );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( size < entrySize ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
articleText += handleResource( type, ptr, entrySize );
|
|
|
|
|
|
|
|
ptr += entrySize;
|
|
|
|
size -= entrySize;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: non-alpha entry type 0x%x for the word %s encountered in \"%s\".\n",
|
|
|
|
type,
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The sequence is stored in each article separately
|
|
|
|
while ( size ) {
|
|
|
|
if ( islower( *ptr ) ) {
|
|
|
|
// Zero-terminated entry
|
|
|
|
size_t len = strlen( ptr + 1 );
|
|
|
|
|
|
|
|
if ( size < len + 2 ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
articleText += handleResource( *ptr, ptr + 1, len );
|
|
|
|
|
|
|
|
ptr += len + 2;
|
|
|
|
size -= len + 2;
|
|
|
|
}
|
|
|
|
else if ( isupper( *ptr ) ) {
|
|
|
|
// An entry which havs its size before contents
|
|
|
|
if ( size < sizeof( uint32_t ) + 1 ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t entrySize;
|
|
|
|
|
|
|
|
memcpy( &entrySize, ptr + 1, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
entrySize = ntohl( entrySize );
|
|
|
|
|
|
|
|
if ( size < sizeof( uint32_t ) + 1 + entrySize ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: malformed entry for the word %s encountered in \"%s\".\n",
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
articleText += handleResource( *ptr, ptr + 1 + sizeof( uint32_t ), entrySize );
|
|
|
|
|
|
|
|
ptr += sizeof( uint32_t ) + 1 + entrySize;
|
|
|
|
size -= sizeof( uint32_t ) + 1 + entrySize;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: non-alpha entry type 0x%x for the word %s encountered in \"%s\".\n",
|
|
|
|
(unsigned)*ptr,
|
|
|
|
headword.c_str(),
|
|
|
|
getName().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free( articleBody );
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:58:45 +00:00
|
|
|
QString const & StardictDictionary::getDescription()
|
|
|
|
{
|
|
|
|
if ( !dictionaryDescription.isEmpty() )
|
|
|
|
return dictionaryDescription;
|
|
|
|
|
|
|
|
File::Class ifoFile( getDictionaryFilenames()[ 0 ], "r" );
|
|
|
|
Ifo ifo( ifoFile );
|
|
|
|
|
2012-11-19 15:30:26 +00:00
|
|
|
if ( !ifo.copyright.empty() ) {
|
2017-01-03 11:50:05 +00:00
|
|
|
QString copyright = QString::fromUtf8( ifo.copyright.c_str() ).replace( "<br>", "\n", Qt::CaseInsensitive );
|
2023-05-07 12:47:41 +00:00
|
|
|
dictionaryDescription += QObject::tr( "Copyright: %1%2" ).arg( copyright ).arg( "\n\n" );
|
2017-01-03 11:50:05 +00:00
|
|
|
}
|
2012-11-19 15:30:26 +00:00
|
|
|
|
|
|
|
if ( !ifo.author.empty() ) {
|
2017-01-03 11:50:05 +00:00
|
|
|
QString author = QString::fromUtf8( ifo.author.c_str() );
|
2023-05-07 12:47:41 +00:00
|
|
|
dictionaryDescription += QObject::tr( "Author: %1%2" ).arg( author ).arg( "\n\n" );
|
2017-01-03 11:50:05 +00:00
|
|
|
}
|
2012-11-19 15:30:26 +00:00
|
|
|
|
|
|
|
if ( !ifo.email.empty() ) {
|
2017-01-03 11:50:05 +00:00
|
|
|
QString email = QString::fromUtf8( ifo.email.c_str() );
|
2023-05-07 12:47:41 +00:00
|
|
|
dictionaryDescription += QObject::tr( "E-mail: %1%2" ).arg( email ).arg( "\n\n" );
|
2017-01-03 11:50:05 +00:00
|
|
|
}
|
2012-11-19 15:30:26 +00:00
|
|
|
|
2017-01-03 11:02:39 +00:00
|
|
|
if ( !ifo.website.empty() ) {
|
2017-01-03 11:50:05 +00:00
|
|
|
QString website = QString::fromUtf8( ifo.website.c_str() );
|
2023-05-07 12:47:41 +00:00
|
|
|
dictionaryDescription += QObject::tr( "Website: %1%2" ).arg( website ).arg( "\n\n" );
|
2017-01-03 11:50:05 +00:00
|
|
|
}
|
2017-01-03 11:02:39 +00:00
|
|
|
|
|
|
|
if ( !ifo.date.empty() ) {
|
2017-01-03 11:50:05 +00:00
|
|
|
QString date = QString::fromUtf8( ifo.date.c_str() );
|
2023-05-07 12:47:41 +00:00
|
|
|
dictionaryDescription += QObject::tr( "Date: %1%2" ).arg( date ).arg( "\n\n" );
|
2017-01-03 11:50:05 +00:00
|
|
|
}
|
2017-01-03 11:02:39 +00:00
|
|
|
|
2012-09-07 13:58:45 +00:00
|
|
|
if ( !ifo.description.empty() ) {
|
2012-11-19 15:30:26 +00:00
|
|
|
QString desc = QString::fromUtf8( ifo.description.c_str() );
|
|
|
|
desc.replace( "\t", "<br/>" );
|
2016-05-15 09:41:16 +00:00
|
|
|
desc.replace( "\\n", "<br/>" );
|
|
|
|
desc.replace( "<br>", "<br/>", Qt::CaseInsensitive );
|
2023-04-27 15:47:35 +00:00
|
|
|
dictionaryDescription += Html::unescape( desc, Html::HtmlOption::Keep );
|
2012-09-07 13:58:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 15:30:26 +00:00
|
|
|
if ( dictionaryDescription.isEmpty() )
|
|
|
|
dictionaryDescription = "NONE";
|
|
|
|
|
2012-09-07 13:58:45 +00:00
|
|
|
return dictionaryDescription;
|
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2023-04-13 10:08:32 +00:00
|
|
|
QString StardictDictionary::getMainFilename()
|
|
|
|
{
|
|
|
|
return getDictionaryFilenames()[ 0 ].c_str();
|
|
|
|
}
|
2013-06-11 18:55:29 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
void StardictDictionary::makeFTSIndex( QAtomicInt & isCancelled, bool firstIteration )
|
|
|
|
{
|
2023-06-03 00:29:19 +00:00
|
|
|
if ( !( Dictionary::needToRebuildIndex( getDictionaryFilenames(), ftsIdxName )
|
|
|
|
|| FtsHelpers::ftsIndexIsOldOrBad( this ) ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
FTS_index_completed.ref();
|
|
|
|
|
2023-06-03 00:29:19 +00:00
|
|
|
if ( haveFTSIndex() )
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ( ensureInitDone().size() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( firstIteration && getArticleCount() > FTS::MaxDictionarySizeForFastSearch )
|
|
|
|
return;
|
|
|
|
|
|
|
|
gdDebug( "Stardict: Building the full-text index for dictionary: %s\n", getName().c_str() );
|
|
|
|
|
|
|
|
try {
|
|
|
|
FtsHelpers::makeFTSIndex( this, isCancelled );
|
2014-04-17 14:31:51 +00:00
|
|
|
FTS_index_completed.ref();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & ex ) {
|
|
|
|
gdWarning( "Stardict: Failed building full-text search index for \"%s\", reason: %s\n",
|
|
|
|
getName().c_str(),
|
|
|
|
ex.what() );
|
2023-04-13 10:08:32 +00:00
|
|
|
QFile::remove( ftsIdxName.c_str() );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StardictDictionary::getArticleText( uint32_t articleAddress, QString & headword, QString & text )
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
string headwordStr, articleStr;
|
|
|
|
loadArticle( articleAddress, headwordStr, articleStr );
|
|
|
|
|
|
|
|
headword = QString::fromUtf8( headwordStr.data(), headwordStr.size() );
|
|
|
|
|
2023-04-25 00:28:40 +00:00
|
|
|
text = Html::unescape( QString::fromStdString( articleStr ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & ex ) {
|
|
|
|
gdWarning( "Stardict: Failed retrieving article from \"%s\", reason: %s\n", getName().c_str(), ex.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > StardictDictionary::getSearchResults( QString const & searchString,
|
2023-05-30 23:42:31 +00:00
|
|
|
int searchMode,
|
|
|
|
bool matchCase,
|
2018-04-10 14:49:52 +00:00
|
|
|
bool ignoreDiacritics )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2023-05-30 23:42:31 +00:00
|
|
|
return std::make_shared< FtsHelpers::FTSResultsRequest >( *this,
|
|
|
|
searchString,
|
|
|
|
searchMode,
|
|
|
|
matchCase,
|
|
|
|
ignoreDiacritics );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
/// StardictDictionary::findHeadwordsForSynonym()
|
|
|
|
|
|
|
|
class StardictHeadwordsRequest: public Dictionary::WordSearchRequest
|
|
|
|
{
|
|
|
|
|
|
|
|
wstring word;
|
|
|
|
StardictDictionary & dict;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
2023-04-29 04:12:49 +00:00
|
|
|
QFuture< void > f;
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
StardictHeadwordsRequest( wstring const & word_, StardictDictionary & dict_ ):
|
|
|
|
word( word_ ),
|
|
|
|
dict( dict_ )
|
|
|
|
{
|
2023-04-29 04:12:49 +00:00
|
|
|
f = QtConcurrent::run( [ this ]() {
|
|
|
|
this->run();
|
|
|
|
} );
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-05-08 08:41:54 +00:00
|
|
|
void run();
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void cancel() override
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~StardictHeadwordsRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
2023-04-29 04:12:49 +00:00
|
|
|
f.waitForFinished();
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void StardictHeadwordsRequest::run()
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
try {
|
2023-07-06 12:23:49 +00:00
|
|
|
//limited the synomys to at most 10 entries
|
|
|
|
vector< WordArticleLink > chain = dict.findArticles( word, false, 10 );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
wstring caseFolded = Folding::applySimpleCaseOnly( word );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
for ( unsigned x = 0; x < chain.size(); ++x ) {
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
2010-05-01 21:51:57 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
string headword, articleText;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
dict.loadArticle( chain[ x ].articleOffset, headword, articleText );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
wstring headwordDecoded = Utf8::decode( headword );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
if ( caseFolded != Folding::applySimpleCaseOnly( headwordDecoded ) ) {
|
|
|
|
// The headword seems to differ from the input word, which makes the
|
|
|
|
// input word its synonym.
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
matches.push_back( headwordDecoded );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-01 21:51:57 +00:00
|
|
|
catch ( std::exception & e ) {
|
|
|
|
setErrorString( QString::fromUtf8( e.what() ) );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:23:49 +00:00
|
|
|
sptr< Dictionary::WordSearchRequest > StardictDictionary::findHeadwordsForSynonym( wstring const & word )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2023-07-06 12:23:49 +00:00
|
|
|
return synonymSearchEnabled ? std::make_shared< StardictHeadwordsRequest >( word, *this ) :
|
2017-03-09 16:11:17 +00:00
|
|
|
Class::findHeadwordsForSynonym( word );
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// StardictDictionary::getArticle()
|
|
|
|
|
|
|
|
|
|
|
|
class StardictArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
|
|
|
|
wstring word;
|
|
|
|
vector< wstring > alts;
|
|
|
|
StardictDictionary & dict;
|
2018-06-13 16:00:42 +00:00
|
|
|
bool ignoreDiacritics;
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
2023-04-29 04:12:49 +00:00
|
|
|
QFuture< void > f;
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
StardictArticleRequest( wstring const & word_,
|
|
|
|
vector< wstring > const & alts_,
|
2018-06-13 16:00:42 +00:00
|
|
|
StardictDictionary & dict_,
|
|
|
|
bool ignoreDiacritics_ ):
|
|
|
|
word( word_ ),
|
|
|
|
alts( alts_ ),
|
|
|
|
dict( dict_ ),
|
|
|
|
ignoreDiacritics( ignoreDiacritics_ )
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
2023-04-29 04:12:49 +00:00
|
|
|
f = QtConcurrent::run( [ this ]() {
|
|
|
|
this->run();
|
|
|
|
} );
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 05:59:01 +00:00
|
|
|
void run();
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void cancel() override
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~StardictArticleRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
2023-04-29 04:12:49 +00:00
|
|
|
f.waitForFinished();
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void StardictArticleRequest::run()
|
|
|
|
{
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
try {
|
2018-06-13 16:00:42 +00:00
|
|
|
vector< WordArticleLink > chain = dict.findArticles( word, ignoreDiacritics );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-07-03 13:16:31 +00:00
|
|
|
//if alts has more than 100 , great probability that the dictionary is wrong produced or parsed.
|
|
|
|
if ( alts.size() < 100 ) {
|
|
|
|
for ( unsigned x = 0; x < alts.size(); ++x ) {
|
|
|
|
/// Make an additional query for each alt
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-07-03 13:16:31 +00:00
|
|
|
vector< WordArticleLink > altChain = dict.findArticles( alts[ x ], ignoreDiacritics );
|
|
|
|
if ( altChain.size() > 100 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
2010-05-01 21:51:57 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
multimap< wstring, pair< string, string > > mainArticles, alternateArticles;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-07-06 12:23:49 +00:00
|
|
|
set< uint32_t > articlesIncluded; // Some synonyms make it that the articles
|
2010-05-01 21:51:57 +00:00
|
|
|
// appear several times. We combat this
|
|
|
|
// by only allowing them to appear once.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
2018-06-13 16:00:42 +00:00
|
|
|
if ( ignoreDiacritics )
|
|
|
|
wordCaseFolded = Folding::applyDiacriticsOnly( wordCaseFolded );
|
2010-05-01 21:51:57 +00:00
|
|
|
|
2023-07-03 13:16:31 +00:00
|
|
|
//if the chain is too large, it is more likely has some dictionary making or parsing issue.
|
|
|
|
for ( unsigned x = 0; x < qMin( 10, (int)chain.size() ); ++x ) {
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
2010-05-01 21:51:57 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
// Now grab that article
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
string headword, articleText;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
dict.loadArticle( chain[ x ].articleOffset, headword, articleText );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
// Ok. Now, does it go to main articles, or to alternate ones? We list
|
|
|
|
// main ones first, and alternates after.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
// We do the case-folded comparison here.
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-29 02:35:56 +00:00
|
|
|
wstring headwordStripped = Folding::applySimpleCaseOnly( headword );
|
2018-06-13 16:00:42 +00:00
|
|
|
if ( ignoreDiacritics )
|
|
|
|
headwordStripped = Folding::applyDiacriticsOnly( headwordStripped );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
multimap< wstring, pair< string, string > > & mapToUse =
|
|
|
|
( wordCaseFolded == headwordStripped ) ? mainArticles : alternateArticles;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-29 02:35:56 +00:00
|
|
|
mapToUse.insert( pair( Folding::applySimpleCaseOnly( headword ), pair( headword, articleText ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() ) {
|
|
|
|
// No such word
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
string result;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
multimap< wstring, pair< string, string > >::const_iterator i;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-04-15 07:39:49 +00:00
|
|
|
string cleaner = Utils::Html::getHtmlCleaner();
|
2022-04-10 12:07:00 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
for ( i = mainArticles.begin(); i != mainArticles.end(); ++i ) {
|
2022-12-24 22:01:50 +00:00
|
|
|
result += dict.isFromLanguageRTL() ? R"(<h3 class="sdct_headwords" dir="rtl">)" : "<h3 class=\"sdct_headwords\">";
|
2010-05-01 21:51:57 +00:00
|
|
|
result += i->second.first;
|
|
|
|
result += "</h3>";
|
2013-07-10 13:48:09 +00:00
|
|
|
if ( dict.isToLanguageRTL() )
|
2022-12-24 22:01:50 +00:00
|
|
|
result += R"(<div style="display:inline;" dir="rtl">)";
|
2010-05-01 21:51:57 +00:00
|
|
|
result += i->second.second;
|
|
|
|
result += cleaner;
|
2013-07-10 13:48:09 +00:00
|
|
|
if ( dict.isToLanguageRTL() )
|
2017-06-09 12:38:23 +00:00
|
|
|
result += "</div>";
|
2010-05-01 21:51:57 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
for ( i = alternateArticles.begin(); i != alternateArticles.end(); ++i ) {
|
2022-12-24 22:01:50 +00:00
|
|
|
result += dict.isFromLanguageRTL() ? R"(<h3 class="sdct_headwords" dir="rtl">)" : "<h3 class=\"sdct_headwords\">";
|
2010-05-01 21:51:57 +00:00
|
|
|
result += i->second.first;
|
|
|
|
result += "</h3>";
|
2013-07-10 13:48:09 +00:00
|
|
|
if ( dict.isToLanguageRTL() )
|
2022-12-24 22:01:50 +00:00
|
|
|
result += R"(<div style="display:inline;" dir="rtl">)";
|
2010-05-01 21:51:57 +00:00
|
|
|
result += i->second.second;
|
|
|
|
result += cleaner;
|
2013-07-10 13:48:09 +00:00
|
|
|
if ( dict.isToLanguageRTL() )
|
2017-06-09 12:38:23 +00:00
|
|
|
result += "</div>";
|
2010-05-01 21:51:57 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2023-06-23 15:09:31 +00:00
|
|
|
appendString( result );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-05-01 21:51:57 +00:00
|
|
|
hasAnyData = true;
|
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
|
|
|
setErrorString( QString::fromUtf8( e.what() ) );
|
|
|
|
}
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
finish();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
sptr< Dictionary::DataRequest > StardictDictionary::getArticle( wstring const & word,
|
2009-05-29 19:48:50 +00:00
|
|
|
vector< wstring > const & alts,
|
2018-06-13 16:00:42 +00:00
|
|
|
wstring const &,
|
|
|
|
bool ignoreDiacritics )
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< StardictArticleRequest >( word, alts, *this, ignoreDiacritics );
|
2009-04-16 11:33:12 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
static char const * beginsWith( char const * substr, char const * str )
|
|
|
|
{
|
|
|
|
size_t len = strlen( substr );
|
|
|
|
|
|
|
|
return strncmp( str, substr, len ) == 0 ? str + len : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ifo::Ifo( File::Class & f ):
|
|
|
|
wordcount( 0 ),
|
|
|
|
synwordcount( 0 ),
|
|
|
|
idxfilesize( 0 ),
|
|
|
|
idxoffsetbits( 32 )
|
|
|
|
{
|
|
|
|
static string const versionEq( "version=" );
|
|
|
|
|
|
|
|
static string const booknameEq( "bookname=" );
|
|
|
|
|
2022-01-15 07:29:20 +00:00
|
|
|
//GD_DPRINTF( "%s<\n", f.gets().c_str() );
|
|
|
|
//GD_DPRINTF( "%s<\n", f.gets().c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-07-21 01:04:57 +00:00
|
|
|
if ( QString::fromUtf8( f.gets().c_str() ) != "StarDict's dict ifo file"
|
2009-01-28 20:55:45 +00:00
|
|
|
|| f.gets().compare( 0, versionEq.size(), versionEq ) )
|
|
|
|
throw exNotAnIfoFile();
|
|
|
|
|
|
|
|
/// Now go through the file and parse options
|
|
|
|
|
|
|
|
try {
|
|
|
|
char option[ 16384 ];
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
if ( !f.gets( option, sizeof( option ), true ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( char const * val = beginsWith( "bookname=", option ) )
|
|
|
|
bookname = val;
|
|
|
|
else if ( char const * val = beginsWith( "wordcount=", option ) ) {
|
|
|
|
if ( sscanf( val, "%u", &wordcount ) != 1 )
|
|
|
|
throw exBadFieldInIfo( option );
|
|
|
|
}
|
|
|
|
else if ( char const * val = beginsWith( "synwordcount=", option ) ) {
|
|
|
|
if ( sscanf( val, "%u", &synwordcount ) != 1 )
|
|
|
|
throw exBadFieldInIfo( option );
|
|
|
|
}
|
|
|
|
else if ( char const * val = beginsWith( "idxfilesize=", option ) ) {
|
|
|
|
if ( sscanf( val, "%u", &idxfilesize ) != 1 )
|
|
|
|
throw exBadFieldInIfo( option );
|
|
|
|
}
|
|
|
|
else if ( char const * val = beginsWith( "idxoffsetbits=", option ) ) {
|
|
|
|
if ( sscanf( val, "%u", &idxoffsetbits ) != 1 || ( idxoffsetbits != 32 && idxoffsetbits != 64 ) )
|
|
|
|
throw exBadFieldInIfo( option );
|
|
|
|
}
|
|
|
|
else if ( char const * val = beginsWith( "sametypesequence=", option ) )
|
|
|
|
sametypesequence = val;
|
|
|
|
else if ( char const * val = beginsWith( "dicttype=", option ) )
|
|
|
|
dicttype = val;
|
2012-09-07 13:58:45 +00:00
|
|
|
else if ( char const * val = beginsWith( "description=", option ) )
|
|
|
|
description = val;
|
2012-11-19 15:30:26 +00:00
|
|
|
else if ( char const * val = beginsWith( "copyright=", option ) )
|
|
|
|
copyright = val;
|
|
|
|
else if ( char const * val = beginsWith( "author=", option ) )
|
|
|
|
author = val;
|
|
|
|
else if ( char const * val = beginsWith( "email=", option ) )
|
|
|
|
email = val;
|
2017-01-03 11:02:39 +00:00
|
|
|
else if ( char const * val = beginsWith( "website=", option ) )
|
|
|
|
website = val;
|
|
|
|
else if ( char const * val = beginsWith( "date=", option ) )
|
|
|
|
date = val;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( File::exReadError & ) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 13:13:25 +00:00
|
|
|
//// StardictDictionary::getResource()
|
|
|
|
|
|
|
|
|
|
|
|
class StardictResourceRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
|
|
|
|
StardictDictionary & dict;
|
|
|
|
|
|
|
|
string resourceName;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
2023-04-29 04:12:49 +00:00
|
|
|
QFuture< void > f;
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
StardictResourceRequest( StardictDictionary & dict_, string const & resourceName_ ):
|
|
|
|
dict( dict_ ),
|
|
|
|
resourceName( resourceName_ )
|
|
|
|
{
|
2023-04-29 04:12:49 +00:00
|
|
|
f = QtConcurrent::run( [ this ]() {
|
|
|
|
this->run();
|
|
|
|
} );
|
2013-03-25 13:13:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:41:54 +00:00
|
|
|
void run();
|
2013-03-25 13:13:25 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void cancel() override
|
2013-03-25 13:13:25 +00:00
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
~StardictResourceRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
2023-04-29 04:12:49 +00:00
|
|
|
f.waitForFinished();
|
2013-03-25 13:13:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void StardictResourceRequest::run()
|
|
|
|
{
|
|
|
|
// Some runnables linger enough that they are cancelled before they start
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
2013-03-25 13:13:25 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ( resourceName.at( 0 ) == '\x1E' )
|
|
|
|
resourceName = resourceName.erase( 0, 1 );
|
|
|
|
if ( resourceName.at( resourceName.length() - 1 ) == '\x1F' )
|
|
|
|
resourceName.erase( resourceName.length() - 1, 1 );
|
|
|
|
|
2023-04-28 16:09:45 +00:00
|
|
|
string n =
|
|
|
|
dict.getContainingFolder().toStdString() + Utils::Fs::separator() + "res" + Utils::Fs::separator() + resourceName;
|
2013-03-25 13:13:25 +00:00
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "n is %s\n", n.c_str() );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
try {
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
File::loadFromFile( n, data );
|
|
|
|
}
|
2018-05-22 14:48:14 +00:00
|
|
|
catch ( File::exCantOpen & ) {
|
2013-03-25 13:13:25 +00:00
|
|
|
// Try reading from zip file
|
|
|
|
|
|
|
|
if ( dict.resourceZip.isOpen() ) {
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
if ( !dict.resourceZip.loadFile( Utf8::decode( resourceName ), data ) )
|
|
|
|
throw; // Make it fail since we couldn't read the archive
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Filetype::isNameOfTiff( resourceName ) ) {
|
|
|
|
// Convert it
|
|
|
|
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2022-04-05 13:25:07 +00:00
|
|
|
GdTiff::tiff2img( data );
|
2013-03-25 13:13:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 14:01:50 +00:00
|
|
|
if ( Filetype::isNameOfCSS( resourceName ) ) {
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2013-05-23 14:01:50 +00:00
|
|
|
|
|
|
|
QString css = QString::fromUtf8( data.data(), data.size() );
|
2016-03-02 14:39:51 +00:00
|
|
|
|
|
|
|
// Correct some url's
|
|
|
|
|
|
|
|
QString id = QString::fromUtf8( dict.getId().c_str() );
|
|
|
|
int pos = 0;
|
2018-02-27 16:42:21 +00:00
|
|
|
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", QRegularExpression::CaseInsensitiveOption );
|
2018-02-27 16:42:21 +00:00
|
|
|
|
|
|
|
QString newCSS;
|
|
|
|
QRegularExpressionMatchIterator it = links.globalMatch( css );
|
|
|
|
while ( it.hasNext() ) {
|
|
|
|
QRegularExpressionMatch match = it.next();
|
2022-02-27 05:17:37 +00:00
|
|
|
newCSS += css.mid( pos, match.capturedStart() - pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = match.capturedEnd();
|
|
|
|
|
|
|
|
QString url = match.captured( 2 );
|
|
|
|
|
|
|
|
if ( url.indexOf( ":/" ) >= 0 || url.indexOf( "data:" ) >= 0 ) {
|
|
|
|
// External link
|
|
|
|
newCSS += match.captured();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString newUrl =
|
|
|
|
QString( "url(" ) + match.captured( 1 ) + "bres://" + id + "/" + url + match.captured( 3 ) + ")";
|
|
|
|
newCSS += newUrl;
|
|
|
|
}
|
|
|
|
if ( pos ) {
|
2022-02-27 05:17:37 +00:00
|
|
|
newCSS += css.mid( pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
css = newCSS;
|
|
|
|
newCSS.clear();
|
|
|
|
}
|
2016-03-02 14:39:51 +00:00
|
|
|
|
2013-05-23 14:01:50 +00:00
|
|
|
dict.isolateCSS( css );
|
|
|
|
QByteArray bytes = css.toUtf8();
|
|
|
|
data.resize( bytes.size() );
|
|
|
|
memcpy( &data.front(), bytes.constData(), bytes.size() );
|
|
|
|
}
|
2013-03-25 13:13:25 +00:00
|
|
|
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &dataMutex );
|
2013-03-25 13:13:25 +00:00
|
|
|
hasAnyData = true;
|
|
|
|
}
|
2013-09-24 13:56:47 +00:00
|
|
|
catch ( std::exception & ex ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Stardict: Failed loading resource \"%s\" for \"%s\", reason: %s\n",
|
2013-09-24 13:56:47 +00:00
|
|
|
resourceName.c_str(),
|
|
|
|
dict.getName().c_str(),
|
|
|
|
ex.what() );
|
|
|
|
// Resource not loaded -- we don't set the hasAnyData flag then
|
2013-03-25 13:13:25 +00:00
|
|
|
}
|
|
|
|
catch ( ... ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > StardictDictionary::getResource( string const & name )
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2013-03-25 13:13:25 +00:00
|
|
|
{
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< StardictResourceRequest >( *this, name );
|
2013-03-25 13:13:25 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static void findCorrespondingFiles( string const & ifo, string & idx, string & dict, string & syn )
|
|
|
|
{
|
|
|
|
string base( ifo, 0, ifo.size() - 3 );
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
if ( !( File::tryPossibleName( base + "idx", idx ) || File::tryPossibleName( base + "idx.gz", idx )
|
|
|
|
|| File::tryPossibleName( base + "idx.dz", idx ) || File::tryPossibleName( base + "IDX", idx )
|
|
|
|
|| File::tryPossibleName( base + "IDX.GZ", idx ) || File::tryPossibleName( base + "IDX.DZ", idx ) ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw exNoIdxFile( ifo );
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
if ( !( File::tryPossibleName( base + "dict", dict ) || File::tryPossibleName( base + "dict.dz", dict )
|
|
|
|
|| File::tryPossibleName( base + "DICT", dict ) || File::tryPossibleName( base + "dict.DZ", dict ) ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw exNoDictFile( ifo );
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
if ( !( File::tryPossibleName( base + "syn", syn ) || File::tryPossibleName( base + "syn.gz", syn )
|
|
|
|
|| File::tryPossibleName( base + "syn.dz", syn ) || File::tryPossibleName( base + "SYN", syn )
|
|
|
|
|| File::tryPossibleName( base + "SYN.GZ", syn ) || File::tryPossibleName( base + "SYN.DZ", syn ) ) )
|
2009-04-14 16:35:47 +00:00
|
|
|
syn.clear();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleIdxSynFile( string const & fileName,
|
|
|
|
IndexedWords & indexedWords,
|
|
|
|
ChunkedStorage::Writer & chunks,
|
|
|
|
vector< uint32_t > * articleOffsets,
|
2017-10-25 14:37:39 +00:00
|
|
|
bool isSynFile,
|
|
|
|
bool parseHeadwords )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2012-01-25 16:35:00 +00:00
|
|
|
gzFile stardictIdx = gd_gzopen( fileName.c_str() );
|
2011-09-09 12:05:28 +00:00
|
|
|
if ( !stardictIdx )
|
|
|
|
throw exCantReadFile( fileName );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
vector< char > image;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
size_t oldSize = image.size();
|
|
|
|
|
|
|
|
image.resize( oldSize + 65536 );
|
|
|
|
|
|
|
|
int rd = gzread( stardictIdx, &image.front() + oldSize, 65536 );
|
|
|
|
|
|
|
|
if ( rd < 0 ) {
|
|
|
|
gzclose( stardictIdx );
|
|
|
|
throw exCantReadFile( fileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rd != 65536 ) {
|
|
|
|
image.resize( oldSize + rd + 1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-09 12:05:28 +00:00
|
|
|
gzclose( stardictIdx );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// We append one zero byte to catch runaway string at the end, if any
|
|
|
|
|
|
|
|
image.back() = 0;
|
|
|
|
|
|
|
|
// Now parse it
|
|
|
|
|
|
|
|
for ( char const * ptr = &image.front(); ptr != &image.back(); ) {
|
|
|
|
size_t wordLen = strlen( ptr );
|
|
|
|
|
|
|
|
if ( ptr + wordLen + 1 + ( isSynFile ? sizeof( uint32_t ) : sizeof( uint32_t ) * 2 ) > &image.back() ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_FDPRINTF( stderr, "Warning: sudden end of file %s\n", fileName.c_str() );
|
2009-05-26 10:25:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
char const * word = ptr;
|
|
|
|
|
|
|
|
ptr += wordLen + 1;
|
|
|
|
|
|
|
|
uint32_t offset;
|
|
|
|
|
2013-02-18 18:10:06 +00:00
|
|
|
if ( strstr( word, "&#" ) ) {
|
|
|
|
// Decode some html-coded symbols in headword
|
|
|
|
string unescapedWord = Html::unescapeUtf8( word );
|
|
|
|
strncpy( (char *)word, unescapedWord.c_str(), wordLen );
|
|
|
|
wordLen = strlen( word );
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !isSynFile ) {
|
|
|
|
// We're processing the .idx file
|
|
|
|
uint32_t articleOffset, articleSize;
|
|
|
|
|
|
|
|
memcpy( &articleOffset, ptr, sizeof( uint32_t ) );
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
memcpy( &articleSize, ptr, sizeof( uint32_t ) );
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
|
|
|
articleOffset = ntohl( articleOffset );
|
|
|
|
articleSize = ntohl( articleSize );
|
|
|
|
|
|
|
|
// Create an entry for the article in the chunked storage
|
|
|
|
|
|
|
|
offset = chunks.startNewBlock();
|
|
|
|
|
|
|
|
if ( articleOffsets )
|
|
|
|
articleOffsets->push_back( offset );
|
|
|
|
|
|
|
|
chunks.addToBlock( &articleOffset, sizeof( uint32_t ) );
|
|
|
|
chunks.addToBlock( &articleSize, sizeof( uint32_t ) );
|
|
|
|
chunks.addToBlock( word, wordLen + 1 );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We're processing the .syn file
|
|
|
|
uint32_t offsetInIndex;
|
|
|
|
|
|
|
|
memcpy( &offsetInIndex, ptr, sizeof( uint32_t ) );
|
|
|
|
ptr += sizeof( uint32_t );
|
|
|
|
|
|
|
|
offsetInIndex = ntohl( offsetInIndex );
|
|
|
|
|
|
|
|
if ( offsetInIndex >= articleOffsets->size() )
|
|
|
|
throw exIncorrectOffset( fileName );
|
|
|
|
|
|
|
|
offset = ( *articleOffsets )[ offsetInIndex ];
|
2009-10-19 23:05:28 +00:00
|
|
|
|
|
|
|
// Some StarDict dictionaries are in fact badly converted Babylon ones.
|
|
|
|
// They contain a lot of superfluous slashed entries with dollar signs.
|
|
|
|
// We try to filter them out here, since those entries become much more
|
|
|
|
// apparent in GoldenDict than they were in StarDict because of
|
|
|
|
// punctuation folding. Hopefully there are not a whole lot of valid
|
|
|
|
// synonyms which really start from slash and contain dollar signs, or
|
|
|
|
// end with dollar and contain slashes.
|
|
|
|
if ( *word == '/' ) {
|
|
|
|
if ( strchr( word, '$' ) )
|
|
|
|
continue; // Skip this entry
|
|
|
|
}
|
|
|
|
else if ( wordLen && word[ wordLen - 1 ] == '$' ) {
|
|
|
|
if ( strchr( word, '/' ) )
|
|
|
|
continue; // Skip this entry
|
2023-07-03 13:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the entry is hypen, skip
|
|
|
|
if ( wordLen == 1 && *word == '-' ) {
|
|
|
|
continue; // Skip this entry
|
2009-10-19 23:05:28 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert new entry into an index
|
|
|
|
|
2017-10-25 14:37:39 +00:00
|
|
|
if ( parseHeadwords )
|
|
|
|
indexedWords.addWord( Utf8::decode( word ), offset );
|
|
|
|
else
|
|
|
|
indexedWords.addSingleWord( Utf8::decode( word ), offset );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "%u entires made\n", (unsigned)indexedWords.size() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 13:00:38 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
2017-10-25 14:37:39 +00:00
|
|
|
Dictionary::Initializing & initializing,
|
|
|
|
unsigned maxHeadwordsToExpand )
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for ( vector< string >::const_iterator i = fileNames.begin(); i != fileNames.end(); ++i ) {
|
|
|
|
if ( i->size() < 4 || strcasecmp( i->c_str() + ( i->size() - 4 ), ".ifo" ) != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
vector< string > dictFiles( 1, *i );
|
|
|
|
|
2009-05-05 21:57:38 +00:00
|
|
|
string idxFileName, dictFileName, synFileName;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
findCorrespondingFiles( *i, idxFileName, dictFileName, synFileName );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
dictFiles.push_back( idxFileName );
|
|
|
|
dictFiles.push_back( dictFileName );
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( synFileName.size() )
|
2009-01-28 20:55:45 +00:00
|
|
|
dictFiles.push_back( synFileName );
|
|
|
|
|
2013-03-25 13:13:25 +00:00
|
|
|
// See if there's a zip file with resources present. If so, include it.
|
|
|
|
|
|
|
|
string zipFileName;
|
2023-04-14 03:53:23 +00:00
|
|
|
string baseName =
|
2023-04-28 16:09:45 +00:00
|
|
|
QDir( QString::fromStdString( idxFileName ) ).absolutePath().toStdString() + Utils::Fs::separator();
|
2013-03-25 13:13:25 +00:00
|
|
|
|
2017-04-24 14:42:01 +00:00
|
|
|
if ( File::tryPossibleZipName( baseName + "res.zip", zipFileName )
|
|
|
|
|| File::tryPossibleZipName( baseName + "RES.ZIP", zipFileName )
|
2023-04-28 16:09:45 +00:00
|
|
|
|| File::tryPossibleZipName( baseName + "res" + Utils::Fs::separator() + "res.zip", zipFileName ) )
|
2013-03-25 13:13:25 +00:00
|
|
|
dictFiles.push_back( zipFileName );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || indexIsOldOrBad( indexFile ) ) {
|
2009-01-28 20:55:45 +00:00
|
|
|
// Building the index
|
2009-04-14 16:35:47 +00:00
|
|
|
|
|
|
|
File::Class ifoFile( *i, "r" );
|
|
|
|
|
|
|
|
Ifo ifo( ifoFile );
|
|
|
|
|
2013-11-16 18:34:09 +00:00
|
|
|
gdDebug( "Stardict: Building the index for dictionary: %s\n", ifo.bookname.c_str() );
|
2013-09-20 14:25:44 +00:00
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
if ( ifo.idxoffsetbits == 64 )
|
|
|
|
throw ex64BitsNotSupported();
|
|
|
|
|
|
|
|
if ( ifo.dicttype.size() )
|
|
|
|
throw exDicttypeNotSupported();
|
|
|
|
|
|
|
|
if ( synFileName.empty() ) {
|
|
|
|
if ( ifo.synwordcount ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF(
|
|
|
|
"Warning: dictionary has synwordcount specified, but no "
|
|
|
|
"corresponding .syn file was found\n" );
|
2011-06-14 06:55:24 +00:00
|
|
|
ifo.synwordcount = 0; // Pretend it wasn't there
|
|
|
|
}
|
2009-04-14 16:35:47 +00:00
|
|
|
}
|
|
|
|
else if ( !ifo.synwordcount ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "Warning: ignoring .syn file %s, since there's no synwordcount in .ifo specified\n",
|
|
|
|
synFileName.c_str() );
|
2009-04-14 16:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "bookname = %s\n", ifo.bookname.c_str() );
|
|
|
|
GD_DPRINTF( "wordcount = %u\n", ifo.wordcount );
|
2009-04-14 16:35:47 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
initializing.indexingDictionary( ifo.bookname );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
idx.write( ifo.bookname.data(), ifo.bookname.size() );
|
|
|
|
idx.write( ifo.sametypesequence.data(), ifo.sametypesequence.size() );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
IndexedWords indexedWords;
|
|
|
|
|
|
|
|
ChunkedStorage::Writer chunks( idx );
|
|
|
|
|
|
|
|
// Load indices
|
|
|
|
if ( !ifo.synwordcount )
|
2017-10-25 14:37:39 +00:00
|
|
|
handleIdxSynFile( idxFileName,
|
|
|
|
indexedWords,
|
|
|
|
chunks,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
!maxHeadwordsToExpand || ifo.wordcount < maxHeadwordsToExpand );
|
2009-01-28 20:55:45 +00:00
|
|
|
else {
|
|
|
|
vector< uint32_t > articleOffsets;
|
|
|
|
|
|
|
|
articleOffsets.reserve( ifo.wordcount );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
handleIdxSynFile( idxFileName,
|
|
|
|
indexedWords,
|
|
|
|
chunks,
|
|
|
|
&articleOffsets,
|
2017-10-25 14:37:39 +00:00
|
|
|
false,
|
|
|
|
!maxHeadwordsToExpand || ( ifo.wordcount + ifo.synwordcount ) < maxHeadwordsToExpand );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
handleIdxSynFile( synFileName,
|
|
|
|
indexedWords,
|
|
|
|
chunks,
|
|
|
|
&articleOffsets,
|
2017-10-25 14:37:39 +00:00
|
|
|
true,
|
|
|
|
!maxHeadwordsToExpand || ( ifo.wordcount + ifo.synwordcount ) < maxHeadwordsToExpand );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finish with the chunks
|
|
|
|
|
|
|
|
idxHeader.chunksOffset = chunks.finish();
|
|
|
|
|
|
|
|
// Build index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( indexedWords, idx );
|
|
|
|
|
|
|
|
idxHeader.indexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.indexRootOffset = idxInfo.rootOffset;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
idxHeader.wordCount = ifo.wordcount;
|
|
|
|
idxHeader.synWordCount = ifo.synwordcount;
|
|
|
|
idxHeader.bookNameSize = ifo.bookname.size();
|
|
|
|
idxHeader.sameTypeSequenceSize = ifo.sametypesequence.size();
|
|
|
|
|
2009-05-05 21:51:21 +00:00
|
|
|
// read languages
|
2009-04-23 19:57:39 +00:00
|
|
|
QPair< quint32, quint32 > langs = LangCoder::findIdsForFilename( QString::fromStdString( dictFileName ) );
|
2009-05-05 21:51:21 +00:00
|
|
|
|
|
|
|
// if no languages found, try dictionary's name
|
|
|
|
if ( langs.first == 0 || langs.second == 0 ) {
|
2009-05-05 21:57:38 +00:00
|
|
|
langs = LangCoder::findIdsForFilename( QString::fromStdString( ifo.bookname ) );
|
2009-05-05 21:51:21 +00:00
|
|
|
}
|
|
|
|
|
2009-05-05 22:45:02 +00:00
|
|
|
idxHeader.langFrom = langs.first;
|
|
|
|
idxHeader.langTo = langs.second;
|
|
|
|
|
2013-03-25 13:13:25 +00:00
|
|
|
// If there was a zip file, index it too
|
|
|
|
|
|
|
|
if ( zipFileName.size() ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "Indexing zip file\n" );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
idxHeader.hasZipFile = 1;
|
|
|
|
|
|
|
|
IndexedWords zipFileNames;
|
|
|
|
IndexedZip zipFile;
|
2023-04-13 10:08:32 +00:00
|
|
|
if ( zipFile.openZipFile( QDir::fromNativeSeparators( zipFileName.c_str() ) ) )
|
|
|
|
zipFile.indexFile( zipFileNames );
|
2013-03-25 13:13:25 +00:00
|
|
|
|
|
|
|
if ( !zipFileNames.empty() ) {
|
|
|
|
// Build the resulting zip file index
|
|
|
|
|
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( zipFileNames, idx );
|
|
|
|
|
|
|
|
idxHeader.zipIndexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.zipIndexRootOffset = idxInfo.rootOffset;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Bad zip file -- no index (though the mark that we have one
|
|
|
|
// remains)
|
|
|
|
idxHeader.zipIndexBtreeMaxElements = 0;
|
|
|
|
idxHeader.zipIndexRootOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
idxHeader.hasZipFile = 0;
|
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
2009-05-05 22:45:02 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
dictionaries.push_back( std::make_shared< StardictDictionary >( dictId, indexFile, dictFiles ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
2014-04-25 13:13:56 +00:00
|
|
|
gdWarning( "Stardict dictionary initializing failed: %s, error: %s\n", i->c_str(), e.what() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Stardict
|