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
25 #include <idzebra/util.h>
26 #include <idzebra/recctrl.h>
27 #include <yaz/oid_db.h>
33 static void *filter_init(Res res, RecType recType)
35 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
40 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
42 struct filter_info *tinfo = (struct filter_info*) clientData;
46 tinfo->sep = xstrdup(args);
50 static void filter_destroy(void *clientData)
52 struct filter_info *tinfo = clientData;
58 struct recExtractCtrl *p;
64 static struct buf_info *buf_open(struct recExtractCtrl *p)
66 struct buf_info *fi = (struct buf_info *) xmalloc(sizeof(*fi));
69 fi->buf = (char *) xmalloc(4096);
75 static int buf_getchar(struct filter_info *tinfo, struct buf_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)++];
87 if (tinfo->sep && *dst == *tinfo->sep)
89 off_t off = fi->p->stream->tellf(fi->p->stream);
90 off_t end_offset = off - (fi->max - fi->offset);
91 fi->p->stream->endf(fi->p->stream, &end_offset);
97 static void buf_close(struct buf_info *fi)
103 static int filter_extract(void *clientData, struct recExtractCtrl *p)
105 struct filter_info *tinfo = clientData;
109 struct buf_info *fi = buf_open(p);
113 yaz_log(YLOG_LOG, "filter_extract off=%ld",
114 (long) (*fi->p->tellf)(fi->p->fh));
116 (*p->init)(p, &recWord);
121 r = buf_getchar(tinfo, fi, w);
122 while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
125 r = buf_getchar(tinfo, fi, w + i);
130 recWord.term_buf = w;
131 recWord.term_len = i;
132 (*p->tokenAdd)(&recWord);
137 return RECCTRL_EXTRACT_EOF;
138 return RECCTRL_EXTRACT_OK;
141 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
143 int r, filter_ptr = 0;
144 static char *filter_buf = NULL;
145 static int filter_size = 0;
148 const char *elementSetName = NULL;
151 if (p->comp && p->comp->which == Z_RecordComp_simple &&
152 p->comp->u.simple->which == Z_ElementSetNames_generic)
153 elementSetName = p->comp->u.simple->u.generic;
157 /* don't make header for the R(aw) element set name */
158 if (!strcmp(elementSetName, "R"))
163 /* only make header for the H(eader) element set name */
164 else if (!strcmp(elementSetName, "H"))
172 if (filter_ptr + 4096 >= filter_size)
176 filter_size = 2*filter_size + 8192;
177 nb = (char *) xmalloc(filter_size);
180 memcpy(nb, filter_buf, filter_ptr);
185 if (make_header && filter_ptr == 0)
189 sprintf(filter_buf, "Rank: %d\n", p->score);
190 filter_ptr = strlen(filter_buf);
192 sprintf(filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
194 filter_ptr = strlen(filter_buf);
197 sprintf(filter_buf + filter_ptr, "Filename: %s\n", p->fname);
198 filter_ptr = strlen(filter_buf);
200 strcpy(filter_buf+filter_ptr++, "\n");
204 r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
209 filter_buf[filter_ptr] = '\0';
212 if (!strcmp(elementSetName, "B"))
214 if (!strcmp(elementSetName, "M"))
219 char *p = filter_buf;
222 while (++i <= no_lines && (p = strchr(p, '\n')))
227 filter_ptr = p-filter_buf;
230 p->output_format = yaz_oid_recsyn_sutrs;
231 p->rec_buf = filter_buf;
232 p->rec_len = filter_ptr;
236 static struct recType filter_type = {
247 #ifdef IDZEBRA_STATIC_TEXT
260 * indent-tabs-mode: nil
262 * vim: shiftwidth=4 tabstop=8 expandtab