For text filter, return only header if elementSetName=H . elementSetName=R
[idzebra-moved-to-github.git] / recctrl / rectext.c
1 /* $Id: rectext.c,v 1.16 2004-03-22 20:52:11 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 static void *text_init (RecType recType)
32 {
33     return 0;
34 }
35
36 static void text_destroy (void *clientData)
37 {
38 }
39
40 struct buf_info {
41     struct recExtractCtrl *p;
42     char *buf;
43     int offset;
44     int max;
45 };
46
47 struct buf_info *buf_open (struct recExtractCtrl *p)
48 {
49     struct buf_info *fi = (struct buf_info *) xmalloc (sizeof(*fi));
50
51     fi->p = p;
52     fi->buf = (char *) xmalloc (4096);
53     fi->offset = 1;
54     fi->max = 1;
55     return fi;
56 }
57
58 int buf_read (struct buf_info *fi, char *dst)
59 {
60     if (fi->offset >= fi->max)
61     {
62         if (fi->max <= 0)
63             return 0;
64         fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
65         fi->offset = 0;
66         if (fi->max <= 0)
67             return 0;
68     }
69     *dst = fi->buf[(fi->offset)++];
70     return 1;
71 }
72
73 void buf_close (struct buf_info *fi)
74 {
75     xfree (fi->buf);
76     xfree (fi);
77 }
78
79 static int text_extract (void *clientData, struct recExtractCtrl *p)
80 {
81     char w[512];
82     RecWord recWord;
83     int r;
84     struct buf_info *fi = buf_open (p);
85
86     (*p->init)(p, &recWord);
87     recWord.reg_type = 'w';
88     do
89     {
90         int i = 0;
91             
92         r = buf_read (fi, w);
93         while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
94         {
95             i++;
96             r = buf_read (fi, w + i);
97         }
98         if (i)
99         {
100             recWord.string = w;
101             recWord.length = i;
102             (*p->tokenAdd)(&recWord);
103         }
104     } while (r > 0);
105     buf_close (fi);
106     return RECCTRL_EXTRACT_OK;
107 }
108
109 static int text_retrieve (void *clientData, struct recRetrieveCtrl *p)
110 {
111     int r, text_ptr = 0;
112     static char *text_buf = NULL;
113     static int text_size = 0;
114     int make_header = 1;
115     int make_body = 1;
116     const char *elementSetName = NULL;
117     int no_lines = 0;
118
119     if (p->comp && p->comp->which == Z_RecordComp_simple &&
120         p->comp->u.simple->which == Z_ElementSetNames_generic)
121         elementSetName = p->comp->u.simple->u.generic;
122
123     if (elementSetName)
124     {
125         /* don't make header for the R(aw) element set name */
126         if (!strcmp(elementSetName, "R"))
127         {
128             make_header = 0;
129             make_body = 1;
130         }
131         /* only make header for the H(eader) element set name */
132         else if (!strcmp(elementSetName, "H"))
133         {
134             make_header = 1;
135             make_body = 0;
136         }
137     }
138     while (1)
139     {
140         if (text_ptr + 4096 >= text_size)
141         {
142             char *nb;
143
144             text_size = 2*text_size + 8192;
145             nb = (char *) xmalloc (text_size);
146             if (text_buf)
147             {
148                 memcpy (nb, text_buf, text_ptr);
149                 xfree (text_buf);
150             }
151             text_buf = nb;
152         }
153         if (make_header && text_ptr == 0)
154         {
155             if (p->score >= 0)
156             {
157                 sprintf (text_buf, "Rank: %d\n", p->score);
158                 text_ptr = strlen(text_buf);
159             }
160             sprintf (text_buf + text_ptr, "Local Number: %d\n", p->localno);
161             text_ptr = strlen(text_buf);
162             if (p->fname)
163             {
164                 sprintf (text_buf + text_ptr, "Filename: %s\n", p->fname);
165                 text_ptr = strlen(text_buf);
166             }
167             strcpy(text_buf+text_ptr++, "\n");
168         }
169         if (!make_body)
170             break;
171         r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
172         if (r <= 0)
173             break;
174         text_ptr += r;
175     }
176     text_buf[text_ptr] = '\0';
177     if (elementSetName)
178     {
179         if (!strcmp (elementSetName, "B"))
180             no_lines = 4;
181         if (!strcmp (elementSetName, "M"))
182             no_lines = 20;
183     }
184     if (no_lines)
185     {
186         char *p = text_buf;
187         int i = 0;
188
189         while (++i <= no_lines && (p = strchr (p, '\n')))
190             p++;
191         if (p)
192         {
193             p[1] = '\0';
194             text_ptr = p-text_buf;
195         }
196     }
197     p->output_format = VAL_SUTRS;
198     p->rec_buf = text_buf;
199     p->rec_len = text_ptr; 
200     return 0;
201 }
202
203 static struct recType text_type = {
204     "text",
205     text_init,
206     text_destroy,
207     text_extract,
208     text_retrieve
209 };
210
211 RecType recTypeText = &text_type;