Truncate extremly long safari record lines.
[idzebra-moved-to-github.git] / recctrl / alvis.c
1 /* $Id: alvis.c,v 1.3 2005-06-23 06:45:47 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 void filter_config(void *clientData, Res res, const char *args)
43 {
44
45 }
46
47 static void filter_destroy (void *clientData)
48 {
49     struct filter_info *tinfo = clientData;
50     xfree (tinfo->sep);
51     xfree (tinfo);
52 }
53
54 struct buf_info {
55     struct recExtractCtrl *p;
56     char *buf;
57     int offset;
58     int max;
59 };
60
61 static struct buf_info *buf_open (struct recExtractCtrl *p)
62 {
63     struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
64
65     fi->p = p;
66     fi->buf = (char *) xmalloc (4096);
67     fi->offset = 1;
68     fi->max = 1;
69     return fi;
70 }
71
72 static int buf_read (struct filter_info *tinfo, struct buf_info *fi, char *dst)
73 {
74     if (fi->offset >= fi->max)
75     {
76         if (fi->max <= 0)
77             return 0;
78         fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
79         fi->offset = 0;
80         if (fi->max <= 0)
81             return 0;
82     }
83     *dst = fi->buf[(fi->offset)++];
84     if (tinfo->sep && *dst == *tinfo->sep)
85     {
86         off_t off = (*fi->p->tellf)(fi->p->fh);
87         (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->offset));
88         return 0;
89     }
90     return 1;
91 }
92
93 static void buf_close (struct buf_info *fi)
94 {
95     xfree (fi->buf);
96     xfree (fi);
97 }
98
99 static int filter_extract (void *clientData, struct recExtractCtrl *p)
100 {
101     struct filter_info *tinfo = clientData;
102     char w[512];
103     RecWord recWord;
104     int r;
105     struct buf_info *fi = buf_open (p);
106
107 #if 0
108     yaz_log(YLOG_LOG, "filter_extract off=%ld",
109             (long) (*fi->p->tellf)(fi->p->fh));
110 #endif
111     xfree(tinfo->sep);
112     tinfo->sep = 0;
113     (*p->init)(p, &recWord);
114     do
115     {
116         int i = 0;
117             
118         r = buf_read (tinfo, fi, w);
119         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
120         {
121             i++;
122             r = buf_read (tinfo, fi, w + i); 
123         }
124         if (i)
125         {
126             recWord.term_buf = w;
127             recWord.term_len = i;
128             (*p->tokenAdd)(&recWord);
129         }
130     } while (r > 0);
131     buf_close (fi);
132     return RECCTRL_EXTRACT_OK;
133 }
134
135 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
136 {
137     int r, filter_ptr = 0;
138     static char *filter_buf = NULL;
139     static int filter_size = 0;
140     int make_header = 1;
141     int make_body = 1;
142     const char *elementSetName = NULL;
143     int no_lines = 0;
144
145     if (p->comp && p->comp->which == Z_RecordComp_simple &&
146         p->comp->u.simple->which == Z_ElementSetNames_generic)
147         elementSetName = p->comp->u.simple->u.generic;
148
149     if (elementSetName)
150     {
151         /* don't make header for the R(aw) element set name */
152         if (!strcmp(elementSetName, "R"))
153         {
154             make_header = 0;
155             make_body = 1;
156         }
157         /* only make header for the H(eader) element set name */
158         else if (!strcmp(elementSetName, "H"))
159         {
160             make_header = 1;
161             make_body = 0;
162         }
163     }
164     while (1)
165     {
166         if (filter_ptr + 4096 >= filter_size)
167         {
168             char *nb;
169
170             filter_size = 2*filter_size + 8192;
171             nb = (char *) xmalloc (filter_size);
172             if (filter_buf)
173             {
174                 memcpy (nb, filter_buf, filter_ptr);
175                 xfree (filter_buf);
176             }
177             filter_buf = nb;
178         }
179         if (make_header && filter_ptr == 0)
180         {
181             if (p->score >= 0)
182             {
183                 sprintf (filter_buf, "Rank: %d\n", p->score);
184                 filter_ptr = strlen(filter_buf);
185             }
186             sprintf (filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
187                      p->localno);
188             filter_ptr = strlen(filter_buf);
189             if (p->fname)
190             {
191                 sprintf (filter_buf + filter_ptr, "Filename: %s\n", p->fname);
192                 filter_ptr = strlen(filter_buf);
193             }
194             strcpy(filter_buf+filter_ptr++, "\n");
195         }
196         if (!make_body)
197             break;
198         r = (*p->readf)(p->fh, filter_buf + filter_ptr, 4096);
199         if (r <= 0)
200             break;
201         filter_ptr += r;
202     }
203     filter_buf[filter_ptr] = '\0';
204     if (elementSetName)
205     {
206         if (!strcmp (elementSetName, "B"))
207             no_lines = 4;
208         if (!strcmp (elementSetName, "M"))
209             no_lines = 20;
210     }
211     if (no_lines)
212     {
213         char *p = filter_buf;
214         int i = 0;
215
216         while (++i <= no_lines && (p = strchr (p, '\n')))
217             p++;
218         if (p)
219         {
220             p[1] = '\0';
221             filter_ptr = p-filter_buf;
222         }
223     }
224     p->output_format = VAL_SUTRS;
225     p->rec_buf = filter_buf;
226     p->rec_len = filter_ptr; 
227     return 0;
228 }
229
230 static struct recType filter_type = {
231     0,
232     "alvis",
233     filter_init,
234     filter_config,
235     filter_destroy,
236     filter_extract,
237     filter_retrieve
238 };
239
240 RecType
241 #ifdef IDZEBRA_STATIC_ALVIS
242 idzebra_filter_alvis
243 #else
244 idzebra_filter
245 #endif
246
247 [] = {
248     &filter_type,
249     0,
250 };