[comp.sys.next] accessing the Thesaurus through libtext.a, a workaround

sahayman@iuvax.cs.indiana.edu (Steve Hayman) (05/14/91)

[Sorry if this is a duplicate posting, we've been having some
news problems.]

If you've obtained the libtext.a and libbtree.a libraries from
NeXT so that you can write your own programs to access the
dictionary and thesaurus, you may have noticed that this
simple program core dumps:

        #include <text/webster.h>
        main()
        {
            ReferenceBook *book;
            book = referenceOpen("Webster-Thesaurus");
        }

So you can't use referenceOpen to get to the thesaurus.  This is a
drag.  I think I've isolated the problem, though.  The crash seems to
happen at the point where referenceOpen() is deciding whether the
database in question has a FullTextIndex.  The dictionary has both a
Headword index and a FullTextIndex; the thesaurus has only a Headword
index.  Some sort of strcmp() in referenceOpen() is messing up when the
FullTextIndex isn't there, and the program crashes.


Here is a workaround.  Simply add an empty FullTextIndex to the
thesaurus itself.  This adds a mere 8K to the 
/NextLibrary/References/Webster-Thesaurus/Thesaurus.nxbf file.  Of course
an empty FullTextIndex is useless for looking anything up, but you can
still look up words using the normal Headword index.

Below is a little program, "addfulltextindex"  that can be used to add a
FullTextIndex to some .nxbf file.  Use
"addfulltextindex /NextLibrary/References/Webster-Thesaurus/Thesaurus.nxbf"
to fix up the thesaurus.  The Digital Webster program doesn't seem
to object to this new index.

Please forgive any weirdness in the code, this is only about my
2nd Objective-C program.

also, a new version of my webster daemon with working thesaurus support
is now available for anon ftp from iuvax.cs.indiana.edu, in
pub/webster/NeXT-2.0/NeXT-websterd-2.0.shar.Z .

Steve Hayman

/*
 * addfulltextindex  file.nxbf
 * Add an empty BTree called "FullTextIndex" to the file named on
 * the command line.  This can be used to repair the thesaurus
 * by doing
 * 
 *   addfulltextindex  /NextLibrary/References/Webster-Thesaurus/Thesaurus.nxbf
 *
 * Now that the thesaurus has a full text index, the NeXT libtext() routines
 * can be used on the thesaurus without crashing.
 *
 * to compile, cc -o addfulltextindex addfulltextindex.c -lbtree
 *
 * Steve Hayman
 * sahayman@cs.indiana.edu
 * May 13 1991
 * 
 */
#include "websterd.h"
#import <objc/Object.h>
#import <BTreeFile.h>
#import <BTree.h>

main(argc, argv)
int argc;
char *argv[];
{
	
    id TBF;
    char **TB_contents;

    BTree *FullTextIndex;

    if ( argc != 2 ) {
	fprintf(stderr, "%s: Usage: %s file.nxbf\n", argv[0], argv[0]);
	exit(1);
    }

NX_DURING
    TBF = [BTreeFile
	openFile: argv[1]
	forWriting: 1 ];
NX_HANDLER
    /*
     * I don't really know how to handle this yet, or what facilities
     * libbtree.a provides for handling errors, or what the various
     * btree error codes mean.
     */
    fprintf(stderr, "%s: Exception occurred while trying to open %s\n",
	argv[0], argv[1] );
    fprintf(stderr, "%s: Exception Code: %d Data1: %s Data2: %s\n",
	argv[0],
	NXLocalHandler.code,
	NXLocalHandler.data1,
	NXLocalHandler.data2 );
    /*
     * I do know that you seem to get error 9011 if the dictionary
     * couldn't be locked
     */

    if ( NXLocalHandler.code == 9011 ) {
	fprintf(stderr, "%s: Perhaps the file couldn't be locked.  Kill Webster if it's running\n",
	    argv[0]);
    }
    exit(1);
NX_ENDHANDLER


    TB_contents = [TBF contents];

    while ( TB_contents && *TB_contents ) {
	if (strcmp(*TB_contents, "FullTextIndex") == 0 ) {
	    fprintf(stderr, "%s: %s already has a FullTextIndex.  No action taken\n",
		argv[0], argv[1]);
	    exit(1);
	}

	TB_contents++;
    }

    /*
     * ok here we go
     */
    FullTextIndex = [TBF createBTreeNamed: "FullTextIndex"];

    [FullTextIndex empty];

    [TBF save];
    exit(0);
}