1 /* $Id: rectext.c,v 1.7 2007-10-29 09:25:40 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
28 #include <idzebra/util.h>
29 #include <idzebra/recctrl.h>
30 #include <yaz/oid_db.h>
36 static void *filter_init(Res res, RecType recType)
38 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
43 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
45 struct filter_info *tinfo = (struct filter_info*) clientData;
49 tinfo->sep = xstrdup(args);
53 static void filter_destroy(void *clientData)
55 struct filter_info *tinfo = clientData;
61 struct recExtractCtrl *p;
67 static struct buf_info *buf_open(struct recExtractCtrl *p)
69 struct buf_info *fi = (struct buf_info *) xmalloc(sizeof(*fi));
72 fi->buf = (char *) xmalloc(4096);
78 static int buf_getchar(struct filter_info *tinfo, struct buf_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)++];
90 if (tinfo->sep && *dst == *tinfo->sep)
92 off_t off = fi->p->stream->tellf(fi->p->stream);
93 off_t end_offset = off - (fi->max - fi->offset);
94 fi->p->stream->endf(fi->p->stream, &end_offset);
100 static void buf_close(struct buf_info *fi)
106 static int filter_extract(void *clientData, struct recExtractCtrl *p)
108 struct filter_info *tinfo = clientData;
112 struct buf_info *fi = buf_open(p);
116 yaz_log(YLOG_LOG, "filter_extract off=%ld",
117 (long) (*fi->p->tellf)(fi->p->fh));
119 (*p->init)(p, &recWord);
124 r = buf_getchar(tinfo, fi, w);
125 while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
128 r = buf_getchar(tinfo, fi, w + i);
133 recWord.term_buf = w;
134 recWord.term_len = i;
135 (*p->tokenAdd)(&recWord);
140 return RECCTRL_EXTRACT_EOF;
141 return RECCTRL_EXTRACT_OK;
144 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
146 int r, filter_ptr = 0;
147 static char *filter_buf = NULL;
148 static int filter_size = 0;
151 const char *elementSetName = NULL;
154 if (p->comp && p->comp->which == Z_RecordComp_simple &&
155 p->comp->u.simple->which == Z_ElementSetNames_generic)
156 elementSetName = p->comp->u.simple->u.generic;
160 /* don't make header for the R(aw) element set name */
161 if (!strcmp(elementSetName, "R"))
166 /* only make header for the H(eader) element set name */
167 else if (!strcmp(elementSetName, "H"))
175 if (filter_ptr + 4096 >= filter_size)
179 filter_size = 2*filter_size + 8192;
180 nb = (char *) xmalloc(filter_size);
183 memcpy(nb, filter_buf, filter_ptr);
188 if (make_header && filter_ptr == 0)
192 sprintf(filter_buf, "Rank: %d\n", p->score);
193 filter_ptr = strlen(filter_buf);
195 sprintf(filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
197 filter_ptr = strlen(filter_buf);
200 sprintf(filter_buf + filter_ptr, "Filename: %s\n", p->fname);
201 filter_ptr = strlen(filter_buf);
203 strcpy(filter_buf+filter_ptr++, "\n");
207 r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
212 filter_buf[filter_ptr] = '\0';
215 if (!strcmp(elementSetName, "B"))
217 if (!strcmp(elementSetName, "M"))
222 char *p = filter_buf;
225 while (++i <= no_lines && (p = strchr(p, '\n')))
230 filter_ptr = p-filter_buf;
233 p->output_format = yaz_oid_recsyn_sutrs;
234 p->rec_buf = filter_buf;
235 p->rec_len = filter_ptr;
239 static struct recType filter_type = {
250 #ifdef IDZEBRA_STATIC_TEXT
263 * indent-tabs-mode: nil
265 * vim: shiftwidth=4 tabstop=8 expandtab