b3b7d6e20a77a3eb3d6f2d21a746db6acc0ef3d9
[idzebra-moved-to-github.git] / recctrl / safari.c
1 /* $Id: safari.c,v 1.4 2005-03-05 09:19:16 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 <zebrautl.h>
29 #include <idzebra/recctrl.h>
30
31 struct safari_info {
32     char *sep;
33 };
34
35 static void *safari_init (Res res, RecType recType)
36 {
37     struct safari_info *tinfo = (struct safari_info *) xmalloc(sizeof(*tinfo));
38     tinfo->sep = 0;
39     return tinfo;
40 }
41
42 static void safari_config(void *clientData, Res res, const char *args)
43 {
44
45 }
46
47 static void safari_destroy(void *clientData)
48 {
49     struct safari_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 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 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 int fi_gets(struct fi_info *fi, char *dst, int max)
88 {
89     int l;
90     for (l = 0; l < max; l++)
91     {
92         if (!fi_getchar(fi, dst+l))
93             return 0;
94         if (dst[l] == '\n')
95             break;
96     }
97     dst[l] = '\0';
98     return 1;
99 }
100
101 void fi_close (struct fi_info *fi)
102 {
103     xfree (fi->buf);
104     xfree (fi);
105 }
106
107 static int safari_extract(void *clientData, struct recExtractCtrl *p)
108 {
109     struct safari_info *tinfo = clientData;
110     char line[512];
111     RecWord recWord;
112     struct fi_info *fi = fi_open(p);
113
114 #if 0
115     yaz_log(YLOG_LOG, "safari_extract off=%ld",
116             (long) (*fi->p->tellf)(fi->p->fh));
117 #endif
118     xfree(tinfo->sep);
119     tinfo->sep = 0;
120     (*p->init)(p, &recWord);
121
122     if (!fi_gets(fi, line, sizeof(line)-1))
123         return RECCTRL_EXTRACT_ERROR_GENERIC;
124     sscanf(line, "%255s", p->match_criteria);
125     
126     recWord.reg_type = 'w';
127     while (fi_gets(fi, line, sizeof(line)-1))
128     {
129         int nor = 0;
130         char field[40];
131         char *cp;
132         yaz_log(YLOG_LOG, "safari line: %s", line);
133         if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
134                    &recWord.record_id, &recWord.section_id, &recWord.seqno,
135                    field, &nor) < 4)
136         {
137             yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
138             return RECCTRL_EXTRACT_ERROR_GENERIC;
139         }
140         for (cp = line + nor; *cp == ' '; cp++)
141             ;
142         recWord.attrStr = field;
143         recWord.term_buf = cp;
144         recWord.term_len = strlen(cp);
145         (*p->tokenAdd)(&recWord);
146     }
147     fi_close(fi);
148     return RECCTRL_EXTRACT_OK;
149 }
150
151 static int safari_retrieve (void *clientData, struct recRetrieveCtrl *p)
152 {
153     int r, safari_ptr = 0;
154     static char *safari_buf = NULL;
155     static int safari_size = 0;
156     int make_header = 1;
157     int make_body = 1;
158     const char *elementSetName = NULL;
159     int no_lines = 0;
160
161     if (p->comp && p->comp->which == Z_RecordComp_simple &&
162         p->comp->u.simple->which == Z_ElementSetNames_generic)
163         elementSetName = p->comp->u.simple->u.generic;
164
165     if (elementSetName)
166     {
167         /* don't make header for the R(aw) element set name */
168         if (!strcmp(elementSetName, "R"))
169         {
170             make_header = 0;
171             make_body = 1;
172         }
173         /* only make header for the H(eader) element set name */
174         else if (!strcmp(elementSetName, "H"))
175         {
176             make_header = 1;
177             make_body = 0;
178         }
179     }
180     while (1)
181     {
182         if (safari_ptr + 4096 >= safari_size)
183         {
184             char *nb;
185
186             safari_size = 2*safari_size + 8192;
187             nb = (char *) xmalloc (safari_size);
188             if (safari_buf)
189             {
190                 memcpy (nb, safari_buf, safari_ptr);
191                 xfree (safari_buf);
192             }
193             safari_buf = nb;
194         }
195         if (make_header && safari_ptr == 0)
196         {
197             if (p->score >= 0)
198             {
199                 sprintf (safari_buf, "Rank: %d\n", p->score);
200                 safari_ptr = strlen(safari_buf);
201             }
202             sprintf (safari_buf + safari_ptr, "Local Number: " ZINT_FORMAT "\n",
203                      p->localno);
204             safari_ptr = strlen(safari_buf);
205             if (p->fname)
206             {
207                 sprintf (safari_buf + safari_ptr, "Filename: %s\n", p->fname);
208                 safari_ptr = strlen(safari_buf);
209             }
210             strcpy(safari_buf+safari_ptr++, "\n");
211         }
212         if (!make_body)
213             break;
214         r = (*p->readf)(p->fh, safari_buf + safari_ptr, 4096);
215         if (r <= 0)
216             break;
217         safari_ptr += r;
218     }
219     safari_buf[safari_ptr] = '\0';
220     if (elementSetName)
221     {
222         if (!strcmp (elementSetName, "B"))
223             no_lines = 4;
224         if (!strcmp (elementSetName, "M"))
225             no_lines = 20;
226     }
227     if (no_lines)
228     {
229         char *p = safari_buf;
230         int i = 0;
231
232         while (++i <= no_lines && (p = strchr (p, '\n')))
233             p++;
234         if (p)
235         {
236             p[1] = '\0';
237             safari_ptr = p-safari_buf;
238         }
239     }
240     p->output_format = VAL_SUTRS;
241     p->rec_buf = safari_buf;
242     p->rec_len = safari_ptr; 
243     return 0;
244 }
245
246 static struct recType safari_type = {
247     "safari",
248     safari_init,
249     safari_config,
250     safari_destroy,
251     safari_extract,
252     safari_retrieve
253 };
254
255 RecType
256 #ifdef IDZEBRA_STATIC_SAFARI
257 idzebra_filter_safari
258 #else
259 idzebra_filter
260 #endif
261
262 [] = {
263     &safari_type,
264     0,
265 };