f7f25958f679354f59de359b93bb009449a9976d
[idzebra-moved-to-github.git] / index / safari.c
1 /* $Id: safari.c,v 1.1 2006-07-03 14:27:09 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
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     char *sep;
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->sep = 0;
39     return tinfo;
40 }
41
42 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
43 {
44     return ZEBRA_OK;
45 }
46
47 static void filter_destroy(void *clientData)
48 {
49     struct filter_info *tinfo = clientData;
50     xfree (tinfo->sep);
51     xfree (tinfo);
52 }
53
54 struct fi_info {
55     struct recExtractCtrl *p;
56     char *buf;
57     int offset;
58     int max;
59 };
60
61 static struct fi_info *fi_open(struct recExtractCtrl *p)
62 {
63     struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
64
65     fi->p = p;
66     fi->buf = (char *) xmalloc (4096);
67     fi->offset = 1;
68     fi->max = 1;
69     return fi;
70 }
71
72 static int fi_getchar(struct fi_info *fi, char *dst)
73 {
74     if (fi->offset >= fi->max)
75     {
76         if (fi->max <= 0)
77             return 0;
78         fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
79         fi->offset = 0;
80         if (fi->max <= 0)
81             return 0;
82     }
83     *dst = fi->buf[(fi->offset)++];
84     return 1;
85 }
86
87 static int fi_gets(struct fi_info *fi, char *dst, int max)
88 {
89     int l = 0;
90     while(1)
91     {
92         char dstbyte;
93         if (!fi_getchar(fi, &dstbyte))
94             return 0;
95         if (dstbyte == '\n')
96             break;
97         if (l < max)
98             dst[l++] = dstbyte;
99     }
100     dst[l] = '\0';
101     return 1;
102 }
103
104 static void fi_close (struct fi_info *fi)
105 {
106     xfree (fi->buf);
107     xfree (fi);
108 }
109
110 static int filter_extract(void *clientData, struct recExtractCtrl *p)
111 {
112     struct filter_info *tinfo = clientData;
113     char line[512];
114     RecWord recWord;
115     struct fi_info *fi = fi_open(p);
116
117 #if 0
118     yaz_log(YLOG_LOG, "filter_extract off=%ld",
119             (long) (*fi->p->tellf)(fi->p->fh));
120 #endif
121     xfree(tinfo->sep);
122     tinfo->sep = 0;
123     (*p->init)(p, &recWord);
124
125     if (!fi_gets(fi, line, sizeof(line)-1))
126         return RECCTRL_EXTRACT_ERROR_GENERIC;
127     sscanf(line, "%255s", p->match_criteria);
128     
129     recWord.index_type = '0';
130     while (fi_gets(fi, line, sizeof(line)-1))
131     {
132         int nor = 0;
133         char field[40];
134         char *cp;
135 #if 0
136         yaz_log(YLOG_LOG, "safari line: %s", line);
137 #endif
138         if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
139                    &recWord.record_id, &recWord.section_id, &recWord.seqno,
140                    field, &nor) < 4)
141         {
142             yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
143             return RECCTRL_EXTRACT_ERROR_GENERIC;
144         }
145         for (cp = line + nor; *cp == ' '; cp++)
146             ;
147         recWord.index_name = field;
148         recWord.term_buf = cp;
149         recWord.term_len = strlen(cp);
150         (*p->tokenAdd)(&recWord);
151     }
152     fi_close(fi);
153     return RECCTRL_EXTRACT_OK;
154 }
155
156 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
157 {
158     int r, filter_ptr = 0;
159     static char *filter_buf = NULL;
160     static int filter_size = 0;
161     int make_header = 1;
162     int make_body = 1;
163     const char *elementSetName = NULL;
164     int no_lines = 0;
165
166     if (p->comp && p->comp->which == Z_RecordComp_simple &&
167         p->comp->u.simple->which == Z_ElementSetNames_generic)
168         elementSetName = p->comp->u.simple->u.generic;
169
170     if (elementSetName)
171     {
172         /* don't make header for the R(aw) element set name */
173         if (!strcmp(elementSetName, "R"))
174         {
175             make_header = 0;
176             make_body = 1;
177         }
178         /* only make header for the H(eader) element set name */
179         else if (!strcmp(elementSetName, "H"))
180         {
181             make_header = 1;
182             make_body = 0;
183         }
184     }
185     while (1)
186     {
187         if (filter_ptr + 4096 >= filter_size)
188         {
189             char *nb;
190
191             filter_size = 2*filter_size + 8192;
192             nb = (char *) xmalloc (filter_size);
193             if (filter_buf)
194             {
195                 memcpy (nb, filter_buf, filter_ptr);
196                 xfree (filter_buf);
197             }
198             filter_buf = nb;
199         }
200         if (make_header && filter_ptr == 0)
201         {
202             if (p->score >= 0)
203             {
204                 sprintf (filter_buf, "Rank: %d\n", p->score);
205                 filter_ptr = strlen(filter_buf);
206             }
207             sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
208                      p->localno);
209             filter_ptr = strlen(filter_buf);
210             if (p->fname)
211             {
212                 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
213                 filter_ptr = strlen(filter_buf);
214             }
215             strcpy(filter_buf+filter_ptr++, "\n");
216         }
217         if (!make_body)
218             break;
219         r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096);
220         if (r <= 0)
221             break;
222         filter_ptr += r;
223     }
224     filter_buf[filter_ptr] = '\0';
225     if (elementSetName)
226     {
227         if (!strcmp (elementSetName, "B"))
228             no_lines = 4;
229         if (!strcmp (elementSetName, "M"))
230             no_lines = 20;
231     }
232     if (no_lines)
233     {
234         char *p = filter_buf;
235         int i = 0;
236
237         while (++i <= no_lines && (p = strchr (p, '\n')))
238             p++;
239         if (p)
240         {
241             p[1] = '\0';
242             filter_ptr = p-filter_buf;
243         }
244     }
245     p->output_format = VAL_SUTRS;
246     p->rec_buf = filter_buf;
247     p->rec_len = filter_ptr; 
248     return 0;
249 }
250
251 static struct recType filter_type = {
252     0,
253     "safari",
254     filter_init,
255     filter_config,
256     filter_destroy,
257     filter_extract,
258     filter_retrieve
259 };
260
261 RecType
262 #ifdef IDZEBRA_STATIC_SAFARI
263 idzebra_filter_safari
264 #else
265 idzebra_filter
266 #endif
267
268 [] = {
269     &filter_type,
270     0,
271 };
272 /*
273  * Local variables:
274  * c-basic-offset: 4
275  * indent-tabs-mode: nil
276  * End:
277  * vim: shiftwidth=4 tabstop=8 expandtab
278  */
279