Towards GPL
[idzebra-moved-to-github.git] / recctrl / rectext.c
1 /* $Id: rectext.c,v 1.15 2002-08-02 19:26:56 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 start_flag = 1;
115     const char *elementSetName = NULL;
116     int no_lines = 0;
117
118     if (p->comp && p->comp->which == Z_RecordComp_simple &&
119         p->comp->u.simple->which == Z_ElementSetNames_generic)
120         elementSetName = p->comp->u.simple->u.generic;
121
122     /* don't make header for the R(aw) element set name */
123     if (elementSetName && !strcmp(elementSetName, "R"))
124         start_flag = 0;
125     while (1)
126     {
127         if (text_ptr + 4096 >= text_size)
128         {
129             char *nb;
130
131             text_size = 2*text_size + 8192;
132             nb = (char *) xmalloc (text_size);
133             if (text_buf)
134             {
135                 memcpy (nb, text_buf, text_ptr);
136                 xfree (text_buf);
137             }
138             text_buf = nb;
139         }
140         if (start_flag)
141         {
142             start_flag = 0;
143             if (p->score >= 0)
144             {
145                 sprintf (text_buf, "Rank: %d\n", p->score);
146                 text_ptr = strlen(text_buf);
147             }
148             sprintf (text_buf + text_ptr, "Local Number: %d\n", p->localno);
149             text_ptr = strlen(text_buf);
150         }
151         r = (*p->readf)(p->fh, text_buf + text_ptr, 4096);
152         if (r <= 0)
153             break;
154         text_ptr += r;
155     }
156     text_buf[text_ptr] = '\0';
157     if (elementSetName)
158     {
159         if (!strcmp (elementSetName, "B"))
160             no_lines = 4;
161         if (!strcmp (elementSetName, "M"))
162             no_lines = 20;
163     }
164     if (no_lines)
165     {
166         char *p = text_buf;
167         int i = 0;
168
169         while (++i <= no_lines && (p = strchr (p, '\n')))
170             p++;
171         if (p)
172         {
173             p[1] = '\0';
174             text_ptr = p-text_buf;
175         }
176     }
177     p->output_format = VAL_SUTRS;
178     p->rec_buf = text_buf;
179     p->rec_len = text_ptr; 
180     return 0;
181 }
182
183 static struct recType text_type = {
184     "text",
185     text_init,
186     text_destroy,
187     text_extract,
188     text_retrieve
189 };
190
191 RecType recTypeText = &text_type;