de72fdb5c11026e5869874cee0ba348f221456f2
[idzebra-moved-to-github.git] / recctrl / rectext.c
1 /* $Id: rectext.c,v 1.18 2004-06-16 20:32:41 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 <zebrautl.h>
29 #include "rectext.h"
30
31 struct text_info {
32     char *sep;
33 };
34
35 static void *text_init (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_destroy (void *clientData)
43 {
44     struct text_info *tinfo = clientData;
45     xfree (tinfo->sep);
46     xfree (tinfo);
47 }
48
49 struct buf_info {
50     struct recExtractCtrl *p;
51     char *buf;
52     int offset;
53     int max;
54 };
55
56 struct buf_info *buf_open (struct recExtractCtrl *p)
57 {
58     struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
59
60     fi->p = p;
61     fi->buf = (char *) xmalloc (4096);
62     fi->offset = 1;
63     fi->max = 1;
64     return fi;
65 }
66
67 int buf_read (struct text_info *tinfo, struct buf_info *fi, char *dst)
68 {
69     if (fi->offset >= fi->max)
70     {
71         if (fi->max <= 0)
72             return 0;
73         fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
74         fi->offset = 0;
75         if (fi->max <= 0)
76             return 0;
77     }
78     *dst = fi->buf[(fi->offset)++];
79     if (tinfo->sep && *dst == *tinfo->sep)
80     {
81         off_t off = (*fi->p->tellf)(fi->p->fh);
82         (*fi->p->endf)(fi->p->fh, off - (fi->max - fi->offset));
83         return 0;
84     }
85     return 1;
86 }
87
88 void buf_close (struct buf_info *fi)
89 {
90     xfree (fi->buf);
91     xfree (fi);
92 }
93
94 static int text_extract (void *clientData, struct recExtractCtrl *p)
95 {
96     struct text_info *tinfo = clientData;
97     char w[512];
98     RecWord recWord;
99     int r;
100     struct buf_info *fi = buf_open (p);
101
102 #if 0
103     yaz_log(LOG_LOG, "text_extract off=%ld",
104             (long) (*fi->p->tellf)(fi->p->fh));
105 #endif
106     xfree(tinfo->sep);
107     tinfo->sep = 0;
108     if (p->subType) {
109         if (!strncmp(p->subType, "sep=", 4))
110             tinfo->sep = xstrdup(p->subType+4);
111     }
112     (*p->init)(p, &recWord);
113     recWord.reg_type = 'w';
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.string = w;
127             recWord.length = i;
128             (*p->tokenAdd)(&recWord);
129         }
130     } while (r > 0);
131     buf_close (fi);
132     return RECCTRL_EXTRACT_OK;
133 }
134
135 static int text_retrieve (void *clientData, struct recRetrieveCtrl *p)
136 {
137     int r, text_ptr = 0;
138     static char *text_buf = NULL;
139     static int text_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 (text_ptr + 4096 >= text_size)
167         {
168             char *nb;
169
170             text_size = 2*text_size + 8192;
171             nb = (char *) xmalloc (text_size);
172             if (text_buf)
173             {
174                 memcpy (nb, text_buf, text_ptr);
175                 xfree (text_buf);
176             }
177             text_buf = nb;
178         }
179         if (make_header && text_ptr == 0)
180         {
181             if (p->score >= 0)
182             {
183                 sprintf (text_buf, "Rank: %d\n", p->score);
184                 text_ptr = strlen(text_buf);
185             }
186             sprintf (text_buf + text_ptr, "Local Number: %d\n", p->localno);
187             text_ptr = strlen(text_buf);
188             if (p->fname)
189             {
190                 sprintf (text_buf + text_ptr, "Filename: %s\n", p->fname);
191                 text_ptr = strlen(text_buf);
192             }
193             strcpy(text_buf+text_ptr++, "\n");
194         }
195         if (!make_body)
196             break;
197         r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
198         if (r <= 0)
199             break;
200         text_ptr += r;
201     }
202     text_buf[text_ptr] = '\0';
203     if (elementSetName)
204     {
205         if (!strcmp (elementSetName, "B"))
206             no_lines = 4;
207         if (!strcmp (elementSetName, "M"))
208             no_lines = 20;
209     }
210     if (no_lines)
211     {
212         char *p = text_buf;
213         int i = 0;
214
215         while (++i <= no_lines && (p = strchr (p, '\n')))
216             p++;
217         if (p)
218         {
219             p[1] = '\0';
220             text_ptr = p-text_buf;
221         }
222     }
223     p->output_format = VAL_SUTRS;
224     p->rec_buf = text_buf;
225     p->rec_len = text_ptr; 
226     return 0;
227 }
228
229 static struct recType text_type = {
230     "text",
231     text_init,
232     text_destroy,
233     text_extract,
234     text_retrieve
235 };
236
237 RecType recTypeText = &text_type;