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