IDZEBRA  2.2.7
mod_safari.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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <assert.h>
25 #include <ctype.h>
26 
27 #include <yaz/oid_db.h>
28 #include <yaz/snprintf.h>
29 
30 #include <idzebra/util.h>
31 #include <idzebra/recctrl.h>
32 
33 struct filter_info {
34  int segments;
35 };
36 
37 static void *filter_init(Res res, RecType recType)
38 {
39  struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
40  tinfo->segments = 0;
41  return tinfo;
42 }
43 
44 static void *filter_init2(Res res, RecType recType)
45 {
46  struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
47  tinfo->segments = 1;
48  return tinfo;
49 }
50 
51 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
52 {
53  return ZEBRA_OK;
54 }
55 
56 static void filter_destroy(void *clientData)
57 {
58  struct filter_info *tinfo = clientData;
59  xfree (tinfo);
60 }
61 
62 struct fi_info {
63  struct recExtractCtrl *p;
64  char *buf;
65  int offset;
66  int max;
67 };
68 
69 static struct fi_info *fi_open(struct recExtractCtrl *p)
70 {
71  struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
72 
73  fi->p = p;
74  fi->buf = (char *) xmalloc (4096);
75  fi->offset = 1;
76  fi->max = 1;
77  return fi;
78 }
79 
80 static int fi_getchar(struct fi_info *fi, char *dst)
81 {
82  if (fi->offset >= fi->max)
83  {
84  if (fi->max <= 0)
85  return 0;
86  fi->max = fi->p->stream->readf(fi->p->stream, fi->buf, 4096);
87  fi->offset = 0;
88  if (fi->max <= 0)
89  return 0;
90  }
91  *dst = fi->buf[(fi->offset)++];
92  return 1;
93 }
94 
95 static int fi_gets(struct fi_info *fi, char *dst, int max)
96 {
97  int l = 0;
98  while(1)
99  {
100  char dstbyte;
101  if (!fi_getchar(fi, &dstbyte))
102  return 0;
103  if (dstbyte == '\n')
104  break;
105  if (l < max)
106  dst[l++] = dstbyte;
107  }
108  dst[l] = '\0';
109  return 1;
110 }
111 
112 static void fi_close (struct fi_info *fi)
113 {
114  xfree (fi->buf);
115  xfree (fi);
116 }
117 
118 static int filter_extract(void *clientData, struct recExtractCtrl *p)
119 {
120  struct filter_info *tinfo = clientData;
121  char line[512];
122  RecWord recWord;
123  int ret = RECCTRL_EXTRACT_OK;
124  struct fi_info *fi = fi_open(p);
125 
126 #if 0
127  yaz_log(YLOG_LOG, "filter_extract off=%ld",
128  (long) (*fi->p->tellf)(fi->p->fh));
129 #endif
130  (*p->init)(p, &recWord);
131 
132  if (!fi_gets(fi, line, sizeof(line)-1))
133  ret = RECCTRL_EXTRACT_EOF;
134  else
135  {
136  sscanf(line, "%255s", p->match_criteria);
137  while (fi_gets(fi, line, sizeof(line)-1))
138  {
139  int nor = 0;
140  char field[40];
141  const char *cp = line;
142  char type_cstr[2];
143 #if 0
144  yaz_log(YLOG_LOG, "safari line: %s", line);
145 #endif
146  type_cstr[1] = '\0';
147  if (*cp >= '0' && *cp <= '9')
148  type_cstr[0] = '0'; /* the default is 0 (raw) */
149  else
150  type_cstr[0] = *cp++; /* type given */
151  type_cstr[1] = '\0';
152 
153  recWord.index_type = type_cstr;
154  if (tinfo->segments)
155  {
156  if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT
157  ZINT_FORMAT " %39s %n",
158  &recWord.record_id, &recWord.section_id,
159  &recWord.segment,
160  &recWord.seqno,
161  field, &nor) < 5)
162  {
163  yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
165  break;
166  }
167  }
168  else
169  {
170  if (sscanf(cp, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
171  &recWord.record_id, &recWord.section_id, &recWord.seqno,
172  field, &nor) < 4)
173  {
174  yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
176  break;
177  }
178  }
179  for (cp = cp + nor; *cp == ' '; cp++)
180  ;
181  recWord.index_name = field;
182  recWord.term_buf = cp;
183  recWord.term_len = strlen(cp);
184  (*p->tokenAdd)(&recWord);
185  }
186  }
187  fi_close(fi);
188  return ret;
189 }
190 
191 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
192 {
193  int r, filter_ptr = 0;
194  /* not reentrant and thread safe as static buffer is returned */
195  static char *filter_buf = NULL;
196  static int filter_size = 0;
197  int make_header = 1;
198  int make_body = 1;
199  const char *elementSetName = NULL;
200  int no_lines = 0;
201 
202  if (p->comp && p->comp->which == Z_RecordComp_simple &&
203  p->comp->u.simple->which == Z_ElementSetNames_generic)
204  elementSetName = p->comp->u.simple->u.generic;
205 
206  if (elementSetName)
207  {
208  /* don't make header for the R(aw) element set name */
209  if (!strcmp(elementSetName, "R"))
210  {
211  make_header = 0;
212  make_body = 1;
213  }
214  /* only make header for the H(eader) element set name */
215  else if (!strcmp(elementSetName, "H"))
216  {
217  make_header = 1;
218  make_body = 0;
219  }
220  }
221  while (1)
222  {
223  if (filter_ptr + 4096 >= filter_size)
224  {
225  char *nb;
226 
227  filter_size = 2*filter_size + 8192;
228  nb = (char *) xmalloc(filter_size);
229  if (filter_buf)
230  {
231  memcpy(nb, filter_buf, filter_ptr);
232  xfree(filter_buf);
233  }
234  filter_buf = nb;
235  }
236  if (make_header && filter_ptr == 0)
237  {
238  if (p->score >= 0)
239  {
240  yaz_snprintf(filter_buf, 50, "Rank: %d\n", p->score);
241  filter_ptr = strlen(filter_buf);
242  }
243  yaz_snprintf(filter_buf + filter_ptr, 50, "Local Number: " ZINT_FORMAT "\n",
244  p->localno);
245  filter_ptr = strlen(filter_buf);
246  if (p->fname)
247  {
248  yaz_snprintf(filter_buf + filter_ptr, 200, "Filename: %s\n", p->fname);
249  filter_ptr = strlen(filter_buf);
250  }
251  strcpy(filter_buf+filter_ptr++, "\n");
252  }
253  if (!make_body)
254  break;
255  r = p->stream->readf(p->stream, filter_buf + filter_ptr, 4096);
256  if (r <= 0)
257  break;
258  filter_ptr += r;
259  }
260  filter_buf[filter_ptr] = '\0';
261  if (elementSetName)
262  {
263  if (!strcmp(elementSetName, "B"))
264  no_lines = 4;
265  if (!strcmp(elementSetName, "M"))
266  no_lines = 20;
267  }
268  if (no_lines)
269  {
270  char *p = filter_buf;
271  int i = 0;
272 
273  while (++i <= no_lines && (p = strchr (p, '\n')))
274  p++;
275  if (p)
276  {
277  p[1] = '\0';
278  filter_ptr = p-filter_buf;
279  }
280  }
281  p->output_format = yaz_oid_recsyn_sutrs;
282  p->rec_buf = filter_buf;
283  p->rec_len = filter_ptr;
284  return 0;
285 }
286 
287 static struct recType filter_type = {
288  0,
289  "safari",
290  filter_init,
295 };
296 
297 static struct recType filter_type2 = {
298  0,
299  "safari2",
300  filter_init2,
305 };
306 
307 RecType
308 #if IDZEBRA_STATIC_SAFARI
309 idzebra_filter_safari
310 #else
312 #endif
313 
314 [] = {
315  &filter_type,
316  &filter_type2,
317  0,
318 };
319 /*
320  * Local variables:
321  * c-basic-offset: 4
322  * c-file-style: "Stroustrup"
323  * indent-tabs-mode: nil
324  * End:
325  * vim: shiftwidth=4 tabstop=8 expandtab
326  */
327 
static void filter_destroy(void *clientData)
Definition: mod_safari.c:56
RecType idzebra_filter[]
Definition: mod_safari.c:314
static void fi_close(struct fi_info *fi)
Definition: mod_safari.c:112
static struct recType filter_type2
Definition: mod_safari.c:297
static int fi_gets(struct fi_info *fi, char *dst, int max)
Definition: mod_safari.c:95
static struct fi_info * fi_open(struct recExtractCtrl *p)
Definition: mod_safari.c:69
static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
Definition: mod_safari.c:191
static int filter_extract(void *clientData, struct recExtractCtrl *p)
Definition: mod_safari.c:118
static struct recType filter_type
Definition: mod_safari.c:287
static int fi_getchar(struct fi_info *fi, char *dst)
Definition: mod_safari.c:80
static void * filter_init(Res res, RecType recType)
Definition: mod_safari.c:37
static void * filter_init2(Res res, RecType recType)
Definition: mod_safari.c:44
static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
Definition: mod_safari.c:51
#define RECCTRL_EXTRACT_EOF
Definition: recctrl.h:164
#define RECCTRL_EXTRACT_ERROR_GENERIC
Definition: recctrl.h:165
#define RECCTRL_EXTRACT_OK
Definition: recctrl.h:163
const char * term_buf
Definition: recctrl.h:56
zint record_id
Definition: recctrl.h:64
const char * index_type
Definition: recctrl.h:52
zint seqno
Definition: recctrl.h:60
int term_len
Definition: recctrl.h:58
zint section_id
Definition: recctrl.h:66
zint segment
Definition: recctrl.h:62
const char * index_name
Definition: recctrl.h:54
int(* readf)(struct ZebraRecStream *s, char *buf, size_t count)
read function
Definition: recctrl.h:75
char * buf
Definition: mod_safari.c:64
int max
Definition: mod_safari.c:66
struct recExtractCtrl * p
Definition: mod_safari.c:63
int offset
Definition: mod_safari.c:65
int segments
Definition: mod_safari.c:34
record extract for indexing
Definition: recctrl.h:101
void(* init)(struct recExtractCtrl *p, RecWord *w)
Definition: recctrl.h:103
char match_criteria[256]
Definition: recctrl.h:109
void(* tokenAdd)(RecWord *w)
Definition: recctrl.h:105
struct ZebraRecStream * stream
Definition: recctrl.h:102
Definition: res.c:46
#define ZINT_FORMAT
Definition: util.h:72
#define ZEBRA_OK
Definition: util.h:82
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80