1 /* $Id: rectext.c,v 1.4 2007-01-15 15:10:17 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>
35 static void *filter_init (Res res, RecType recType)
37 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
42 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
44 struct filter_info *tinfo = (struct filter_info*) clientData;
48 tinfo->sep = xstrdup(args);
52 static void filter_destroy (void *clientData)
54 struct filter_info *tinfo = clientData;
60 struct recExtractCtrl *p;
66 static struct buf_info *buf_open (struct recExtractCtrl *p)
68 struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
71 fi->buf = (char *) xmalloc (4096);
77 static int buf_getchar (struct filter_info *tinfo, struct buf_info *fi, char *dst)
79 if (fi->offset >= fi->max)
83 fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
88 *dst = fi->buf[(fi->offset)++];
89 if (tinfo->sep && *dst == *tinfo->sep)
91 off_t off = fi->p->stream->tellf(fi->p->stream);
92 off_t end_offset = off - (fi->max - fi->offset);
93 fi->p->stream->endf(fi->p->stream, &end_offset);
99 static void buf_close (struct buf_info *fi)
105 static int filter_extract (void *clientData, struct recExtractCtrl *p)
107 struct filter_info *tinfo = clientData;
111 struct buf_info *fi = buf_open (p);
115 yaz_log(YLOG_LOG, "filter_extract off=%ld",
116 (long) (*fi->p->tellf)(fi->p->fh));
118 (*p->init)(p, &recWord);
123 r = buf_getchar (tinfo, fi, w);
124 while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
127 r = buf_getchar (tinfo, fi, w + i);
132 recWord.term_buf = w;
133 recWord.term_len = i;
134 (*p->tokenAdd)(&recWord);
139 return RECCTRL_EXTRACT_EOF;
140 return RECCTRL_EXTRACT_OK;
143 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
145 int r, filter_ptr = 0;
146 static char *filter_buf = NULL;
147 static int filter_size = 0;
150 const char *elementSetName = NULL;
153 if (p->comp && p->comp->which == Z_RecordComp_simple &&
154 p->comp->u.simple->which == Z_ElementSetNames_generic)
155 elementSetName = p->comp->u.simple->u.generic;
159 /* don't make header for the R(aw) element set name */
160 if (!strcmp(elementSetName, "R"))
165 /* only make header for the H(eader) element set name */
166 else if (!strcmp(elementSetName, "H"))
174 if (filter_ptr + 4096 >= filter_size)
178 filter_size = 2*filter_size + 8192;
179 nb = (char *) xmalloc (filter_size);
182 memcpy (nb, filter_buf, filter_ptr);
187 if (make_header && filter_ptr == 0)
191 sprintf (filter_buf, "Rank: %d\n", p->score);
192 filter_ptr = strlen(filter_buf);
194 sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
196 filter_ptr = strlen(filter_buf);
199 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
200 filter_ptr = strlen(filter_buf);
202 strcpy(filter_buf+filter_ptr++, "\n");
206 r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
211 filter_buf[filter_ptr] = '\0';
214 if (!strcmp (elementSetName, "B"))
216 if (!strcmp (elementSetName, "M"))
221 char *p = filter_buf;
224 while (++i <= no_lines && (p = strchr (p, '\n')))
229 filter_ptr = p-filter_buf;
232 p->output_format = VAL_SUTRS;
233 p->rec_buf = filter_buf;
234 p->rec_len = filter_ptr;
238 static struct recType filter_type = {
249 #ifdef IDZEBRA_STATIC_TEXT
262 * indent-tabs-mode: nil
264 * vim: shiftwidth=4 tabstop=8 expandtab