IDZEBRA  2.1.2
mod_text.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdio.h>
25 #include <assert.h>
26 #include <ctype.h>
27 
28 #include <idzebra/util.h>
29 #include <idzebra/recctrl.h>
30 #include <yaz/oid_db.h>
31 
32 struct filter_info {
33  char *sep;
34 };
35 
36 static void *filter_init(Res res, RecType recType)
37 {
38  struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
39  tinfo->sep = 0;
40  return tinfo;
41 }
42 
43 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
44 {
45  struct filter_info *tinfo = (struct filter_info*) clientData;
46  xfree(tinfo->sep);
47  tinfo->sep = 0;
48  if (args && *args)
49  tinfo->sep = xstrdup(args);
50  return ZEBRA_OK;
51 }
52 
53 static void filter_destroy(void *clientData)
54 {
55  struct filter_info *tinfo = clientData;
56  xfree(tinfo->sep);
57  xfree(tinfo);
58 }
59 
60 struct buf_info {
61  struct recExtractCtrl *p;
62  char *buf;
63  int offset;
64  int max;
65 };
66 
67 static struct buf_info *buf_open(struct recExtractCtrl *p)
68 {
69  struct buf_info *fi = (struct buf_info *) xmalloc(sizeof(*fi));
70 
71  fi->p = p;
72  fi->buf = (char *) xmalloc(4096);
73  fi->offset = 1;
74  fi->max = 1;
75  return fi;
76 }
77 
78 static int buf_getchar(struct filter_info *tinfo, struct buf_info *fi, char *dst)
79 {
80  if (fi->offset >= fi->max)
81  {
82  if (fi->max <= 0)
83  return 0;
84  fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
85  fi->offset = 0;
86  if (fi->max <= 0)
87  return 0;
88  }
89  *dst = fi->buf[(fi->offset)++];
90  if (tinfo->sep && *dst == *tinfo->sep)
91  {
92  off_t off = fi->p->stream->tellf(fi->p->stream);
93  off_t end_offset = off - (fi->max - fi->offset);
94  fi->p->stream->endf(fi->p->stream, &end_offset);
95  return 0;
96  }
97  return 1;
98 }
99 
100 static void buf_close(struct buf_info *fi)
101 {
102  xfree(fi->buf);
103  xfree(fi);
104 }
105 
106 static int filter_extract(void *clientData, struct recExtractCtrl *p)
107 {
108  struct filter_info *tinfo = clientData;
109  char w[512];
110  RecWord recWord;
111  int r;
112  struct buf_info *fi = buf_open(p);
113  int no_read = 0;
114 
115 #if 0
116  yaz_log(YLOG_LOG, "filter_extract off=%ld",
117  (long) (*fi->p->tellf)(fi->p->fh));
118 #endif
119  (*p->init)(p, &recWord);
120  do
121  {
122  int i = 0;
123 
124  r = buf_getchar(tinfo, fi, w);
125  while (r > 0 && i < 511 && w[i] != '\n' && w[i] != '\r')
126  {
127  i++;
128  r = buf_getchar(tinfo, fi, w + i);
129  }
130  if (i)
131  {
132  no_read += i;
133  recWord.term_buf = w;
134  recWord.term_len = i;
135  (*p->tokenAdd)(&recWord);
136  }
137  } while (r > 0);
138  buf_close(fi);
139  if (no_read == 0)
140  return RECCTRL_EXTRACT_EOF;
141  return RECCTRL_EXTRACT_OK;
142 }
143 
144 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
145 {
146  int r, filter_ptr = 0;
147  static char *filter_buf = NULL;
148  static int filter_size = 0;
149  int make_header = 1;
150  int make_body = 1;
151  const char *elementSetName = NULL;
152  int no_lines = 0;
153 
154  if (p->comp && p->comp->which == Z_RecordComp_simple &&
155  p->comp->u.simple->which == Z_ElementSetNames_generic)
156  elementSetName = p->comp->u.simple->u.generic;
157 
158  if (elementSetName)
159  {
160  /* don't make header for the R(aw) element set name */
161  if (!strcmp(elementSetName, "R"))
162  {
163  make_header = 0;
164  make_body = 1;
165  }
166  /* only make header for the H(eader) element set name */
167  else if (!strcmp(elementSetName, "H"))
168  {
169  make_header = 1;
170  make_body = 0;
171  }
172  }
173  while (1)
174  {
175  if (filter_ptr + 4096 >= filter_size)
176  {
177  char *nb;
178 
179  filter_size = 2*filter_size + 8192;
180  nb = (char *) xmalloc(filter_size);
181  if (filter_buf)
182  {
183  memcpy(nb, filter_buf, filter_ptr);
184  xfree(filter_buf);
185  }
186  filter_buf = nb;
187  }
188  if (make_header && filter_ptr == 0)
189  {
190  if (p->score >= 0)
191  {
192  sprintf(filter_buf, "Rank: %d\n", p->score);
193  filter_ptr = strlen(filter_buf);
194  }
195  sprintf(filter_buf + filter_ptr, "Local Number: " ZINT_FORMAT "\n",
196  p->localno);
197  filter_ptr = strlen(filter_buf);
198  if (p->fname)
199  {
200  sprintf(filter_buf + filter_ptr, "Filename: %s\n", p->fname);
201  filter_ptr = strlen(filter_buf);
202  }
203  strcpy(filter_buf+filter_ptr++, "\n");
204  }
205  if (!make_body)
206  break;
207  r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
208  if (r <= 0)
209  break;
210  filter_ptr += r;
211  }
212  filter_buf[filter_ptr] = '\0';
213  if (elementSetName)
214  {
215  if (!strcmp(elementSetName, "B"))
216  no_lines = 4;
217  if (!strcmp(elementSetName, "M"))
218  no_lines = 20;
219  }
220  if (no_lines)
221  {
222  char *p = filter_buf;
223  int i = 0;
224 
225  while (++i <= no_lines && (p = strchr(p, '\n')))
226  p++;
227  if (p)
228  {
229  p[1] = '\0';
230  filter_ptr = p-filter_buf;
231  }
232  }
233  p->output_format = yaz_oid_recsyn_sutrs;
234  p->rec_buf = filter_buf;
235  p->rec_len = filter_ptr;
236  return 0;
237 }
238 
239 static struct recType filter_type = {
240  0,
241  "text",
242  filter_init,
247 };
248 
249 RecType
250 #if IDZEBRA_STATIC_TEXT
251 idzebra_filter_text
252 #else
254 #endif
255 
256 [] = {
257  &filter_type,
258  0,
259 };
260 /*
261  * Local variables:
262  * c-basic-offset: 4
263  * c-file-style: "Stroustrup"
264  * indent-tabs-mode: nil
265  * End:
266  * vim: shiftwidth=4 tabstop=8 expandtab
267  */
268 
int max
Definition: mod_text.c:64
#define RECCTRL_EXTRACT_OK
Definition: recctrl.h:163
void(* tokenAdd)(RecWord *w)
Definition: recctrl.h:105
#define ZEBRA_OK
Definition: util.h:82
int term_len
Definition: recctrl.h:58
RecType idzebra_filter[]
Definition: mod_text.c:256
static struct recType filter_type
Definition: mod_text.c:239
static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
Definition: mod_text.c:43
char * sep
Definition: mod_text.c:33
#define RECCTRL_EXTRACT_EOF
Definition: recctrl.h:164
Definition: res.c:46
const Odr_oid * output_format
Definition: recctrl.h:134
static void * filter_init(Res res, RecType recType)
Definition: mod_text.c:36
static void filter_destroy(void *clientData)
Definition: mod_text.c:53
void(* init)(struct recExtractCtrl *p, RecWord *w)
Definition: recctrl.h:103
static void buf_close(struct buf_info *fi)
Definition: mod_text.c:100
off_t(* endf)(struct ZebraRecStream *s, off_t *offset)
set and get of record position
Definition: recctrl.h:81
int offset
Definition: mod_text.c:63
int(* readf)(struct ZebraRecStream *s, char *buf, size_t count)
read function
Definition: recctrl.h:75
static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
Definition: mod_text.c:144
char * buf
Definition: mod_text.c:62
Z_RecordComposition * comp
Definition: recctrl.h:124
struct ZebraRecStream * stream
Definition: recctrl.h:119
static int filter_extract(void *clientData, struct recExtractCtrl *p)
Definition: mod_text.c:106
const char * term_buf
Definition: recctrl.h:56
static struct buf_info * buf_open(struct recExtractCtrl *p)
Definition: mod_text.c:67
void * rec_buf
Definition: recctrl.h:135
off_t(* tellf)(struct ZebraRecStream *s)
tell function
Definition: recctrl.h:79
record extract for indexing
Definition: recctrl.h:101
char * fname
Definition: recctrl.h:130
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80
struct ZebraRecStream * stream
Definition: recctrl.h:102
struct recExtractCtrl * p
Definition: mod_text.c:61
static int buf_getchar(struct filter_info *tinfo, struct buf_info *fi, char *dst)
Definition: mod_text.c:78
#define ZINT_FORMAT
Definition: util.h:72