Moved zebrautl.h to idzebra/util.h.
[idzebra-moved-to-github.git] / recctrl / safari.c
1 /* $Id: safari.c,v 1.6 2005-03-30 09:25:24 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 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 #if 0
133         yaz_log(YLOG_LOG, "safari line: %s", line);
134 #endif
135         if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
136                    &recWord.record_id, &recWord.section_id, &recWord.seqno,
137                    field, &nor) < 4)
138         {
139             yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
140             return RECCTRL_EXTRACT_ERROR_GENERIC;
141         }
142         for (cp = line + nor; *cp == ' '; cp++)
143             ;
144         recWord.attrStr = field;
145         recWord.term_buf = cp;
146         recWord.term_len = strlen(cp);
147         (*p->tokenAdd)(&recWord);
148     }
149     fi_close(fi);
150     return RECCTRL_EXTRACT_OK;
151 }
152
153 static int safari_retrieve (void *clientData, struct recRetrieveCtrl *p)
154 {
155     int r, safari_ptr = 0;
156     static char *safari_buf = NULL;
157     static int safari_size = 0;
158     int make_header = 1;
159     int make_body = 1;
160     const char *elementSetName = NULL;
161     int no_lines = 0;
162
163     if (p->comp && p->comp->which == Z_RecordComp_simple &&
164         p->comp->u.simple->which == Z_ElementSetNames_generic)
165         elementSetName = p->comp->u.simple->u.generic;
166
167     if (elementSetName)
168     {
169         /* don't make header for the R(aw) element set name */
170         if (!strcmp(elementSetName, "R"))
171         {
172             make_header = 0;
173             make_body = 1;
174         }
175         /* only make header for the H(eader) element set name */
176         else if (!strcmp(elementSetName, "H"))
177         {
178             make_header = 1;
179             make_body = 0;
180         }
181     }
182     while (1)
183     {
184         if (safari_ptr + 4096 >= safari_size)
185         {
186             char *nb;
187
188             safari_size = 2*safari_size + 8192;
189             nb = (char *) xmalloc (safari_size);
190             if (safari_buf)
191             {
192                 memcpy (nb, safari_buf, safari_ptr);
193                 xfree (safari_buf);
194             }
195             safari_buf = nb;
196         }
197         if (make_header && safari_ptr == 0)
198         {
199             if (p->score >= 0)
200             {
201                 sprintf (safari_buf, "Rank: %d\n", p->score);
202                 safari_ptr = strlen(safari_buf);
203             }
204             sprintf (safari_buf + safari_ptr, "Local Number: " ZINT_FORMAT "\n",
205                      p->localno);
206             safari_ptr = strlen(safari_buf);
207             if (p->fname)
208             {
209                 sprintf (safari_buf + safari_ptr, "Filename: %s\n", p->fname);
210                 safari_ptr = strlen(safari_buf);
211             }
212             strcpy(safari_buf+safari_ptr++, "\n");
213         }
214         if (!make_body)
215             break;
216         r = (*p->readf)(p->fh, safari_buf + safari_ptr, 4096);
217         if (r <= 0)
218             break;
219         safari_ptr += r;
220     }
221     safari_buf[safari_ptr] = '\0';
222     if (elementSetName)
223     {
224         if (!strcmp (elementSetName, "B"))
225             no_lines = 4;
226         if (!strcmp (elementSetName, "M"))
227             no_lines = 20;
228     }
229     if (no_lines)
230     {
231         char *p = safari_buf;
232         int i = 0;
233
234         while (++i <= no_lines && (p = strchr (p, '\n')))
235             p++;
236         if (p)
237         {
238             p[1] = '\0';
239             safari_ptr = p-safari_buf;
240         }
241     }
242     p->output_format = VAL_SUTRS;
243     p->rec_buf = safari_buf;
244     p->rec_len = safari_ptr; 
245     return 0;
246 }
247
248 static struct recType safari_type = {
249     "safari",
250     safari_init,
251     safari_config,
252     safari_destroy,
253     safari_extract,
254     safari_retrieve
255 };
256
257 RecType
258 #ifdef IDZEBRA_STATIC_SAFARI
259 idzebra_filter_safari
260 #else
261 idzebra_filter
262 #endif
263
264 [] = {
265     &safari_type,
266     0,
267 };