1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <yaz/oid_db.h>
25 #include <idzebra/util.h>
26 #include <idzebra/recctrl.h>
32 static void *filter_init(Res res, RecType recType)
34 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
39 static void *filter_init2(Res res, RecType recType)
41 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
46 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
51 static void filter_destroy(void *clientData)
53 struct filter_info *tinfo = clientData;
58 struct recExtractCtrl *p;
64 static struct fi_info *fi_open(struct recExtractCtrl *p)
66 struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
69 fi->buf = (char *) xmalloc (4096);
75 static int fi_getchar(struct fi_info *fi, char *dst)
77 if (fi->offset >= fi->max)
81 fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
86 *dst = fi->buf[(fi->offset)++];
90 static int fi_gets(struct fi_info *fi, char *dst, int max)
96 if (!fi_getchar(fi, &dstbyte))
107 static void fi_close (struct fi_info *fi)
113 static int filter_extract(void *clientData, struct recExtractCtrl *p)
115 struct filter_info *tinfo = clientData;
118 int ret = RECCTRL_EXTRACT_OK;
119 struct fi_info *fi = fi_open(p);
122 yaz_log(YLOG_LOG, "filter_extract off=%ld",
123 (long) (*fi->p->tellf)(fi->p->fh));
125 (*p->init)(p, &recWord);
127 if (!fi_gets(fi, line, sizeof(line)-1))
128 ret = RECCTRL_EXTRACT_EOF;
131 sscanf(line, "%255s", p->match_criteria);
132 while (fi_gets(fi, line, sizeof(line)-1))
136 const char *cp = line;
139 yaz_log(YLOG_LOG, "safari line: %s", line);
142 if (*cp >= '0' && *cp <= '9')
143 type_cstr[0] = '0'; /* the default is 0 (raw) */
145 type_cstr[0] = *cp++; /* type given */
148 recWord.index_type = type_cstr;
151 if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT
152 ZINT_FORMAT " %39s %n",
153 &recWord.record_id, &recWord.section_id,
158 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
159 ret = RECCTRL_EXTRACT_ERROR_GENERIC;
165 if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
166 &recWord.record_id, &recWord.section_id, &recWord.seqno,
169 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
170 ret = RECCTRL_EXTRACT_ERROR_GENERIC;
174 for (cp = cp + nor; *cp == ' '; cp++)
176 recWord.index_name = field;
177 recWord.term_buf = cp;
178 recWord.term_len = strlen(cp);
179 (*p->tokenAdd)(&recWord);
186 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
188 int r, filter_ptr = 0;
189 static char *filter_buf = NULL;
190 static int filter_size = 0;
193 const char *elementSetName = NULL;
196 if (p->comp && p->comp->which == Z_RecordComp_simple &&
197 p->comp->u.simple->which == Z_ElementSetNames_generic)
198 elementSetName = p->comp->u.simple->u.generic;
202 /* don't make header for the R(aw) element set name */
203 if (!strcmp(elementSetName, "R"))
208 /* only make header for the H(eader) element set name */
209 else if (!strcmp(elementSetName, "H"))
217 if (filter_ptr + 4096 >= filter_size)
221 filter_size = 2*filter_size + 8192;
222 nb = (char *) xmalloc (filter_size);
225 memcpy (nb, filter_buf, filter_ptr);
230 if (make_header && filter_ptr == 0)
234 sprintf (filter_buf, "Rank: %d\n", p->score);
235 filter_ptr = strlen(filter_buf);
237 sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
239 filter_ptr = strlen(filter_buf);
242 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
243 filter_ptr = strlen(filter_buf);
245 strcpy(filter_buf+filter_ptr++, "\n");
249 r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
254 filter_buf[filter_ptr] = '\0';
257 if (!strcmp (elementSetName, "B"))
259 if (!strcmp (elementSetName, "M"))
264 char *p = filter_buf;
267 while (++i <= no_lines && (p = strchr (p, '\n')))
272 filter_ptr = p-filter_buf;
275 p->output_format = yaz_oid_recsyn_sutrs;
276 p->rec_buf = filter_buf;
277 p->rec_len = filter_ptr;
281 static struct recType filter_type = {
291 static struct recType filter_type2 = {
302 #ifdef IDZEBRA_STATIC_SAFARI
303 idzebra_filter_safari
316 * indent-tabs-mode: nil
318 * vim: shiftwidth=4 tabstop=8 expandtab