source: trunk/gsdl/packages/mg/src/text/query_term_list.c@ 1014

Last change on this file since 1014 was 439, checked in by sjboddie, 25 years ago

renamed mg-1.3d directory mg

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