2
* wordtree.c - Routines to manage word tree and synonimous list
4
* Copyright (C) 2003 Giuseppe Modugno
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
/* Internal function prototypes. */
29
void write_bigend( FILE *f, unsigned int n );
30
void wordtree_output_idx( WTelem *tree, FILE *idx );
31
void wordtree_output_dat( WTelem *tree, FILE *dat );
36
wordtree_add( char *word, WTelem **tree )
38
/* Add an element in the tree, maintaining alphabetic order of words.
39
* Return the new allocated element in the tree.
40
* If word exists, return the tree element present in tree without
41
* allocating another element. */
45
/* Create a new element in the tree. */
46
if( (*tree=(WTelem *)malloc(sizeof(WTelem)))==NULL )
49
(*tree)->word = strdup( word );
50
/* Set default status for the word. */
51
(*tree)->synlist = NULL;
54
(*tree)->right = NULL;
59
if( (cmp=strcmp(word,(*tree)->word))>0 )
60
return wordtree_add(word,&(*tree)->right);
62
return wordtree_add(word,&(*tree)->left);
64
return(*tree); /* Word is present in the tree. */
70
synlist_add( WTelem *word, WTelem *syn )
72
/* Add synonimous syn to the word word. */
73
SLelem **list=&(word->synlist);
77
/* Check if synonimous <syn> is the first of the list. */
78
if( (*list==NULL) || (cmp=strcmp( syn->word, (*list)->syn->word ))<0 ) {
79
/* Add synonimous <syn> as the first element of the list. */
80
if( (new=(SLelem *)malloc(sizeof(SLelem)))==NULL )
88
/* First element of synonimous list is equal to the new synonimous. */
89
fprintf( stderr, "Warning: synonimous %s yet added to word %s\n", syn->word, word->word );
93
/* Go down to the list up to the element alphabetically previous of new
94
* synonimous <syn>. */
95
while( ((*list)->next!=NULL) && (cmp=strcmp(syn->word,(*list)->next->syn->word))>0 )
96
list = &((*list)->next);
98
/* Found the same synonimous in the list. */
99
fprintf( stderr, "Warning: synonimous %s yet added to word %s\n", syn->word, word->word );
100
return( (*list)->next );
103
/* Allocate new element list. */
104
if( (new=(SLelem *)malloc(sizeof(SLelem)))==NULL )
107
new->next = (*list)->next;
110
/* Add new element to the list. */
111
return( (*list)->next=new );
117
wordtree_output_idx( WTelem *tree, FILE *idx )
119
/* Write output .idx files. */
120
static unsigned int index=0;
121
static unsigned int dat_offset=0;
123
if( tree->left!=NULL )
124
wordtree_output_idx( tree->left, idx );
126
tree->index = index++;
127
/* Write word and offset of dat file to idx file. */
128
fprintf( idx, "%s,%u\n", tree->word, dat_offset );
129
dat_offset += 2+(tree->num_syn<<1);
132
fprintf( stderr, "Warning: Word %s is only a synonimous\n", tree->word );
134
fprintf( stderr, "Warning: Word %s has no synonimous\n", tree->word );
136
if( tree->right!=NULL )
137
wordtree_output_idx( tree->right, idx );
144
wordtree_output_dat( WTelem *tree, FILE *dat )
146
/* Write output .dat files. */
148
static unsigned int dat_offset = 0;
150
if( tree->left!=NULL )
151
wordtree_output_dat( tree->left, dat );
153
write_bigend( dat, tree->num_syn );
157
write_bigend( dat, sle->syn->index );
162
if( tree->right!=NULL )
163
wordtree_output_dat( tree->right, dat );
169
wordtree_output( WTelem *tree, FILE *idx, FILE *dat )
171
/* Write output .idx and .dat files. */
173
/* It's important to write .idx file first because
174
* write_output_idx() enumerate words too and index
175
* is used by write_output_dat() routine. */
176
wordtree_output_idx( tree, idx );
177
wordtree_output_dat( tree, dat );
182
write_bigend( FILE *f, unsigned int n )
184
/* Write number n to file f as a 16-bit Big Endian format. */
185
fprintf( f, "%c", (unsigned char)((n&0xFF00)>>8) );
186
fprintf( f, "%c", (unsigned char)(n&0x00FF) );
192
wordtree_free( WTelem *tree )
194
/* Free tree memory. */
197
if( tree->left!=NULL )
198
wordtree_free( tree->left );
200
if( tree->right!=NULL )
201
wordtree_free( tree->right );
203
/* Free synonimous list of that word. */
211
/* Free tree element. */