2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
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-01-28 20:55:45 +00:00
|
|
|
#include <zlib.h>
|
2009-03-28 12:35:52 +00:00
|
|
|
#include <zip.h>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
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-01-28 20:55:45 +00:00
|
|
|
// For TIFF conversion
|
|
|
|
#include <QImage>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QBuffer>
|
|
|
|
|
|
|
|
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 )
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Signature = 0x584c5344, // DSLX on little-endian, XLSD on big-endian
|
|
|
|
CurrentFormatVersion = 6 + BtreeIndexing::FormatVersion + Folding::Version
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, DSLX
|
|
|
|
uint32_t formatVersion; // File format version (CurrentFormatVersion)
|
|
|
|
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-01-28 20:55:45 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
bool indexIsOldOrBad( string const & indexFile )
|
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != Signature ||
|
|
|
|
header.formatVersion != CurrentFormatVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
class 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;
|
|
|
|
ChunkedStorage::Reader chunks;
|
|
|
|
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;
|
|
|
|
zip * resourceZip;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DslDictionary( string const & id, string const & indexFile,
|
|
|
|
vector< string > const & dictionaryFiles );
|
|
|
|
|
|
|
|
~DslDictionary();
|
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return dictionaryName; }
|
|
|
|
|
|
|
|
virtual map< Dictionary::Property, string > getProperties() throw()
|
|
|
|
{ return map< Dictionary::Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
|
|
|
{ return 0; }
|
|
|
|
|
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 &,
|
|
|
|
vector< wstring > const & alts )
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Loads the article. Does not process the DSL language.
|
|
|
|
void loadArticle( uint32_t address,
|
|
|
|
string & headword,
|
|
|
|
list< wstring > & displayedHeadwords,
|
|
|
|
wstring & articleText );
|
|
|
|
|
|
|
|
/// Converts DSL language to an Html.
|
|
|
|
string dslToHtml( wstring const & );
|
|
|
|
|
|
|
|
// Parts of dslToHtml()
|
|
|
|
string nodeToHtml( ArticleDom::Node const & );
|
|
|
|
string processNodeChildren( ArticleDom::Node const & node );
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
friend class DslArticleRequest;
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DslDictionary::DslDictionary( string const & id,
|
|
|
|
string const & indexFile,
|
|
|
|
vector< string > const & dictionaryFiles ):
|
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
|
|
|
chunks( idx, idxHeader.chunksOffset )
|
|
|
|
{
|
|
|
|
// Open the .dict file
|
|
|
|
|
|
|
|
dz = dict_data_open( dictionaryFiles[ 0 ].c_str(), 0 );
|
|
|
|
|
|
|
|
if ( !dz )
|
|
|
|
throw exCantReadFile( dictionaryFiles[ 0 ] );
|
|
|
|
|
|
|
|
// 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() );
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
printf( "Loading %u abbrv\n", total );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements,
|
|
|
|
idxHeader.indexRootOffset ),
|
|
|
|
idx, idxMutex );
|
2009-03-28 17:16:12 +00:00
|
|
|
|
|
|
|
// Open a resource zip file, if there's one
|
|
|
|
resourceZip = zip_open( ( getDictionaryFilenames()[ 0 ] + ".files.zip" ).c_str(), 0, 0 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DslDictionary::~DslDictionary()
|
|
|
|
{
|
2009-03-28 17:16:12 +00:00
|
|
|
if ( resourceZip )
|
|
|
|
zip_close( resourceZip );
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( dz )
|
|
|
|
dict_data_close( dz );
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
string & headword,
|
|
|
|
list< wstring > & displayedHeadwords,
|
|
|
|
wstring & articleText )
|
|
|
|
{
|
|
|
|
wstring articleData;
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< char > chunk;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
char * articleProps;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( idxMutex );
|
|
|
|
|
|
|
|
articleProps = chunks.getBlock( address, chunk );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
uint32_t articleOffset, articleSize;
|
|
|
|
|
|
|
|
memcpy( &articleOffset, articleProps, sizeof( articleOffset ) );
|
|
|
|
memcpy( &articleSize, articleProps + sizeof( articleOffset ),
|
|
|
|
sizeof( articleSize ) );
|
|
|
|
|
|
|
|
printf( "offset = %x\n", articleOffset );
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
|
|
|
|
char * articleBody;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _( dzMutex );
|
|
|
|
|
|
|
|
articleBody = dict_data_read_( dz, articleOffset, articleSize, 0, 0 );
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( !articleBody )
|
|
|
|
throw exCantReadFile( getDictionaryFilenames()[ 0 ] );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
articleData =
|
|
|
|
DslIconv::toWstring(
|
|
|
|
DslIconv::getEncodingNameFor( DslEncoding( idxHeader.dslEncoding ) ),
|
|
|
|
articleBody, articleSize );
|
2009-04-18 17:28:23 +00:00
|
|
|
free( articleBody );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
free( articleBody );
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
size_t pos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ) );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( pos == wstring::npos )
|
|
|
|
pos = articleData.size();
|
|
|
|
|
|
|
|
wstring firstHeadword( articleData, 0, pos );
|
|
|
|
|
|
|
|
printf( "first headword = %ls\n", firstHeadword.c_str() );
|
|
|
|
|
|
|
|
// Make a headword
|
|
|
|
{
|
|
|
|
wstring str( firstHeadword );
|
|
|
|
list< wstring > lst;
|
|
|
|
|
|
|
|
processUnsortedParts( str, true );
|
|
|
|
expandOptionalParts( str, lst );
|
|
|
|
|
|
|
|
headword = Utf8::encode( lst.front() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate displayed headwords
|
|
|
|
|
|
|
|
displayedHeadwords.clear();
|
|
|
|
|
|
|
|
processUnsortedParts( firstHeadword, false );
|
|
|
|
expandOptionalParts( firstHeadword, displayedHeadwords );
|
|
|
|
|
|
|
|
// Now skip alts until we reach the body itself
|
|
|
|
while ( pos != articleData.size() )
|
|
|
|
{
|
|
|
|
if ( articleData[ pos ] == '\r' )
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if ( pos != articleData.size() )
|
|
|
|
{
|
|
|
|
if ( articleData[ pos ] == '\n' )
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( pos != articleData.size() && !isDslWs( articleData[ pos ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
// Skip any alt headwords
|
2009-04-18 17:20:12 +00:00
|
|
|
pos = articleData.find_first_of( GD_NATIVE_TO_WS( L"\n\r" ), pos );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( pos == wstring::npos )
|
|
|
|
pos = articleData.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
string DslDictionary::dslToHtml( wstring const & str )
|
|
|
|
{
|
|
|
|
ArticleDom dom( str );
|
|
|
|
|
|
|
|
string html = processNodeChildren( dom.root );
|
|
|
|
|
|
|
|
// Lines seem to indicate paragraphs in Dsls, so we enclose each line within
|
|
|
|
// a <p></p>.
|
|
|
|
|
|
|
|
for( size_t x = html.size(); x--; )
|
|
|
|
if ( html[ x ] == '\n' )
|
|
|
|
html.insert( x + 1, "</p><p>" );
|
|
|
|
|
|
|
|
return "<!-- DSL Source:\n" + Utf8::encode( str ) + "\n-->"
|
|
|
|
"<p>" + html + "</p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if ( !node.isTag )
|
|
|
|
return Html::escape( Utf8::encode( node.text ) );
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
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-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_u\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
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"*" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_opt\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
else
|
|
|
|
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
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"s" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
string filename = Utf8::encode( node.renderAsText() );
|
|
|
|
|
|
|
|
if ( Filetype::isNameOfSound( filename ) )
|
|
|
|
{
|
|
|
|
// If we have the file here, do the exact reference to this dictionary.
|
|
|
|
// Otherwise, make a global 'search' one.
|
|
|
|
|
|
|
|
string n =
|
|
|
|
FsEncoding::dirname( getDictionaryFilenames()[ 0 ] ) +
|
|
|
|
FsEncoding::separator() +
|
|
|
|
FsEncoding::encode( filename );
|
|
|
|
|
|
|
|
bool search = true;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2009-02-02 15:02:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
File::Class f( n, "rb" );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
|
|
|
n = getDictionaryFilenames()[ 0 ] + ".files" +
|
|
|
|
FsEncoding::separator() +
|
|
|
|
FsEncoding::encode( filename );
|
|
|
|
|
2009-03-28 16:16:56 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
File::Class f( n, "rb" );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
|
|
|
// Try zip file
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
if ( resourceZip )
|
2009-03-28 16:16:56 +00:00
|
|
|
{
|
|
|
|
string fname = FsEncoding::encode( filename );
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
int result = zip_name_locate( resourceZip, fname.c_str(), 0 );
|
2009-03-28 16:16:56 +00:00
|
|
|
|
|
|
|
if ( result == -1 )
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2009-02-02 15:02:57 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
search = false;
|
|
|
|
}
|
|
|
|
catch( File::Ex & )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-04-12 11:27:43 +00:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( search ? "search" : getId().c_str() ) );
|
|
|
|
url.setPath( QString::fromUtf8( filename.c_str() ) );
|
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
2009-04-10 21:07:03 +00:00
|
|
|
|
|
|
|
result += addAudioLink( ref );
|
|
|
|
|
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 ) )
|
|
|
|
{
|
2009-04-12 11:27:43 +00:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "bres" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
|
|
|
url.setPath( QString::fromUtf8( filename.c_str() ) );
|
|
|
|
|
|
|
|
result += string( "<img src=\"" ) + url.toEncoded().data()
|
2009-01-28 20:55:45 +00:00
|
|
|
+ "\" alt=\"" + Html::escape( filename ) + "\"/>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unknown file type, downgrade to a hyperlink
|
2009-04-12 11:27:43 +00:00
|
|
|
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "bres" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
|
|
|
url.setPath( QString::fromUtf8( filename.c_str() ) );
|
|
|
|
|
|
|
|
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" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<a class=\"dsl_url\" href=\"" + Html::escape( Utf8::encode( node.renderAsText() ) ) +"\">" + processNodeChildren( node ) + "</a>";
|
|
|
|
else
|
2009-04-18 17:20:12 +00:00
|
|
|
if ( node.tagName == GD_NATIVE_TO_WS( L"!trs" ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
result += "<span class=\"dsl_trs\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
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() )
|
|
|
|
result += " title=\"" + Html::escape( i->second ) + "\"";
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-04-08 16:02:12 +00:00
|
|
|
result += "<span class=\"dsl_stress\">" + processNodeChildren( node ) + Utf8::encode( wstring( 1, 0x301 ) ) + "</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
|
|
|
{
|
|
|
|
result += "<span class=\"dsl_lang\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
result += "<a class=\"dsl_ref\" href=\"bword://" + Html::escape( Utf8::encode( node.renderAsText() ) ) +"\">" + processNodeChildren( node ) + "</a>";
|
|
|
|
}
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result += "<span class=\"dsl_unknown\">" + processNodeChildren( node ) + "</span>";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
vector< wstring > StardictDictionary::findHeadwordsForSynonym( wstring const & str )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< wstring > result;
|
|
|
|
|
|
|
|
vector< WordArticleLink > chain = findArticles( str );
|
|
|
|
|
|
|
|
wstring caseFolded = Folding::applySimpleCaseOnly( str );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
string headword, articleText;
|
|
|
|
|
|
|
|
loadArticle( chain[ x ].articleOffset,
|
|
|
|
headword, articleText );
|
|
|
|
|
|
|
|
wstring headwordDecoded = Utf8::decode( headword );
|
|
|
|
|
|
|
|
if ( caseFolded != Folding::applySimpleCaseOnly( headwordDecoded ) )
|
|
|
|
{
|
|
|
|
// The headword seems to differ from the input word, which makes the
|
|
|
|
// input word its synonym.
|
|
|
|
result.push_back( headwordDecoded );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DslArticleRequestRunnable( DslArticleRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DslArticleRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
~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()
|
|
|
|
{
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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() );
|
|
|
|
}
|
|
|
|
|
|
|
|
multimap< wstring, 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 )
|
|
|
|
{
|
2009-04-16 11:33:12 +00:00
|
|
|
// Check if we're cancelled occasionally
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
|
|
|
|
|
|
|
// Now grab that article
|
|
|
|
|
|
|
|
string headword;
|
|
|
|
|
|
|
|
list< wstring > displayedHeadwords;
|
|
|
|
wstring articleBody;
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
dict.loadArticle( chain[ x ].articleOffset, headword, displayedHeadwords,
|
|
|
|
articleBody );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
string articleText;
|
|
|
|
|
|
|
|
articleText += "<span class=\"dsl_article\">";
|
|
|
|
articleText += "<div class=\"dsl_headwords\">";
|
|
|
|
|
|
|
|
for( list< wstring >::const_iterator i = displayedHeadwords.begin();
|
|
|
|
i != displayedHeadwords.end(); ++i )
|
2009-04-16 11:33:12 +00:00
|
|
|
articleText += dict.dslToHtml( *i );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
articleText += "</div>";
|
|
|
|
|
|
|
|
if ( displayedHeadwords.size() )
|
|
|
|
expandTildes( articleBody, displayedHeadwords.front() );
|
|
|
|
|
|
|
|
articleText += "<div class=\"dsl_definition\">";
|
2009-04-16 11:33:12 +00:00
|
|
|
articleText += dict.dslToHtml( articleBody );
|
2009-01-28 20:55:45 +00:00
|
|
|
articleText += "</div>";
|
|
|
|
articleText += "</span>";
|
2009-04-16 11:33:12 +00:00
|
|
|
|
2009-01-28 20:55:45 +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, string > & mapToUse =
|
|
|
|
( wordCaseFolded == headwordStripped ) ?
|
|
|
|
mainArticles : alternateArticles;
|
|
|
|
|
|
|
|
mapToUse.insert( pair< wstring, string >(
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( headword ) ),
|
|
|
|
articleText ) );
|
|
|
|
|
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() )
|
2009-04-16 11:33:12 +00:00
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
string result;
|
|
|
|
|
|
|
|
multimap< wstring, string >::const_iterator i;
|
|
|
|
|
|
|
|
for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
|
|
|
|
result += i->second;
|
|
|
|
|
|
|
|
for( i = alternateArticles.begin(); i != alternateArticles.end(); ++i )
|
|
|
|
result += i->second;
|
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
data.resize( result.size() );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
memcpy( &data.front(), result.data(), result.size() );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-16 11:33:12 +00:00
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > DslDictionary::getArticle( wstring const & word,
|
|
|
|
vector< wstring > const & alts )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
return new DslArticleRequest( word, alts, *this );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-02-02 15:02:57 +00:00
|
|
|
void loadFromFile( string const & n, vector< char > & data )
|
|
|
|
{
|
|
|
|
File::Class f( n, "rb" );
|
|
|
|
|
|
|
|
f.seekEnd();
|
|
|
|
|
|
|
|
data.resize( f.tell() );
|
|
|
|
|
|
|
|
f.rewind();
|
|
|
|
|
|
|
|
f.read( &data.front(), data.size() );
|
|
|
|
}
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
//// DslDictionary::getResource()
|
|
|
|
|
|
|
|
class DslResourceRequest;
|
|
|
|
|
|
|
|
class DslResourceRequestRunnable: public QRunnable
|
|
|
|
{
|
|
|
|
DslResourceRequest & r;
|
|
|
|
QSemaphore & hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DslResourceRequestRunnable( DslResourceRequest & r_,
|
|
|
|
QSemaphore & hasExited_ ): r( r_ ),
|
|
|
|
hasExited( hasExited_ )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DslResourceRequestRunnable()
|
|
|
|
{
|
|
|
|
hasExited.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DslResourceRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
friend class DslResourceRequestRunnable;
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
Mutex & resourceZipMutex;
|
|
|
|
zip * resourceZip;
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
string dictionaryFileName, resourceName;
|
|
|
|
|
|
|
|
QAtomicInt isCancelled;
|
|
|
|
QSemaphore hasExited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
DslResourceRequest( Mutex & resourceZipMutex_,
|
|
|
|
zip * resourceZip_,
|
|
|
|
string const & dictionaryFileName_,
|
2009-03-28 16:06:00 +00:00
|
|
|
string const & resourceName_ ):
|
2009-03-28 17:16:12 +00:00
|
|
|
resourceZipMutex( resourceZipMutex_ ),
|
|
|
|
resourceZip( resourceZip_ ),
|
2009-03-28 16:06:00 +00:00
|
|
|
dictionaryFileName( dictionaryFileName_ ),
|
|
|
|
resourceName( resourceName_ )
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->start(
|
|
|
|
new DslResourceRequestRunnable( *this, hasExited ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(); // Run from another thread by DslResourceRequestRunnable
|
|
|
|
|
|
|
|
virtual void cancel()
|
|
|
|
{
|
|
|
|
isCancelled.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
~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
|
|
|
|
if ( isCancelled )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
string n =
|
2009-03-28 16:06:00 +00:00
|
|
|
FsEncoding::dirname( dictionaryFileName ) +
|
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
|
|
|
|
|
|
|
printf( "n is %s\n", n.c_str() );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2009-02-02 15:02:57 +00:00
|
|
|
try
|
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
2009-02-02 15:02:57 +00:00
|
|
|
loadFromFile( n, data );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
n = dictionaryFileName + ".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 );
|
|
|
|
|
2009-03-28 12:35:52 +00:00
|
|
|
loadFromFile( n, data );
|
|
|
|
}
|
|
|
|
catch( File::exCantOpen & )
|
|
|
|
{
|
|
|
|
// Try reading from zip file
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
if ( resourceZip )
|
2009-03-28 12:35:52 +00:00
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
string fname = FsEncoding::encode( resourceName );
|
2009-03-28 12:35:52 +00:00
|
|
|
|
|
|
|
struct zip_stat st;
|
|
|
|
zip_file * zf;
|
|
|
|
|
|
|
|
zip_stat_init( &st );
|
|
|
|
|
2009-03-28 17:16:12 +00:00
|
|
|
Mutex::Lock _( resourceZipMutex );
|
|
|
|
|
|
|
|
int fileIndex;
|
|
|
|
|
|
|
|
if ( !isCancelled &&
|
|
|
|
( fileIndex = zip_name_locate( resourceZip, fname.c_str(), 0 ) ) != -1 &&
|
|
|
|
!zip_stat_index( resourceZip, fileIndex, 0, &st ) &&
|
|
|
|
( zf = zip_fopen_index( resourceZip, fileIndex, 0 ) ) )
|
2009-03-28 12:35:52 +00:00
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
int result;
|
2009-03-28 12:35:52 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
data.resize( st.size );
|
2009-03-28 17:16:12 +00:00
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
result = zip_fread( zf, &data.front(), data.size() );
|
|
|
|
}
|
2009-03-28 12:35:52 +00:00
|
|
|
|
|
|
|
zip_fclose( zf );
|
|
|
|
|
2009-03-28 16:06:00 +00:00
|
|
|
if ( result != (int)st.size )
|
2009-03-28 12:35:52 +00:00
|
|
|
throw; // Make it fail since we couldn't read the archive
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
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() );
|
|
|
|
|
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() );
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
catch( File::Ex & )
|
|
|
|
{
|
2009-03-28 16:06:00 +00:00
|
|
|
// No such resource -- we don't set the hasAnyData flag then
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
2009-03-28 16:06:00 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > DslDictionary::getResource( string const & name )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
2009-03-28 17:16:12 +00:00
|
|
|
return new DslResourceRequest( resourceZipMutex, resourceZip,
|
|
|
|
getDictionaryFilenames()[ 0 ], name );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static bool tryPossibleName( string const & name, string & copyTo )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File::Class f( name, "rb" );
|
|
|
|
|
|
|
|
copyTo = name;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries(
|
|
|
|
vector< string > const & fileNames,
|
|
|
|
string const & indicesDir,
|
|
|
|
Dictionary::Initializing & initializing )
|
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
|
|
|
|
|
|
|
|
if ( ( i->size() < 4 ||
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 4 ), ".dsl" ) != 0 ) &&
|
|
|
|
( i->size() < 7 ||
|
|
|
|
strcasecmp( i->c_str() + ( i->size() - 7 ), ".dsl.dz" ) != 0 ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ( tryPossibleName( baseName + "_abrv.dsl", abrvFileName ) ||
|
|
|
|
tryPossibleName( baseName + "_abrv.dsl.dz", abrvFileName ) ||
|
|
|
|
tryPossibleName( baseName + "_ABRV.DSL", abrvFileName ) ||
|
|
|
|
tryPossibleName( baseName + "_ABRV.DSL.DZ", abrvFileName ) ||
|
|
|
|
tryPossibleName( baseName + "_ABRV.DSL.dz", abrvFileName ) )
|
|
|
|
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
|
|
|
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) ||
|
2009-01-28 20:55:45 +00:00
|
|
|
indexIsOldOrBad( indexFile ) )
|
|
|
|
{
|
|
|
|
DslScanner scanner( *i );
|
|
|
|
|
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() ) );
|
|
|
|
|
|
|
|
printf( "Dictionary name: %ls\n", scanner.getDictionaryName().c_str() );
|
|
|
|
|
|
|
|
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
|
|
|
|
if ( !abrvScanner.readNextLine( curString, curOffset ) )
|
|
|
|
break;
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( curString.empty() || isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
string key = Utf8::encode( curString );
|
|
|
|
|
|
|
|
if ( !abrvScanner.readNextLine( curString, curOffset ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Warning: premature end of file %s\n", abrvFileName.c_str() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( curString.empty() || !isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Warning: malformed file %s\n", abrvFileName.c_str() );
|
|
|
|
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
|
|
|
|
|
|
|
abrv[ key ] = Utf8::encode( curString );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
printf( "%s:%s\n", i->first.c_str(), i->second.c_str() );
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Error reading abrv file %s: %s. Skipping it.\n",
|
|
|
|
abrvFileName.c_str(), e.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasString = false;
|
|
|
|
wstring curString;
|
|
|
|
size_t curOffset;
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
// Find the main headword
|
|
|
|
|
|
|
|
if ( !hasString && !scanner.readNextLine( curString, curOffset ) )
|
|
|
|
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
|
|
|
{
|
|
|
|
fprintf( stderr, "Warning: garbage string in %s at offset 0x%X\n", i->c_str(), curOffset );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, got the headword
|
|
|
|
|
|
|
|
list< wstring > allEntryWords;
|
|
|
|
|
|
|
|
processUnsortedParts( curString, true );
|
|
|
|
expandOptionalParts( curString, allEntryWords );
|
|
|
|
|
|
|
|
uint32_t articleOffset = curOffset;
|
|
|
|
|
|
|
|
//printf( "Headword: %ls\n", curString.c_str() );
|
|
|
|
|
|
|
|
// More headwords may follow
|
|
|
|
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
if ( ! ( hasString = scanner.readNextLine( curString, curOffset ) ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Warning: premature end of file %s\n", i->c_str() );
|
|
|
|
exit( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( curString.empty() || isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
break; // No more headwords
|
|
|
|
|
|
|
|
printf( "Alt headword: %ls\n", curString.c_str() );
|
|
|
|
|
|
|
|
processUnsortedParts( curString, true );
|
|
|
|
expandTildes( curString, allEntryWords.front() );
|
|
|
|
expandOptionalParts( curString, allEntryWords );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-08 16:02:12 +00:00
|
|
|
indexedWords.addWord( *j, descOffset );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the article's body
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
if ( ! ( hasString = scanner.readNextLine( curString, curOffset ) ) )
|
|
|
|
break;
|
|
|
|
|
2009-04-17 22:29:06 +00:00
|
|
|
if ( curString.size() && !isDslWs( curString[ 0 ] ) )
|
2009-01-28 20:55:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ) );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
|
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
dictionaries.push_back( new DslDictionary( dictId,
|
|
|
|
indexFile,
|
|
|
|
dictFiles ) );
|
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "DSL dictionary reading failed: %s, error: %s\n",
|
|
|
|
i->c_str(), e.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|