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 "dsl.hh"
|
|
|
|
#include "dsl_details.hh"
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "chunkedstorage.hh"
|
|
|
|
#include "dictzip.h"
|
|
|
|
#include "htmlescape.hh"
|
|
|
|
#include "iconv.hh"
|
|
|
|
#include "filetype.hh"
|
|
|
|
#include "fsencoding.hh"
|
2009-04-10 21:07:03 +00:00
|
|
|
#include "audiolink.hh"
|
2009-04-22 21:37:32 +00:00
|
|
|
#include "langcoder.hh"
|
2009-05-10 14:07:05 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-10-25 22:49:24 +00:00
|
|
|
#include "zipfile.hh"
|
|
|
|
#include "indexedzip.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2014-02-11 14:02:00 +00:00
|
|
|
#include "tiff.hh"
|
2014-04-16 16:18:28 +00:00
|
|
|
#include "fulltextsearch.hh"
|
|
|
|
#include "ftshelpers.hh"
|
|
|
|
#include "language.hh"
|
2009-04-22 21:37:32 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
2009-04-29 23:18:26 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stub_msvc.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
#include <QSemaphore>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QAtomicInt>
|
2009-04-12 11:27:43 +00:00
|
|
|
#include <QUrl>
|
2009-03-28 16:06:00 +00:00
|
|
|
|
2009-04-21 22:01:01 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QPainter>
|
2014-04-16 16:18:28 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QRegExp>
|
2009-04-21 22:01:01 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// For TIFF conversion
|
|
|
|
#include <QImage>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QBuffer>
|
|
|
|
|
2013-05-25 12:07:49 +00:00
|
|
|
// For SVG handling
|
|
|
|
#include <QtSvg/QSvgRenderer>
|
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
#include "qt4x5.hh"
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
namespace Dsl {
|
|
|
|
|
|
|
|
using namespace Details;
|
|
|
|
|
|
|
|
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;
|
|
|
|
using gd::wchar;
|
2009-01-28 20:55:45 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::list;
|
|
|
|
|
|
|
|
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_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
|
2014-04-16 16:18:28 +00:00
|
|
|
DEF_EX( exUserAbort, "User abort", 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
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Signature = 0x584c5344, // DSLX on little-endian, XLSD on big-endian
|
2014-04-24 18:56:47 +00:00
|
|
|
CurrentFormatVersion = 21 + BtreeIndexing::FormatVersion + Folding::Version,
|
2014-11-22 14:22:04 +00:00
|
|
|
CurrentZipSupportVersion = 2,
|
|
|
|
CurrentFtsIndexVersion = 1
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, DSLX
|
|
|
|
uint32_t formatVersion; // File format version (CurrentFormatVersion)
|
2009-10-25 22:49:24 +00:00
|
|
|
uint32_t zipSupportVersion; // Zip support version -- narrows down reindexing
|
|
|
|
// when it changes only for dictionaries with the
|
|
|
|
// zip files
|
2009-01-28 20:55:45 +00:00
|
|
|
int dslEncoding; // Which encoding is used for the file indexed
|
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
|
|
|
uint32_t hasAbrv; // Non-zero means file has abrvs at abrvAddress
|
|
|
|
uint32_t abrvAddress; // Address of abrv map in the chunked storage
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
2009-04-20 23:20:53 +00:00
|
|
|
uint32_t articleCount; // Number of articles this dictionary has
|
|
|
|
uint32_t wordCount; // Number of headwords this dictionary has
|
2009-04-22 21:37:32 +00:00
|
|
|
uint32_t langFrom; // Source language
|
|
|
|
uint32_t langTo; // Target language
|
2009-10-25 22:49:24 +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-04-29 23:18:26 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-11-22 13:03:07 +00:00
|
|
|
struct InsidedCard
|
|
|
|
{
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t size;
|
2014-04-24 13:56:38 +00:00
|
|
|
QVector< wstring > headwords;
|
|
|
|
InsidedCard( uint32_t _offset, uint32_t _size, QVector< wstring > const & words ) :
|
|
|
|
offset( _offset ), size( _size ), headwords( words )
|
2012-11-22 13:03:07 +00:00
|
|
|
{}
|
|
|
|
InsidedCard( InsidedCard const & e ) :
|
2014-04-24 13:56:38 +00:00
|
|
|
offset( e.offset ), size( e.size ), headwords( e.headwords )
|
2012-11-22 13:03:07 +00:00
|
|
|
{}
|
|
|
|
InsidedCard() {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
bool indexIsOldOrBad( string const & indexFile, bool hasZipFile )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != Signature ||
|
2009-10-25 22:49:24 +00:00
|
|
|
header.formatVersion != CurrentFormatVersion ||
|
|
|
|
(bool) header.hasZipFile != hasZipFile ||
|
|
|
|
( hasZipFile && header.zipSupportVersion != CurrentZipSupportVersion );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DslDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
Mutex idxMutex;
|
2009-01-28 20:55:45 +00:00
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
2009-05-17 13:35:19 +00:00
|
|
|
sptr< ChunkedStorage::Reader > chunks;
|
2009-01-28 20:55:45 +00:00
|
|
|
string dictionaryName;
|
|
|
|
map< string, string > abrv;
|
2009-04-16 11:33:12 +00:00
|
|
|
Mutex dzMutex;
|
2009-01-28 20:55:45 +00:00
|
|
|
dictData * dz;
|
2009-03-28 17:16:12 +00:00
|
|
|
Mutex resourceZipMutex;
|
2009-10-25 22:49:24 +00:00
|
|
|
IndexedZip resourceZip;
|
|
|
|
BtreeIndex resourceZipIndex;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
QAtomicInt deferredInitDone;
|
|
|
|
Mutex deferredInitMutex;
|
|
|
|
bool deferredInitRunnableStarted;
|
|
|
|
QSemaphore deferredInitRunnableExited;
|
|
|
|
|
|
|
|
string initError;
|
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
int optionalPartNom;
|
|
|
|
quint8 articleNom;
|
2012-12-07 11:59:29 +00:00
|
|
|
int maxPictureWidth;
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
DslDictionary( string const & id, string const & indexFile,
|
2012-12-07 11:59:29 +00:00
|
|
|
vector< string > const & dictionaryFiles,
|
|
|
|
int maxPictureWidth_ );
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
virtual void deferredInit();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
~DslDictionary();
|
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return dictionaryName; }
|
|
|
|
|
|
|
|
virtual map< Dictionary::Property, string > getProperties() throw()
|
|
|
|
{ return map< Dictionary::Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
2009-04-20 23:20:53 +00:00
|
|
|
{ return idxHeader.articleCount; }
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
2009-04-20 23:20:53 +00:00
|
|
|
{ return idxHeader.wordCount; }
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-22 21:37:32 +00:00
|
|
|
inline virtual quint32 getLangFrom() const
|
|
|
|
{ return idxHeader.langFrom; }
|
|
|
|
|
|
|
|
inline virtual quint32 getLangTo() const
|
|
|
|
{ return idxHeader.langTo; }
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
#if 0
|
2009-01-28 20:55:45 +00:00
|
|
|
virtual vector< wstring > findHeadwordsForSynonym( wstring const & )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return vector< wstring >();
|
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getArticle( wstring const &,
|
2009-05-29 19:48:50 +00:00
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-03-26 19:00:08 +00:00
|
|
|
throw( std::exception );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getResource( string const & name )
|
|
|
|
throw( std::exception );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getSearchResults( QString const & searchString,
|
|
|
|
int searchMode, bool matchCase,
|
|
|
|
int distanceBetweenWords,
|
|
|
|
int maxResults );
|
2012-09-07 16:17:44 +00:00
|
|
|
virtual QString const& getDescription();
|
|
|
|
|
2012-11-29 17:12:54 +00:00
|
|
|
virtual QString getMainFilename();
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
virtual void getArticleText( uint32_t articleAddress, QString & headword, QString & text );
|
|
|
|
|
|
|
|
virtual void makeFTSIndex(QAtomicInt & isCancelled, bool firstIteration );
|
|
|
|
|
2014-04-17 14:31:51 +00:00
|
|
|
virtual void setFTSParameters( Config::FullTextSearch const & fts )
|
|
|
|
{
|
|
|
|
if( ensureInitDone().size() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
can_FTS = fts.enabled
|
|
|
|
&& !fts.disabledTypes.contains( "DSL", Qt::CaseInsensitive )
|
|
|
|
&& ( fts.maxDictionarySize == 0 || getArticleCount() <= fts.maxDictionarySize );
|
|
|
|
}
|
|
|
|
|
2014-11-22 14:22:04 +00:00
|
|
|
virtual uint32_t getFtsIndexVersion()
|
|
|
|
{ return CurrentFtsIndexVersion; }
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void loadIcon() throw();
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
private:
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
virtual string const & ensureInitDone();
|
|
|
|
void doDeferredInit();
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
/// Loads the article. Does not process the DSL language.
|
|
|
|
void loadArticle( uint32_t address,
|
2009-05-10 15:44:21 +00:00
|
|
|
wstring const & requestedHeadwordFolded,
|
|
|
|
wstring & tildeValue,
|
|
|
|
wstring & displayedHeadword,
|
|
|
|
unsigned & headwordIndex,
|
2009-01-28 20:55:45 +00:00
|
|
|
wstring & articleText );
|
|
|
|
|
|
|
|
/// Converts DSL language to an Html.
|
2014-04-18 12:34:44 +00:00
|
|
|
string dslToHtml( wstring const &, wstring const & headword = wstring() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// Parts of dslToHtml()
|
|
|
|
string nodeToHtml( ArticleDom::Node const & );
|
|
|
|
string processNodeChildren( ArticleDom::Node const & node );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
bool hasHiddenZones() /// Return true if article has hidden zones
|
|
|
|
{ return optionalPartNom != 0; }
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
friend class DslArticleRequest;
|
2009-05-17 13:35:19 +00:00
|
|
|
friend class DslResourceRequest;
|
2014-04-16 16:18:28 +00:00
|
|
|
friend class DslFTSResultsRequest;
|
2009-05-17 13:35:19 +00:00
|
|
|
friend class DslDeferredInitRunnable;
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DslDictionary::DslDictionary( string const & id,
|
|
|
|
string const & indexFile,
|
2012-12-07 11:59:29 +00:00
|
|
|
vector< string > const & dictionaryFiles,
|
|
|
|
int maxPictureWidth_ ):
|
2009-01-28 20:55:45 +00:00
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
2009-05-17 13:35:19 +00:00
|
|
|
dz( 0 ),
|
2012-09-16 10:19:47 +00:00
|
|
|
deferredInitRunnableStarted( false ),
|
|
|
|
optionalPartNom( 0 ),
|
2012-12-07 11:59:29 +00:00
|
|
|
articleNom( 0 ),
|
|
|
|
maxPictureWidth( maxPictureWidth_ )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
can_FTS = true;
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
ftsIdxName = indexFile + "_FTS";
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
if( !Dictionary::needToRebuildIndex( dictionaryFiles, ftsIdxName )
|
2014-11-22 14:22:04 +00:00
|
|
|
&& !FtsHelpers::ftsIndexIsOldOrBad( ftsIdxName, this ) )
|
2014-05-08 12:38:00 +00:00
|
|
|
FTS_index_completed.ref();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Read the dictionary name
|
|
|
|
|
|
|
|
idx.seek( sizeof( idxHeader ) );
|
|
|
|
|
|
|
|
vector< char > dName( idx.read< uint32_t >() );
|
|
|
|
idx.read( &dName.front(), dName.size() );
|
|
|
|
dictionaryName = string( &dName.front(), dName.size() );
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
// Everything else would be done in deferred init
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
DslDictionary::~DslDictionary()
|
|
|
|
{
|
|
|
|
Mutex::Lock _( deferredInitMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
// Wait for init runnable to complete if it was ever started
|
|
|
|
if ( deferredInitRunnableStarted )
|
|
|
|
deferredInitRunnableExited.acquire();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
if ( dz )
|
|
|
|
dict_data_close( dz );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
//////// DslDictionary::deferredInit()
|
|
|
|
|
|
|
|
class DslDeferredInitRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
DslDictionary & dictionary;
|
|
|
|
QSemaphore & hasExited;
|
|
|
|
|
|
|
|
public:
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
DslDeferredInitRunnable( DslDictionary & dictionary_,
|
|
|
|
QSemaphore & hasExited_ ):
|
|
|
|
dictionary( dictionary_ ), hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DslDeferredInitRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
virtual void run()
|
|
|
|
{
|
|
|
|
dictionary.doDeferredInit();
|
|
|
|
}
|
|
|
|
};
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
void DslDictionary::deferredInit()
|
|
|
|
{
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( !Qt4x5::AtomicInt::loadAcquire( deferredInitDone ) )
|
2009-05-17 13:35:19 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _( deferredInitMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( Qt4x5::AtomicInt::loadAcquire( deferredInitDone ) )
|
2009-05-17 13:35:19 +00:00
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
if ( !deferredInitRunnableStarted )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new DslDeferredInitRunnable( *this, deferredInitRunnableExited ),
|
|
|
|
-1000 );
|
|
|
|
deferredInitRunnableStarted = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-17 13:35:19 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
string const & DslDictionary::ensureInitDone()
|
|
|
|
{
|
|
|
|
// Simple, really.
|
|
|
|
doDeferredInit();
|
2009-03-28 17:16:12 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
return initError;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
void DslDictionary::doDeferredInit()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( !Qt4x5::AtomicInt::loadAcquire( deferredInitDone ) )
|
2009-05-17 13:35:19 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _( deferredInitMutex );
|
2009-03-28 17:16:12 +00:00
|
|
|
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( Qt4x5::AtomicInt::loadAcquire( deferredInitDone ) )
|
2009-05-17 13:35:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Do deferred init
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Don't lock index file - no one should be working with it until
|
|
|
|
// the init is complete.
|
|
|
|
//Mutex::Lock _( idxMutex );
|
|
|
|
|
|
|
|
chunks = new ChunkedStorage::Reader( idx, idxHeader.chunksOffset );
|
|
|
|
|
2013-08-04 19:19:57 +00:00
|
|
|
// Open the .dsl file
|
2009-05-17 13:35:19 +00:00
|
|
|
|
2014-04-24 18:50:47 +00:00
|
|
|
DZ_ERRORS error;
|
|
|
|
dz = dict_data_open( getDictionaryFilenames()[ 0 ].c_str(), &error, 0 );
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
if ( !dz )
|
2014-04-24 18:50:47 +00:00
|
|
|
throw exDictzipError( string( dz_error_str( error ) )
|
|
|
|
+ "(" + getDictionaryFilenames()[ 0 ] + ")" );
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
// Read the abrv, if any
|
|
|
|
|
|
|
|
if ( idxHeader.hasAbrv )
|
|
|
|
{
|
|
|
|
vector< char > chunk;
|
|
|
|
|
|
|
|
char * abrvBlock = chunks->getBlock( idxHeader.abrvAddress, chunk );
|
|
|
|
|
|
|
|
uint32_t total;
|
|
|
|
memcpy( &total, abrvBlock, sizeof( uint32_t ) );
|
|
|
|
abrvBlock += sizeof( uint32_t );
|
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "Loading %u abbrv\n", total );
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
while( total-- )
|
|
|
|
{
|
|
|
|
uint32_t keySz;
|
|
|
|
memcpy( &keySz, abrvBlock, sizeof( uint32_t ) );
|
|
|
|
abrvBlock += sizeof( uint32_t );
|
|
|
|
|
|
|
|
char * key = abrvBlock;
|
|
|
|
|
|
|
|
abrvBlock += keySz;
|
|
|
|
|
|
|
|
uint32_t valueSz;
|
|
|
|
memcpy( &valueSz, abrvBlock, sizeof( uint32_t ) );
|
|
|
|
abrvBlock += sizeof( uint32_t );
|
|
|
|
|
|
|
|
abrv[ string( key, keySz ) ] = string( abrvBlock, valueSz );
|
|
|
|
|
|
|
|
abrvBlock += valueSz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the index
|
|
|
|
|
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements,
|
|
|
|
idxHeader.indexRootOffset ),
|
|
|
|
idx, idxMutex );
|
|
|
|
|
|
|
|
// Open a resource zip file, if there's one
|
2009-10-09 13:58:58 +00:00
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
if ( idxHeader.hasZipFile &&
|
|
|
|
( idxHeader.zipIndexBtreeMaxElements ||
|
|
|
|
idxHeader.zipIndexRootOffset ) )
|
2009-10-09 13:58:58 +00:00
|
|
|
{
|
2009-10-25 22:49:24 +00:00
|
|
|
resourceZip.openIndex( IndexInfo( idxHeader.zipIndexBtreeMaxElements,
|
|
|
|
idxHeader.zipIndexRootOffset ),
|
|
|
|
idx, idxMutex );
|
|
|
|
|
|
|
|
QString zipName = QDir::fromNativeSeparators(
|
2011-09-09 12:05:28 +00:00
|
|
|
FsEncoding::decode( getDictionaryFilenames().back().c_str() ) );
|
2009-10-25 22:49:24 +00:00
|
|
|
|
|
|
|
if ( zipName.endsWith( ".zip", Qt::CaseInsensitive ) ) // Sanity check
|
|
|
|
resourceZip.openZipFile( zipName );
|
2009-10-09 13:58:58 +00:00
|
|
|
}
|
2009-05-17 13:35:19 +00:00
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
initError = e.what();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
initError = "Unknown error";
|
|
|
|
}
|
|
|
|
|
|
|
|
deferredInitDone.ref();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
void DslDictionary::loadIcon() throw()
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString fileName =
|
2011-09-09 12:05:28 +00:00
|
|
|
QDir::fromNativeSeparators( FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() ) );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
// Remove the extension
|
|
|
|
if ( fileName.endsWith( ".dsl.dz", Qt::CaseInsensitive ) )
|
|
|
|
fileName.chop( 6 );
|
|
|
|
else
|
|
|
|
fileName.chop( 3 );
|
2009-04-21 22:01:01 +00:00
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
if ( !loadIconFromFile( fileName ) )
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
|
|
|
// Load failed -- use default icons
|
|
|
|
dictionaryIcon = QIcon(":/icons/icon32_dsl.png");
|
|
|
|
dictionaryNativeIcon = QIcon(":/icons/icon_dsl_native.png");
|
2009-04-21 22:01:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 17:50:03 +00:00
|
|
|
dictionaryIconLoaded = true;
|
2009-04-21 22:01:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
/// Determines whether or not this char is treated as whitespace for dsl
|
|
|
|
/// parsing or not. We can't rely on any Unicode standards here, since the
|
|
|
|
/// only standard that matters here is the original Dsl compiler's insides.
|
|
|
|
/// Some dictionaries, for instance, are known to specifically use a non-
|
|
|
|
/// breakable space (0xa0) to indicate that a headword begins with a space,
|
|
|
|
/// so nbsp is not a whitespace character for Dsl compiler.
|
|
|
|
/// For now we have only space and tab, since those are most likely the only
|
|
|
|
/// ones recognized as spaces by that compiler.
|
2009-04-18 17:20:12 +00:00
|
|
|
bool isDslWs( wchar ch )
|
2009-04-17 22:29:06 +00:00
|
|
|
{
|
|
|
|
switch( ch )
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
void DslDictionary::loadArticle( uint32_t address,
|
2009-05-10 15:44:21 +00:00
|
|
|
wstring const & requestedHeadwordFolded,
|
|
|
|
wstring & tildeValue,
|
|
|
|
wstring & displayedHeadword,
|
|
|
|
unsigned & headwordIndex,
|
2009-01-28 20:55:45 +00:00
|
|
|
wstring & articleText )
|
|
|
|
{
|
|
|
|
wstring articleData;
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< char > chunk;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
char * articleProps;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( idxMutex );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
articleProps = chunks->getBlock( address, chunk );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
uint32_t articleOffset, articleSize;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
memcpy( &articleOffset, articleProps, sizeof( articleOffset ) );
|
|
|
|
memcpy( &articleSize, articleProps + sizeof( articleOffset ),
|
|
|
|
sizeof( articleSize ) );
|
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "offset = %x\n", articleOffset );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
char * articleBody;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dzMutex );
|
|
|
|
|
|
|
|
articleBody = dict_data_read_( dz, articleOffset, articleSize, 0, 0 );
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !articleBody )
|
|
|
|
{
|
2013-03-15 12:27:32 +00:00
|
|
|
// throw exCantReadFile( getDictionaryFilenames()[ 0 ] );
|
|
|
|
articleData = GD_NATIVE_TO_WS( L"\n\r\t" ) + gd::toWString( QString( "DICTZIP error: " ) + dict_error_str( dz ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2013-03-15 12:27:32 +00:00
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-03-15 12:27:32 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
articleData =
|
|
|
|
DslIconv::toWstring(
|
|
|
|
DslIconv::getEncodingNameFor( DslEncoding( idxHeader.dslEncoding ) ),
|
|
|
|
articleBody, articleSize );
|
|
|
|
free( articleBody );
|
2013-07-07 10:48:37 +00:00
|
|
|
|
|
|
|
// Strip DSL comments
|
|
|
|
bool b = false;
|
|
|
|
stripComments( articleData, b );
|
2013-03-15 12:27:32 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
free( articleBody );
|
|
|
|
throw;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
bool hadFirstHeadword = false;
|
|
|
|
bool foundDisplayedHeadword = false;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
// Check is we retrieve insided card
|
|
|
|
bool insidedCard = isDslWs( articleData.at( 0 ) );
|
|
|
|
|
2009-05-10 16:28:20 +00:00
|
|
|
wstring tildeValueWithUnsorted; // This one has unsorted parts left
|
2009-05-10 15:44:21 +00:00
|
|
|
for( headwordIndex = 0; ; )
|
|
|
|
{
|
|
|
|
size_t begin = pos;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
pos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ), begin );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
if ( pos == wstring::npos )
|
|
|
|
pos = articleData.size();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
if ( !foundDisplayedHeadword )
|
|
|
|
{
|
|
|
|
// Process the headword
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 16:28:20 +00:00
|
|
|
wstring rawHeadword = wstring( articleData, begin, pos - begin );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if( insidedCard && !rawHeadword.empty() && isDslWs( rawHeadword[ 0 ] ) )
|
2009-05-10 15:44:21 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
// Headword of the insided card
|
|
|
|
wstring::size_type hpos = rawHeadword.find( L'@' );
|
|
|
|
if( hpos != string::npos )
|
|
|
|
{
|
|
|
|
wstring head = Folding::trimWhitespace( rawHeadword.substr( hpos + 1 ) );
|
|
|
|
hpos = head.find( L'~' );
|
|
|
|
while( hpos != string::npos )
|
|
|
|
{
|
|
|
|
if( hpos == 0 || head[ hpos ] != L'\\' )
|
|
|
|
break;
|
|
|
|
hpos = head.find( L'~', hpos + 1 );
|
|
|
|
}
|
|
|
|
if( hpos == string::npos )
|
|
|
|
rawHeadword = head;
|
|
|
|
else
|
|
|
|
rawHeadword.clear();
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if( !rawHeadword.empty() )
|
|
|
|
{
|
|
|
|
if ( !hadFirstHeadword )
|
|
|
|
{
|
|
|
|
// We need our tilde expansion value
|
|
|
|
tildeValue = rawHeadword;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
list< wstring > lst;
|
2009-05-10 16:28:20 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
expandOptionalParts( tildeValue, &lst );
|
2009-05-10 16:28:20 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( lst.size() ) // Should always be
|
|
|
|
tildeValue = lst.front();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
tildeValueWithUnsorted = tildeValue;
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
processUnsortedParts( tildeValue, false );
|
|
|
|
}
|
|
|
|
wstring str = rawHeadword;
|
2009-05-10 16:28:20 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( hadFirstHeadword )
|
|
|
|
expandTildes( str, tildeValueWithUnsorted );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
processUnsortedParts( str, true );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
str = Folding::applySimpleCaseOnly( str );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
list< wstring > lst;
|
|
|
|
expandOptionalParts( str, &lst );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
// Does one of the results match the requested word? If so, we'd choose
|
|
|
|
// it as our headword.
|
2009-05-10 16:28:20 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
for( list< wstring >::iterator i = lst.begin(); i != lst.end(); ++i )
|
2009-05-10 15:44:21 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
unescapeDsl( *i );
|
|
|
|
normalizeHeadword( *i );
|
2009-05-10 16:28:20 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( Folding::trimWhitespace( *i ) == requestedHeadwordFolded )
|
|
|
|
{
|
|
|
|
// Found it. Now we should make a displayed headword for it.
|
|
|
|
if ( hadFirstHeadword )
|
|
|
|
expandTildes( rawHeadword, tildeValueWithUnsorted );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
processUnsortedParts( rawHeadword, false );
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
displayedHeadword = rawHeadword;
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2012-11-27 12:50:27 +00:00
|
|
|
foundDisplayedHeadword = true;
|
2014-04-24 13:56:38 +00:00
|
|
|
break;
|
2012-11-27 12:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( !foundDisplayedHeadword )
|
|
|
|
{
|
|
|
|
++headwordIndex;
|
|
|
|
hadFirstHeadword = true;
|
|
|
|
}
|
2009-05-10 15:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
if ( pos == articleData.size() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Skip \n\r
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( articleData[ pos ] == '\r' )
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if ( pos != articleData.size() )
|
|
|
|
{
|
|
|
|
if ( articleData[ pos ] == '\n' )
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( pos == articleData.size() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
// Ok, it's end of article
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
2009-05-10 15:44:21 +00:00
|
|
|
}
|
2014-04-24 13:56:38 +00:00
|
|
|
if( isDslWs( articleData[ pos ] ) )
|
|
|
|
{
|
|
|
|
// Check for begin article text
|
|
|
|
if( insidedCard )
|
|
|
|
{
|
|
|
|
// Check for next insided headword
|
|
|
|
wstring::size_type hpos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ), pos );
|
|
|
|
if ( hpos == wstring::npos )
|
|
|
|
hpos = articleData.size();
|
|
|
|
|
|
|
|
wstring str = wstring( articleData, pos, hpos - pos );
|
|
|
|
|
|
|
|
hpos = str.find( L'@');
|
|
|
|
if( hpos == wstring::npos || str[ hpos - 1 ] == L'\\' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-05-10 15:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !foundDisplayedHeadword )
|
|
|
|
{
|
|
|
|
// This is strange. Anyway, use tilde expansion value, it's better
|
2014-04-24 13:56:38 +00:00
|
|
|
// than nothing (or requestedHeadwordFolded for insided card.
|
|
|
|
if( insidedCard )
|
|
|
|
displayedHeadword = requestedHeadwordFolded;
|
|
|
|
else
|
|
|
|
displayedHeadword = tildeValue;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( pos != articleData.size() )
|
|
|
|
articleText = wstring( articleData, pos );
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
articleText.clear();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 12:34:44 +00:00
|
|
|
string DslDictionary::dslToHtml( wstring const & str, wstring const & headword )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-05-13 10:34:53 +00:00
|
|
|
// Normalize the string
|
2013-07-06 18:18:43 +00:00
|
|
|
wstring normalizedStr = gd::normalize( str );
|
2009-05-13 10:34:53 +00:00
|
|
|
|
2014-04-18 12:34:44 +00:00
|
|
|
ArticleDom dom( normalizedStr, getName(), headword );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2012-09-16 10:19:47 +00:00
|
|
|
optionalPartNom = 0;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
string html = processNodeChildren( dom.root );
|
|
|
|
|
2013-10-06 15:24:19 +00:00
|
|
|
return html;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string DslDictionary::processNodeChildren( ArticleDom::Node const & node )
|
|
|
|
{
|
|
|
|
string result;
|
|
|
|
|
|
|
|
for( ArticleDom::Node::const_iterator i = node.begin(); i != node.end();
|
|
|
|
++i )
|
|
|
|
result += nodeToHtml( *i );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
|
|
|
|
{
|
2013-10-06 15:24:19 +00:00
|
|
|
string result;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !node.isTag )
|
2013-10-06 15:24:19 +00:00
|
|
|
{
|
|
|
|
result = Html::escape( Utf8::encode( node.text ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-10-06 15:24:19 +00:00
|
|
|
// Handle all end-of-line
|
|
|
|
|
|
|
|
string::size_type n;
|
|
|
|
|
|
|
|
// Strip all '\r'
|
|
|
|
while( ( n = result.find( '\r' ) ) != string::npos )
|
|
|
|
result.erase( n, 1 );
|
|
|
|
|
|
|
|
// Replace all '\n'
|
|
|
|
while( ( n = result.find( '\n' ) ) != string::npos )
|
|
|
|
result.replace( n, 1, "<p></p>" );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"b" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<b class=\"dsl_b\">" + processNodeChildren( node ) + "</b>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"i" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<i class=\"dsl_i\">" + processNodeChildren( node ) + "</i>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"u" ) )
|
2009-05-10 17:17:21 +00:00
|
|
|
{
|
|
|
|
string nodeText = processNodeChildren( node );
|
|
|
|
|
|
|
|
if ( nodeText.size() && isDslWs( nodeText[ 0 ] ) )
|
|
|
|
result.push_back( ' ' ); // Fix a common problem where in "foo[i] bar[/i]"
|
|
|
|
// the space before "bar" gets underlined.
|
|
|
|
|
|
|
|
result += "<span class=\"dsl_u\">" + nodeText + "</span>";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"c" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
result += "<font color=\"" + ( node.tagAttrs.size() ?
|
|
|
|
Html::escape( Utf8::encode( node.tagAttrs ) ) : string( "c_default_color" ) )
|
|
|
|
+ "\">" + processNodeChildren( node ) + "</font>";
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"*" ) )
|
2012-09-16 10:19:47 +00:00
|
|
|
{
|
|
|
|
string id = "O" + getId().substr( 0, 7 ) + "_" +
|
|
|
|
QString::number( articleNom ).toStdString() +
|
|
|
|
"_opt_" + QString::number( optionalPartNom++ ).toStdString();
|
2017-06-09 12:38:23 +00:00
|
|
|
result += "<span class=\"dsl_opt\" id=\"" + id + "\">" + processNodeChildren( node ) + "</span>";
|
2012-09-16 10:19:47 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2013-07-03 13:59:32 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"m" ) )
|
2013-07-03 14:45:23 +00:00
|
|
|
result += "<div class=\"dsl_m\">" + processNodeChildren( node ) + "</div>";
|
2013-07-03 13:59:32 +00:00
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( node.tagName.size() == 2 && node.tagName[ 0 ] == L'm' &&
|
|
|
|
iswdigit( node.tagName[ 1 ] ) )
|
|
|
|
result += "<div class=\"dsl_" + Utf8::encode( node.tagName ) + "\">" + processNodeChildren( node ) + "</div>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"trn" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_trn\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"ex" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_ex\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"com" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_com\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
else
|
2014-10-29 14:46:42 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"s" ) || node.tagName == GD_NATIVE_TO_WS( L"video" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
string filename = Utf8::encode( node.renderAsText() );
|
2012-12-07 11:59:29 +00:00
|
|
|
string n =
|
|
|
|
getDictionaryFilenames()[ 0 ] + ".files" +
|
|
|
|
FsEncoding::separator() +
|
|
|
|
FsEncoding::encode( filename );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( Filetype::isNameOfSound( filename ) )
|
|
|
|
{
|
|
|
|
// If we have the file here, do the exact reference to this dictionary.
|
|
|
|
// Otherwise, make a global 'search' one.
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
bool search =
|
2012-12-07 11:59:29 +00:00
|
|
|
!File::exists( n ) && !File::exists( FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
|
|
|
|
FsEncoding::separator() +
|
|
|
|
FsEncoding::encode( filename ) ) &&
|
2009-10-25 22:49:24 +00:00
|
|
|
( !resourceZip.isOpen() ||
|
|
|
|
!resourceZip.hasFile( Utf8::decode( filename ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2009-04-12 11:27:43 +00:00
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( search ? "search" : getId().c_str() ) );
|
2013-05-31 04:20:25 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
|
2009-04-12 11:27:43 +00:00
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2009-05-14 20:38:17 +00:00
|
|
|
result += addAudioLink( ref, getId() );
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_s_wav\"><a href=" + ref
|
|
|
|
+ "><img src=\"qrcx://localhost/icons/playsound.png\" border=\"0\" align=\"absmiddle\" alt=\"Play\"/></a></span>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( Filetype::isNameOfPicture( filename ) )
|
|
|
|
{
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2009-04-12 11:27:43 +00:00
|
|
|
url.setScheme( "bres" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
2013-05-31 04:20:25 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
|
2009-04-12 11:27:43 +00:00
|
|
|
|
2012-12-07 11:59:29 +00:00
|
|
|
vector< char > imgdata;
|
|
|
|
bool resize = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File::loadFromFile( n, imgdata );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
2012-12-07 17:29:10 +00:00
|
|
|
try
|
2012-12-07 11:59:29 +00:00
|
|
|
{
|
2012-12-07 17:29:10 +00:00
|
|
|
n = FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
|
|
|
|
FsEncoding::separator() +
|
|
|
|
FsEncoding::encode( filename );
|
|
|
|
File::loadFromFile( n, imgdata );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
|
|
|
// Try reading from zip file
|
|
|
|
if ( resourceZip.isOpen() )
|
|
|
|
{
|
|
|
|
Mutex::Lock _( resourceZipMutex );
|
|
|
|
resourceZip.loadFile( Utf8::decode( filename ), imgdata );
|
|
|
|
}
|
2012-12-07 11:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !imgdata.empty() )
|
|
|
|
{
|
2013-05-25 12:07:49 +00:00
|
|
|
if( Filetype::isNameOfSvg( filename ) )
|
|
|
|
{
|
|
|
|
// We don't need to render svg file now
|
|
|
|
|
|
|
|
QSvgRenderer svg;
|
|
|
|
svg.load( QByteArray::fromRawData( imgdata.data(), imgdata.size() ) );
|
|
|
|
if( svg.isValid() )
|
|
|
|
{
|
|
|
|
QSize imgsize = svg.defaultSize();
|
|
|
|
resize = maxPictureWidth > 0
|
|
|
|
&& imgsize.width() > maxPictureWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QImage img = QImage::fromData( (unsigned char *) &imgdata.front(),
|
|
|
|
imgdata.size() );
|
2014-02-11 17:27:24 +00:00
|
|
|
|
|
|
|
#ifdef MAKE_EXTRA_TIFF_HANDLER
|
|
|
|
if( img.isNull() && Filetype::isNameOfTiff( filename ) )
|
|
|
|
GdTiff::tiffToQImage( &imgdata.front(), imgdata.size(), img );
|
|
|
|
#endif
|
|
|
|
|
2013-05-25 12:07:49 +00:00
|
|
|
resize = maxPictureWidth > 0
|
|
|
|
&& img.width() > maxPictureWidth;
|
|
|
|
}
|
2012-12-07 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( resize )
|
|
|
|
{
|
|
|
|
string link( url.toEncoded().data() );
|
|
|
|
link.replace( 0, 4, "gdpicture" );
|
|
|
|
result += string( "<a href=\"" ) + link + "\">"
|
|
|
|
+ "<img src=\"" + url.toEncoded().data()
|
|
|
|
+ "\" alt=\"" + Html::escape( filename ) + "\""
|
2012-12-07 16:08:09 +00:00
|
|
|
+ "width=\"" + QString::number( maxPictureWidth).toStdString() + "\"/>"
|
2012-12-07 11:59:29 +00:00
|
|
|
+ "</a>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result += string( "<img src=\"" ) + url.toEncoded().data()
|
|
|
|
+ "\" alt=\"" + Html::escape( filename ) + "\"/>";
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
2013-06-22 16:36:25 +00:00
|
|
|
if ( Filetype::isNameOfVideo( filename ) ) {
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "gdvideo" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
2013-07-05 10:10:11 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
|
2013-06-22 16:36:25 +00:00
|
|
|
|
|
|
|
result += string( "<a class=\"dsl_s dsl_video\" href=\"" ) + url.toEncoded().data() + "\">"
|
|
|
|
+ "<span class=\"img\"></span>"
|
|
|
|
+ "<span class=\"filename\">" + processNodeChildren( node ) + "</span>" + "</a>";
|
|
|
|
}
|
|
|
|
else
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Unknown file type, downgrade to a hyperlink
|
2009-04-12 11:27:43 +00:00
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2009-04-12 11:27:43 +00:00
|
|
|
url.setScheme( "bres" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
2013-05-31 04:20:25 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
|
2009-04-12 11:27:43 +00:00
|
|
|
|
|
|
|
result += string( "<a class=\"dsl_s\" href=\"" ) + url.toEncoded().data()
|
2009-01-28 20:55:45 +00:00
|
|
|
+ "\">" + processNodeChildren( node ) + "</a>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"url" ) )
|
2013-07-27 18:35:10 +00:00
|
|
|
{
|
|
|
|
string link = Html::escape( Utf8::encode( node.renderAsText() ) );
|
|
|
|
if( QUrl::fromEncoded( link.c_str() ).scheme().isEmpty() )
|
|
|
|
link = "http://" + link;
|
2016-06-30 17:20:55 +00:00
|
|
|
|
|
|
|
QUrl url( QString::fromUtf8( link.c_str() ) );
|
|
|
|
if( url.isLocalFile() && url.host().isEmpty() )
|
|
|
|
{
|
|
|
|
// Convert relative links to local files to absolute ones
|
|
|
|
QString name = QFileInfo( getMainFilename() ).absolutePath();
|
|
|
|
name += url.toLocalFile();
|
|
|
|
QFileInfo info( name );
|
|
|
|
if( info.isFile() )
|
|
|
|
{
|
|
|
|
name = info.canonicalFilePath();
|
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( QUrl::fromLocalFile( name ).path() ) );
|
|
|
|
link = string( url.toEncoded().data() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 18:35:10 +00:00
|
|
|
result += "<a class=\"dsl_url\" href=\"" + link +"\">" + processNodeChildren( node ) + "</a>";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"!trs" ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_trs\">" + processNodeChildren( node ) + "</span>";
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"p") )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
result += "<span class=\"dsl_p\"";
|
|
|
|
|
|
|
|
string val = Utf8::encode( node.renderAsText() );
|
|
|
|
|
|
|
|
// If we have such a key, display a title
|
|
|
|
|
|
|
|
map< string, string >::const_iterator i = abrv.find( val );
|
|
|
|
|
|
|
|
if ( i != abrv.end() )
|
2009-05-14 09:36:24 +00:00
|
|
|
{
|
|
|
|
string title;
|
|
|
|
|
2009-05-14 21:47:35 +00:00
|
|
|
if ( Utf8::decode( i->second ).size() < 70 )
|
|
|
|
{
|
2009-05-14 22:00:56 +00:00
|
|
|
// Replace all spaces with non-breakable ones, since that's how
|
|
|
|
// Lingvo shows tooltips
|
2009-05-14 21:47:35 +00:00
|
|
|
title.reserve( i->second.size() );
|
|
|
|
|
|
|
|
for( char const * c = i->second.c_str(); *c; ++c )
|
2016-08-03 14:29:15 +00:00
|
|
|
{
|
2009-05-14 21:47:35 +00:00
|
|
|
if ( *c == ' ' || *c == '\t' )
|
|
|
|
{
|
|
|
|
// u00A0 in utf8
|
|
|
|
title.push_back( 0xC2 );
|
|
|
|
title.push_back( 0xA0 );
|
|
|
|
}
|
2016-08-03 14:29:15 +00:00
|
|
|
else
|
|
|
|
if( *c == '-' ) // Change minus to non-breaking hyphen (uE28091 in utf8)
|
|
|
|
{
|
|
|
|
title.push_back( 0xE2 );
|
|
|
|
title.push_back( 0x80 );
|
|
|
|
title.push_back( 0x91 );
|
|
|
|
}
|
2009-05-14 21:47:35 +00:00
|
|
|
else
|
|
|
|
title.push_back( *c );
|
2016-08-03 14:29:15 +00:00
|
|
|
}
|
2009-05-14 21:47:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
title = i->second;
|
2009-05-14 09:36:24 +00:00
|
|
|
|
|
|
|
result += " title=\"" + Html::escape( title ) + "\"";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
result += ">" + processNodeChildren( node ) + "</span>";
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"'" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2011-06-02 11:30:38 +00:00
|
|
|
// There are two ways to display the stress: by adding an accent sign or via font styles.
|
|
|
|
// We generate two spans, one with accented data and another one without it, so the
|
|
|
|
// user could pick up the best suitable option.
|
|
|
|
string data = processNodeChildren( node );
|
|
|
|
result += "<span class=\"dsl_stress\"><span class=\"dsl_stress_without_accent\">" + data + "</span>"
|
|
|
|
+ "<span class=\"dsl_stress_with_accent\">" + data + Utf8::encode( wstring( 1, 0x301 ) )
|
|
|
|
+ "</span></span>";
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"lang" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2016-12-22 19:58:01 +00:00
|
|
|
result += "<span class=\"dsl_lang\"";
|
|
|
|
if( !node.tagAttrs.empty() )
|
|
|
|
{
|
|
|
|
// Find ISO 639-1 code
|
|
|
|
string langcode;
|
|
|
|
QString attr = gd::toQString( node.tagAttrs );
|
|
|
|
int n = attr.indexOf( "id=" );
|
|
|
|
if( n >= 0 )
|
|
|
|
{
|
|
|
|
int id = attr.mid( n + 3 ).toInt();
|
|
|
|
if( id )
|
|
|
|
langcode = findCodeForDslId( id );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = attr.indexOf( "name=\"" );
|
|
|
|
if( n >= 0 )
|
|
|
|
{
|
|
|
|
int n2 = attr.indexOf( '\"', n + 6 );
|
|
|
|
if( n2 > 0 )
|
|
|
|
{
|
|
|
|
quint32 id = dslLanguageToId( gd::toWString( attr.mid( n + 6, n2 - n - 6 ) ) );
|
|
|
|
langcode = LangCoder::intToCode2( id ).toStdString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !langcode.empty() )
|
|
|
|
result += " lang=\"" + langcode + "\"";
|
|
|
|
}
|
|
|
|
result += ">" + processNodeChildren( node ) + "</span>";
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"ref" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2009-05-10 14:07:05 +00:00
|
|
|
|
|
|
|
url.setScheme( "gdlookup" );
|
|
|
|
url.setHost( "localhost" );
|
2013-05-31 04:20:25 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( gd::toQString( node.renderAsText() ) ) );
|
2012-11-24 20:30:32 +00:00
|
|
|
if( !node.tagAttrs.empty() )
|
|
|
|
{
|
|
|
|
QString attr = gd::toQString( node.tagAttrs ).remove( '\"' );
|
|
|
|
int n = attr.indexOf( '=' );
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
|
|
|
QList< QPair< QString, QString > > query;
|
|
|
|
query.append( QPair< QString, QString >( attr.left( n ), attr.mid( n + 1 ) ) );
|
2013-05-30 13:24:21 +00:00
|
|
|
Qt4x5::Url::setQueryItems( url, query );
|
2012-11-24 20:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-10 14:07:05 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
result += string( "<a class=\"dsl_ref\" href=\"" ) + url.toEncoded().data() +"\">"
|
|
|
|
+ processNodeChildren( node ) + "</a>";
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
else
|
2012-11-26 19:17:58 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"@" ) )
|
|
|
|
{
|
|
|
|
// Special case - insided card header was not parsed
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2012-11-26 19:17:58 +00:00
|
|
|
|
|
|
|
url.setScheme( "gdlookup" );
|
|
|
|
url.setHost( "localhost" );
|
|
|
|
wstring nodeStr = node.renderAsText();
|
2013-06-07 12:58:10 +00:00
|
|
|
normalizeHeadword( nodeStr );
|
2013-06-08 04:27:18 +00:00
|
|
|
url.setPath( Qt4x5::Url::ensureLeadingSlash( gd::toQString( nodeStr ) ) );
|
2012-11-26 19:17:58 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
result += string( "<a class=\"dsl_ref\" href=\"" ) + url.toEncoded().data() +"\">"
|
|
|
|
+ processNodeChildren( node ) + "</a>";
|
2012-11-26 19:17:58 +00:00
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"sub" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
result += "<sub>" + processNodeChildren( node ) + "</sub>";
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"sup" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
result += "<sup>" + processNodeChildren( node ) + "</sup>";
|
|
|
|
}
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"t" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
result += "<span class=\"dsl_t\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
}
|
2013-06-07 12:58:10 +00:00
|
|
|
else
|
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"br" ) )
|
|
|
|
{
|
|
|
|
result += "<br />";
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else
|
|
|
|
result += "<span class=\"dsl_unknown\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-07 16:17:44 +00:00
|
|
|
QString const& DslDictionary::getDescription()
|
|
|
|
{
|
|
|
|
if( !dictionaryDescription.isEmpty() )
|
|
|
|
return dictionaryDescription;
|
|
|
|
|
|
|
|
dictionaryDescription = "NONE";
|
|
|
|
|
|
|
|
QString fileName =
|
|
|
|
QDir::fromNativeSeparators( FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() ) );
|
|
|
|
|
|
|
|
// Remove the extension
|
|
|
|
if ( fileName.endsWith( ".dsl.dz", Qt::CaseInsensitive ) )
|
|
|
|
fileName.chop( 6 );
|
|
|
|
else
|
|
|
|
fileName.chop( 3 );
|
|
|
|
|
|
|
|
fileName += "ann";
|
|
|
|
QFileInfo info( fileName );
|
|
|
|
|
|
|
|
if ( info.exists() )
|
|
|
|
{
|
|
|
|
QFile annFile( fileName );
|
|
|
|
if( !annFile.open( QFile::ReadOnly | QFile::Text ) )
|
|
|
|
return dictionaryDescription;
|
|
|
|
|
|
|
|
QTextStream annStream( &annFile );
|
|
|
|
QString data, str;
|
|
|
|
|
|
|
|
str = annStream.readLine();
|
|
|
|
|
|
|
|
if( str.left( 10 ).compare( "#LANGUAGE " ) != 0 )
|
|
|
|
{
|
|
|
|
annStream.seek( 0 );
|
|
|
|
dictionaryDescription = annStream.readAll();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Multilanguage annotation
|
|
|
|
|
|
|
|
qint32 gdLang, annLang;
|
|
|
|
QString langStr;
|
2013-02-03 20:19:55 +00:00
|
|
|
gdLang = LangCoder::code2toInt( QLocale::system().name().left( 2 ).toLatin1().data() );
|
2012-09-07 16:17:44 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
data.clear();
|
|
|
|
langStr = str.mid( 10 ).replace( '\"', ' ').trimmed();
|
|
|
|
annLang = LangCoder::findIdForLanguage( gd::toWString( langStr ) );
|
|
|
|
do
|
|
|
|
{
|
|
|
|
str = annStream.readLine();
|
|
|
|
if( str.left( 10 ).compare( "#LANGUAGE " ) == 0 )
|
|
|
|
break;
|
|
|
|
if( !str.endsWith( '\n' ) )
|
|
|
|
str.append( '\n' );
|
|
|
|
data += str;
|
|
|
|
}
|
|
|
|
while ( !annStream.atEnd() );
|
|
|
|
if( dictionaryDescription.compare( "NONE ") == 0 || langStr.compare( "English", Qt::CaseInsensitive ) == 0 || gdLang == annLang )
|
|
|
|
dictionaryDescription = data.trimmed();
|
|
|
|
if( gdLang == annLang || annStream.atEnd() )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dictionaryDescription;
|
|
|
|
}
|
|
|
|
|
2012-11-29 17:12:54 +00:00
|
|
|
QString DslDictionary::getMainFilename()
|
|
|
|
{
|
|
|
|
return FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
void DslDictionary::makeFTSIndex( QAtomicInt & isCancelled, bool firstIteration )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
if( !( Dictionary::needToRebuildIndex( getDictionaryFilenames(), ftsIdxName )
|
2014-11-22 14:22:04 +00:00
|
|
|
|| FtsHelpers::ftsIndexIsOldOrBad( ftsIdxName, this ) ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
FTS_index_completed.ref();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
if( haveFTSIndex() )
|
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
if( ensureInitDone().size() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( firstIteration && getArticleCount() > FTS::MaxDictionarySizeForFastSearch )
|
|
|
|
return;
|
|
|
|
|
|
|
|
gdDebug( "Dsl: 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 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
gdWarning( "DSL: Failed building full-text search index for \"%s\", reason: %s\n", getName().c_str(), ex.what() );
|
|
|
|
QFile::remove( FsEncoding::decode( ftsIdxName.c_str() ) );
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
void DslDictionary::getArticleText( uint32_t articleAddress, QString & headword, QString & text )
|
|
|
|
{
|
2014-04-17 16:38:41 +00:00
|
|
|
headword.clear();
|
|
|
|
text.clear();
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
vector< char > chunk;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
char * articleProps;
|
|
|
|
wstring articleData;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
Mutex::Lock _( idxMutex );
|
|
|
|
articleProps = chunks->getBlock( articleAddress, chunk );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t articleOffset, articleSize;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
memcpy( &articleOffset, articleProps, sizeof( articleOffset ) );
|
|
|
|
memcpy( &articleSize, articleProps + sizeof( articleOffset ),
|
|
|
|
sizeof( articleSize ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
char * articleBody;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dzMutex );
|
|
|
|
articleBody = dict_data_read_( dz, articleOffset, articleSize, 0, 0 );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
if ( !articleBody )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
articleData =
|
|
|
|
DslIconv::toWstring(
|
|
|
|
DslIconv::getEncodingNameFor( DslEncoding( idxHeader.dslEncoding ) ),
|
|
|
|
articleBody, articleSize );
|
|
|
|
free( articleBody );
|
|
|
|
|
|
|
|
// Strip DSL comments
|
|
|
|
bool b = false;
|
|
|
|
stripComments( articleData, b );
|
|
|
|
}
|
|
|
|
catch( ... )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2014-04-16 16:18:28 +00:00
|
|
|
free( articleBody );
|
|
|
|
return;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
// Skip headword
|
|
|
|
|
|
|
|
size_t pos = 0;
|
2014-04-17 16:38:41 +00:00
|
|
|
wstring articleHeadword;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
// Check if we retrieve insided card
|
|
|
|
bool insidedCard = isDslWs( articleData.at( 0 ) );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
size_t begin = pos;
|
|
|
|
|
|
|
|
pos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ), begin );
|
|
|
|
|
2014-04-17 16:38:41 +00:00
|
|
|
if ( articleHeadword.empty() )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
// Process the headword
|
|
|
|
|
2014-04-17 16:38:41 +00:00
|
|
|
articleHeadword = wstring( articleData, begin, pos - begin );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if( insidedCard && !articleHeadword.empty() && isDslWs( articleHeadword[ 0 ] ) )
|
|
|
|
{
|
|
|
|
// Headword of the insided card
|
|
|
|
wstring::size_type hpos = articleHeadword.find( L'@' );
|
|
|
|
if( hpos != string::npos )
|
|
|
|
{
|
|
|
|
wstring head = Folding::trimWhitespace( articleHeadword.substr( hpos + 1 ) );
|
|
|
|
hpos = head.find( L'~' );
|
|
|
|
while( hpos != string::npos )
|
|
|
|
{
|
|
|
|
if( hpos == 0 || head[ hpos ] != L'\\' )
|
|
|
|
break;
|
|
|
|
hpos = head.find( L'~', hpos + 1 );
|
|
|
|
}
|
|
|
|
if( hpos == string::npos )
|
|
|
|
articleHeadword = head;
|
|
|
|
else
|
|
|
|
articleHeadword.clear();
|
|
|
|
}
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if( !articleHeadword.empty() )
|
|
|
|
{
|
|
|
|
list< wstring > lst;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
expandOptionalParts( articleHeadword, &lst );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( lst.size() ) // Should always be
|
|
|
|
articleHeadword = lst.front();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
processUnsortedParts( articleHeadword, true );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( pos == articleData.size() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Skip \n\r
|
|
|
|
|
|
|
|
if ( articleData[ pos ] == '\r' )
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if ( pos != articleData.size() )
|
|
|
|
{
|
|
|
|
if ( articleData[ pos ] == '\n' )
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
if ( pos == articleData.size() )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
// Ok, it's end of article
|
2014-04-16 16:18:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-24 13:56:38 +00:00
|
|
|
if( isDslWs( articleData[ pos ] ) )
|
2014-04-17 16:38:41 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
// Check for begin article text
|
|
|
|
if( insidedCard )
|
2014-04-17 16:38:41 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
// Check for next insided headword
|
|
|
|
wstring::size_type hpos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ), pos );
|
|
|
|
if ( hpos == wstring::npos )
|
|
|
|
hpos = articleData.size();
|
2014-04-17 16:38:41 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
wstring str = wstring( articleData, pos, hpos - pos );
|
|
|
|
|
|
|
|
hpos = str.find( L'@');
|
|
|
|
if( hpos == wstring::npos || str[ hpos - 1 ] == L'\\' )
|
|
|
|
break;
|
2014-04-17 16:38:41 +00:00
|
|
|
}
|
2014-04-24 13:56:38 +00:00
|
|
|
else
|
|
|
|
break;
|
2014-04-17 16:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-24 13:56:38 +00:00
|
|
|
|
|
|
|
if( !articleHeadword.empty() )
|
2014-04-17 16:38:41 +00:00
|
|
|
{
|
|
|
|
unescapeDsl( articleHeadword );
|
2014-04-21 16:20:24 +00:00
|
|
|
normalizeHeadword( articleHeadword );
|
2014-04-17 16:38:41 +00:00
|
|
|
headword = gd::toQString( articleHeadword );
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
wstring articleText;
|
|
|
|
|
|
|
|
if ( pos != articleData.size() )
|
|
|
|
articleText = wstring( articleData, pos );
|
|
|
|
else
|
|
|
|
articleText.clear();
|
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
if( !articleText.empty() )
|
|
|
|
{
|
|
|
|
text = gd::toQString( articleText ).normalized( QString::NormalizationForm_C );
|
|
|
|
|
|
|
|
articleText.clear();
|
|
|
|
|
|
|
|
// Parse article text
|
|
|
|
|
2014-11-22 14:22:04 +00:00
|
|
|
// Strip some areas
|
|
|
|
|
|
|
|
const int stripTagsNumber = 5;
|
|
|
|
static QString stripTags[ stripTagsNumber ] =
|
|
|
|
{
|
|
|
|
"s",
|
|
|
|
"url",
|
|
|
|
"!trs",
|
|
|
|
"video",
|
|
|
|
"preview"
|
|
|
|
};
|
|
|
|
static QString stripEndTags[ stripTagsNumber ] =
|
|
|
|
{
|
|
|
|
"[/s]",
|
|
|
|
"[/url]",
|
|
|
|
"[/!trs]",
|
|
|
|
"[/video]",
|
|
|
|
"[/preview]"
|
|
|
|
};
|
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
int pos = 0;
|
|
|
|
while( pos >= 0 )
|
|
|
|
{
|
2014-11-22 14:22:04 +00:00
|
|
|
pos = text.indexOf( '[', pos, Qt::CaseInsensitive );
|
2014-04-28 13:58:38 +00:00
|
|
|
if( pos >= 0 )
|
|
|
|
{
|
2014-11-22 14:22:04 +00:00
|
|
|
if( ( pos > 0 && text[ pos - 1 ] == '\\' && ( pos < 2 || text[ pos - 2 ] != '\\' ) )
|
|
|
|
|| ( pos > text.size() - 2 || text[ pos + 1 ] == '/' ) )
|
|
|
|
{
|
|
|
|
pos += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos2 = text.indexOf( ']', pos + 1, Qt::CaseInsensitive );
|
|
|
|
if( pos2 < 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
QString tag = text.mid( pos + 1, pos2 - pos - 1 );
|
|
|
|
|
|
|
|
int n;
|
|
|
|
for( n = 0; n < stripTagsNumber; n++ )
|
|
|
|
{
|
|
|
|
if( tag.compare( stripTags[ n ], Qt::CaseInsensitive ) == 0 )
|
|
|
|
{
|
|
|
|
pos2 = text.indexOf( stripEndTags[ n ] , pos + stripTags[ n ].size() + 2, Qt::CaseInsensitive );
|
|
|
|
text.remove( pos, pos2 > 0 ? pos2 - pos + stripEndTags[ n ].length() : text.length() - pos );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( n >= stripTagsNumber )
|
|
|
|
pos += 1;
|
2014-04-28 13:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-22 14:22:04 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
// Strip tags
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
text.remove( QRegExp( "\\[[^\\\\\\[\\]]+\\]", Qt::CaseInsensitive ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
// Chech for insided cards
|
|
|
|
|
|
|
|
bool haveInsidedCards = false;
|
|
|
|
pos = 0;
|
|
|
|
while( pos >= 0 )
|
|
|
|
{
|
|
|
|
pos = text.indexOf( "@", pos );
|
|
|
|
if( pos > 0 && text.at( pos - 1 ) != '\\' )
|
|
|
|
{
|
|
|
|
haveInsidedCards = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
if( pos >= 0 )
|
|
|
|
pos += 1;
|
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
if( haveInsidedCards )
|
|
|
|
{
|
|
|
|
// Use base DSL parser for articles with insided cards
|
|
|
|
ArticleDom dom( gd::toWString( text ), getName(), articleHeadword );
|
|
|
|
text = gd::toQString( dom.root.renderAsText( true ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unescape DSL symbols
|
|
|
|
pos = 0;
|
|
|
|
while( pos >= 0 )
|
|
|
|
{
|
|
|
|
pos = text.indexOf( '\\', pos );
|
|
|
|
if( pos >= 0 )
|
|
|
|
{
|
|
|
|
if( text[ pos + 1 ] == '\\' )
|
|
|
|
pos += 1;
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-28 13:58:38 +00:00
|
|
|
text.remove( pos, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
/// DslDictionary::getArticle()
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
class DslArticleRequest;
|
|
|
|
|
|
|
|
class DslArticleRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
DslArticleRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
DslArticleRequestRunnable( DslArticleRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DslArticleRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DslArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class DslArticleRequestRunnable;
|
|
|
|
|
|
|
|
wstring word;
|
|
|
|
vector< wstring > alts;
|
|
|
|
DslDictionary & dict;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DslArticleRequest( wstring const & word_,
|
|
|
|
vector< wstring > const & alts_,
|
|
|
|
DslDictionary & dict_ ):
|
|
|
|
word( word_ ), alts( alts_ ), dict( dict_ )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new DslArticleRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(); // Run from another thread by DslArticleRequestRunnable
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
~DslArticleRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DslArticleRequestRunnable::run()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DslArticleRequest::run()
|
|
|
|
{
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
if ( dict.ensureInitDone().size() )
|
|
|
|
{
|
|
|
|
setErrorString( QString::fromUtf8( dict.ensureInitDone().c_str() ) );
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< WordArticleLink > chain = dict.findArticles( word );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
for( unsigned x = 0; x < alts.size(); ++x )
|
|
|
|
{
|
|
|
|
/// Make an additional query for each alt
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
vector< WordArticleLink > altChain = dict.findArticles( alts[ x ] );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
// Some synonyms make it that the articles appear several times. We combat
|
|
|
|
// this by only allowing them to appear once. Dsl treats different headwords
|
|
|
|
// of the same article as different articles, so we also include headword
|
|
|
|
// index here.
|
|
|
|
set< pair< uint32_t, unsigned > > articlesIncluded;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
// Check if we're cancelled occasionally
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
// Grab that article
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
wstring tildeValue;
|
|
|
|
wstring displayedHeadword;
|
2009-01-28 20:55:45 +00:00
|
|
|
wstring articleBody;
|
2009-05-10 15:44:21 +00:00
|
|
|
unsigned headwordIndex;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
string articleText, articleAfter;
|
2009-05-10 15:44:21 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
dict.loadArticle( chain[ x ].articleOffset, wordCaseFolded, tildeValue,
|
|
|
|
displayedHeadword, headwordIndex, articleBody );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
if ( !articlesIncluded.insert( std::make_pair( chain[ x ].articleOffset,
|
|
|
|
headwordIndex ) ).second )
|
|
|
|
continue; // We already have this article in the body.
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
dict.articleNom += 1;
|
2012-11-22 13:03:07 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
if( displayedHeadword.empty() || isDslWs( displayedHeadword[ 0 ] ) )
|
|
|
|
displayedHeadword = word; // Special case - insided card
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2017-06-09 12:38:23 +00:00
|
|
|
articleText += "<div style=\"display:inline;\" class=\"dsl_article\">";
|
2013-09-24 13:56:47 +00:00
|
|
|
articleText += "<div class=\"dsl_headwords\"";
|
|
|
|
if( dict.isFromLanguageRTL() )
|
|
|
|
articleText += " dir=\"rtl\"";
|
2013-10-06 17:40:06 +00:00
|
|
|
articleText += "><p>";
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
if( displayedHeadword.size() == 1 && displayedHeadword[0] == '<' ) // Fix special case - "<" header
|
|
|
|
articleText += "<"; // dslToHtml can't handle it correctly.
|
|
|
|
else
|
2014-04-18 12:34:44 +00:00
|
|
|
articleText += dict.dslToHtml( displayedHeadword, displayedHeadword );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
/// After this may be expand button will be inserted
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2013-10-06 17:40:06 +00:00
|
|
|
articleAfter += "</p></div>";
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
expandTildes( articleBody, tildeValue );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
articleAfter += "<div class=\"dsl_definition\"";
|
|
|
|
if( dict.isToLanguageRTL() )
|
|
|
|
articleAfter += " dir=\"rtl\"";
|
|
|
|
articleAfter += ">";
|
2013-07-10 13:48:09 +00:00
|
|
|
|
2014-04-18 12:34:44 +00:00
|
|
|
articleAfter += dict.dslToHtml( articleBody, displayedHeadword );
|
2013-09-24 13:56:47 +00:00
|
|
|
articleAfter += "</div>";
|
2017-06-09 12:38:23 +00:00
|
|
|
articleAfter += "</div>";
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
if( dict.hasHiddenZones() )
|
|
|
|
{
|
|
|
|
string prefix = "O" + dict.getId().substr( 0, 7 ) + "_" + QString::number( dict.articleNom ).toStdString();
|
|
|
|
string id1 = prefix + "_expand";
|
|
|
|
string id2 = prefix + "_opt_";
|
2013-10-06 17:40:06 +00:00
|
|
|
string button = " <img src=\"qrcx://localhost/icons/expand_opt.png\" class=\"hidden_expand_opt\" id=\"" + id1 +
|
2013-09-24 13:56:47 +00:00
|
|
|
"\" onclick=\"gdExpandOptPart('" + id1 + "','" + id2 +"')\" alt=\"[+]\"/>";
|
|
|
|
if( articleText.compare( articleText.size() - 4, 4, "</p>" ) == 0 )
|
|
|
|
articleText.insert( articleText.size() - 4, " " + button );
|
|
|
|
else
|
|
|
|
articleText += button;
|
|
|
|
}
|
2012-09-16 10:19:47 +00:00
|
|
|
|
2013-09-24 13:56:47 +00:00
|
|
|
articleText += articleAfter;
|
|
|
|
}
|
|
|
|
catch( std::exception &ex )
|
2012-09-16 10:19:47 +00:00
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "DSL: Failed loading article from \"%s\", reason: %s\n", dict.getName().c_str(), ex.what() );
|
2013-09-24 13:56:47 +00:00
|
|
|
articleText = string( "<span class=\"dsl_article\">" )
|
|
|
|
+ string( QObject::tr( "Article loading error" ).toUtf8().constData() )
|
|
|
|
+ "</span>";
|
2012-09-16 10:19:47 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
data.resize( data.size() + articleText.size() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
memcpy( &data.front() + data.size() - articleText.size(),
|
|
|
|
articleText.data(), articleText.size() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-10 15:44:21 +00:00
|
|
|
hasAnyData = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > DslDictionary::getArticle( wstring const & word,
|
2009-05-29 19:48:50 +00:00
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-04-16 11:33:12 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new DslArticleRequest( word, alts, *this );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
//// DslDictionary::getResource()
|
|
|
|
|
|
|
|
class DslResourceRequest;
|
|
|
|
|
|
|
|
class DslResourceRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
DslResourceRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
DslResourceRequestRunnable( DslResourceRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DslResourceRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DslResourceRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class DslResourceRequestRunnable;
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
DslDictionary & dict;
|
2009-03-28 17:16:12 +00:00
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
string resourceName;
|
2009-03-28 16:06:00 +00:00
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
DslResourceRequest( DslDictionary & dict_,
|
2009-03-28 16:06:00 +00:00
|
|
|
string const & resourceName_ ):
|
2009-05-17 13:35:19 +00:00
|
|
|
dict( dict_ ),
|
2009-03-28 16:06:00 +00:00
|
|
|
resourceName( resourceName_ )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new DslResourceRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(); // Run from another thread by DslResourceRequestRunnable
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
~DslResourceRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DslResourceRequestRunnable::run()
|
|
|
|
{
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DslResourceRequest::run()
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
// Some runnables linger enough that they are cancelled before they start
|
2013-05-30 13:24:21 +00:00
|
|
|
if ( Qt4x5::AtomicInt::loadAcquire( isCancelled ) )
|
2009-03-28 16:06:00 +00:00
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
if ( dict.ensureInitDone().size() )
|
|
|
|
{
|
|
|
|
setErrorString( QString::fromUtf8( dict.ensureInitDone().c_str() ) );
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-21 19:03:16 +00:00
|
|
|
string n =
|
2009-05-17 13:35:19 +00:00
|
|
|
FsEncoding::dirname( dict.getDictionaryFilenames()[ 0 ] ) +
|
2009-01-28 20:55:45 +00:00
|
|
|
FsEncoding::separator() +
|
2009-03-28 16:06:00 +00:00
|
|
|
FsEncoding::encode( resourceName );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "n is %s\n", n.c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2009-02-02 15:02:57 +00:00
|
|
|
try
|
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
File::loadFromFile( n, data );
|
2009-02-02 15:02:57 +00:00
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
2009-05-17 13:35:19 +00:00
|
|
|
n = dict.getDictionaryFilenames()[ 0 ] + ".files" +
|
2009-02-02 15:02:57 +00:00
|
|
|
FsEncoding::separator() +
|
2009-03-28 16:06:00 +00:00
|
|
|
FsEncoding::encode( resourceName );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-28 12:35:52 +00:00
|
|
|
try
|
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
File::loadFromFile( n, data );
|
2009-03-28 12:35:52 +00:00
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
|
|
|
// Try reading from zip file
|
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
if ( dict.resourceZip.isOpen() )
|
2009-03-28 12:35:52 +00:00
|
|
|
{
|
2009-05-17 13:35:19 +00:00
|
|
|
Mutex::Lock _( dict.resourceZipMutex );
|
2009-03-28 17:16:12 +00:00
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
Mutex::Lock __( dataMutex );
|
2009-03-28 12:35:52 +00:00
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
if ( !dict.resourceZip.loadFile( Utf8::decode( resourceName ), data ) )
|
|
|
|
throw; // Make it fail since we couldn't read the archive
|
2009-03-28 12:35:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2009-02-02 15:02:57 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
if ( Filetype::isNameOfTiff( resourceName ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Convert it
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
dataMutex.lock();
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
QImage img = QImage::fromData( (unsigned char *) &data.front(),
|
|
|
|
data.size() );
|
|
|
|
|
2014-02-11 14:02:00 +00:00
|
|
|
#ifdef MAKE_EXTRA_TIFF_HANDLER
|
|
|
|
if( img.isNull() )
|
|
|
|
GdTiff::tiffToQImage( &data.front(), data.size(), img );
|
|
|
|
#endif
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
dataMutex.unlock();
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !img.isNull() )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
// Managed to load -- now store it back as BMP
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer( &ba );
|
|
|
|
buffer.open( QIODevice::WriteOnly );
|
|
|
|
img.save( &buffer, "BMP" );
|
2009-03-28 16:06:00 +00:00
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
data.resize( buffer.size() );
|
2009-04-21 19:03:16 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
memcpy( &data.front(), buffer.data(), data.size() );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2013-09-24 13:56:47 +00:00
|
|
|
catch( std::exception &ex )
|
2009-10-25 22:49:24 +00:00
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "DSL: Failed loading resource \"%s\" for \"%s\", reason: %s\n",
|
|
|
|
resourceName.c_str(), dict.getName().c_str(), ex.what() );
|
2013-09-24 13:56:47 +00:00
|
|
|
// Resource not loaded -- we don't set the hasAnyData flag then
|
2009-10-25 22:49:24 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > DslDictionary::getResource( string const & name )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
2009-05-17 13:35:19 +00:00
|
|
|
return new DslResourceRequest( *this, name );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void findCorrespondingFiles( string const & ifo,
|
|
|
|
string & idx, string & dict, string & syn,
|
|
|
|
bool needSyn )
|
|
|
|
{
|
|
|
|
string base( ifo, 0, ifo.size() - 3 );
|
|
|
|
|
|
|
|
if ( !(
|
|
|
|
tryPossibleName( base + "idx", idx ) ||
|
|
|
|
tryPossibleName( base + "idx.gz", idx ) ||
|
|
|
|
tryPossibleName( base + "idx.dz", idx ) ||
|
|
|
|
tryPossibleName( base + "IDX", idx ) ||
|
|
|
|
tryPossibleName( base + "IDX.GZ", idx ) ||
|
|
|
|
tryPossibleName( base + "IDX.DZ", idx )
|
|
|
|
) )
|
|
|
|
throw exNoIdxFile( ifo );
|
|
|
|
|
|
|
|
if ( !(
|
|
|
|
tryPossibleName( base + "dict", dict ) ||
|
|
|
|
tryPossibleName( base + "dict.dz", dict ) ||
|
|
|
|
tryPossibleName( base + "DICT", dict ) ||
|
|
|
|
tryPossibleName( base + "dict.DZ", dict )
|
|
|
|
) )
|
|
|
|
throw exNoDictFile( ifo );
|
|
|
|
|
|
|
|
if ( needSyn && !(
|
|
|
|
tryPossibleName( base + "syn", syn ) ||
|
|
|
|
tryPossibleName( base + "syn.gz", syn ) ||
|
|
|
|
tryPossibleName( base + "syn.dz", syn ) ||
|
|
|
|
tryPossibleName( base + "SYN", syn ) ||
|
|
|
|
tryPossibleName( base + "SYN.GZ", syn ) ||
|
|
|
|
tryPossibleName( base + "SYN.DZ", syn )
|
|
|
|
) )
|
|
|
|
throw exNoSynFile( ifo );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
sptr< Dictionary::DataRequest > DslDictionary::getSearchResults( QString const & searchString,
|
|
|
|
int searchMode, bool matchCase,
|
|
|
|
int distanceBetweenWords,
|
|
|
|
int maxResults )
|
|
|
|
{
|
|
|
|
return new FtsHelpers::FTSResultsRequest( *this, searchString,searchMode, matchCase, distanceBetweenWords, maxResults );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
/// makeDictionaries
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries(
|
|
|
|
vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
2012-12-07 11:59:29 +00:00
|
|
|
Dictionary::Initializing & initializing,
|
2013-01-11 09:58:14 +00:00
|
|
|
int maxPictureWidth, unsigned int maxHeadwordSize )
|
2009-01-28 20:55:45 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for( vector< string >::const_iterator i = fileNames.begin(); i != fileNames.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
// Try .dsl and .dsl.dz suffixes
|
|
|
|
|
2009-04-23 11:43:20 +00:00
|
|
|
bool uncompressedDsl = ( i->size() >= 4 &&
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 4 ), ".dsl" ) == 0 );
|
|
|
|
if ( !uncompressedDsl &&
|
2009-01-28 20:55:45 +00:00
|
|
|
( i->size() < 7 ||
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 7 ), ".dsl.dz" ) != 0 ) )
|
|
|
|
continue;
|
|
|
|
|
2009-04-23 11:43:20 +00:00
|
|
|
// Make sure it's not an abbreviation file
|
|
|
|
|
|
|
|
int extSize = ( uncompressedDsl ? 4 : 7 );
|
|
|
|
if ( i->size() - extSize >= 5 &&
|
|
|
|
strncasecmp( i->c_str() + i->size() - extSize - 5, "_abrv", 5 ) == 0 )
|
|
|
|
{
|
|
|
|
// It is, skip it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-01 10:05:24 +00:00
|
|
|
unsigned atLine = 0; // Indicates current line in .dsl, for debug purposes
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
vector< string > dictFiles( 1, *i );
|
|
|
|
|
|
|
|
// Check if there is an 'abrv' file present
|
|
|
|
string baseName = ( (*i)[ i->size() - 4 ] == '.' ) ?
|
|
|
|
string( *i, 0, i->size() - 4 ) : string( *i, 0, i->size() - 7 );
|
|
|
|
|
|
|
|
string abrvFileName;
|
|
|
|
|
2012-01-30 13:11:41 +00:00
|
|
|
if ( File::tryPossibleName( baseName + "_abrv.dsl", abrvFileName ) ||
|
|
|
|
File::tryPossibleName( baseName + "_abrv.dsl.dz", abrvFileName ) ||
|
|
|
|
File::tryPossibleName( baseName + "_ABRV.DSL", abrvFileName ) ||
|
|
|
|
File::tryPossibleName( baseName + "_ABRV.DSL.DZ", abrvFileName ) ||
|
|
|
|
File::tryPossibleName( baseName + "_ABRV.DSL.dz", abrvFileName ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
dictFiles.push_back( abrvFileName );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
// See if there's a zip file with resources present. If so, include it.
|
|
|
|
|
|
|
|
string zipFileName;
|
|
|
|
|
2017-04-24 14:42:01 +00:00
|
|
|
if ( File::tryPossibleZipName( baseName + ".dsl.files.zip", zipFileName ) ||
|
|
|
|
File::tryPossibleZipName( baseName + ".dsl.dz.files.zip", zipFileName ) ||
|
|
|
|
File::tryPossibleZipName( baseName + ".DSL.FILES.ZIP", zipFileName ) ||
|
|
|
|
File::tryPossibleZipName( baseName + ".DSL.DZ.FILES.ZIP", zipFileName ) )
|
2009-10-25 22:49:24 +00:00
|
|
|
dictFiles.push_back( zipFileName );
|
|
|
|
|
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 ) ||
|
2009-10-25 22:49:24 +00:00
|
|
|
indexIsOldOrBad( indexFile, zipFileName.size() ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
DslScanner scanner( *i );
|
|
|
|
|
2009-08-01 10:05:24 +00:00
|
|
|
try { // Here we intercept any errors during the read to save line at
|
|
|
|
// which the incident happened. We need alive scanner for that.
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( scanner.getDictionaryName() == GD_NATIVE_TO_WS( L"Abbrev" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
continue; // For now just skip abbreviations
|
|
|
|
|
|
|
|
// Building the index
|
|
|
|
initializing.indexingDictionary( Utf8::encode( scanner.getDictionaryName() ) );
|
|
|
|
|
2013-11-16 18:34:09 +00:00
|
|
|
gdDebug( "Dsl: Building the index for dictionary: %s\n",
|
|
|
|
gd::toQString( scanner.getDictionaryName() ).toUtf8().data() );
|
2009-01-28 20:55:45 +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 );
|
|
|
|
|
|
|
|
string dictionaryName = Utf8::encode( scanner.getDictionaryName() );
|
|
|
|
|
|
|
|
idx.write( (uint32_t) dictionaryName.size() );
|
|
|
|
idx.write( dictionaryName.data(), dictionaryName.size() );
|
|
|
|
|
|
|
|
idxHeader.dslEncoding = scanner.getEncoding();
|
|
|
|
|
|
|
|
IndexedWords indexedWords;
|
|
|
|
|
|
|
|
ChunkedStorage::Writer chunks( idx );
|
|
|
|
|
|
|
|
// Read the abbreviations
|
|
|
|
|
|
|
|
if ( abrvFileName.size() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DslScanner abrvScanner( abrvFileName );
|
|
|
|
|
|
|
|
map< string, string > abrv;
|
|
|
|
|
|
|
|
wstring curString;
|
|
|
|
size_t curOffset;
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
// Skip any whitespace
|
2013-07-07 10:48:37 +00:00
|
|
|
if ( !abrvScanner.readNextLineWithoutComments( curString, curOffset ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( curString.empty() || isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
continue;
|
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
list< wstring > keys;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
bool eof = false;
|
|
|
|
|
|
|
|
// Insert the key and read more, or get to the definition
|
|
|
|
for( ; ; )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-05-12 14:24:22 +00:00
|
|
|
processUnsortedParts( curString, true );
|
|
|
|
|
|
|
|
if ( keys.size() )
|
|
|
|
expandTildes( curString, keys.front() );
|
|
|
|
|
2012-09-07 21:32:49 +00:00
|
|
|
expandOptionalParts( curString, &keys );
|
2009-05-12 14:24:22 +00:00
|
|
|
|
2013-07-07 10:48:37 +00:00
|
|
|
if ( !abrvScanner.readNextLineWithoutComments( curString, curOffset ) || curString.empty() )
|
2009-05-12 14:24:22 +00:00
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Warning: premature end of file %s\n", abrvFileName.c_str() );
|
2009-05-12 14:24:22 +00:00
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isDslWs( curString[ 0 ] ) )
|
|
|
|
break;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
if ( eof )
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
curString.erase( 0, curString.find_first_not_of( GD_NATIVE_TO_WS( L" \t" ) ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
if ( keys.size() )
|
|
|
|
expandTildes( curString, keys.front() );
|
|
|
|
|
2009-04-23 13:37:27 +00:00
|
|
|
// If the string has any dsl markup, we strip it
|
2009-05-12 14:24:22 +00:00
|
|
|
string value = Utf8::encode( ArticleDom( curString ).root.renderAsText() );
|
2009-04-23 13:37:27 +00:00
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
for( list< wstring >::iterator i = keys.begin(); i != keys.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
unescapeDsl( *i );
|
2009-06-06 16:02:52 +00:00
|
|
|
normalizeHeadword( *i );
|
|
|
|
|
2009-05-12 14:24:22 +00:00
|
|
|
abrv[ Utf8::encode( Folding::trimWhitespace( *i ) ) ] = value;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idxHeader.hasAbrv = 1;
|
|
|
|
idxHeader.abrvAddress = chunks.startNewBlock();
|
|
|
|
|
|
|
|
uint32_t sz = abrv.size();
|
|
|
|
|
|
|
|
chunks.addToBlock( &sz, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
for( map< string, string >::const_iterator i = abrv.begin();
|
|
|
|
i != abrv.end(); ++i )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
// DPRINTF( "%s:%s\n", i->first.c_str(), i->second.c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
sz = i->first.size();
|
|
|
|
chunks.addToBlock( &sz, sizeof( uint32_t ) );
|
|
|
|
chunks.addToBlock( i->first.data(), sz );
|
|
|
|
sz = i->second.size();
|
|
|
|
chunks.addToBlock( &sz, sizeof( uint32_t ) );
|
|
|
|
chunks.addToBlock( i->second.data(), sz );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Error reading abrv file \"%s\", error: %s. Skipping it.\n",
|
|
|
|
abrvFileName.c_str(), e.what() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasString = false;
|
|
|
|
wstring curString;
|
|
|
|
size_t curOffset;
|
|
|
|
|
2009-04-20 23:20:53 +00:00
|
|
|
uint32_t articleCount = 0, wordCount = 0;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
// Find the main headword
|
|
|
|
|
2013-07-07 10:48:37 +00:00
|
|
|
if ( !hasString && !scanner.readNextLineWithoutComments( curString, curOffset ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
break; // Clean end of file
|
|
|
|
|
|
|
|
hasString = false;
|
|
|
|
|
|
|
|
// The line read should either consist of pure whitespace, or be a
|
|
|
|
// headword
|
|
|
|
|
|
|
|
if ( curString.empty() )
|
|
|
|
continue;
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// The first character is blank. Let's make sure that all other
|
|
|
|
// characters are blank, too.
|
|
|
|
for( size_t x = 1; x < curString.size(); ++x )
|
|
|
|
{
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( !isDslWs( curString[ x ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Warning: garbage string in %s at offset 0x%lX\n", i->c_str(), (unsigned long) curOffset );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, got the headword
|
|
|
|
|
|
|
|
list< wstring > allEntryWords;
|
|
|
|
|
|
|
|
processUnsortedParts( curString, true );
|
2012-09-07 21:32:49 +00:00
|
|
|
expandOptionalParts( curString, &allEntryWords );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
uint32_t articleOffset = curOffset;
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "Headword: %ls\n", curString.c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
// More headwords may follow
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
2013-07-07 10:48:37 +00:00
|
|
|
if ( ! ( hasString = scanner.readNextLineWithoutComments( curString, curOffset ) ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Warning: premature end of file %s\n", i->c_str() );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-24 05:01:02 +00:00
|
|
|
// Lingvo skips empty strings between the headwords
|
|
|
|
if ( curString.empty() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
break; // No more headwords
|
|
|
|
|
2013-09-20 14:25:44 +00:00
|
|
|
#ifdef QT_DEBUG
|
2013-08-04 19:19:57 +00:00
|
|
|
qDebug() << "Alt headword" << gd::toQString( curString );
|
2013-09-20 14:25:44 +00:00
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
processUnsortedParts( curString, true );
|
|
|
|
expandTildes( curString, allEntryWords.front() );
|
2012-09-07 21:32:49 +00:00
|
|
|
expandOptionalParts( curString, &allEntryWords );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !hasString )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Insert new entry
|
|
|
|
|
|
|
|
uint32_t descOffset = chunks.startNewBlock();
|
|
|
|
|
|
|
|
chunks.addToBlock( &articleOffset, sizeof( articleOffset ) );
|
|
|
|
|
|
|
|
for( list< wstring >::iterator j = allEntryWords.begin();
|
|
|
|
j != allEntryWords.end(); ++j )
|
|
|
|
{
|
|
|
|
unescapeDsl( *j );
|
2009-06-06 16:02:52 +00:00
|
|
|
normalizeHeadword( *j );
|
2013-01-11 09:58:14 +00:00
|
|
|
indexedWords.addWord( *j, descOffset, maxHeadwordSize );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 23:20:53 +00:00
|
|
|
++articleCount;
|
|
|
|
wordCount += allEntryWords.size();
|
|
|
|
|
2012-11-22 13:03:07 +00:00
|
|
|
int insideInsided = 0;
|
|
|
|
wstring headword;
|
|
|
|
QVector< InsidedCard > insidedCards;
|
|
|
|
uint32_t offset = curOffset;
|
2014-04-24 13:56:38 +00:00
|
|
|
QVector< wstring > insidedHeadwords;
|
|
|
|
unsigned linesInsideCard = 0;
|
2012-11-22 13:03:07 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// Skip the article's body
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
|
2013-07-07 10:48:37 +00:00
|
|
|
if ( ! ( hasString = scanner.readNextLineWithoutComments( curString, curOffset ) )
|
2012-11-22 13:03:07 +00:00
|
|
|
|| ( curString.size() && !isDslWs( curString[ 0 ] ) ) )
|
|
|
|
{
|
|
|
|
if( insideInsided )
|
2014-04-24 13:56:38 +00:00
|
|
|
insidedCards.append( InsidedCard( offset, curOffset - offset, insidedHeadwords ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
2012-11-22 13:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find embedded cards
|
|
|
|
|
|
|
|
wstring::size_type n = curString.find( L'@' );
|
|
|
|
if( n == wstring::npos || curString[ n - 1 ] == L'\\' )
|
2014-04-24 13:56:38 +00:00
|
|
|
{
|
|
|
|
if( insideInsided )
|
|
|
|
linesInsideCard++;
|
|
|
|
|
2012-11-22 13:03:07 +00:00
|
|
|
continue;
|
2014-04-24 13:56:38 +00:00
|
|
|
}
|
2012-11-22 13:03:07 +00:00
|
|
|
|
|
|
|
// Handle embedded card
|
|
|
|
|
|
|
|
if( insideInsided )
|
2014-04-24 13:56:38 +00:00
|
|
|
{
|
|
|
|
if( linesInsideCard )
|
|
|
|
{
|
|
|
|
insidedCards.append( InsidedCard( offset, curOffset - offset, insidedHeadwords ) );
|
|
|
|
|
|
|
|
insidedHeadwords.clear();
|
|
|
|
linesInsideCard = 0;
|
|
|
|
offset = curOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = curOffset;
|
|
|
|
linesInsideCard = 0;
|
|
|
|
}
|
2012-11-22 13:03:07 +00:00
|
|
|
|
|
|
|
headword = Folding::trimWhitespace( curString.substr( n + 1 ) );
|
|
|
|
|
|
|
|
if( !headword.empty() )
|
|
|
|
{
|
|
|
|
processUnsortedParts( headword, true );
|
|
|
|
expandTildes( headword, allEntryWords.front() );
|
2014-04-24 13:56:38 +00:00
|
|
|
insidedHeadwords.append( headword );
|
2012-11-22 13:03:07 +00:00
|
|
|
insideInsided = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
insideInsided = false;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're having read the first string after the article
|
|
|
|
// itself, we can use its offset to calculate the article's size.
|
|
|
|
// An end of file works here, too.
|
|
|
|
|
|
|
|
uint32_t articleSize = ( curOffset - articleOffset );
|
|
|
|
|
|
|
|
chunks.addToBlock( &articleSize, sizeof( articleSize ) );
|
|
|
|
|
2012-11-22 13:03:07 +00:00
|
|
|
for( QVector< InsidedCard >::iterator i = insidedCards.begin(); i != insidedCards.end(); ++i )
|
|
|
|
{
|
|
|
|
uint32_t descOffset = chunks.startNewBlock();
|
|
|
|
chunks.addToBlock( &(*i).offset, sizeof( (*i).offset ) );
|
|
|
|
chunks.addToBlock( &(*i).size, sizeof( (*i).size ) );
|
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
for( int x = 0; x < (*i).headwords.size(); x++ )
|
2013-06-06 16:37:33 +00:00
|
|
|
{
|
2014-04-24 13:56:38 +00:00
|
|
|
allEntryWords.clear();
|
|
|
|
expandOptionalParts( (*i).headwords[ x ], &allEntryWords );
|
2012-11-22 13:03:07 +00:00
|
|
|
|
2014-04-24 13:56:38 +00:00
|
|
|
for( list< wstring >::iterator j = allEntryWords.begin();
|
|
|
|
j != allEntryWords.end(); ++j )
|
|
|
|
{
|
|
|
|
unescapeDsl( *j );
|
|
|
|
normalizeHeadword( *j );
|
|
|
|
indexedWords.addWord( *j, descOffset, maxHeadwordSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
wordCount += allEntryWords.size();
|
|
|
|
}
|
2012-11-22 13:03:07 +00:00
|
|
|
++articleCount;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( !hasString )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2009-10-25 22:49:24 +00:00
|
|
|
indexedWords.clear(); // Release memory -- no need for this data
|
|
|
|
|
|
|
|
// 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" );
|
2009-10-25 22:49:24 +00:00
|
|
|
|
|
|
|
idxHeader.hasZipFile = 1;
|
|
|
|
|
2012-01-30 13:13:58 +00:00
|
|
|
IndexedWords zipFileNames;
|
|
|
|
IndexedZip zipFile;
|
|
|
|
if( zipFile.openZipFile( QDir::fromNativeSeparators(
|
|
|
|
FsEncoding::decode( zipFileName.c_str() ) ) ) )
|
|
|
|
zipFile.indexFile( zipFileNames );
|
2009-10-25 22:49:24 +00:00
|
|
|
|
2012-01-30 13:13:58 +00:00
|
|
|
if( !zipFileNames.empty() )
|
2009-10-25 22:49:24 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
2009-10-25 22:49:24 +00:00
|
|
|
idxHeader.zipSupportVersion = CurrentZipSupportVersion;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-04-20 23:20:53 +00:00
|
|
|
idxHeader.articleCount = articleCount;
|
|
|
|
idxHeader.wordCount = wordCount;
|
|
|
|
|
2009-04-23 11:43:20 +00:00
|
|
|
idxHeader.langFrom = dslLanguageToId( scanner.getLangFrom() );
|
|
|
|
idxHeader.langTo = dslLanguageToId( scanner.getLangTo() );
|
2009-04-22 21:37:32 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
2009-08-01 10:05:24 +00:00
|
|
|
|
|
|
|
} // In-place try for saving line count
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
atLine = scanner.getLinesRead();
|
|
|
|
throw;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-01 10:05:24 +00:00
|
|
|
} // if need to rebuild
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
dictionaries.push_back( new DslDictionary( dictId,
|
|
|
|
indexFile,
|
2012-12-07 11:59:29 +00:00
|
|
|
dictFiles,
|
|
|
|
maxPictureWidth ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "DSL dictionary reading failed: %s:%u, error: %s\n",
|
|
|
|
i->c_str(), atLine, e.what() );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|