1 /* $Id: safari.c,v 1.7 2005-03-31 12:42:07 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 void 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)
90 for (l = 0; l < max; l++)
92 if (!fi_getchar(fi, dst+l))
101 static void fi_close (struct fi_info *fi)
107 static int filter_extract(void *clientData, struct recExtractCtrl *p)
109 struct filter_info *tinfo = clientData;
112 struct fi_info *fi = fi_open(p);
115 yaz_log(YLOG_LOG, "filter_extract off=%ld",
116 (long) (*fi->p->tellf)(fi->p->fh));
120 (*p->init)(p, &recWord);
122 if (!fi_gets(fi, line, sizeof(line)-1))
123 return RECCTRL_EXTRACT_ERROR_GENERIC;
124 sscanf(line, "%255s", p->match_criteria);
126 recWord.reg_type = 'w';
127 while (fi_gets(fi, line, sizeof(line)-1))
133 yaz_log(YLOG_LOG, "safari line: %s", line);
135 if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
136 &recWord.record_id, &recWord.section_id, &recWord.seqno,
139 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
140 return RECCTRL_EXTRACT_ERROR_GENERIC;
142 for (cp = line + nor; *cp == ' '; cp++)
144 recWord.attrStr = field;
145 recWord.term_buf = cp;
146 recWord.term_len = strlen(cp);
147 (*p->tokenAdd)(&recWord);
150 return RECCTRL_EXTRACT_OK;
153 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
155 int r, filter_ptr = 0;
156 static char *filter_buf = NULL;
157 static int filter_size = 0;
160 const char *elementSetName = NULL;
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;
169 /* don't make header for the R(aw) element set name */
170 if (!strcmp(elementSetName, "R"))
175 /* only make header for the H(eader) element set name */
176 else if (!strcmp(elementSetName, "H"))
184 if (filter_ptr + 4096 >= filter_size)
188 filter_size = 2*filter_size + 8192;
189 nb = (char *) xmalloc (filter_size);
192 memcpy (nb, filter_buf, filter_ptr);
197 if (make_header && filter_ptr == 0)
201 sprintf (filter_buf, "Rank: %d\n", p->score);
202 filter_ptr = strlen(filter_buf);
204 sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
206 filter_ptr = strlen(filter_buf);
209 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
210 filter_ptr = strlen(filter_buf);
212 strcpy(filter_buf+filter_ptr++, "\n");
216 r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096);
221 filter_buf[filter_ptr] = '\0';
224 if (!strcmp (elementSetName, "B"))
226 if (!strcmp (elementSetName, "M"))
231 char *p = filter_buf;
234 while (++i <= no_lines && (p = strchr (p, '\n')))
239 filter_ptr = p-filter_buf;
242 p->output_format = VAL_SUTRS;
243 p->rec_buf = filter_buf;
244 p->rec_len = filter_ptr;
248 static struct recType filter_type = {
259 #ifdef IDZEBRA_STATIC_SAFARI
260 idzebra_filter_safari