1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * filename -- description
|
---|
4 | * Copyright (C) 1994 Authors
|
---|
5 | *
|
---|
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.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: query_term_list.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | /*
|
---|
25 | $Log$
|
---|
26 | Revision 1.1 2003/02/20 21:18:24 mdewsnip
|
---|
27 | Addition of MG package for search and retrieval
|
---|
28 |
|
---|
29 | Revision 1.1 1999/08/10 21:18:21 sjboddie
|
---|
30 | renamed mg-1.3d directory mg
|
---|
31 |
|
---|
32 | Revision 1.1 1998/11/17 09:35:36 rjmcnab
|
---|
33 | *** empty log message ***
|
---|
34 |
|
---|
35 | * Revision 1.1 1994/10/20 03:57:07 tes
|
---|
36 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
37 | * components of the boolean query.
|
---|
38 | *
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include "sysfuncs.h"
|
---|
42 |
|
---|
43 | #include "memlib.h"
|
---|
44 | #include "local_strings.h"
|
---|
45 | #include "query_term_list.h"
|
---|
46 | #include "term_lists.h" /* for MAXTERMSTRLEN */
|
---|
47 | #include "messages.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /* =========================================================================
|
---|
51 | * Function: MakeQueryTermList
|
---|
52 | * Description:
|
---|
53 | * Input:
|
---|
54 | * Output:
|
---|
55 | * ========================================================================= */
|
---|
56 | QueryTermList *
|
---|
57 | MakeQueryTermList (int n)
|
---|
58 | {
|
---|
59 | QueryTermList *t;
|
---|
60 | int list_size = (n == 0 ? 1 : n); /* always allocate at least one node */
|
---|
61 |
|
---|
62 | t = Xmalloc (sizeof (QueryTermList) + (list_size - 1) * sizeof (QueryTermEntry));
|
---|
63 | if (!t)
|
---|
64 | FatalError (1, "Unable to allocate query term list");
|
---|
65 |
|
---|
66 | t->num = n;
|
---|
67 | t->list_size = list_size;
|
---|
68 |
|
---|
69 | return t;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* =========================================================================
|
---|
73 | * Function: ResizeQueryTermList
|
---|
74 | * Description:
|
---|
75 | * Input:
|
---|
76 | * Output:
|
---|
77 | * ========================================================================= */
|
---|
78 |
|
---|
79 | #define GROWTH_FACTOR 2
|
---|
80 | #define MIN_SIZE 2
|
---|
81 |
|
---|
82 | static void
|
---|
83 | ResizeQueryTermList (QueryTermList ** query_list)
|
---|
84 | {
|
---|
85 | QueryTermList *qtl = *query_list;
|
---|
86 |
|
---|
87 | if (qtl->num > qtl->list_size)
|
---|
88 | {
|
---|
89 | if (qtl->list_size)
|
---|
90 | qtl->list_size *= GROWTH_FACTOR;
|
---|
91 | else
|
---|
92 | qtl->list_size = MIN_SIZE;
|
---|
93 | }
|
---|
94 | qtl = Xrealloc (qtl, sizeof (QueryTermList) + (qtl->list_size - 1) * sizeof (QueryTermEntry));
|
---|
95 |
|
---|
96 | if (!qtl)
|
---|
97 | FatalError (1, "Unable to resize query term list");
|
---|
98 |
|
---|
99 | *query_list = qtl;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* =========================================================================
|
---|
103 | * Function: ConvertQueryTermsToString
|
---|
104 | * Description:
|
---|
105 | * Convert term list into null-terminated string
|
---|
106 | * Input:
|
---|
107 | * query_term_list = query list
|
---|
108 | * Output:
|
---|
109 | * str = term string
|
---|
110 | * ========================================================================= */
|
---|
111 |
|
---|
112 | void
|
---|
113 | ConvertQueryTermsToString (QueryTermList * query_term_list, char *str)
|
---|
114 | {
|
---|
115 | int i = 0;
|
---|
116 | int total_len = 0;
|
---|
117 |
|
---|
118 | /* terms_str should be preallocated */
|
---|
119 | if (!str)
|
---|
120 | return;
|
---|
121 |
|
---|
122 | for (i = 0; i < query_term_list->num; i++)
|
---|
123 | {
|
---|
124 | unsigned char *word = query_term_list->QTE[i].Term;
|
---|
125 | int len = word[0];
|
---|
126 | total_len += len + 1; /* +1 for space */
|
---|
127 | if (query_term_list->QTE[i].stem_method >= 0)
|
---|
128 | total_len += 2; /* '#' and the stem number */
|
---|
129 | if (total_len > MAXTERMSTRLEN)
|
---|
130 | break;
|
---|
131 | strncpy (str, (char *) word + 1, len);
|
---|
132 | str += len;
|
---|
133 | if (query_term_list->QTE[i].stem_method >= 0)
|
---|
134 | {
|
---|
135 | *str++ = '#';
|
---|
136 | *str++ = '0' + query_term_list->QTE[i].stem_method;
|
---|
137 | }
|
---|
138 | if (i != (query_term_list->num) - 1)
|
---|
139 | {
|
---|
140 | *str = ' ';
|
---|
141 | str++; /* add space gap */
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 | *str = '\0';
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* =========================================================================
|
---|
149 | * Function: ResetTermList
|
---|
150 | * Description:
|
---|
151 | * Input:
|
---|
152 | * Output:
|
---|
153 | * ========================================================================= */
|
---|
154 |
|
---|
155 | void
|
---|
156 | ResetQueryTermList (QueryTermList ** qtl)
|
---|
157 | {
|
---|
158 | if (*qtl)
|
---|
159 | FreeQueryTermList (qtl);
|
---|
160 | *qtl = MakeQueryTermList (0);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* =========================================================================
|
---|
164 | * Function: AddQueryTermEntry
|
---|
165 | * Description:
|
---|
166 | * Input:
|
---|
167 | * Output:
|
---|
168 | * ========================================================================= */
|
---|
169 |
|
---|
170 | int
|
---|
171 | AddQueryTermEntry (QueryTermList ** query_term_list, QueryTermEntry * qte)
|
---|
172 | {
|
---|
173 | QueryTermList *qtl = *query_term_list;
|
---|
174 |
|
---|
175 | qtl->num++;
|
---|
176 | ResizeQueryTermList (query_term_list);
|
---|
177 | qtl = *query_term_list;
|
---|
178 |
|
---|
179 | /* copy the structure contents */
|
---|
180 | bcopy ((char *) qte, (char *) &(qtl->QTE[qtl->num - 1]), sizeof (QueryTermEntry));
|
---|
181 |
|
---|
182 | return qtl->num - 1;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 |
|
---|
187 | /* =========================================================================
|
---|
188 | * Function: AddQueryTerm
|
---|
189 | * Description:
|
---|
190 | * Input:
|
---|
191 | * Output:
|
---|
192 | * ========================================================================= */
|
---|
193 |
|
---|
194 | int
|
---|
195 | AddQueryTerm (QueryTermList ** query_term_list, u_char * Term, int Count, int stem_method)
|
---|
196 | {
|
---|
197 | /* Create a new entry in the list for the new word */
|
---|
198 | QueryTermEntry qte;
|
---|
199 |
|
---|
200 | qte.Count = Count;
|
---|
201 | qte.stem_method = stem_method;
|
---|
202 | qte.Term = copy_string (Term);
|
---|
203 | if (!qte.Term)
|
---|
204 | FatalError (1, "Could NOT create memory to add term");
|
---|
205 |
|
---|
206 | return AddQueryTermEntry (query_term_list, &qte);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /* =========================================================================
|
---|
210 | * Function: FreeQueryTermList
|
---|
211 | * Description:
|
---|
212 | * Input:
|
---|
213 | * Output:
|
---|
214 | * ========================================================================= */
|
---|
215 |
|
---|
216 | void
|
---|
217 | FreeQueryTermList (QueryTermList ** the_qtl)
|
---|
218 | {
|
---|
219 | int j;
|
---|
220 | QueryTermList *qtl = *the_qtl;
|
---|
221 |
|
---|
222 | for (j = 0; j < qtl->num; j++)
|
---|
223 | if (qtl->QTE[j].Term)
|
---|
224 | Xfree (qtl->QTE[j].Term);
|
---|
225 | Xfree (qtl);
|
---|
226 |
|
---|
227 | *the_qtl = NULL;
|
---|
228 | }
|
---|