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