Changed type of index_type char/int to string.
[idzebra-moved-to-github.git] / index / safari.c
1 /* $Id: safari.c,v 1.10 2007-10-29 16:57:53 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <yaz/oid_db.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include <idzebra/util.h>
29 #include <idzebra/recctrl.h>
30
31 struct filter_info {
32     int segments;
33 };
34
35 static void *filter_init(Res res, RecType recType)
36 {
37     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
38     tinfo->segments = 0;
39     return tinfo;
40 }
41
42 static void *filter_init2(Res res, RecType recType)
43 {
44     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
45     tinfo->segments = 1;
46     return tinfo;
47 }
48
49 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
50 {
51     return ZEBRA_OK;
52 }
53
54 static void filter_destroy(void *clientData)
55 {
56     struct filter_info *tinfo = clientData;
57     xfree (tinfo);
58 }
59
60 struct fi_info {
61     struct recExtractCtrl *p;
62     char *buf;
63     int offset;
64     int max;
65 };
66
67 static struct fi_info *fi_open(struct recExtractCtrl *p)
68 {
69     struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
70
71     fi->p = p;
72     fi->buf = (char *) xmalloc (4096);
73     fi->offset = 1;
74     fi->max = 1;
75     return fi;
76 }
77
78 static int fi_getchar(struct fi_info *fi, char *dst)
79 {
80     if (fi->offset >= fi->max)
81     {
82         if (fi->max <= 0)
83             return 0;
84         fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
85         fi->offset = 0;
86         if (fi->max <= 0)
87             return 0;
88     }
89     *dst = fi->buf[(fi->offset)++];
90     return 1;
91 }
92
93 static int fi_gets(struct fi_info *fi, char *dst, int max)
94 {
95     int l = 0;
96     while(1)
97     {
98         char dstbyte;
99         if (!fi_getchar(fi, &dstbyte))
100             return 0;
101         if (dstbyte == '\n')
102             break;
103         if (l < max)
104             dst[l++] = dstbyte;
105     }
106     dst[l] = '\0';
107     return 1;
108 }
109
110 static void fi_close (struct fi_info *fi)
111 {
112     xfree (fi->buf);
113     xfree (fi);
114 }
115
116 static int filter_extract(void *clientData, struct recExtractCtrl *p)
117 {
118     struct filter_info *tinfo = clientData;
119     char line[512];
120     RecWord recWord;
121     struct fi_info *fi = fi_open(p);
122
123 #if 0
124     yaz_log(YLOG_LOG, "filter_extract off=%ld",
125             (long) (*fi->p->tellf)(fi->p->fh));
126 #endif
127     (*p->init)(p, &recWord);
128
129     if (!fi_gets(fi, line, sizeof(line)-1))
130         return RECCTRL_EXTRACT_EOF;
131     sscanf(line, "%255s", p->match_criteria);
132     
133     while (fi_gets(fi, line, sizeof(line)-1))
134     {
135         int nor = 0;
136         char field[40];
137         const char *cp = line;
138         char type_cstr[2];
139 #if 0
140         yaz_log(YLOG_LOG, "safari line: %s", line);
141 #endif
142         type_cstr[1] = '\0';
143         if (*cp >= '0' && *cp <= '9')
144             type_cstr[0] = '0'; /* the default is 0 (raw) */
145         else
146             type_cstr[0] = *cp++; /* type given */
147         type_cstr[1] = '\0';
148
149         recWord.index_type = type_cstr;
150         if (tinfo->segments)
151         {
152             if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT 
153                        ZINT_FORMAT " %39s %n",
154                        &recWord.record_id, &recWord.section_id, 
155                        &recWord.segment,
156                        &recWord.seqno,
157                        field, &nor) < 5)
158             {
159                 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
160                 return RECCTRL_EXTRACT_ERROR_GENERIC;
161             }
162         }
163         else
164         {
165             if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
166                        &recWord.record_id, &recWord.section_id, &recWord.seqno,
167                        field, &nor) < 4)
168             {
169                 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
170                 return RECCTRL_EXTRACT_ERROR_GENERIC;
171             }
172         }
173         for (cp = cp + nor; *cp == ' '; cp++)
174             ;
175         recWord.index_name = field;
176         recWord.term_buf = cp;
177         recWord.term_len = strlen(cp);
178         (*p->tokenAdd)(&recWord);
179     }
180     fi_close(fi);
181     return RECCTRL_EXTRACT_OK;
182 }
183
184 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
185 {
186     int r, filter_ptr = 0;
187     static char *filter_buf = NULL;
188     static int filter_size = 0;
189     int make_header = 1;
190     int make_body = 1;
191     const char *elementSetName = NULL;
192     int no_lines = 0;
193
194     if (p->comp && p->comp->which == Z_RecordComp_simple &&
195         p->comp->u.simple->which == Z_ElementSetNames_generic)
196         elementSetName = p->comp->u.simple->u.generic;
197
198     if (elementSetName)
199     {
200         /* don't make header for the R(aw) element set name */
201         if (!strcmp(elementSetName, "R"))
202         {
203             make_header = 0;
204             make_body = 1;
205         }
206         /* only make header for the H(eader) element set name */
207         else if (!strcmp(elementSetName, "H"))
208         {
209             make_header = 1;
210             make_body = 0;
211         }
212     }
213     while (1)
214     {
215         if (filter_ptr + 4096 >= filter_size)
216         {
217             char *nb;
218
219             filter_size = 2*filter_size + 8192;
220             nb = (char *) xmalloc (filter_size);
221             if (filter_buf)
222             {
223                 memcpy (nb, filter_buf, filter_ptr);
224                 xfree (filter_buf);
225             }
226             filter_buf = nb;
227         }
228         if (make_header && filter_ptr == 0)
229         {
230             if (p->score >= 0)
231             {
232                 sprintf (filter_buf, "Rank: %d\n", p->score);
233                 filter_ptr = strlen(filter_buf);
234             }
235             sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
236                      p->localno);
237             filter_ptr = strlen(filter_buf);
238             if (p->fname)
239             {
240                 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
241                 filter_ptr = strlen(filter_buf);
242             }
243             strcpy(filter_buf+filter_ptr++, "\n");
244         }
245         if (!make_body)
246             break;
247         r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
248         if (r <= 0)
249             break;
250         filter_ptr += r;
251     }
252     filter_buf[filter_ptr] = '\0';
253     if (elementSetName)
254     {
255         if (!strcmp (elementSetName, "B"))
256             no_lines = 4;
257         if (!strcmp (elementSetName, "M"))
258             no_lines = 20;
259     }
260     if (no_lines)
261     {
262         char *p = filter_buf;
263         int i = 0;
264
265         while (++i <= no_lines && (p = strchr (p, '\n')))
266             p++;
267         if (p)
268         {
269             p[1] = '\0';
270             filter_ptr = p-filter_buf;
271         }
272     }
273     p->output_format = yaz_oid_recsyn_sutrs;
274     p->rec_buf = filter_buf;
275     p->rec_len = filter_ptr; 
276     return 0;
277 }
278
279 static struct recType filter_type = {
280     0,
281     "safari",
282     filter_init,
283     filter_config,
284     filter_destroy,
285     filter_extract,
286     filter_retrieve
287 };
288
289 static struct recType filter_type2 = {
290     0,
291     "safari2",
292     filter_init2,
293     filter_config,
294     filter_destroy,
295     filter_extract,
296     filter_retrieve
297 };
298
299 RecType
300 #ifdef IDZEBRA_STATIC_SAFARI
301 idzebra_filter_safari
302 #else
303 idzebra_filter
304 #endif
305
306 [] = {
307     &filter_type,
308     &filter_type2,
309     0,
310 };
311 /*
312  * Local variables:
313  * c-basic-offset: 4
314  * indent-tabs-mode: nil
315  * End:
316  * vim: shiftwidth=4 tabstop=8 expandtab
317  */
318