5aac9d2ea47e0a06777575b38e22e9831dcb28f7
[idzebra-moved-to-github.git] / index / mod_text.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21 #include <stdio.h>
22 #include <assert.h>
23 #include <ctype.h>
24
25 #include <idzebra/util.h>
26 #include <idzebra/recctrl.h>
27 #include <yaz/oid_db.h>
28
29 struct filter_info {
30     char *sep;
31 };
32
33 static void *filter_init(Res res, RecType recType)
34 {
35     struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
36     tinfo->sep = 0;
37     return tinfo;
38 }
39
40 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
41 {
42     struct filter_info *tinfo = (struct filter_info*) clientData;
43     xfree(tinfo->sep);
44     tinfo->sep = 0;
45     if (args && *args)
46         tinfo->sep = xstrdup(args);
47     return ZEBRA_OK;
48 }
49
50 static void filter_destroy(void *clientData)
51 {
52     struct filter_info *tinfo = clientData;
53     xfree(tinfo->sep);
54     xfree(tinfo);
55 }
56
57 struct buf_info {
58     struct recExtractCtrl *p;
59     char *buf;
60     int offset;
61     int max;
62 };
63
64 static struct buf_info *buf_open(struct recExtractCtrl *p)
65 {
66     struct buf_info *fi = (struct buf_info *) xmalloc(sizeof(*fi));
67
68     fi->p = p;
69     fi->buf = (char *) xmalloc(4096);
70     fi->offset = 1;
71     fi->max = 1;
72     return fi;
73 }
74
75 static int buf_getchar(struct filter_info *tinfo, struct buf_info *fi, char *dst)
76 {
77     if (fi->offset >= fi->max)
78     {
79         if (fi->max <= 0)
80             return 0;
81         fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
82         fi->offset = 0;
83         if (fi->max <= 0)
84             return 0;
85     }
86     *dst = fi->buf[(fi->offset)++];
87     if (tinfo->sep && *dst == *tinfo->sep)
88     {
89         off_t off = fi->p->stream->tellf(fi->p->stream);
90         off_t end_offset = off - (fi->max - fi->offset);
91         fi->p->stream->endf(fi->p->stream, &end_offset);
92         return 0;
93     }
94     return 1;
95 }
96
97 static void buf_close(struct buf_info *fi)
98 {
99     xfree(fi->buf);
100     xfree(fi);
101 }
102
103 static int filter_extract(void *clientData, struct recExtractCtrl *p)
104 {
105     struct filter_info *tinfo = clientData;
106     char w[512];
107     RecWord recWord;
108     int r;
109     struct buf_info *fi = buf_open(p);
110     int no_read = 0;
111
112 #if 0
113     yaz_log(YLOG_LOG, "filter_extract off=%ld",
114             (long) (*fi->p->tellf)(fi->p->fh));
115 #endif
116     (*p->init)(p, &recWord);
117     do
118     {
119         int i = 0;
120             
121         r = buf_getchar(tinfo, fi, w);
122         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
123         {
124             i++;
125             r = buf_getchar(tinfo, fi, w + i); 
126         }
127         if (i)
128         {
129             no_read += i;
130             recWord.term_buf = w;
131             recWord.term_len = i;
132             (*p->tokenAdd)(&recWord);
133         }
134     } while (r > 0);
135     buf_close(fi);
136     if (no_read == 0)
137         return RECCTRL_EXTRACT_EOF;
138     return RECCTRL_EXTRACT_OK;
139 }
140
141 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
142 {
143     int r, filter_ptr = 0;
144     static char *filter_buf = NULL;
145     static int filter_size = 0;
146     int make_header = 1;
147     int make_body = 1;
148     const char *elementSetName = NULL;
149     int no_lines = 0;
150
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;
154
155     if (elementSetName)
156     {
157         /* don't make header for the R(aw) element set name */
158         if (!strcmp(elementSetName, "R"))
159         {
160             make_header = 0;
161             make_body = 1;
162         }
163         /* only make header for the H(eader) element set name */
164         else if (!strcmp(elementSetName, "H"))
165         {
166             make_header = 1;
167             make_body = 0;
168         }
169     }
170     while (1)
171     {
172         if (filter_ptr + 4096 >= filter_size)
173         {
174             char *nb;
175
176             filter_size = 2*filter_size + 8192;
177             nb = (char *) xmalloc(filter_size);
178             if (filter_buf)
179             {
180                 memcpy(nb, filter_buf, filter_ptr);
181                 xfree(filter_buf);
182             }
183             filter_buf = nb;
184         }
185         if (make_header && filter_ptr == 0)
186         {
187             if (p->score >= 0)
188             {
189                 sprintf(filter_buf, "Rank: %d\n", p->score);
190                 filter_ptr = strlen(filter_buf);
191             }
192             sprintf(filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
193                      p->localno);
194             filter_ptr = strlen(filter_buf);
195             if (p->fname)
196             {
197                 sprintf(filter_buf + filter_ptr, "Filename: %s\n", p->fname);
198                 filter_ptr = strlen(filter_buf);
199             }
200             strcpy(filter_buf+filter_ptr++, "\n");
201         }
202         if (!make_body)
203             break;
204         r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
205         if (r <= 0)
206             break;
207         filter_ptr += r;
208     }
209     filter_buf[filter_ptr] = '\0';
210     if (elementSetName)
211     {
212         if (!strcmp(elementSetName, "B"))
213             no_lines = 4;
214         if (!strcmp(elementSetName, "M"))
215             no_lines = 20;
216     }
217     if (no_lines)
218     {
219         char *p = filter_buf;
220         int i = 0;
221
222         while (++i <= no_lines && (p = strchr(p, '\n')))
223             p++;
224         if (p)
225         {
226             p[1] = '\0';
227             filter_ptr = p-filter_buf;
228         }
229     }
230     p->output_format = yaz_oid_recsyn_sutrs;
231     p->rec_buf = filter_buf;
232     p->rec_len = filter_ptr; 
233     return 0;
234 }
235
236 static struct recType filter_type = {
237     0,
238     "text",
239     filter_init,
240     filter_config,
241     filter_destroy,
242     filter_extract,
243     filter_retrieve
244 };
245
246 RecType
247 #ifdef IDZEBRA_STATIC_TEXT
248 idzebra_filter_text
249 #else
250 idzebra_filter
251 #endif
252
253 [] = {
254     &filter_type,
255     0,
256 };
257 /*
258  * Local variables:
259  * c-basic-offset: 4
260  * c-file-style: "Stroustrup"
261  * indent-tabs-mode: nil
262  * End:
263  * vim: shiftwidth=4 tabstop=8 expandtab
264  */
265