1 /* $Id: rectext.c,v 1.21 2004-09-28 10:15:03 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
29 #include <idzebra/recctrl.h>
35 static void *text_init (Res res, RecType recType)
37 struct text_info *tinfo = (struct text_info *) xmalloc(sizeof(*tinfo));
42 static void text_config(void *clientData, Res res, const char *args)
47 static void text_destroy (void *clientData)
49 struct text_info *tinfo = clientData;
55 struct recExtractCtrl *p;
61 struct buf_info *buf_open (struct recExtractCtrl *p)
63 struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
66 fi->buf = (char *) xmalloc (4096);
72 int buf_read (struct text_info *tinfo, struct buf_info *fi, char *dst)
74 if (fi->offset >= fi->max)
78 fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
83 *dst = fi->buf[(fi->offset)++];
84 if (tinfo->sep && *dst == *tinfo->sep)
86 off_t off = (*fi->p->tellf)(fi->p->fh);
87 (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->offset));
93 void buf_close (struct buf_info *fi)
99 static int text_extract (void *clientData, struct recExtractCtrl *p)
101 struct text_info *tinfo = clientData;
105 struct buf_info *fi = buf_open (p);
108 yaz_log(LOG_LOG, "text_extract off=%ld",
109 (long) (*fi->p->tellf)(fi->p->fh));
113 (*p->init)(p, &recWord);
114 recWord.reg_type = 'w';
119 r = buf_read (tinfo, fi, w);
120 while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
123 r = buf_read (tinfo, fi, w + i);
129 (*p->tokenAdd)(&recWord);
133 return RECCTRL_EXTRACT_OK;
136 static int text_retrieve (void *clientData, struct recRetrieveCtrl *p)
139 static char *text_buf = NULL;
140 static int text_size = 0;
143 const char *elementSetName = NULL;
146 if (p->comp && p->comp->which == Z_RecordComp_simple &&
147 p->comp->u.simple->which == Z_ElementSetNames_generic)
148 elementSetName = p->comp->u.simple->u.generic;
152 /* don't make header for the R(aw) element set name */
153 if (!strcmp(elementSetName, "R"))
158 /* only make header for the H(eader) element set name */
159 else if (!strcmp(elementSetName, "H"))
167 if (text_ptr + 4096 >= text_size)
171 text_size = 2*text_size + 8192;
172 nb = (char *) xmalloc (text_size);
175 memcpy (nb, text_buf, text_ptr);
180 if (make_header && text_ptr == 0)
184 sprintf (text_buf, "Rank: %d\n", p->score);
185 text_ptr = strlen(text_buf);
187 sprintf (text_buf + text_ptr, "Local Number: " ZINT_FORMAT "\n",
189 text_ptr = strlen(text_buf);
192 sprintf (text_buf + text_ptr, "Filename: %s\n", p->fname);
193 text_ptr = strlen(text_buf);
195 strcpy(text_buf+text_ptr++, "\n");
199 r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
204 text_buf[text_ptr] = '\0';
207 if (!strcmp (elementSetName, "B"))
209 if (!strcmp (elementSetName, "M"))
217 while (++i <= no_lines && (p = strchr (p, '\n')))
222 text_ptr = p-text_buf;
225 p->output_format = VAL_SUTRS;
226 p->rec_buf = text_buf;
227 p->rec_len = text_ptr;
231 static struct recType text_type = {
241 #ifdef IDZEBRA_STATIC_TEXT