2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2012-02-09 12:50:38 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "aard.hh"
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "chunkedstorage.hh"
|
|
|
|
#include "langcoder.hh"
|
|
|
|
#include "dprintf.hh"
|
|
|
|
#include "fsencoding.hh"
|
|
|
|
#include "decompress.hh"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stub_msvc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QSemaphore>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QAtomicInt>
|
|
|
|
#include <QDomDocument>
|
2013-03-29 19:19:50 +00:00
|
|
|
#include <QUrl>
|
2013-05-08 12:52:05 +00:00
|
|
|
#include <QtEndian>
|
2012-02-09 12:50:38 +00:00
|
|
|
|
|
|
|
#include "ufile.hh"
|
|
|
|
#include "wstring_qt.hh"
|
|
|
|
|
|
|
|
namespace Aard {
|
|
|
|
|
|
|
|
using std::map;
|
|
|
|
using std::multimap;
|
|
|
|
using std::pair;
|
|
|
|
using std::set;
|
|
|
|
using std::string;
|
|
|
|
using gd::wstring;
|
|
|
|
|
|
|
|
using BtreeIndexing::WordArticleLink;
|
|
|
|
using BtreeIndexing::IndexedWords;
|
|
|
|
using BtreeIndexing::IndexInfo;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
DEF_EX_STR( exNotDctFile, "Not an Sdictionary file", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exWordIsTooLarge, "Enountered a word that is too large:", Dictionary::Ex )
|
|
|
|
DEF_EX_STR( exSuddenEndOfFile, "Sudden end of file", Dictionary::Ex )
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack( push, 1 )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// AAR file header
|
|
|
|
struct AAR_header
|
|
|
|
{
|
|
|
|
char signature[4];
|
|
|
|
char checksum[40];
|
2013-05-08 12:52:05 +00:00
|
|
|
uint16_t version;
|
2012-02-09 12:50:38 +00:00
|
|
|
char uuid[16];
|
2013-05-08 12:52:05 +00:00
|
|
|
uint16_t volume;
|
|
|
|
uint16_t totalVolumes;
|
|
|
|
uint32_t metaLength;
|
|
|
|
uint32_t wordsCount;
|
|
|
|
uint32_t articleOffset;
|
2012-02-09 12:50:38 +00:00
|
|
|
char indexItemFormat[4];
|
|
|
|
char keyLengthFormat[2];
|
|
|
|
char articleLengthFormat[2];
|
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
struct IndexElement
|
|
|
|
{
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t wordOffset;
|
|
|
|
uint32_t articleOffset;
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2012-09-17 15:47:45 +00:00
|
|
|
struct IndexElement64
|
|
|
|
{
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t wordOffset;
|
|
|
|
uint64_t articleOffset;
|
2012-09-17 15:47:45 +00:00
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
Signature = 0x58524141, // AARX on little-endian, XRAA on big-endian
|
2013-04-08 14:50:44 +00:00
|
|
|
CurrentFormatVersion = 4 + BtreeIndexing::FormatVersion + Folding::Version
|
2012-02-09 12:50:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, AARX
|
|
|
|
uint32_t formatVersion; // File format version (CurrentFormatVersion)
|
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
|
|
|
uint32_t wordCount;
|
|
|
|
uint32_t articleCount;
|
|
|
|
uint32_t langFrom; // Source language
|
|
|
|
uint32_t langTo; // Target language
|
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack( pop, 1 )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool indexIsOldOrBad( string const & indexFile )
|
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != Signature ||
|
|
|
|
header.formatVersion != CurrentFormatVersion;
|
|
|
|
}
|
|
|
|
|
2012-10-26 16:47:54 +00:00
|
|
|
void readJSONValue( string const & source, string & str, string::size_type & pos)
|
2012-02-09 12:50:38 +00:00
|
|
|
{
|
|
|
|
int level = 1;
|
|
|
|
char endChar;
|
|
|
|
str.push_back( source[pos] );
|
|
|
|
if( source[pos] == '{')
|
|
|
|
endChar = '}';
|
|
|
|
else if( source[pos] == '[' )
|
|
|
|
endChar = ']';
|
|
|
|
else if( source[pos] == '\"' )
|
|
|
|
{
|
|
|
|
str.clear();
|
|
|
|
endChar = '\"';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
endChar = ',';
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
char ch = 0;
|
|
|
|
char lastCh = 0;
|
|
|
|
while( !( ch == endChar && lastCh != '\\' && level == 0 )
|
|
|
|
&& pos < source.size() )
|
|
|
|
{
|
|
|
|
lastCh = ch;
|
|
|
|
ch = source[ pos++ ];
|
|
|
|
if( ( ch == '{' || ch == '[' ) && lastCh != '\\' )
|
|
|
|
level++;
|
|
|
|
if( ( ch == '}' || ch == ']' ) && lastCh != '\\' )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
if( ch == endChar &&
|
|
|
|
( ( ch == '\"' && lastCh != '\\' ) || ch == ',' )
|
|
|
|
&& level == 1)
|
|
|
|
break;
|
|
|
|
str.push_back( ch );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
map< string, string > parseMetaData( string const & metaData )
|
|
|
|
{
|
|
|
|
// Parsing JSON string
|
|
|
|
map< string, string > data;
|
|
|
|
string name, value;
|
2012-10-26 19:49:31 +00:00
|
|
|
string::size_type n = 0;
|
2012-09-07 11:53:41 +00:00
|
|
|
|
2012-10-31 13:58:35 +00:00
|
|
|
while( n < metaData.length() && metaData[n] != '{' )
|
2012-09-07 11:53:41 +00:00
|
|
|
n++;
|
|
|
|
while( n < metaData.length() )
|
|
|
|
{
|
|
|
|
// Skip to '"'
|
2012-10-31 13:58:35 +00:00
|
|
|
while( n < metaData.length() && metaData[n] != '\"' )
|
2012-09-07 11:53:41 +00:00
|
|
|
n++;
|
|
|
|
if( ++n >= metaData.length() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Read name
|
2012-10-31 13:58:35 +00:00
|
|
|
while( n < metaData.length() &&
|
|
|
|
!( ( metaData[n] == '\"' || metaData[n] == '{' ) && metaData[n-1] != '\\' ) )
|
2012-09-07 11:53:41 +00:00
|
|
|
name.push_back( metaData[n++]);
|
|
|
|
|
|
|
|
// Skip to ':'
|
|
|
|
if( ++n >= metaData.length() )
|
|
|
|
break;
|
2012-10-31 13:58:35 +00:00
|
|
|
while( n < metaData.length() && metaData[n] != ':' )
|
2012-09-07 11:53:41 +00:00
|
|
|
n++;
|
|
|
|
if( ++n >= metaData.length() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Find value start after ':'
|
2012-10-31 13:58:35 +00:00
|
|
|
while( n < metaData.length()
|
|
|
|
&& !( ( metaData[n] == '\"'
|
|
|
|
|| metaData[n] == '{'
|
|
|
|
|| metaData[n] == '['
|
|
|
|
|| ( metaData[n] >= '0' && metaData[n] <= '9' ) )
|
|
|
|
&& metaData[n-1] != '\\' ) )
|
2012-09-07 11:53:41 +00:00
|
|
|
n++;
|
|
|
|
if( n >= metaData.length() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
readJSONValue( metaData, value, n);
|
|
|
|
|
|
|
|
data[name] = value;
|
|
|
|
|
|
|
|
name.clear();
|
|
|
|
value.clear();
|
|
|
|
if( ++n >= metaData.length() )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
class AardDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
|
|
|
Mutex idxMutex;
|
2012-09-07 11:53:41 +00:00
|
|
|
Mutex aardMutex;
|
2012-02-09 12:50:38 +00:00
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
ChunkedStorage::Reader chunks;
|
|
|
|
string dictionaryName;
|
|
|
|
File::Class df;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AardDictionary( string const & id, string const & indexFile,
|
|
|
|
vector< string > const & dictionaryFiles );
|
|
|
|
|
|
|
|
~AardDictionary();
|
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return dictionaryName; }
|
|
|
|
|
|
|
|
virtual map< Dictionary::Property, string > getProperties() throw()
|
|
|
|
{ return map< Dictionary::Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
|
|
|
{ return idxHeader.articleCount; }
|
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
|
|
|
{ return idxHeader.wordCount; }
|
|
|
|
|
|
|
|
inline virtual quint32 getLangFrom() const
|
|
|
|
{ return idxHeader.langFrom; }
|
|
|
|
|
|
|
|
inline virtual quint32 getLangTo() const
|
|
|
|
{ return idxHeader.langTo; }
|
|
|
|
|
|
|
|
virtual sptr< Dictionary::DataRequest > getArticle( wstring const &,
|
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
|
|
|
throw( std::exception );
|
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
virtual QString const& getDescription();
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void loadIcon() throw();
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
private:
|
2012-02-09 12:50:38 +00:00
|
|
|
|
|
|
|
/// Loads the article.
|
|
|
|
void loadArticle( uint32_t address,
|
|
|
|
string & articleText );
|
|
|
|
string convert( string const & in_data );
|
|
|
|
|
|
|
|
friend class AardArticleRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
AardDictionary::AardDictionary( string const & id,
|
|
|
|
string const & indexFile,
|
|
|
|
vector< string > const & dictionaryFiles ):
|
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
|
|
|
chunks( idx, idxHeader.chunksOffset ),
|
2012-12-05 15:29:24 +00:00
|
|
|
df( dictionaryFiles[ 0 ], "rb" )
|
2012-02-09 12:50:38 +00:00
|
|
|
{
|
|
|
|
// Read dictionary name
|
|
|
|
|
|
|
|
idx.seek( sizeof( idxHeader ) );
|
|
|
|
vector< char > dName( idx.read< uint32_t >() );
|
|
|
|
if( dName.size() )
|
|
|
|
{
|
|
|
|
idx.read( &dName.front(), dName.size() );
|
|
|
|
dictionaryName = string( &dName.front(), dName.size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the index
|
|
|
|
|
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements,
|
|
|
|
idxHeader.indexRootOffset ),
|
|
|
|
idx, idxMutex );
|
2012-09-07 11:53:41 +00:00
|
|
|
|
|
|
|
// Read decription
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AardDictionary::~AardDictionary()
|
|
|
|
{
|
|
|
|
df.close();
|
|
|
|
}
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
void AardDictionary::loadIcon() throw()
|
2012-02-09 12:50:38 +00:00
|
|
|
{
|
2013-01-31 22:30:11 +00:00
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
QString fileName =
|
|
|
|
QDir::fromNativeSeparators( FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() ) );
|
|
|
|
|
|
|
|
// Remove the extension
|
|
|
|
fileName.chop( 3 );
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
if( !loadIconFromFile( fileName ) )
|
2012-02-09 12:50:38 +00:00
|
|
|
{
|
|
|
|
// Load failed -- use default icons
|
|
|
|
dictionaryNativeIcon = dictionaryIcon = QIcon(":/icons/icon32_aard.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string AardDictionary::convert( const string & in )
|
|
|
|
{
|
|
|
|
string inConverted;
|
|
|
|
char inCh, lastCh = 0;
|
|
|
|
bool afterEol = false;
|
|
|
|
|
|
|
|
for( string::const_iterator i = in.begin(), j = in.end(); i != j; ++i )
|
|
|
|
{
|
|
|
|
inCh = *i;
|
|
|
|
if( lastCh == '\\' )
|
|
|
|
{
|
|
|
|
inConverted.erase( inConverted.size() - 1 );
|
|
|
|
lastCh = 0;
|
|
|
|
if( inCh == 'n' )
|
|
|
|
{
|
|
|
|
inConverted.append( "<br/>");
|
|
|
|
afterEol = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( inCh == 'r')
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( inCh == ' ' && afterEol )
|
|
|
|
{
|
|
|
|
inConverted.append( " " );
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
lastCh = inCh;
|
|
|
|
afterEol = false;
|
|
|
|
inConverted.push_back( inCh );
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:53:23 +00:00
|
|
|
QString text = QString::fromUtf8( inConverted.c_str() );
|
|
|
|
text.replace( QRegExp( "<\\s*a\\s*href\\s*=\\s*[\\\"'](w:|s:){0,1}([^#](?!ttp://)[^\\\"']*)(.)" ),
|
|
|
|
"<a href=\"bword:\\2\"");
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-04-25 18:53:23 +00:00
|
|
|
return text.toUtf8().data();
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AardDictionary::loadArticle( uint32_t address,
|
|
|
|
string & articleText )
|
|
|
|
{
|
|
|
|
uint32_t articleOffset = address;
|
|
|
|
uint32_t articleSize;
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t size;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
|
|
|
vector< char > articleBody;
|
|
|
|
|
|
|
|
articleText.clear();
|
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
|
|
|
|
while( 1 )
|
2012-09-07 11:53:41 +00:00
|
|
|
{
|
2013-04-29 13:57:19 +00:00
|
|
|
articleText = string( QObject::tr( "Article loading error" ).toUtf8().constData() );
|
2013-03-04 12:57:12 +00:00
|
|
|
try
|
|
|
|
{
|
2012-09-07 11:53:41 +00:00
|
|
|
Mutex::Lock _( aardMutex );
|
|
|
|
df.seek( articleOffset );
|
|
|
|
df.read( &size, sizeof(size) );
|
2013-05-08 12:52:05 +00:00
|
|
|
articleSize = qFromBigEndian( size );
|
2013-04-23 16:40:35 +00:00
|
|
|
|
|
|
|
// Don't try to read and decode too big articles,
|
|
|
|
// it is most likely error in dictionary
|
|
|
|
if( articleSize > 1048576 )
|
|
|
|
break;
|
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
articleBody.resize( articleSize );
|
|
|
|
df.read( &articleBody.front(), articleSize );
|
2013-03-04 12:57:12 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
if ( articleBody.empty() )
|
|
|
|
break;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
articleText.clear();
|
|
|
|
|
|
|
|
string text = decompressBzip2( articleBody.data(), articleSize );
|
|
|
|
if( text.empty() )
|
2012-02-09 12:50:38 +00:00
|
|
|
text = decompressZlib( articleBody.data(), articleSize );
|
2013-03-04 12:57:12 +00:00
|
|
|
if( text.empty() )
|
2012-02-09 12:50:38 +00:00
|
|
|
text = string( articleBody.data(), articleSize );
|
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
if( text.empty() || text[ 0 ] != '[' )
|
|
|
|
break;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
string::size_type n = text.find( '\"' );
|
|
|
|
if( n == string::npos )
|
|
|
|
break;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
readJSONValue( text, articleText, n );
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
if( articleText.empty() )
|
|
|
|
{
|
2012-02-09 12:50:38 +00:00
|
|
|
n = text.find( "\"r\"" );
|
2013-03-04 12:57:12 +00:00
|
|
|
if( n != string::npos && n + 3 < text.size() )
|
2012-02-09 12:50:38 +00:00
|
|
|
{
|
2013-03-04 12:57:12 +00:00
|
|
|
n = text.find( '\"', n + 3 );
|
|
|
|
if( n == string::npos )
|
|
|
|
break;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
string link;
|
|
|
|
readJSONValue( text, link, n );
|
|
|
|
if( !link.empty() )
|
2013-03-29 19:19:50 +00:00
|
|
|
{
|
|
|
|
string encodedLink;
|
|
|
|
encodedLink.reserve( link.size() );
|
|
|
|
bool prev = false;
|
|
|
|
for( string::const_iterator i = link.begin(); i != link.end(); ++i )
|
|
|
|
{
|
|
|
|
if( *i == '\\' )
|
|
|
|
{
|
|
|
|
if( !prev )
|
|
|
|
{
|
|
|
|
prev = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
encodedLink.push_back( *i );
|
|
|
|
prev = false;
|
|
|
|
}
|
|
|
|
encodedLink = string( QUrl::toPercentEncoding( QString::fromUtf8( encodedLink.c_str() ) ).data() );
|
|
|
|
articleText = "<a href=\"" + encodedLink + "\">" + link + "</a>";
|
|
|
|
}
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
2013-03-04 12:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !articleText.empty() )
|
2013-03-04 12:57:12 +00:00
|
|
|
articleText = convert( articleText );
|
|
|
|
else
|
2013-04-29 13:57:19 +00:00
|
|
|
articleText = string( QObject::tr( "Article decoding error" ).toUtf8().constData() );
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-03-04 12:57:12 +00:00
|
|
|
articleText = "<div class=\"aard\">" + articleText + "</div>";
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
QString const& AardDictionary::getDescription()
|
|
|
|
{
|
|
|
|
if( !dictionaryDescription.isEmpty() )
|
|
|
|
return dictionaryDescription;
|
|
|
|
|
|
|
|
AAR_header dictHeader;
|
|
|
|
uint32_t size;
|
|
|
|
vector< char > data;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( aardMutex );
|
|
|
|
df.seek( 0 );
|
|
|
|
df.read( &dictHeader, sizeof(dictHeader) );
|
2013-05-08 12:52:05 +00:00
|
|
|
size = qFromBigEndian( dictHeader.metaLength );
|
2012-09-07 11:53:41 +00:00
|
|
|
data.resize( size );
|
|
|
|
df.read( &data.front(), size );
|
|
|
|
}
|
|
|
|
|
|
|
|
string metaStr = decompressBzip2( data.data(), size );
|
|
|
|
if( metaStr.empty() )
|
|
|
|
metaStr = decompressZlib( data.data(), size );
|
|
|
|
|
|
|
|
map< string, string > meta = parseMetaData( metaStr );
|
|
|
|
|
|
|
|
if( !meta.empty() )
|
|
|
|
{
|
2012-11-19 15:30:26 +00:00
|
|
|
map< string, string >::const_iterator iter = meta.find( "copyright" );
|
|
|
|
if( iter != meta.end() )
|
|
|
|
dictionaryDescription = "Copyright: " + QString::fromUtf8( iter->second.c_str() ) + "\n\n";
|
|
|
|
|
|
|
|
iter = meta.find( "version" );
|
|
|
|
if( iter != meta.end() )
|
|
|
|
dictionaryDescription = "Version: " + QString::fromUtf8( iter->second.c_str() ) + "\n\n";
|
|
|
|
|
|
|
|
iter = meta.find( "description" );
|
2012-09-07 11:53:41 +00:00
|
|
|
if( iter != meta.end() )
|
2012-11-19 15:30:26 +00:00
|
|
|
{
|
|
|
|
QString desc = QString::fromUtf8( iter->second.c_str() );
|
|
|
|
desc.replace( "\\n", "\n" );
|
|
|
|
desc.replace( "\\t", "\t" );
|
|
|
|
dictionaryDescription += desc;
|
|
|
|
}
|
2012-09-07 11:53:41 +00:00
|
|
|
}
|
2012-11-19 15:30:26 +00:00
|
|
|
|
|
|
|
if( dictionaryDescription.isEmpty() )
|
|
|
|
dictionaryDescription = "NONE";
|
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
return dictionaryDescription;
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
/// AardDictionary::getArticle()
|
|
|
|
|
|
|
|
class AardArticleRequest;
|
|
|
|
|
|
|
|
class AardArticleRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
AardArticleRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AardArticleRequestRunnable( AardArticleRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~AardArticleRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class AardArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class AardArticleRequestRunnable;
|
|
|
|
|
|
|
|
wstring word;
|
|
|
|
vector< wstring > alts;
|
|
|
|
AardDictionary & dict;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AardArticleRequest( wstring const & word_,
|
|
|
|
vector< wstring > const & alts_,
|
|
|
|
AardDictionary & dict_ ):
|
|
|
|
word( word_ ), alts( alts_ ), dict( dict_ )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new AardArticleRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(); // Run from another thread by DslArticleRequestRunnable
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AardArticleRequest()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
hasExited.acquire();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void AardArticleRequestRunnable::run()
|
|
|
|
{
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AardArticleRequest::run()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< WordArticleLink > chain = dict.findArticles( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < alts.size(); ++x )
|
|
|
|
{
|
|
|
|
/// Make an additional query for each alt
|
|
|
|
|
|
|
|
vector< WordArticleLink > altChain = dict.findArticles( alts[ x ] );
|
|
|
|
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
multimap< wstring, pair< string, string > > mainArticles, alternateArticles;
|
|
|
|
|
|
|
|
set< uint32_t > articlesIncluded; // Some synonims make it that the articles
|
|
|
|
// appear several times. We combat this
|
|
|
|
// by only allowing them to appear once.
|
|
|
|
|
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
|
|
|
|
|
|
|
// Now grab that article
|
|
|
|
|
|
|
|
string headword, articleText;
|
|
|
|
|
|
|
|
headword = chain[ x ].word;
|
2013-03-04 12:57:12 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
dict.loadArticle( chain[ x ].articleOffset, articleText );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
}
|
2012-02-09 12:50:38 +00:00
|
|
|
|
|
|
|
// Ok. Now, does it go to main articles, or to alternate ones? We list
|
|
|
|
// main ones first, and alternates after.
|
|
|
|
|
|
|
|
// We do the case-folded comparison here.
|
|
|
|
|
|
|
|
wstring headwordStripped =
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( headword ) );
|
|
|
|
|
|
|
|
multimap< wstring, pair< string, string > > & mapToUse =
|
|
|
|
( wordCaseFolded == headwordStripped ) ?
|
|
|
|
mainArticles : alternateArticles;
|
|
|
|
|
|
|
|
mapToUse.insert( pair< wstring, pair< string, string > >(
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( headword ) ),
|
|
|
|
pair< string, string >( headword, articleText ) ) );
|
|
|
|
|
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() )
|
|
|
|
{
|
|
|
|
// No such word
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
|
|
|
multimap< wstring, pair< string, string > >::const_iterator i;
|
|
|
|
|
|
|
|
for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<h3>";
|
|
|
|
result += i->second.first;
|
|
|
|
result += "</h3>";
|
|
|
|
result += i->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = alternateArticles.begin(); i != alternateArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<h3>";
|
|
|
|
result += i->second.first;
|
|
|
|
result += "</h3>";
|
|
|
|
result += i->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
data.resize( result.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front(), result.data(), result.size() );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > AardDictionary::getArticle( wstring const & word,
|
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new AardArticleRequest( word, alts, *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries(
|
|
|
|
vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
|
|
|
Dictionary::Initializing & initializing )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for( vector< string >::const_iterator i = fileNames.begin(); i != fileNames.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
// Skip files with the extensions different to .aar to speed up the
|
|
|
|
// scanning
|
|
|
|
if ( i->size() < 4 ||
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 4 ), ".aar" ) != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Got the file -- check if we need to rebuid the index
|
|
|
|
|
|
|
|
vector< string > dictFiles( 1, *i );
|
|
|
|
|
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
|
|
|
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) ||
|
|
|
|
indexIsOldOrBad( indexFile ) )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-09-17 15:47:45 +00:00
|
|
|
{
|
|
|
|
QFileInfo info( FsEncoding::decode( i->c_str() ) );
|
|
|
|
if( info.size() > ULONG_MAX )
|
|
|
|
{
|
|
|
|
DPRINTF( "File %s is too large", i->c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
File::Class df( *i, "rb" );
|
|
|
|
|
|
|
|
AAR_header dictHeader;
|
|
|
|
|
|
|
|
df.read( &dictHeader, sizeof(dictHeader) );
|
2012-09-17 15:47:45 +00:00
|
|
|
bool has64bitIndex = !strncmp( dictHeader.indexItemFormat, ">LQ", 4 );
|
2012-02-09 12:50:38 +00:00
|
|
|
if( strncmp( dictHeader.signature, "aard", 4 )
|
2012-09-17 15:47:45 +00:00
|
|
|
|| ( !has64bitIndex && strncmp( dictHeader.indexItemFormat, ">LL", 4 ) )
|
2012-02-09 12:50:38 +00:00
|
|
|
|| strncmp( dictHeader.keyLengthFormat, ">H", 2 )
|
|
|
|
|| strncmp( dictHeader.articleLengthFormat, ">L", 2) )
|
|
|
|
{
|
|
|
|
DPRINTF( "File %s is not in supported aard format", i->c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< char > data;
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t size = qFromBigEndian( dictHeader.metaLength );
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2012-09-07 11:53:41 +00:00
|
|
|
if( size == 0 )
|
|
|
|
{
|
|
|
|
DPRINTF( "File %s has invalid metadata", i->c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
data.resize( size );
|
|
|
|
df.read( &data.front(), size );
|
|
|
|
string metaStr = decompressBzip2( data.data(), size );
|
|
|
|
if( metaStr.empty() )
|
|
|
|
metaStr = decompressZlib( data.data(), size );
|
|
|
|
|
|
|
|
map< string, string > meta = parseMetaData( metaStr );
|
|
|
|
|
|
|
|
if( meta.empty() )
|
|
|
|
{
|
|
|
|
DPRINTF( "File %s has invalid metadata", i->c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
string dictName;
|
|
|
|
map< string, string >::const_iterator iter = meta.find( "title" );
|
|
|
|
if( iter != meta.end() )
|
|
|
|
dictName = iter->second;
|
|
|
|
|
|
|
|
string langFrom;
|
|
|
|
iter = meta.find( "index_language" );
|
|
|
|
if( iter != meta.end() )
|
|
|
|
langFrom = iter->second;
|
|
|
|
|
|
|
|
string langTo;
|
|
|
|
iter = meta.find( "article_language" );
|
|
|
|
if( iter != meta.end() )
|
|
|
|
langTo = iter->second;
|
|
|
|
|
2013-04-04 11:44:43 +00:00
|
|
|
if( ( dictName.compare( "Wikipedia") == 0 || dictName.compare( "Wikiquote" ) == 0 || dictName.compare( "Wiktionary" ) == 0 )
|
2013-04-02 13:44:53 +00:00
|
|
|
&& !langTo.empty() )
|
|
|
|
{
|
2013-04-08 14:50:44 +00:00
|
|
|
string capitalized = langTo.c_str();
|
|
|
|
capitalized[0] = toupper( capitalized[0] );
|
|
|
|
dictName = dictName + " (" + capitalized + ")";
|
2013-04-02 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 12:52:05 +00:00
|
|
|
uint16_t volumes = qFromBigEndian( dictHeader.totalVolumes );
|
2013-04-02 13:44:53 +00:00
|
|
|
if( volumes > 1 )
|
|
|
|
{
|
|
|
|
QString ss;
|
2013-05-08 12:52:05 +00:00
|
|
|
ss.sprintf( " (%i/%i)", qFromBigEndian( dictHeader.volume ), volumes );
|
2013-04-02 13:44:53 +00:00
|
|
|
dictName += ss.toLocal8Bit().data();
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
initializing.indexingDictionary( dictName );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
idx.write( (uint32_t) dictName.size() );
|
|
|
|
if( !dictName.empty() )
|
|
|
|
idx.write( dictName.data(), dictName.size() );
|
|
|
|
|
|
|
|
IndexedWords indexedWords;
|
|
|
|
|
|
|
|
ChunkedStorage::Writer chunks( idx );
|
|
|
|
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t wordCount = qFromBigEndian( dictHeader.wordsCount );
|
2012-02-09 12:50:38 +00:00
|
|
|
set< uint32_t > articleOffsets;
|
2013-05-08 12:52:05 +00:00
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
uint32_t pos = df.tell();
|
2012-09-17 15:47:45 +00:00
|
|
|
uint32_t wordsBase = pos + wordCount * ( has64bitIndex ? sizeof( IndexElement64 ) : sizeof( IndexElement ) );
|
2013-05-08 12:52:05 +00:00
|
|
|
uint32_t articlesBase = qFromBigEndian( dictHeader.articleOffset );
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2013-05-08 12:52:05 +00:00
|
|
|
data.clear();
|
2012-02-09 12:50:38 +00:00
|
|
|
for( uint32_t j = 0; j < wordCount; j++ )
|
|
|
|
{
|
2012-09-17 15:47:45 +00:00
|
|
|
uint32_t articleOffset;
|
|
|
|
uint32_t wordOffset;
|
|
|
|
|
|
|
|
if( has64bitIndex )
|
|
|
|
{
|
|
|
|
IndexElement64 el64;
|
2012-02-09 12:50:38 +00:00
|
|
|
|
2012-09-17 15:47:45 +00:00
|
|
|
df.seek( pos );
|
|
|
|
df.read( &el64, sizeof(el64) );
|
2013-05-08 12:52:05 +00:00
|
|
|
articleOffset = articlesBase + qFromBigEndian( el64.articleOffset );
|
|
|
|
wordOffset = wordsBase + qFromBigEndian( el64.wordOffset );
|
2012-09-17 15:47:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IndexElement el;
|
|
|
|
|
|
|
|
df.seek( pos );
|
|
|
|
df.read( &el, sizeof(el) );
|
2013-05-08 12:52:05 +00:00
|
|
|
articleOffset = articlesBase + qFromBigEndian( el.articleOffset );
|
|
|
|
wordOffset = wordsBase + qFromBigEndian( el.wordOffset );
|
2012-09-17 15:47:45 +00:00
|
|
|
}
|
2012-02-09 12:50:38 +00:00
|
|
|
|
|
|
|
df.seek( wordOffset );
|
|
|
|
|
2013-05-08 12:52:05 +00:00
|
|
|
uint16_t sizeBE;
|
2012-02-09 12:50:38 +00:00
|
|
|
df.read( &sizeBE, sizeof(sizeBE) );
|
2013-05-08 12:52:05 +00:00
|
|
|
uint16_t wordSize = qFromBigEndian( sizeBE );
|
|
|
|
if( data.size() < wordSize )
|
|
|
|
data.resize( wordSize );
|
2012-02-09 12:50:38 +00:00
|
|
|
df.read( &data.front(), wordSize );
|
|
|
|
|
|
|
|
if( articleOffsets.find( articleOffset ) == articleOffsets.end() )
|
|
|
|
articleOffsets.insert( articleOffset );
|
|
|
|
|
|
|
|
// Insert new entry
|
|
|
|
indexedWords.addWord( Utf8::decode( string( data.data(), wordSize ) ), articleOffset);
|
|
|
|
|
2012-09-17 15:47:45 +00:00
|
|
|
pos += has64bitIndex ? sizeof( IndexElement64 ) : sizeof( IndexElement );
|
2012-02-09 12:50:38 +00:00
|
|
|
}
|
2013-05-08 12:52:05 +00:00
|
|
|
data.clear();
|
|
|
|
|
|
|
|
idxHeader.articleCount = articleOffsets.size();
|
|
|
|
articleOffsets.clear();
|
|
|
|
|
2012-02-09 12:50:38 +00:00
|
|
|
// Finish with the chunks
|
|
|
|
|
|
|
|
idxHeader.chunksOffset = chunks.finish();
|
|
|
|
|
|
|
|
// Build index
|
|
|
|
|
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( indexedWords, idx );
|
|
|
|
|
|
|
|
idxHeader.indexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.indexRootOffset = idxInfo.rootOffset;
|
|
|
|
|
|
|
|
indexedWords.clear(); // Release memory -- no need for this data
|
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
|
|
|
|
idxHeader.wordCount = wordCount;
|
|
|
|
|
|
|
|
if( langFrom.size() == 3)
|
|
|
|
idxHeader.langFrom = LangCoder::code3toInt( langFrom.c_str() );
|
|
|
|
else if( langFrom.size() == 2 )
|
|
|
|
idxHeader.langFrom = LangCoder::code2toInt( langFrom.c_str() );
|
|
|
|
|
|
|
|
if( langTo.size() == 3)
|
|
|
|
idxHeader.langTo = LangCoder::code3toInt( langTo.c_str() );
|
|
|
|
else if( langTo.size() == 2 )
|
|
|
|
idxHeader.langTo = LangCoder::code2toInt( langTo.c_str() );
|
|
|
|
|
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
FDPRINTF( stderr, "Aard dictionary indexing failed: %s, error: %s\n",
|
|
|
|
i->c_str(), e.what() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
FDPRINTF( stderr, "Aard dictionary indexing failed\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} // if need to rebuild
|
|
|
|
dictionaries.push_back( new AardDictionary( dictId,
|
|
|
|
indexFile,
|
|
|
|
dictFiles ) );
|
|
|
|
}
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|