1 /* $Id: rectext.c,v 1.29 2005-06-23 06:45:47 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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_config(void *clientData, Res res, const char *args)
44 struct filter_info *tinfo = (struct filter_info*) clientData;
48 tinfo->sep = xstrdup(args);
51 static void filter_destroy (void *clientData)
53 struct filter_info *tinfo = clientData;
59 struct recExtractCtrl *p;
65 static struct buf_info *buf_open (struct recExtractCtrl *p)
67 struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
70 fi->buf = (char *) xmalloc (4096);
76 static int buf_getchar (struct filter_info *tinfo, struct buf_info *fi, char *dst)
78 if (fi->offset >= fi->max)
82 fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
87 *dst = fi->buf[(fi->offset)++];
88 if (tinfo->sep && *dst == *tinfo->sep)
90 off_t off = (*fi->p->tellf)(fi->p->fh);
91 (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->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->readf)(p->fh, 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 = VAL_SUTRS;
231 p->rec_buf = filter_buf;
232 p->rec_len = filter_ptr;
236 static struct recType filter_type = {
247 #ifdef IDZEBRA_STATIC_TEXT