1 /* $Id: safari.c,v 1.11 2007-12-03 11:46:39 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <yaz/oid_db.h>
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_init2(Res res, RecType recType)
44 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
49 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
54 static void filter_destroy(void *clientData)
56 struct filter_info *tinfo = clientData;
61 struct recExtractCtrl *p;
67 static struct fi_info *fi_open(struct recExtractCtrl *p)
69 struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
72 fi->buf = (char *) xmalloc (4096);
78 static int fi_getchar(struct fi_info *fi, char *dst)
80 if (fi->offset >= fi->max)
84 fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
89 *dst = fi->buf[(fi->offset)++];
93 static int fi_gets(struct fi_info *fi, char *dst, int max)
99 if (!fi_getchar(fi, &dstbyte))
110 static void fi_close (struct fi_info *fi)
116 static int filter_extract(void *clientData, struct recExtractCtrl *p)
118 struct filter_info *tinfo = clientData;
121 int ret = RECCTRL_EXTRACT_OK;
122 struct fi_info *fi = fi_open(p);
125 yaz_log(YLOG_LOG, "filter_extract off=%ld",
126 (long) (*fi->p->tellf)(fi->p->fh));
128 (*p->init)(p, &recWord);
130 if (!fi_gets(fi, line, sizeof(line)-1))
131 ret = RECCTRL_EXTRACT_EOF;
134 sscanf(line, "%255s", p->match_criteria);
135 while (fi_gets(fi, line, sizeof(line)-1))
139 const char *cp = line;
142 yaz_log(YLOG_LOG, "safari line: %s", line);
145 if (*cp >= '0' && *cp <= '9')
146 type_cstr[0] = '0'; /* the default is 0 (raw) */
148 type_cstr[0] = *cp++; /* type given */
151 recWord.index_type = type_cstr;
154 if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT
155 ZINT_FORMAT " %39s %n",
156 &recWord.record_id, &recWord.section_id,
161 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
162 ret = RECCTRL_EXTRACT_ERROR_GENERIC;
168 if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
169 &recWord.record_id, &recWord.section_id, &recWord.seqno,
172 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
173 ret = RECCTRL_EXTRACT_ERROR_GENERIC;
177 for (cp = cp + nor; *cp == ' '; cp++)
179 recWord.index_name = field;
180 recWord.term_buf = cp;
181 recWord.term_len = strlen(cp);
182 (*p->tokenAdd)(&recWord);
189 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
191 int r, filter_ptr = 0;
192 static char *filter_buf = NULL;
193 static int filter_size = 0;
196 const char *elementSetName = NULL;
199 if (p->comp && p->comp->which == Z_RecordComp_simple &&
200 p->comp->u.simple->which == Z_ElementSetNames_generic)
201 elementSetName = p->comp->u.simple->u.generic;
205 /* don't make header for the R(aw) element set name */
206 if (!strcmp(elementSetName, "R"))
211 /* only make header for the H(eader) element set name */
212 else if (!strcmp(elementSetName, "H"))
220 if (filter_ptr + 4096 >= filter_size)
224 filter_size = 2*filter_size + 8192;
225 nb = (char *) xmalloc (filter_size);
228 memcpy (nb, filter_buf, filter_ptr);
233 if (make_header && filter_ptr == 0)
237 sprintf (filter_buf, "Rank: %d\n", p->score);
238 filter_ptr = strlen(filter_buf);
240 sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
242 filter_ptr = strlen(filter_buf);
245 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
246 filter_ptr = strlen(filter_buf);
248 strcpy(filter_buf+filter_ptr++, "\n");
252 r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
257 filter_buf[filter_ptr] = '\0';
260 if (!strcmp (elementSetName, "B"))
262 if (!strcmp (elementSetName, "M"))
267 char *p = filter_buf;
270 while (++i <= no_lines && (p = strchr (p, '\n')))
275 filter_ptr = p-filter_buf;
278 p->output_format = yaz_oid_recsyn_sutrs;
279 p->rec_buf = filter_buf;
280 p->rec_len = filter_ptr;
284 static struct recType filter_type = {
294 static struct recType filter_type2 = {
305 #ifdef IDZEBRA_STATIC_SAFARI
306 idzebra_filter_safari
319 * indent-tabs-mode: nil
321 * vim: shiftwidth=4 tabstop=8 expandtab