2009-01-13 14:32:07 +01:00
|
|
|
/* -*- compile-command: "g++ -DDEBUG -O0 -Wall -g -o dict2dawg dict2dawg.cpp"; -*- */
|
2006-04-12 06:39:49 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* adapted from perl code that was itself adapted from C++ code
|
|
|
|
* Copyright (C) 2000 Falk Hueffner
|
|
|
|
|
2009-01-13 14:32:07 +01:00
|
|
|
* This version Copyright (C) 2002,2006-2009 Eric House
|
|
|
|
* (xwords@eehouse.org)
|
2006-04-12 06:39:49 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
|
|
|
**************************************************************************
|
|
|
|
* inputs: 0. Name of file mapping letters to 0..31 values. In English
|
|
|
|
* case just contains A..Z. This will be used to translate the tries
|
|
|
|
* on output.
|
|
|
|
* 1. Max number of bytes per binary output file.
|
|
|
|
*
|
|
|
|
* 2. Basename of binary files for output.
|
|
|
|
|
|
|
|
* 3. Name of file to which to write the number of the
|
|
|
|
* startNode, since I'm not rewriting a bunch of code to expect Falk's
|
|
|
|
* '*' node at the start.
|
|
|
|
*
|
|
|
|
|
|
|
|
* In STDIN, the text file to be compressed. It absolutely
|
|
|
|
* must be sorted. The sort doesn't have to follow the order in the
|
|
|
|
* map file, however.
|
|
|
|
|
|
|
|
* This is meant eventually to be runnable as part of a cgi system for
|
|
|
|
* letting users generate Crosswords dicts online.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2008-09-18 05:44:43 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-04-13 04:57:43 +02:00
|
|
|
#include <netinet/in.h>
|
2006-04-14 10:23:28 +02:00
|
|
|
#include <assert.h>
|
2009-01-25 21:13:36 +01:00
|
|
|
#include <errno.h>
|
2008-09-18 05:44:43 +02:00
|
|
|
#include <algorithm>
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
|
2009-01-07 06:13:45 +01:00
|
|
|
typedef unsigned char Letter; // range 1..26 for English, always < 64
|
2006-04-13 04:57:43 +02:00
|
|
|
typedef unsigned int Node;
|
|
|
|
typedef std::vector<Node> NodeList;
|
2009-01-07 06:13:45 +01:00
|
|
|
typedef std::vector<Letter*> WordList;
|
2006-04-13 04:57:43 +02:00
|
|
|
|
2006-04-29 18:40:48 +02:00
|
|
|
#define VERSION_STR "$Rev$"
|
2006-04-13 05:49:41 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
#define MAX_WORD_LEN 15
|
|
|
|
#define T2ABUFLEN(s) (((s)*4)+3)
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
int gFirstDiff;
|
2006-04-14 07:23:30 +02:00
|
|
|
|
2009-01-07 06:13:45 +01:00
|
|
|
static Letter gCurrentWordBuf[MAX_WORD_LEN+1] = { '\0' };
|
2006-04-14 07:23:30 +02:00
|
|
|
// this will never change for non-sort case
|
2009-01-07 06:13:45 +01:00
|
|
|
static Letter* gCurrentWord = gCurrentWordBuf;
|
2006-04-14 07:23:30 +02:00
|
|
|
static int gCurrentWordLen;
|
|
|
|
|
2009-01-25 21:13:36 +01:00
|
|
|
static bool gDone = false;
|
2009-01-07 06:03:13 +01:00
|
|
|
static unsigned int gNextWordIndex;
|
2006-04-14 07:23:30 +02:00
|
|
|
static void (*gReadWordProc)(void) = NULL;
|
2009-01-25 21:13:36 +01:00
|
|
|
static NodeList gNodes; // final array of nodes
|
|
|
|
static unsigned int gNBytesPerOutfile = 0xFFFFFFFF;
|
|
|
|
static char* gTableFile = NULL;
|
2009-01-13 14:32:07 +01:00
|
|
|
static bool gIsMultibyte = false;
|
2009-01-25 21:13:36 +01:00
|
|
|
static const char* gEncoding = NULL;
|
|
|
|
static char* gOutFileBase = NULL;
|
|
|
|
static char* gStartNodeOut = NULL;
|
2006-04-14 07:23:30 +02:00
|
|
|
static FILE* gInFile = NULL;
|
2009-01-25 21:13:36 +01:00
|
|
|
static bool gKillIfMissing = true;
|
|
|
|
static char gTermChar = '\n';
|
|
|
|
static bool gDumpText = false; // dump the dict as text after?
|
|
|
|
static char* gCountFile = NULL;
|
|
|
|
static const char* gLang = NULL;
|
|
|
|
static char* gBytesPerNodeFile = NULL; // where to write whether node
|
2006-05-02 15:28:07 +02:00
|
|
|
// size 3 or 4
|
2006-04-12 06:39:49 +02:00
|
|
|
int gWordCount = 0;
|
2009-01-13 14:32:07 +01:00
|
|
|
std::map<Letter,wchar_t> gTableHash;
|
2006-04-12 06:39:49 +02:00
|
|
|
int gBlankIndex;
|
|
|
|
std::vector<char> gRevMap;
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-12 06:39:49 +02:00
|
|
|
bool gDebug = false;
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-13 04:57:43 +02:00
|
|
|
std::map<NodeList, int> gSubsHash;
|
2006-04-12 06:39:49 +02:00
|
|
|
bool gForceFour = false; // use four bytes regardless of need?
|
2006-05-02 15:28:07 +02:00
|
|
|
static int gFileSize = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
int gNBytesPerNode;
|
|
|
|
bool gUseUnicode;
|
2007-02-17 18:06:05 +01:00
|
|
|
int gLimLow = 2;
|
|
|
|
int gLimHigh = MAX_WORD_LEN;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
|
2006-04-13 05:52:48 +02:00
|
|
|
// OWL is 1.7M
|
2008-09-18 05:44:43 +02:00
|
|
|
#define MAX_POOL_SIZE (10 * 0x100000)
|
2006-04-13 04:57:43 +02:00
|
|
|
#define ERROR_EXIT(...) error_exit( __LINE__, __VA_ARGS__ );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-14 07:23:30 +02:00
|
|
|
static char* parseARGV( int argc, char** argv, const char** inFileName );
|
2006-04-12 06:39:49 +02:00
|
|
|
static void usage( const char* name );
|
2006-04-13 04:57:43 +02:00
|
|
|
static void error_exit( int line, const char* fmt, ... );
|
2006-04-12 06:39:49 +02:00
|
|
|
static void makeTableHash( void );
|
2009-03-14 20:22:15 +01:00
|
|
|
static WordList* parseAndSort( void );
|
2006-04-13 04:57:43 +02:00
|
|
|
static void printWords( WordList* strings );
|
2009-01-07 06:13:45 +01:00
|
|
|
static bool firstBeforeSecond( const Letter* lhs, const Letter* rhs );
|
|
|
|
static char* tileToAscii( char* out, int outSize, const Letter* in );
|
2006-04-13 04:57:43 +02:00
|
|
|
static int buildNode( int depth );
|
|
|
|
static void TrieNodeSetIsLastSibling( Node* nodeR, bool isLastSibling );
|
|
|
|
static int addNodes( NodeList& newedgesR );
|
|
|
|
static void TrieNodeSetIsTerminal( Node* nodeR, bool isTerminal );
|
|
|
|
static bool TrieNodeGetIsTerminal( Node node );
|
|
|
|
static void TrieNodeSetIsLastSibling( Node* nodeR, bool isLastSibling );
|
|
|
|
static bool TrieNodeGetIsLastSibling( Node node );
|
2009-01-13 14:32:07 +01:00
|
|
|
static void TrieNodeSetLetter( Node* nodeR, Letter letter );
|
|
|
|
static Letter TrieNodeGetLetter( Node node );
|
2006-04-13 04:57:43 +02:00
|
|
|
static void TrieNodeSetFirstChildOffset( Node* nodeR, int fco );
|
|
|
|
static int TrieNodeGetFirstChildOffset( Node node );
|
|
|
|
static int findSubArray( NodeList& newedgesR );
|
|
|
|
static void registerSubArray( NodeList& edgesR, int nodeLoc );
|
2009-01-13 14:32:07 +01:00
|
|
|
static Node MakeTrieNode( Letter letter, bool isTerminal,
|
|
|
|
int firstChildOffset, bool isLastSibling );
|
2006-04-13 04:57:43 +02:00
|
|
|
static void printNodes( NodeList& nodesR );
|
|
|
|
static void printNode( int index, Node node );
|
|
|
|
static void moveTopToFront( int* firstRef );
|
|
|
|
static void writeOutStartNode( const char* startNodeOut,
|
|
|
|
int firstRootChildOffset );
|
|
|
|
static void emitNodes( unsigned int nBytesPerOutfile, const char* outFileBase );
|
|
|
|
static void outputNode( Node node, int nBytes, FILE* outfile );
|
|
|
|
static void printOneLevel( int index, char* str, int curlen );
|
2006-04-14 07:23:30 +02:00
|
|
|
static void readFromSortedArray( void );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char** argv )
|
|
|
|
{
|
2006-04-14 07:23:30 +02:00
|
|
|
gReadWordProc = readFromSortedArray;
|
|
|
|
|
|
|
|
const char* inFileName;
|
|
|
|
if ( NULL == parseARGV( argc, argv, &inFileName ) ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
usage(argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-01-28 04:32:21 +01:00
|
|
|
try_english:
|
2009-01-25 21:13:36 +01:00
|
|
|
char buf[32];
|
|
|
|
const char* locale = "";
|
|
|
|
if ( !!gLang && !!gEncoding ) {
|
|
|
|
snprintf( buf, sizeof(buf), "%s.%s", gLang, gEncoding );
|
|
|
|
locale = buf;
|
|
|
|
}
|
|
|
|
char* oldloc = setlocale( LC_ALL, locale );
|
|
|
|
if ( !oldloc ) {
|
2009-01-28 04:32:21 +01:00
|
|
|
// special case for spiritone.net, where non-US locale files aren't
|
|
|
|
// available. Since utf-8 is the same for all locales, we can get by
|
|
|
|
// with en_US instead
|
|
|
|
if ( gIsMultibyte && 0 != strcmp( gLang, "en_US" )) {
|
|
|
|
gLang = "en_US";
|
|
|
|
goto try_english;
|
|
|
|
}
|
|
|
|
|
2009-01-25 21:13:36 +01:00
|
|
|
ERROR_EXIT( "setlocale(%s) failed, error: %s", locale,
|
|
|
|
strerror(errno) );
|
|
|
|
} else {
|
|
|
|
fprintf( stderr, "old locale: %s\n", oldloc );
|
|
|
|
}
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
makeTableHash();
|
|
|
|
|
|
|
|
// Do I need this stupid thing? Better to move the first row to
|
|
|
|
// the front of the array and patch everything else. Or fix the
|
|
|
|
// non-palm dictionary format to include the offset of the first
|
|
|
|
// node.
|
|
|
|
|
|
|
|
Node dummyNode = (Node)0xFFFFFFFF;
|
|
|
|
assert( sizeof(Node) == 4 );
|
|
|
|
gNodes.push_back(dummyNode);
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
if ( NULL == inFileName ) {
|
|
|
|
gInFile = stdin;
|
|
|
|
} else {
|
|
|
|
gInFile = fopen( inFileName, "r" );
|
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-14 07:23:30 +02:00
|
|
|
(*gReadWordProc)();
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
int firstRootChildOffset = buildNode(0);
|
2006-04-13 04:57:43 +02:00
|
|
|
moveTopToFront( &firstRootChildOffset );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gStartNodeOut ) {
|
|
|
|
writeOutStartNode( gStartNodeOut, firstRootChildOffset );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "\n... dumping table ...\n" );
|
|
|
|
printNodes( gNodes );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-12 06:39:49 +02:00
|
|
|
// write out the number of nodes if requested
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gCountFile ) {
|
|
|
|
FILE* OFILE;
|
|
|
|
OFILE = fopen( gCountFile, "w" );
|
|
|
|
unsigned long be = htonl( gWordCount );
|
|
|
|
fwrite( &be, sizeof(be), 1, OFILE );
|
|
|
|
fclose( OFILE );
|
2007-02-17 18:06:05 +01:00
|
|
|
fprintf( stderr, "Wrote %d (word count) to %s\n", gWordCount,
|
|
|
|
gCountFile );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gOutFileBase ) {
|
|
|
|
emitNodes( gNBytesPerOutfile, gOutFileBase );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDumpText && gNodes.size() > 0 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
char buf[(MAX_WORD_LEN*2)+1];
|
2006-04-13 04:57:43 +02:00
|
|
|
printOneLevel( firstRootChildOffset, buf, 0 );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gBytesPerNodeFile ) {
|
|
|
|
FILE* OFILE = fopen( gBytesPerNodeFile, "w" );
|
|
|
|
fprintf( OFILE, "%d", gNBytesPerNode );
|
|
|
|
fclose( OFILE );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
fprintf( stderr, "Used %d per node.\n", gNBytesPerNode );
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
if ( NULL != inFileName ) {
|
|
|
|
fclose( gInFile );
|
|
|
|
}
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
} /* main */
|
|
|
|
|
|
|
|
// We now have an array of nodes with the last subarray being the
|
|
|
|
// logical top of the tree. Move them to the start, fixing all fco
|
|
|
|
// refs, so that legacy code like Palm can assume top==0.
|
|
|
|
//
|
|
|
|
// Note: It'd probably be a bit faster to integrate this with emitNodes
|
|
|
|
// -- unless I need to have an in-memory list that can be used for
|
|
|
|
// lookups. But that's best for debugging, so keep it this way for now.
|
|
|
|
//
|
|
|
|
// Also Note: the first node is a dummy that can and should be tossed
|
|
|
|
// now.
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
moveTopToFront( int* firstRef )
|
|
|
|
{
|
|
|
|
int firstChild = *firstRef;
|
|
|
|
*firstRef = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
NodeList lastSub;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( firstChild > 0 ) {
|
|
|
|
lastSub.assign( gNodes.begin() + firstChild, gNodes.end() );
|
|
|
|
gNodes.erase( gNodes.begin() + firstChild, gNodes.end() );
|
|
|
|
} else if ( gWordCount != 0 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "there should be no words!!" );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
// remove the first (garbage) node
|
2006-04-13 04:57:43 +02:00
|
|
|
gNodes.erase( gNodes.begin() );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
int diff;
|
|
|
|
if ( firstChild > 0 ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
// -1 because all move down by 1; see prev line
|
2006-04-13 04:57:43 +02:00
|
|
|
diff = lastSub.size() - 1;
|
|
|
|
if ( diff < 0 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "something wrong with lastSub.size()" );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 04:57:43 +02:00
|
|
|
diff = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// stick it on the front
|
2006-04-13 04:57:43 +02:00
|
|
|
gNodes.insert( gNodes.begin(), lastSub.begin(), lastSub.end() );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
// We add diff to everything. There's no subtracting because
|
2006-04-12 06:39:49 +02:00
|
|
|
// nobody had any refs to the top list.
|
|
|
|
|
2009-01-07 06:03:13 +01:00
|
|
|
unsigned int ii;
|
|
|
|
for ( ii = 0; ii < gNodes.size(); ++ii ) {
|
|
|
|
int fco = TrieNodeGetFirstChildOffset( gNodes[ii] );
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( fco != 0 ) { // 0 means NONE, not 0th!!
|
2009-01-07 06:03:13 +01:00
|
|
|
TrieNodeSetFirstChildOffset( &gNodes[ii], fco + diff );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // moveTopToFront
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static int
|
|
|
|
buildNode( int depth )
|
|
|
|
{
|
|
|
|
if ( gCurrentWordLen == depth ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
// End of word reached. If the next word isn't a continuation
|
|
|
|
// of the current one, then we've reached the bottom of the
|
|
|
|
// recursion tree.
|
2006-04-14 07:23:30 +02:00
|
|
|
(*gReadWordProc)();
|
2006-04-13 04:57:43 +02:00
|
|
|
if (gFirstDiff < depth || gDone) {
|
2006-04-12 06:39:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
NodeList newedges;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
bool wordEnd;
|
2006-04-12 06:39:49 +02:00
|
|
|
do {
|
2009-01-13 14:32:07 +01:00
|
|
|
Letter letter = gCurrentWord[depth];
|
2006-04-13 04:57:43 +02:00
|
|
|
bool isTerminal = (gCurrentWordLen - 1) == depth;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
int nodeOffset = buildNode( depth + 1 );
|
|
|
|
Node newNode = MakeTrieNode( letter, isTerminal, nodeOffset, false );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
wordEnd = (gFirstDiff != depth) || gDone;
|
|
|
|
if ( wordEnd ) {
|
|
|
|
TrieNodeSetIsLastSibling( &newNode, true );
|
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
newedges.push_back( newNode );
|
|
|
|
} while ( !wordEnd );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
return addNodes( newedges );
|
2006-04-12 06:39:49 +02:00
|
|
|
} // buildNode
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static int
|
|
|
|
addNodes( NodeList& newedgesR )
|
|
|
|
{
|
|
|
|
int found = findSubArray( newedgesR );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( found == 0 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "0 is an invalid match!!!" );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( found < 0 ) {
|
|
|
|
found = gNodes.size();
|
2006-04-13 05:52:48 +02:00
|
|
|
#if defined DEBUG && defined SEVERE_DEBUG
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "adding...\n" );
|
|
|
|
printNodes( newedgesR );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-13 04:57:43 +02:00
|
|
|
gNodes.insert( gNodes.end(), newedgesR.begin(), newedgesR.end() );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
registerSubArray( newedgesR, found );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDebug ) {
|
2007-12-02 20:13:25 +01:00
|
|
|
fprintf( stderr, "%s => %d\n", __func__, found );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-13 04:57:43 +02:00
|
|
|
return found;
|
2006-04-12 06:39:49 +02:00
|
|
|
} // addNodes
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
printNode( int index, Node node )
|
|
|
|
{
|
2009-01-13 14:32:07 +01:00
|
|
|
Letter letter = TrieNodeGetLetter(node);
|
2006-04-13 05:49:41 +02:00
|
|
|
assert( letter < gRevMap.size() );
|
2006-04-13 04:57:43 +02:00
|
|
|
fprintf( stderr,
|
|
|
|
"[%d] letter=%d(%c); isTerminal=%s; isLastSib=%s; fco=%d;\n",
|
|
|
|
index, letter, gRevMap[letter],
|
|
|
|
TrieNodeGetIsTerminal(node)?"true":"false",
|
|
|
|
TrieNodeGetIsLastSibling(node)?"true":"false",
|
|
|
|
TrieNodeGetFirstChildOffset(node));
|
2006-04-12 06:39:49 +02:00
|
|
|
} // printNode
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
printNodes( NodeList& nodesR )
|
|
|
|
{
|
2009-01-07 06:03:13 +01:00
|
|
|
unsigned int ii;
|
|
|
|
for ( ii = 0; ii < nodesR.size(); ++ii ) {
|
|
|
|
Node node = nodesR[ii];
|
|
|
|
printNode( ii, node );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashing. We'll keep a hash of offsets into the existing nodes
|
|
|
|
// array, and as the key use a string that represents the entire sub
|
|
|
|
// array. Since the key is what we're matching for, there should never
|
|
|
|
// be more than one value per hash and so we don't need buckets.
|
|
|
|
// Return -1 if there's no match.
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static int
|
|
|
|
findSubArray( NodeList& newedgesR )
|
|
|
|
{
|
|
|
|
std::map<NodeList, int>::iterator iter = gSubsHash.find( newedgesR );
|
|
|
|
if ( iter != gSubsHash.end() ) {
|
|
|
|
return iter->second;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} // findSubArray
|
|
|
|
|
|
|
|
// add to the hash
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
registerSubArray( NodeList& edgesR, int nodeLoc )
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::map<NodeList, int>::iterator iter = gSubsHash.find( edgesR );
|
|
|
|
if ( iter != gSubsHash.end() ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "entry for key shouldn't exist!!" );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
#endif
|
|
|
|
gSubsHash[edgesR] = nodeLoc;
|
2006-04-12 06:39:49 +02:00
|
|
|
} // registerSubArray
|
|
|
|
|
2009-01-07 06:13:45 +01:00
|
|
|
static int
|
|
|
|
wordlen( const Letter* word )
|
|
|
|
{
|
|
|
|
const char* str = (const char*)word;
|
|
|
|
return strlen( str );
|
|
|
|
}
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
static void
|
2006-04-14 07:23:30 +02:00
|
|
|
readFromSortedArray( void )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2006-04-14 07:23:30 +02:00
|
|
|
// The first time we need a new word, we read 'em all in.
|
|
|
|
static WordList* sInputStrings = NULL; // we'll just let this leak
|
|
|
|
|
|
|
|
if ( sInputStrings == NULL ) {
|
2009-03-14 20:22:15 +01:00
|
|
|
sInputStrings = parseAndSort();
|
2006-04-14 07:23:30 +02:00
|
|
|
gNextWordIndex = 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
|
|
|
printWords( sInputStrings );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
for ( ; ; ) {
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter* word = (Letter*)"";
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
if ( !gDone ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
gDone = gNextWordIndex == sInputStrings->size();
|
|
|
|
if ( !gDone ) {
|
|
|
|
word = sInputStrings->at(gNextWordIndex++);
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2007-02-17 18:06:05 +01:00
|
|
|
} else if ( gDebug ) {
|
|
|
|
fprintf( stderr, "gDone set to true\n" );
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2007-02-17 18:06:05 +01:00
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2007-02-17 18:06:05 +01:00
|
|
|
if ( gDebug ) {
|
|
|
|
char buf[T2ABUFLEN(MAX_WORD_LEN)];
|
|
|
|
fprintf( stderr, "%s: got word: %s\n", __func__,
|
|
|
|
tileToAscii( buf, sizeof(buf), word ) );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2007-02-17 18:06:05 +01:00
|
|
|
}
|
|
|
|
int numCommonLetters = 0;
|
2009-01-07 06:13:45 +01:00
|
|
|
int len = wordlen( word );
|
2007-02-17 18:06:05 +01:00
|
|
|
if ( gCurrentWordLen < len ) {
|
|
|
|
len = gCurrentWordLen;
|
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
while ( gCurrentWord[numCommonLetters] == word[numCommonLetters]
|
|
|
|
&& numCommonLetters < len ) {
|
|
|
|
++numCommonLetters;
|
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
gFirstDiff = numCommonLetters;
|
2009-01-07 06:13:45 +01:00
|
|
|
if ( (gCurrentWordLen > 0) && (wordlen(word) > 0)
|
2007-02-17 18:06:05 +01:00
|
|
|
&& !firstBeforeSecond( gCurrentWord, word ) ) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
|
|
|
char buf1[T2ABUFLEN(MAX_WORD_LEN)];
|
|
|
|
char buf2[T2ABUFLEN(MAX_WORD_LEN)];
|
|
|
|
fprintf( stderr,
|
|
|
|
"%s: words %s and %s are the same or out of order\n",
|
|
|
|
__func__,
|
|
|
|
tileToAscii( buf1, sizeof(buf1), gCurrentWord ),
|
|
|
|
tileToAscii( buf2, sizeof(buf2), word ) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gCurrentWord = word;
|
2009-01-07 06:13:45 +01:00
|
|
|
gCurrentWordLen = wordlen(word);
|
2007-02-17 18:06:05 +01:00
|
|
|
break;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDebug ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
char buf[T2ABUFLEN(MAX_WORD_LEN)];
|
2006-04-13 05:49:41 +02:00
|
|
|
fprintf( stderr, "gCurrentWord now %s\n",
|
|
|
|
tileToAscii( buf, sizeof(buf), gCurrentWord) );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-14 07:23:30 +02:00
|
|
|
} // readFromSortedArray
|
|
|
|
|
2009-01-13 14:32:07 +01:00
|
|
|
static wchar_t
|
|
|
|
getWideChar( FILE* file )
|
|
|
|
{
|
|
|
|
wchar_t dest;
|
|
|
|
char src[4] = { '\0' };
|
|
|
|
const char* srcp = src;
|
|
|
|
int ii;
|
|
|
|
mbstate_t ps = {0};
|
|
|
|
|
|
|
|
for ( ii = 0; ; ++ii ) {
|
|
|
|
int byt = getc( file );
|
|
|
|
size_t siz;
|
|
|
|
|
|
|
|
if ( byt == EOF || byt == gTermChar ) {
|
2009-03-14 20:22:15 +01:00
|
|
|
assert( 0 == ii );
|
2009-01-13 14:32:07 +01:00
|
|
|
dest = byt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( ii < 4 );
|
|
|
|
src[ii] = byt;
|
|
|
|
siz = mbsrtowcs( &dest, &srcp, 1, &ps );
|
|
|
|
|
|
|
|
if ( siz == (size_t)-1 ) {
|
|
|
|
continue;
|
|
|
|
} else if ( siz == 1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fprintf( stderr, "%s=>%lc\n", __func__, dest );
|
|
|
|
return dest;
|
|
|
|
} // getWideChar
|
|
|
|
|
2009-01-07 06:13:45 +01:00
|
|
|
static Letter*
|
|
|
|
readOneWord( Letter* wordBuf, int bufLen, int* lenp, bool* gotEOF )
|
2006-04-14 07:23:30 +02:00
|
|
|
{
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter* result = NULL;
|
2006-04-14 07:23:30 +02:00
|
|
|
int count = 0;
|
|
|
|
bool dropWord = false;
|
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
// for each byte, append to an internal buffer up to size limit.
|
|
|
|
// On reaching an end-of-word or EOF, check if the word formed is
|
|
|
|
// within the length range and contains no unknown chars. If yes,
|
|
|
|
// return it. If no, start over ONLY IF the terminator was not
|
|
|
|
// EOF.
|
2006-04-14 07:23:30 +02:00
|
|
|
for ( ; ; ) {
|
2009-01-13 14:32:07 +01:00
|
|
|
wchar_t byt = gIsMultibyte? getWideChar( gInFile ) : getc( gInFile );
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
// EOF is special: we don't try for another word even if
|
|
|
|
// dropWord is true; we must leave now.
|
|
|
|
if ( byt == EOF || byt == gTermChar ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
bool isEOF = byt == EOF;
|
|
|
|
*gotEOF = isEOF;
|
|
|
|
|
2008-10-08 06:37:44 +02:00
|
|
|
assert( isEOF || count < bufLen || dropWord );
|
2007-02-17 18:06:05 +01:00
|
|
|
if ( !dropWord && (count >= gLimLow) && (count <= gLimHigh) ) {
|
|
|
|
assert( count < bufLen );
|
|
|
|
wordBuf[count] = '\0';
|
|
|
|
result = wordBuf;
|
|
|
|
*lenp = count;
|
|
|
|
++gWordCount;
|
|
|
|
break;
|
|
|
|
} else if ( isEOF ) {
|
|
|
|
assert( !result );
|
2006-04-14 07:23:30 +02:00
|
|
|
break;
|
2007-02-17 18:06:05 +01:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
|
|
|
char buf[T2ABUFLEN(count)];
|
|
|
|
wordBuf[count] = '\0';
|
2009-01-25 21:13:36 +01:00
|
|
|
fprintf( stderr, "%s: dropping word (len %d>=%d): %s\n",
|
|
|
|
__func__, count, gLimHigh,
|
|
|
|
tileToAscii( buf, sizeof(buf), wordBuf ) );
|
2006-04-14 07:23:30 +02:00
|
|
|
}
|
2007-02-17 18:06:05 +01:00
|
|
|
#endif
|
|
|
|
count = 0; // we'll start over
|
|
|
|
dropWord = false;
|
|
|
|
|
|
|
|
} else if ( count >= bufLen ) {
|
|
|
|
// Just drop it...
|
|
|
|
dropWord = true;
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
// Don't call into the hashtable twice here!!
|
|
|
|
} else if ( gTableHash.find(byt) != gTableHash.end() ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
assert( count < bufLen );
|
2009-01-13 14:32:07 +01:00
|
|
|
wordBuf[count++] = gTableHash[byt];
|
2007-02-17 18:06:05 +01:00
|
|
|
if ( count >= bufLen ) {
|
2008-10-08 06:37:44 +02:00
|
|
|
dropWord = true;
|
2007-02-17 18:06:05 +01:00
|
|
|
}
|
|
|
|
} else if ( gKillIfMissing || !dropWord ) {
|
|
|
|
char buf[T2ABUFLEN(count)];
|
|
|
|
wordBuf[count] = '\0';
|
|
|
|
|
|
|
|
tileToAscii( buf, sizeof(buf), wordBuf );
|
|
|
|
|
|
|
|
if ( gKillIfMissing ) {
|
2009-01-13 14:32:07 +01:00
|
|
|
ERROR_EXIT( "chr %lc (%d/0x%x) not in map file %s\n"
|
2007-02-17 18:06:05 +01:00
|
|
|
"last word was %s\n",
|
2009-01-13 14:32:07 +01:00
|
|
|
byt, (int)byt, (int)byt, gTableFile, buf );
|
2007-02-17 18:06:05 +01:00
|
|
|
} else if ( !dropWord ) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "%s: chr %c (%d) not in map file %s\n"
|
|
|
|
"dropping partial word %s\n", __func__,
|
|
|
|
(char)byt, (int)byt, gTableFile, buf );
|
2006-04-14 07:23:30 +02:00
|
|
|
}
|
2007-02-17 18:06:05 +01:00
|
|
|
#endif
|
|
|
|
dropWord = true;
|
2006-04-14 07:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ( NULL != result ) {
|
2009-01-13 14:32:07 +01:00
|
|
|
// char buf[T2ABUFLEN(MAX_WORD_LEN)];
|
2007-12-02 20:13:25 +01:00
|
|
|
// fprintf( stderr, "%s returning %s\n", __func__,
|
2006-04-14 07:23:30 +02:00
|
|
|
// tileToAscii( buf, sizeof(buf), result ) );
|
|
|
|
// }
|
|
|
|
return result;
|
|
|
|
} // readOneWord
|
|
|
|
|
|
|
|
static void
|
|
|
|
readFromFile( void )
|
|
|
|
{
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter wordBuf[MAX_WORD_LEN+1];
|
2006-06-28 16:11:46 +02:00
|
|
|
static bool s_eof = false;
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter* word;
|
2006-04-14 07:23:30 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
gDone = s_eof;
|
2007-02-17 18:06:05 +01:00
|
|
|
|
|
|
|
// Repeat until we get a new word that's not "out-of-order". When
|
|
|
|
// we see this the problem isn't failure to sort, it's duplicates.
|
|
|
|
// So dropping is ok. The alternative would be detecting dupes
|
|
|
|
// during the sort. This seems easier.
|
|
|
|
for ( ; ; ) {
|
|
|
|
if ( !gDone ) {
|
|
|
|
word = readOneWord( wordBuf, sizeof(wordBuf), &len, &s_eof );
|
|
|
|
gDone = NULL == word;
|
|
|
|
}
|
|
|
|
if ( gDone ) {
|
2009-01-07 06:13:45 +01:00
|
|
|
word = (Letter*)"";
|
2007-02-17 18:06:05 +01:00
|
|
|
len = 0;
|
|
|
|
}
|
2006-04-14 07:23:30 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
int numCommonLetters = 0;
|
|
|
|
if ( gCurrentWordLen < len ) {
|
|
|
|
len = gCurrentWordLen;
|
|
|
|
}
|
2006-04-14 07:23:30 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
while ( gCurrentWord[numCommonLetters] == word[numCommonLetters]
|
|
|
|
&& numCommonLetters < len ) {
|
|
|
|
++numCommonLetters;
|
|
|
|
}
|
2006-04-14 07:23:30 +02:00
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
gFirstDiff = numCommonLetters;
|
2009-01-07 06:13:45 +01:00
|
|
|
if ( (gCurrentWordLen > 0) && (wordlen(word) > 0)
|
2007-02-17 18:06:05 +01:00
|
|
|
&& !firstBeforeSecond( gCurrentWord, word ) ) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
|
|
|
char buf1[T2ABUFLEN(MAX_WORD_LEN)];
|
|
|
|
char buf2[T2ABUFLEN(MAX_WORD_LEN)];
|
|
|
|
fprintf( stderr,
|
|
|
|
"%s: words %s and %s are the smae or out of order\n",
|
|
|
|
__func__,
|
|
|
|
tileToAscii( buf1, sizeof(buf1), gCurrentWord ),
|
|
|
|
tileToAscii( buf2, sizeof(buf2), word ) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2006-04-14 07:23:30 +02:00
|
|
|
}
|
2009-01-07 06:13:45 +01:00
|
|
|
gCurrentWordLen = wordlen(word);
|
|
|
|
strncpy( (char*)gCurrentWordBuf, (char*)word, sizeof(gCurrentWordBuf) );
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ( gDebug ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
char buf[T2ABUFLEN(MAX_WORD_LEN)];
|
2006-04-14 07:23:30 +02:00
|
|
|
fprintf( stderr, "gCurrentWord now %s\n",
|
|
|
|
tileToAscii( buf, sizeof(buf), gCurrentWord) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // readFromFile
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
static bool
|
2009-01-07 06:13:45 +01:00
|
|
|
firstBeforeSecond( const Letter* lhs, const Letter* rhs )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2009-01-07 06:13:45 +01:00
|
|
|
bool gt = 0 > strcmp( (char*)lhs, (char*)rhs );
|
2006-04-12 06:39:49 +02:00
|
|
|
return gt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2009-01-07 06:13:45 +01:00
|
|
|
tileToAscii( char* out, int outSize, const Letter* in )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2007-02-17 18:06:05 +01:00
|
|
|
char tiles[outSize];
|
|
|
|
int tilesLen = 1;
|
|
|
|
tiles[0] = '[';
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
char* orig = out;
|
|
|
|
for ( ; ; ) {
|
2009-01-13 14:32:07 +01:00
|
|
|
Letter ch = *in++;
|
2006-04-12 06:39:49 +02:00
|
|
|
if ( '\0' == ch ) {
|
|
|
|
break;
|
|
|
|
}
|
2009-01-13 14:32:07 +01:00
|
|
|
assert( ch < gRevMap.size() );
|
2006-04-12 06:39:49 +02:00
|
|
|
*out++ = gRevMap[ch];
|
2007-02-17 18:06:05 +01:00
|
|
|
tilesLen += sprintf( &tiles[tilesLen], "%d,", ch );
|
2006-04-13 05:49:41 +02:00
|
|
|
assert( (out - orig) < outSize );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2007-02-17 18:06:05 +01:00
|
|
|
|
2009-01-13 14:32:07 +01:00
|
|
|
assert( tilesLen+1 < outSize );
|
2007-02-17 18:06:05 +01:00
|
|
|
tiles[tilesLen] = ']';
|
|
|
|
tiles[tilesLen+1] = '\0';
|
|
|
|
strcpy( out, tiles );
|
|
|
|
|
2006-04-12 06:39:49 +02:00
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static WordList*
|
2009-03-14 20:22:15 +01:00
|
|
|
parseAndSort( void )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2006-04-13 04:57:43 +02:00
|
|
|
WordList* wordlist = new WordList;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
// allocate storage for the actual chars. wordlist's char*
|
|
|
|
// elements will point into this. It'll leak. So what.
|
|
|
|
|
2006-05-02 15:28:07 +02:00
|
|
|
int memleft = gFileSize;
|
|
|
|
if ( memleft == 0 ) {
|
|
|
|
memleft = MAX_POOL_SIZE;
|
|
|
|
}
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter* str = (Letter*)malloc( memleft );
|
2006-04-14 07:23:30 +02:00
|
|
|
if ( NULL == str ) {
|
|
|
|
ERROR_EXIT( "can't allocate main string storage" );
|
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-14 07:23:30 +02:00
|
|
|
bool eof = false;
|
2006-04-12 06:39:49 +02:00
|
|
|
for ( ; ; ) {
|
2006-04-14 07:23:30 +02:00
|
|
|
int len;
|
2009-01-07 06:13:45 +01:00
|
|
|
Letter* word = readOneWord( str, memleft, &len, &eof );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-14 07:23:30 +02:00
|
|
|
if ( NULL == word ) {
|
|
|
|
break;
|
|
|
|
}
|
2006-04-13 05:52:48 +02:00
|
|
|
|
2006-04-14 07:23:30 +02:00
|
|
|
wordlist->push_back( str );
|
|
|
|
++len; // include null byte
|
|
|
|
str += len;
|
|
|
|
memleft -= len;
|
|
|
|
|
|
|
|
if ( eof ) {
|
|
|
|
break;
|
|
|
|
}
|
2006-07-22 18:03:14 +02:00
|
|
|
if ( memleft < 0 ) {
|
2006-04-14 07:23:30 +02:00
|
|
|
ERROR_EXIT( "no memory left\n" );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-14 07:23:30 +02:00
|
|
|
|
|
|
|
if ( gWordCount > 1 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-12 06:39:49 +02:00
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "starting sort...\n" );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-13 04:57:43 +02:00
|
|
|
std::sort( wordlist->begin(), wordlist->end(), firstBeforeSecond );
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-12 06:39:49 +02:00
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "sort finished\n" );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
return wordlist;
|
|
|
|
} // parseAndSort
|
|
|
|
|
|
|
|
static void
|
2009-01-07 06:13:45 +01:00
|
|
|
printWords( WordList* strings )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2009-01-07 06:13:45 +01:00
|
|
|
std::vector<Letter*>::iterator iter = strings->begin();
|
2006-04-12 06:39:49 +02:00
|
|
|
while ( iter != strings->end() ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
char buf[T2ABUFLEN(MAX_WORD_LEN)];
|
2006-04-13 05:49:41 +02:00
|
|
|
tileToAscii( buf, sizeof(buf), *iter );
|
2006-04-12 06:39:49 +02:00
|
|
|
fprintf( stderr, "%s\n", buf );
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Little node-field setters and getters to hide what bits represent
|
|
|
|
* what.
|
|
|
|
|
|
|
|
* high bit (31) is ACCEPTING bit
|
|
|
|
* next bit (30) is LAST_SIBLING bit
|
|
|
|
* next 6 bits (29-24) are tile bit (allowing alphabets of 64 letters)
|
|
|
|
* final 24 bits (23-0) are the index of the first child (fco)
|
|
|
|
******************************************************************************/
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
TrieNodeSetIsTerminal( Node* nodeR, bool isTerminal )
|
|
|
|
{
|
|
|
|
if ( isTerminal ) {
|
|
|
|
*nodeR |= (1 << 31);
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 04:57:43 +02:00
|
|
|
*nodeR &= ~(1 << 31);
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static bool
|
|
|
|
TrieNodeGetIsTerminal( Node node )
|
|
|
|
{
|
|
|
|
return (node & (1 << 31)) != 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
TrieNodeSetIsLastSibling( Node* nodeR, bool isLastSibling )
|
|
|
|
{
|
|
|
|
if ( isLastSibling ) {
|
|
|
|
*nodeR |= (1 << 30);
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 04:57:43 +02:00
|
|
|
*nodeR &= ~(1 << 30);
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static bool
|
|
|
|
TrieNodeGetIsLastSibling( Node node )
|
|
|
|
{
|
|
|
|
return (node & (1 << 30)) != 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
2009-01-13 14:32:07 +01:00
|
|
|
TrieNodeSetLetter( Node* nodeR, Letter letter )
|
2006-04-13 04:57:43 +02:00
|
|
|
{
|
2009-01-13 14:32:07 +01:00
|
|
|
if ( letter >= 64 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "letter %d too big", letter );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
int mask = ~(0x3F << 24);
|
|
|
|
*nodeR &= mask; // clear all the bits
|
|
|
|
*nodeR |= (letter << 24); // set new ones
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2009-01-13 14:32:07 +01:00
|
|
|
static Letter
|
2006-04-13 04:57:43 +02:00
|
|
|
TrieNodeGetLetter( Node node )
|
|
|
|
{
|
|
|
|
node >>= 24;
|
|
|
|
node &= 0x3F; // is 3f ok for 3-byte case???
|
|
|
|
return node;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
TrieNodeSetFirstChildOffset( Node* nodeR, int fco )
|
|
|
|
{
|
|
|
|
if ( (fco & 0xFF000000) != 0 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "%x larger than 24 bits", fco );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
int mask = ~0x00FFFFFF;
|
|
|
|
*nodeR &= mask; // clear all the bits
|
|
|
|
*nodeR |= fco; // set new ones
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static int
|
|
|
|
TrieNodeGetFirstChildOffset( Node node )
|
|
|
|
{
|
|
|
|
node &= 0x00FFFFFF; // 24 bits
|
|
|
|
return node;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static Node
|
2009-01-13 14:32:07 +01:00
|
|
|
MakeTrieNode( Letter letter, bool isTerminal, int firstChildOffset,
|
2006-04-13 04:57:43 +02:00
|
|
|
bool isLastSibling )
|
|
|
|
{
|
|
|
|
Node result = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
TrieNodeSetIsTerminal( &result, isTerminal );
|
|
|
|
TrieNodeSetIsLastSibling( &result, isLastSibling );
|
|
|
|
TrieNodeSetLetter( &result, letter );
|
|
|
|
TrieNodeSetFirstChildOffset( &result, firstChildOffset );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
return result;
|
2006-04-12 06:39:49 +02:00
|
|
|
} // MakeTrieNode
|
|
|
|
|
|
|
|
// Caller may need to know the offset of the first top-level node.
|
|
|
|
// Write it here.
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
writeOutStartNode( const char* startNodeOut, int firstRootChildOffset )
|
|
|
|
{
|
2006-07-22 18:03:14 +02:00
|
|
|
FILE* nodeout;
|
|
|
|
nodeout = fopen( startNodeOut, "w" );
|
2006-04-13 04:57:43 +02:00
|
|
|
unsigned long be = htonl( firstRootChildOffset );
|
2006-07-22 18:03:14 +02:00
|
|
|
(void)fwrite( &be, sizeof(be), 1, nodeout );
|
|
|
|
fclose( nodeout );
|
2006-04-12 06:39:49 +02:00
|
|
|
} // writeOutStartNode
|
|
|
|
|
|
|
|
// build the hash for translating. I'm using a hash assuming it'll be
|
|
|
|
// fast. Key is the letter; value is the 0..31 value to be output.
|
|
|
|
static void
|
|
|
|
makeTableHash( void )
|
|
|
|
{
|
2007-02-14 16:17:00 +01:00
|
|
|
int ii;
|
2006-04-12 06:39:49 +02:00
|
|
|
FILE* TABLEFILE = fopen( gTableFile, "r" );
|
2006-04-14 10:23:28 +02:00
|
|
|
if ( NULL == TABLEFILE ) {
|
|
|
|
ERROR_EXIT( "unable to open %s\n", gTableFile );
|
|
|
|
}
|
2007-02-17 18:06:05 +01:00
|
|
|
|
|
|
|
// Fill the 0th space since references are one-based
|
|
|
|
gRevMap.push_back(0);
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2007-02-14 16:17:00 +01:00
|
|
|
for ( ii = 0; ; ++ii ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
int ch = getc(TABLEFILE);
|
|
|
|
if ( ch == EOF ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( gUseUnicode ) { // skip the first byte each time: tmp HACK!!!
|
|
|
|
ch = getc(TABLEFILE);
|
|
|
|
}
|
|
|
|
if ( ch == EOF ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gRevMap.push_back(ch);
|
|
|
|
|
|
|
|
if ( ch == 0 ) { // blank
|
2007-02-14 16:17:00 +01:00
|
|
|
gBlankIndex = ii;
|
2006-04-12 06:39:49 +02:00
|
|
|
// we want to increment i when blank seen since it is a
|
|
|
|
// tile value
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// die "$0: $gTableFile too large\n"
|
2007-02-14 16:17:00 +01:00
|
|
|
assert( ii < 64 );
|
2006-04-13 06:04:03 +02:00
|
|
|
// die "$0: only blank (0) can be 64th char\n" ;
|
2007-02-14 16:17:00 +01:00
|
|
|
assert( ii < 64 || ch == 0 );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2007-02-14 16:17:00 +01:00
|
|
|
// Add 1 to i so no tile-strings contain 0 and we can treat as
|
|
|
|
// null-terminated. The 1 is subtracted again in
|
|
|
|
// outputNode().
|
|
|
|
gTableHash[ch] = ii + 1;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( TABLEFILE );
|
|
|
|
} // makeTableHash
|
|
|
|
|
|
|
|
// emitNodes. "input" is $gNodes. From it we write up to
|
|
|
|
// $nBytesPerOutfile to files named $outFileBase0..n, mapping the
|
|
|
|
// letter field down to 5 bits with a hash built from $tableFile. If
|
|
|
|
// at any point we encounter a letter not in the hash we fail with an
|
|
|
|
// error.
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
emitNodes( unsigned int nBytesPerOutfile, const char* outFileBase )
|
|
|
|
{
|
2006-04-12 06:39:49 +02:00
|
|
|
// now do the emit.
|
|
|
|
|
|
|
|
// is 17 bits enough?
|
2006-04-13 04:57:43 +02:00
|
|
|
fprintf( stderr, "There are %d (0x%x) nodes in this DAWG.\n",
|
|
|
|
gNodes.size(), gNodes.size() );
|
|
|
|
int nTiles = gTableHash.size(); // blank is not included in this count!
|
|
|
|
if ( gNodes.size() > 0x1FFFF || gForceFour || nTiles > 32 ) {
|
|
|
|
gNBytesPerNode = 4;
|
|
|
|
} else if ( nTiles < 32 ) {
|
|
|
|
gNBytesPerNode = 3;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gBlankIndex == 32 ) { // blank
|
|
|
|
gNBytesPerNode = 3;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "move blank to last position in info.txt "
|
2006-04-13 04:57:43 +02:00
|
|
|
"for smaller DAWG." );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-07 06:03:13 +01:00
|
|
|
unsigned int nextIndex = 0;
|
2006-04-13 04:57:43 +02:00
|
|
|
int nextFileNum;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
for ( nextFileNum = 0; ; ++nextFileNum ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( nextIndex >= gNodes.size() ) {
|
|
|
|
break; // we're done
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( nextFileNum > 99 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "Too many outfiles; infinite loop?" );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
char outName[256];
|
|
|
|
snprintf( outName, sizeof(outName), "%s_%03d.bin",
|
|
|
|
outFileBase, nextFileNum);
|
|
|
|
FILE* OUTFILE = fopen( outName, "w" );
|
|
|
|
assert( OUTFILE );
|
2009-01-07 06:03:13 +01:00
|
|
|
unsigned int curSize = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
while ( nextIndex < gNodes.size() ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
// scan to find the next terminal
|
2009-01-07 06:03:13 +01:00
|
|
|
unsigned int ii;
|
|
|
|
for ( ii = nextIndex; !TrieNodeGetIsLastSibling(gNodes[ii]); ++ii ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
// do nothing but a sanity check
|
2009-01-07 06:03:13 +01:00
|
|
|
if ( ii >= gNodes.size() ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "bad trie format: last node not last sibling" );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-01-07 06:03:13 +01:00
|
|
|
++ii; // move beyond the terminal
|
|
|
|
int nextSize = (ii - nextIndex) * gNBytesPerNode;
|
2006-04-13 04:57:43 +02:00
|
|
|
if (curSize + nextSize > nBytesPerOutfile ) {
|
|
|
|
break;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
|
|
|
// emit the subarray
|
2009-01-07 06:03:13 +01:00
|
|
|
while ( nextIndex < ii ) {
|
2006-04-13 04:57:43 +02:00
|
|
|
outputNode( gNodes[nextIndex], gNBytesPerNode, OUTFILE );
|
|
|
|
++nextIndex;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
curSize += nextSize;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
fclose( OUTFILE );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // emitNodes
|
|
|
|
|
|
|
|
// print out the entire dictionary, as text, to STDERR.
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
printOneLevel( int index, char* str, int curlen )
|
|
|
|
{
|
|
|
|
int inlen = curlen;
|
2006-04-12 06:39:49 +02:00
|
|
|
for ( ; ; ) {
|
2006-04-13 04:57:43 +02:00
|
|
|
Node node = gNodes[index++];
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 05:49:41 +02:00
|
|
|
assert( TrieNodeGetLetter(node) < gRevMap.size() );
|
2006-04-13 04:57:43 +02:00
|
|
|
char lindx = gRevMap[TrieNodeGetLetter(node)];
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( (int)lindx >= 0x20 ) {
|
|
|
|
str[curlen++] = lindx;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( gDebug ) {
|
|
|
|
fprintf( stderr, "sub space\n" );
|
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-13 04:57:43 +02:00
|
|
|
str[curlen++] = '\\';
|
|
|
|
str[curlen++] = '0' + lindx;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
str[curlen] = '\0';
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( TrieNodeGetIsTerminal(node) ) {
|
|
|
|
fprintf( stderr, "%s\n", str );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
int fco = TrieNodeGetFirstChildOffset( node );
|
|
|
|
if ( fco != 0 ) {
|
|
|
|
printOneLevel( fco, str, curlen );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( TrieNodeGetIsLastSibling(node) ) {
|
|
|
|
break;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
curlen = inlen;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
str[inlen] = '\0';
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
static void
|
|
|
|
outputNode( Node node, int nBytes, FILE* outfile )
|
|
|
|
{
|
|
|
|
unsigned int fco = TrieNodeGetFirstChildOffset(node);
|
2009-01-13 14:32:07 +01:00
|
|
|
unsigned int fourthByte = 0;
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( nBytes == 4 ) {
|
|
|
|
fourthByte = fco >> 16;
|
|
|
|
if ( fourthByte > 0xFF ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "fco too big" );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
|
|
|
fco &= 0xFFFF;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formats are different depending on whether it's to have 3- or
|
|
|
|
// 4-byte nodes.
|
|
|
|
|
|
|
|
// Here's what the three-byte node looks like. 16 bits plus one
|
|
|
|
// burried in the last byte for the next node address, five for a
|
|
|
|
// character/tile and one each for accepting and last-edge.
|
|
|
|
|
|
|
|
// 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
|
|
|
|
// |-------- 16 bits of next node address -------| | | | |-tile indx-|
|
|
|
|
// | | |
|
|
|
|
// accepting bit ---+ | |
|
|
|
|
// last edge bit ------+ |
|
|
|
|
// ---- last bit (17th on next node addr)---------+
|
|
|
|
|
|
|
|
// The four-byte format adds a byte at the right end for
|
|
|
|
// addressing, but removes the extra bit (5) in order to let the
|
|
|
|
// chars field be six bits. Bits 7 and 6 remain the same.
|
|
|
|
|
|
|
|
// write the fco (less that one bit). We want two bytes worth
|
|
|
|
// in three-byte mode, and three in four-byte mode
|
|
|
|
|
|
|
|
// first two bytes are low-word of fco, regardless of format
|
2006-04-13 04:57:43 +02:00
|
|
|
for ( int i = 1; i >= 0; --i ) {
|
|
|
|
unsigned char tmp = (fco >> (i * 8)) & 0xFF;
|
|
|
|
fwrite( &tmp, 1, 1, outfile );
|
|
|
|
}
|
|
|
|
fco >>= 16; // it should now be 1 or 0
|
|
|
|
if ( fco > 1 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "fco not 1 or 0" );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
2007-02-14 16:17:00 +01:00
|
|
|
// - 1 below reverses + 1 in makeTableHash()
|
|
|
|
unsigned char chIn5 = TrieNodeGetLetter(node) - 1;
|
2006-04-13 04:57:43 +02:00
|
|
|
unsigned char bits = chIn5;
|
|
|
|
if ( bits > 0x1F && nBytes == 3 ) {
|
2006-04-13 05:49:41 +02:00
|
|
|
ERROR_EXIT( "char %d too big", bits );
|
2006-04-13 04:57:43 +02:00
|
|
|
}
|
2006-04-12 06:39:49 +02:00
|
|
|
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( TrieNodeGetIsLastSibling(node) ) {
|
|
|
|
bits |= 0x40;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( TrieNodeGetIsTerminal(node) ) {
|
|
|
|
bits |= 0x80;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We set the 17th next-node bit only in 3-byte case (where char is
|
|
|
|
// 5 bits)
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( nBytes == 3 && fco != 0 ) {
|
|
|
|
bits |= 0x20;
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 04:57:43 +02:00
|
|
|
fwrite( &bits, 1, 1, outfile );
|
2006-04-12 06:39:49 +02:00
|
|
|
|
|
|
|
// the final byte, if in use
|
2006-04-13 04:57:43 +02:00
|
|
|
if ( nBytes == 4 ) {
|
|
|
|
unsigned char tmp = (unsigned char)fourthByte;
|
|
|
|
fwrite( &tmp, 1, 1, outfile );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
} // outputNode
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage( const char* name )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "usage: %s \n"
|
2009-01-25 21:13:36 +01:00
|
|
|
"\t[-v] # print version and exit\n"
|
|
|
|
"\t[-poolsize] # print hardcoded size of pool and exit\n"
|
|
|
|
"\t[-b bytesPerFile]# for Palm only (default = 0xFFFFFFFF)\n"
|
|
|
|
"\t[-min <0<=num<=15># min length word to keep\n"
|
|
|
|
"\t[-max <0<=num<=15># max length word to keep\n"
|
|
|
|
"\t-m mapFile\n"
|
|
|
|
"\t-mn mapFile # 16 bits per entry\n"
|
|
|
|
"\t-ob outFileBase\n"
|
|
|
|
"\t-sn # start node out file\n"
|
|
|
|
"\t[-if input_file] # default = stdin\n"
|
|
|
|
"\t[-term ch] # word terminator; default = '\\0'\n"
|
|
|
|
"\t[-nosort] # input already sorted in accord with -m\n"
|
|
|
|
"\t # default=sort'\n"
|
|
|
|
"\t[-dump] # write dictionary as text to STDERR \n"
|
|
|
|
"\t # for testing\n"
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2009-01-25 21:13:36 +01:00
|
|
|
"\t[-debug] # turn on verbose output\n"
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2009-01-25 21:13:36 +01:00
|
|
|
"\t[-force4] # always use 4 bytes per node\n"
|
|
|
|
"\t[-lang lang] # e.g. en_US\n"
|
|
|
|
"\t[-fsize nBytes] # max buffer [default %d]\n"
|
|
|
|
"\t[-r] # drop words with letters not in mapfile\n"
|
|
|
|
"\t[-k] # (default) exit on any letter not in mapfile \n",
|
|
|
|
name, MAX_POOL_SIZE
|
2006-04-12 06:39:49 +02:00
|
|
|
);
|
|
|
|
} // usage
|
|
|
|
|
|
|
|
static void
|
2006-04-13 04:57:43 +02:00
|
|
|
error_exit( int line, const char* fmt, ... )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2006-04-13 05:49:41 +02:00
|
|
|
fprintf( stderr, "Error on line %d: ", line );
|
2006-04-12 06:39:49 +02:00
|
|
|
va_list ap;
|
|
|
|
va_start( ap, fmt );
|
|
|
|
vfprintf( stderr, fmt, ap );
|
|
|
|
va_end( ap );
|
2006-04-13 04:57:43 +02:00
|
|
|
fprintf( stderr, "\n" );
|
2006-04-12 06:39:49 +02:00
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2006-04-14 07:23:30 +02:00
|
|
|
parseARGV( int argc, char** argv, const char** inFileName )
|
2006-04-12 06:39:49 +02:00
|
|
|
{
|
2006-04-14 07:23:30 +02:00
|
|
|
*inFileName = NULL;
|
2006-04-12 06:39:49 +02:00
|
|
|
int index = 1;
|
2009-01-13 14:32:07 +01:00
|
|
|
const char* enc = NULL;
|
2006-04-12 06:39:49 +02:00
|
|
|
while ( index < argc ) {
|
|
|
|
|
|
|
|
char* arg = argv[index++];
|
|
|
|
|
2006-04-29 18:40:48 +02:00
|
|
|
if ( 0 == strcmp( arg, "-v" ) ) {
|
2006-04-29 18:47:01 +02:00
|
|
|
fprintf( stderr, "%s (Subversion revision %s)\n", argv[0],
|
2006-04-29 18:40:48 +02:00
|
|
|
VERSION_STR );
|
|
|
|
exit( 0 );
|
2006-05-02 15:28:07 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-poolsize" ) ) {
|
|
|
|
printf( "%d", MAX_POOL_SIZE );
|
|
|
|
exit( 0 );
|
2006-04-29 18:40:48 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-b" ) ) {
|
2006-04-12 06:39:49 +02:00
|
|
|
gNBytesPerOutfile = atol( argv[index++] );
|
|
|
|
} else if ( 0 == strcmp( arg, "-mn" ) ) {
|
|
|
|
gTableFile = argv[index++];
|
|
|
|
gUseUnicode = true;
|
2007-02-17 18:06:05 +01:00
|
|
|
} else if ( 0 == strcmp( arg, "-min" ) ) {
|
|
|
|
gLimLow = atoi(argv[index++]);
|
|
|
|
} else if ( 0 == strcmp( arg, "-max" ) ) {
|
|
|
|
gLimHigh = atoi(argv[index++]);
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-m" ) ) {
|
|
|
|
gTableFile = argv[index++];
|
|
|
|
} else if ( 0 == strcmp( arg, "-ob" ) ) {
|
|
|
|
gOutFileBase = argv[index++];
|
2009-01-13 14:32:07 +01:00
|
|
|
} else if ( 0 == strcmp( arg, "-enc" ) ) {
|
|
|
|
enc = argv[index++];
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-sn" ) ) {
|
|
|
|
gStartNodeOut = argv[index++];
|
|
|
|
} else if ( 0 == strcmp( arg, "-if" ) ) {
|
2006-04-14 07:23:30 +02:00
|
|
|
*inFileName = argv[index++];
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-r" ) ) {
|
|
|
|
gKillIfMissing = false;
|
|
|
|
} else if ( 0 == strcmp( arg, "-k" ) ) {
|
|
|
|
gKillIfMissing = true;
|
|
|
|
} else if ( 0 == strcmp( arg, "-term" ) ) {
|
2006-04-13 04:57:43 +02:00
|
|
|
gTermChar = (char)atoi(argv[index++]);
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-dump" ) ) {
|
|
|
|
gDumpText = true;
|
|
|
|
} else if ( 0 == strcmp( arg, "-nosort" ) ) {
|
2006-04-14 07:23:30 +02:00
|
|
|
gReadWordProc = readFromFile;
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-wc" ) ) {
|
|
|
|
gCountFile = argv[index++];
|
|
|
|
} else if ( 0 == strcmp( arg, "-ns" ) ) {
|
|
|
|
gBytesPerNodeFile = argv[index++];
|
|
|
|
} else if ( 0 == strcmp( arg, "-force4" ) ) {
|
|
|
|
gForceFour = true;
|
2006-05-02 15:28:07 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-fsize" ) ) {
|
2006-06-28 16:11:46 +02:00
|
|
|
gFileSize = atoi(argv[index++]);
|
2009-01-25 21:13:36 +01:00
|
|
|
} else if ( 0 == strcmp( arg, "-lang" ) ) {
|
|
|
|
gLang = argv[index++];
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-12 06:39:49 +02:00
|
|
|
} else if ( 0 == strcmp( arg, "-debug" ) ) {
|
|
|
|
gDebug = true;
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-12 06:39:49 +02:00
|
|
|
} else {
|
2007-02-17 18:06:05 +01:00
|
|
|
ERROR_EXIT( "%s: unexpected arg %s", __func__, arg );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-17 18:06:05 +01:00
|
|
|
if ( gLimHigh > MAX_WORD_LEN || gLimLow > MAX_WORD_LEN ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-01-13 14:32:07 +01:00
|
|
|
if ( !!enc ) {
|
|
|
|
if ( !strcasecmp( enc, "UTF-8" ) ) {
|
|
|
|
gIsMultibyte = true;
|
2009-01-25 21:13:36 +01:00
|
|
|
} else if ( !strcasecmp( enc, "iso-8859-1" ) ) {
|
|
|
|
gIsMultibyte = false;
|
|
|
|
} else if ( !strcasecmp( enc, "iso-latin-1" ) ) {
|
|
|
|
gIsMultibyte = false;
|
2009-03-14 20:22:15 +01:00
|
|
|
} else if ( !strcasecmp( enc, "ISO-8859-2" ) ) {
|
|
|
|
gIsMultibyte = false;
|
2009-01-13 14:32:07 +01:00
|
|
|
} else {
|
|
|
|
ERROR_EXIT( "%s: unknown encoding %s", __func__, enc );
|
|
|
|
}
|
2009-01-25 21:13:36 +01:00
|
|
|
gEncoding = enc;
|
2009-01-13 14:32:07 +01:00
|
|
|
}
|
|
|
|
|
2006-04-13 05:49:41 +02:00
|
|
|
#ifdef DEBUG
|
2006-04-12 06:39:49 +02:00
|
|
|
if ( gDebug ) {
|
2007-02-17 18:06:05 +01:00
|
|
|
fprintf( stderr, "gNBytesPerOutfile=%d\n", gNBytesPerOutfile );
|
|
|
|
fprintf( stderr, "gTableFile=%s\n", gTableFile );
|
|
|
|
fprintf( stderr, "gOutFileBase=%s\n", gOutFileBase );
|
|
|
|
fprintf( stderr, "gStartNodeOut=%s\n", gStartNodeOut );
|
2006-04-12 06:39:49 +02:00
|
|
|
fprintf( stderr, "gTermChar=%c(%d)\n", gTermChar, (int)gTermChar );
|
2007-02-17 18:06:05 +01:00
|
|
|
fprintf( stderr, "gFileSize=%d\n", gFileSize );
|
|
|
|
fprintf( stderr, "gLimLow=%d\n", gLimLow );
|
|
|
|
fprintf( stderr, "gLimHigh=%d\n", gLimHigh );
|
2006-04-12 06:39:49 +02:00
|
|
|
}
|
2006-04-13 05:49:41 +02:00
|
|
|
#endif
|
2006-04-12 06:39:49 +02:00
|
|
|
return gTableFile;
|
|
|
|
} // parseARGV
|