1 /* $Id: safari.c,v 1.11 2006-04-26 11:12:32 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
28 #include <idzebra/util.h>
29 #include <idzebra/recctrl.h>
35 static void *filter_init (Res res, RecType recType)
37 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
42 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
47 static void filter_destroy(void *clientData)
49 struct filter_info *tinfo = clientData;
55 struct recExtractCtrl *p;
61 static struct fi_info *fi_open(struct recExtractCtrl *p)
63 struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
66 fi->buf = (char *) xmalloc (4096);
72 static int fi_getchar(struct fi_info *fi, char *dst)
74 if (fi->offset >= fi->max)
78 fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
83 *dst = fi->buf[(fi->offset)++];
87 static int fi_gets(struct fi_info *fi, char *dst, int max)
93 if (!fi_getchar(fi, &dstbyte))
104 static void fi_close (struct fi_info *fi)
110 static int filter_extract(void *clientData, struct recExtractCtrl *p)
112 struct filter_info *tinfo = clientData;
115 struct fi_info *fi = fi_open(p);
118 yaz_log(YLOG_LOG, "filter_extract off=%ld",
119 (long) (*fi->p->tellf)(fi->p->fh));
123 (*p->init)(p, &recWord);
125 if (!fi_gets(fi, line, sizeof(line)-1))
126 return RECCTRL_EXTRACT_ERROR_GENERIC;
127 sscanf(line, "%255s", p->match_criteria);
129 recWord.index_type = '0';
130 while (fi_gets(fi, line, sizeof(line)-1))
136 yaz_log(YLOG_LOG, "safari line: %s", line);
138 if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
139 &recWord.record_id, &recWord.section_id, &recWord.seqno,
142 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
143 return RECCTRL_EXTRACT_ERROR_GENERIC;
145 for (cp = line + nor; *cp == ' '; cp++)
147 recWord.index_name = field;
148 recWord.term_buf = cp;
149 recWord.term_len = strlen(cp);
150 (*p->tokenAdd)(&recWord);
153 return RECCTRL_EXTRACT_OK;
156 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
158 int r, filter_ptr = 0;
159 static char *filter_buf = NULL;
160 static int filter_size = 0;
163 const char *elementSetName = NULL;
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;
172 /* don't make header for the R(aw) element set name */
173 if (!strcmp(elementSetName, "R"))
178 /* only make header for the H(eader) element set name */
179 else if (!strcmp(elementSetName, "H"))
187 if (filter_ptr + 4096 >= filter_size)
191 filter_size = 2*filter_size + 8192;
192 nb = (char *) xmalloc (filter_size);
195 memcpy (nb, filter_buf, filter_ptr);
200 if (make_header && filter_ptr == 0)
204 sprintf (filter_buf, "Rank: %d\n", p->score);
205 filter_ptr = strlen(filter_buf);
207 sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
209 filter_ptr = strlen(filter_buf);
212 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
213 filter_ptr = strlen(filter_buf);
215 strcpy(filter_buf+filter_ptr++, "\n");
219 r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096);
224 filter_buf[filter_ptr] = '\0';
227 if (!strcmp (elementSetName, "B"))
229 if (!strcmp (elementSetName, "M"))
234 char *p = filter_buf;
237 while (++i <= no_lines && (p = strchr (p, '\n')))
242 filter_ptr = p-filter_buf;
245 p->output_format = VAL_SUTRS;
246 p->rec_buf = filter_buf;
247 p->rec_len = filter_ptr;
251 static struct recType filter_type = {
262 #ifdef IDZEBRA_STATIC_SAFARI
263 idzebra_filter_safari