Common stream reader interface for record filters (struct ZebraRecStream).
[idzebra-moved-to-github.git] / index / rectext.c
1 /* $Id: rectext.c,v 1.3 2006-08-22 13:39:27 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include <idzebra/util.h>
29 #include <idzebra/recctrl.h>
30
31 struct filter_info {
32     char *sep;
33 };
34
35 static void *filter_init (Res res, RecType recType)
36 {
37     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
38     tinfo->sep = 0;
39     return tinfo;
40 }
41
42 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
43 {
44     struct filter_info *tinfo = (struct filter_info*) clientData;
45     xfree(tinfo->sep);
46     tinfo->sep = 0;
47     if (args && *args)
48         tinfo->sep = xstrdup(args);
49     return ZEBRA_OK;
50 }
51
52 static void filter_destroy (void *clientData)
53 {
54     struct filter_info *tinfo = clientData;
55     xfree (tinfo->sep);
56     xfree (tinfo);
57 }
58
59 struct buf_info {
60     struct recExtractCtrl *p;
61     char *buf;
62     int offset;
63     int max;
64 };
65
66 static struct buf_info *buf_open (struct recExtractCtrl *p)
67 {
68     struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
69
70     fi->p = p;
71     fi->buf = (char *) xmalloc (4096);
72     fi->offset = 1;
73     fi->max = 1;
74     return fi;
75 }
76
77 static int buf_getchar (struct filter_info *tinfo, struct buf_info *fi, char *dst)
78 {
79     if (fi->offset >= fi->max)
80     {
81         if (fi->max <= 0)
82             return 0;
83         fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
84         fi->offset = 0;
85         if (fi->max <= 0)
86             return 0;
87     }
88     *dst = fi->buf[(fi->offset)++];
89     if (tinfo->sep && *dst == *tinfo->sep)
90     {
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);
94         return 0;
95     }
96     return 1;
97 }
98
99 static void buf_close (struct buf_info *fi)
100 {
101     xfree (fi->buf);
102     xfree (fi);
103 }
104
105 static int filter_extract (void *clientData, struct recExtractCtrl *p)
106 {
107     struct filter_info *tinfo = clientData;
108     char w[512];
109     RecWord recWord;
110     int r;
111     struct buf_info *fi = buf_open (p);
112     int no_read = 0;
113
114 #if 0
115     yaz_log(YLOG_LOG, "filter_extract off=%ld",
116             (long) (*fi->p->tellf)(fi->p->fh));
117 #endif
118     (*p->init)(p, &recWord);
119     do
120     {
121         int i = 0;
122             
123         r = buf_getchar (tinfo, fi, w);
124         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
125         {
126             i++;
127             r = buf_getchar (tinfo, fi, w + i); 
128         }
129         if (i)
130         {
131             no_read += i;
132             recWord.term_buf = w;
133             recWord.term_len = i;
134             (*p->tokenAdd)(&recWord);
135         }
136     } while (r > 0);
137     buf_close (fi);
138     if (no_read == 0)
139         return RECCTRL_EXTRACT_EOF;
140     return RECCTRL_EXTRACT_OK;
141 }
142
143 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
144 {
145     int r, filter_ptr = 0;
146     static char *filter_buf = NULL;
147     static int filter_size = 0;
148     int make_header = 1;
149     int make_body = 1;
150     const char *elementSetName = NULL;
151     int no_lines = 0;
152
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;
156
157     if (elementSetName)
158     {
159         /* don't make header for the R(aw) element set name */
160         if (!strcmp(elementSetName, "R"))
161         {
162             make_header = 0;
163             make_body = 1;
164         }
165         /* only make header for the H(eader) element set name */
166         else if (!strcmp(elementSetName, "H"))
167         {
168             make_header = 1;
169             make_body = 0;
170         }
171     }
172     while (1)
173     {
174         if (filter_ptr + 4096 >= filter_size)
175         {
176             char *nb;
177
178             filter_size = 2*filter_size + 8192;
179             nb = (char *) xmalloc (filter_size);
180             if (filter_buf)
181             {
182                 memcpy (nb, filter_buf, filter_ptr);
183                 xfree (filter_buf);
184             }
185             filter_buf = nb;
186         }
187         if (make_header && filter_ptr == 0)
188         {
189             if (p->score >= 0)
190             {
191                 sprintf (filter_buf, "Rank: %d\n", p->score);
192                 filter_ptr = strlen(filter_buf);
193             }
194             sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
195                      p->localno);
196             filter_ptr = strlen(filter_buf);
197             if (p->fname)
198             {
199                 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
200                 filter_ptr = strlen(filter_buf);
201             }
202             strcpy(filter_buf+filter_ptr++, "\n");
203         }
204         if (!make_body)
205             break;
206         r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
207         if (r <= 0)
208             break;
209         filter_ptr += r;
210     }
211     filter_buf[filter_ptr] = '\0';
212     if (elementSetName)
213     {
214         if (!strcmp (elementSetName, "B"))
215             no_lines = 4;
216         if (!strcmp (elementSetName, "M"))
217             no_lines = 20;
218     }
219     if (no_lines)
220     {
221         char *p = filter_buf;
222         int i = 0;
223
224         while (++i <= no_lines && (p = strchr (p, '\n')))
225             p++;
226         if (p)
227         {
228             p[1] = '\0';
229             filter_ptr = p-filter_buf;
230         }
231     }
232     p->output_format = VAL_SUTRS;
233     p->rec_buf = filter_buf;
234     p->rec_len = filter_ptr; 
235     return 0;
236 }
237
238 static struct recType filter_type = {
239     0,
240     "text",
241     filter_init,
242     filter_config,
243     filter_destroy,
244     filter_extract,
245     filter_retrieve
246 };
247
248 RecType
249 #ifdef IDZEBRA_STATIC_TEXT
250 idzebra_filter_text
251 #else
252 idzebra_filter
253 #endif
254
255 [] = {
256     &filter_type,
257     0,
258 };
259 /*
260  * Local variables:
261  * c-basic-offset: 4
262  * indent-tabs-mode: nil
263  * End:
264  * vim: shiftwidth=4 tabstop=8 expandtab
265  */
266