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