64cefe55051a11a087b612485c7aaf950a450e98
[idzebra-moved-to-github.git] / index / rectext.c
1 /* $Id: rectext.c,v 1.2 2006-08-14 10:40:15 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->readf)(fi->p->fh, 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->tellf)(fi->p->fh);
92         (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->offset));
93         return 0;
94     }
95     return 1;
96 }
97
98 static void buf_close (struct buf_info *fi)
99 {
100     xfree (fi->buf);
101     xfree (fi);
102 }
103
104 static int filter_extract (void *clientData, struct recExtractCtrl *p)
105 {
106     struct filter_info *tinfo = clientData;
107     char w[512];
108     RecWord recWord;
109     int r;
110     struct buf_info *fi = buf_open (p);
111     int no_read = 0;
112
113 #if 0
114     yaz_log(YLOG_LOG, "filter_extract off=%ld",
115             (long) (*fi->p->tellf)(fi->p->fh));
116 #endif
117     (*p->init)(p, &recWord);
118     do
119     {
120         int i = 0;
121             
122         r = buf_getchar (tinfo, fi, w);
123         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
124         {
125             i++;
126             r = buf_getchar (tinfo, fi, w + i); 
127         }
128         if (i)
129         {
130             no_read += i;
131             recWord.term_buf = w;
132             recWord.term_len = i;
133             (*p->tokenAdd)(&recWord);
134         }
135     } while (r > 0);
136     buf_close (fi);
137     if (no_read == 0)
138         return RECCTRL_EXTRACT_EOF;
139     return RECCTRL_EXTRACT_OK;
140 }
141
142 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
143 {
144     int r, filter_ptr = 0;
145     static char *filter_buf = NULL;
146     static int filter_size = 0;
147     int make_header = 1;
148     int make_body = 1;
149     const char *elementSetName = NULL;
150     int no_lines = 0;
151
152     if (p->comp && p->comp->which == Z_RecordComp_simple &&
153         p->comp->u.simple->which == Z_ElementSetNames_generic)
154         elementSetName = p->comp->u.simple->u.generic;
155
156     if (elementSetName)
157     {
158         /* don't make header for the R(aw) element set name */
159         if (!strcmp(elementSetName, "R"))
160         {
161             make_header = 0;
162             make_body = 1;
163         }
164         /* only make header for the H(eader) element set name */
165         else if (!strcmp(elementSetName, "H"))
166         {
167             make_header = 1;
168             make_body = 0;
169         }
170     }
171     while (1)
172     {
173         if (filter_ptr + 4096 >= filter_size)
174         {
175             char *nb;
176
177             filter_size = 2*filter_size + 8192;
178             nb = (char *) xmalloc (filter_size);
179             if (filter_buf)
180             {
181                 memcpy (nb, filter_buf, filter_ptr);
182                 xfree (filter_buf);
183             }
184             filter_buf = nb;
185         }
186         if (make_header && filter_ptr == 0)
187         {
188             if (p->score >= 0)
189             {
190                 sprintf (filter_buf, "Rank: %d\n", p->score);
191                 filter_ptr = strlen(filter_buf);
192             }
193             sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
194                      p->localno);
195             filter_ptr = strlen(filter_buf);
196             if (p->fname)
197             {
198                 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
199                 filter_ptr = strlen(filter_buf);
200             }
201             strcpy(filter_buf+filter_ptr++, "\n");
202         }
203         if (!make_body)
204             break;
205         r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096);
206         if (r <= 0)
207             break;
208         filter_ptr += r;
209     }
210     filter_buf[filter_ptr] = '\0';
211     if (elementSetName)
212     {
213         if (!strcmp (elementSetName, "B"))
214             no_lines = 4;
215         if (!strcmp (elementSetName, "M"))
216             no_lines = 20;
217     }
218     if (no_lines)
219     {
220         char *p = filter_buf;
221         int i = 0;
222
223         while (++i <= no_lines && (p = strchr (p, '\n')))
224             p++;
225         if (p)
226         {
227             p[1] = '\0';
228             filter_ptr = p-filter_buf;
229         }
230     }
231     p->output_format = VAL_SUTRS;
232     p->rec_buf = filter_buf;
233     p->rec_len = filter_ptr; 
234     return 0;
235 }
236
237 static struct recType filter_type = {
238     0,
239     "text",
240     filter_init,
241     filter_config,
242     filter_destroy,
243     filter_extract,
244     filter_retrieve
245 };
246
247 RecType
248 #ifdef IDZEBRA_STATIC_TEXT
249 idzebra_filter_text
250 #else
251 idzebra_filter
252 #endif
253
254 [] = {
255     &filter_type,
256     0,
257 };
258 /*
259  * Local variables:
260  * c-basic-offset: 4
261  * indent-tabs-mode: nil
262  * End:
263  * vim: shiftwidth=4 tabstop=8 expandtab
264  */
265