Moved zebrautl.h to idzebra/util.h.
[idzebra-moved-to-github.git] / recctrl / rectext.c
1 /* $Id: rectext.c,v 1.25 2005-03-30 09:25:24 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 text_info {
32     char *sep;
33 };
34
35 static void *text_init (Res res, RecType recType)
36 {
37     struct text_info *tinfo = (struct text_info *) xmalloc(sizeof(*tinfo));
38     tinfo->sep = 0;
39     return tinfo;
40 }
41
42 static void text_config(void *clientData, Res res, const char *args)
43 {
44
45 }
46
47 static void text_destroy (void *clientData)
48 {
49     struct text_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 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 int buf_read (struct text_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 void buf_close (struct buf_info *fi)
94 {
95     xfree (fi->buf);
96     xfree (fi);
97 }
98
99 static int text_extract (void *clientData, struct recExtractCtrl *p)
100 {
101     struct text_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, "text_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     recWord.reg_type = 'w';
115     do
116     {
117         int i = 0;
118             
119         r = buf_read (tinfo, fi, w);
120         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
121         {
122             i++;
123             r = buf_read (tinfo, fi, w + i); 
124         }
125         if (i)
126         {
127             recWord.term_buf = w;
128             recWord.term_len = i;
129             (*p->tokenAdd)(&recWord);
130         }
131     } while (r > 0);
132     buf_close (fi);
133     return RECCTRL_EXTRACT_OK;
134 }
135
136 static int text_retrieve (void *clientData, struct recRetrieveCtrl *p)
137 {
138     int r, text_ptr = 0;
139     static char *text_buf = NULL;
140     static int text_size = 0;
141     int make_header = 1;
142     int make_body = 1;
143     const char *elementSetName = NULL;
144     int no_lines = 0;
145
146     if (p->comp && p->comp->which == Z_RecordComp_simple &&
147         p->comp->u.simple->which == Z_ElementSetNames_generic)
148         elementSetName = p->comp->u.simple->u.generic;
149
150     if (elementSetName)
151     {
152         /* don't make header for the R(aw) element set name */
153         if (!strcmp(elementSetName, "R"))
154         {
155             make_header = 0;
156             make_body = 1;
157         }
158         /* only make header for the H(eader) element set name */
159         else if (!strcmp(elementSetName, "H"))
160         {
161             make_header = 1;
162             make_body = 0;
163         }
164     }
165     while (1)
166     {
167         if (text_ptr + 4096 >= text_size)
168         {
169             char *nb;
170
171             text_size = 2*text_size + 8192;
172             nb = (char *) xmalloc (text_size);
173             if (text_buf)
174             {
175                 memcpy (nb, text_buf, text_ptr);
176                 xfree (text_buf);
177             }
178             text_buf = nb;
179         }
180         if (make_header && text_ptr == 0)
181         {
182             if (p->score >= 0)
183             {
184                 sprintf (text_buf, "Rank: %d\n", p->score);
185                 text_ptr = strlen(text_buf);
186             }
187             sprintf (text_buf + text_ptr, "Local Number: " ZINT_FORMAT "\n",
188                      p->localno);
189             text_ptr = strlen(text_buf);
190             if (p->fname)
191             {
192                 sprintf (text_buf + text_ptr, "Filename: %s\n", p->fname);
193                 text_ptr = strlen(text_buf);
194             }
195             strcpy(text_buf+text_ptr++, "\n");
196         }
197         if (!make_body)
198             break;
199         r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
200         if (r <= 0)
201             break;
202         text_ptr += r;
203     }
204     text_buf[text_ptr] = '\0';
205     if (elementSetName)
206     {
207         if (!strcmp (elementSetName, "B"))
208             no_lines = 4;
209         if (!strcmp (elementSetName, "M"))
210             no_lines = 20;
211     }
212     if (no_lines)
213     {
214         char *p = text_buf;
215         int i = 0;
216
217         while (++i <= no_lines && (p = strchr (p, '\n')))
218             p++;
219         if (p)
220         {
221             p[1] = '\0';
222             text_ptr = p-text_buf;
223         }
224     }
225     p->output_format = VAL_SUTRS;
226     p->rec_buf = text_buf;
227     p->rec_len = text_ptr; 
228     return 0;
229 }
230
231 static struct recType text_type = {
232     "text",
233     text_init,
234     text_config,
235     text_destroy,
236     text_extract,
237     text_retrieve
238 };
239
240 RecType
241 #ifdef IDZEBRA_STATIC_TEXT
242 idzebra_filter_text
243 #else
244 idzebra_filter
245 #endif
246
247 [] = {
248     &text_type,
249     0,
250 };