1 /* $Id: safari.c,v 1.4 2005-03-05 09:19:16 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
29 #include <idzebra/recctrl.h>
35 static void *safari_init (Res res, RecType recType)
37 struct safari_info *tinfo = (struct safari_info *) xmalloc(sizeof(*tinfo));
42 static void safari_config(void *clientData, Res res, const char *args)
47 static void safari_destroy(void *clientData)
49 struct safari_info *tinfo = clientData;
55 struct recExtractCtrl *p;
61 struct fi_info *fi_open(struct recExtractCtrl *p)
63 struct fi_info *fi = (struct fi_info *) xmalloc (sizeof(*fi));
66 fi->buf = (char *) xmalloc (4096);
72 int fi_getchar(struct fi_info *fi, char *dst)
74 if (fi->offset >= fi->max)
78 fi->max = (*fi->p->readf)(fi->p->fh, fi->buf, 4096);
83 *dst = fi->buf[(fi->offset)++];
87 int fi_gets(struct fi_info *fi, char *dst, int max)
90 for (l = 0; l < max; l++)
92 if (!fi_getchar(fi, dst+l))
101 void fi_close (struct fi_info *fi)
107 static int safari_extract(void *clientData, struct recExtractCtrl *p)
109 struct safari_info *tinfo = clientData;
112 struct fi_info *fi = fi_open(p);
115 yaz_log(YLOG_LOG, "safari_extract off=%ld",
116 (long) (*fi->p->tellf)(fi->p->fh));
120 (*p->init)(p, &recWord);
122 if (!fi_gets(fi, line, sizeof(line)-1))
123 return RECCTRL_EXTRACT_ERROR_GENERIC;
124 sscanf(line, "%255s", p->match_criteria);
126 recWord.reg_type = 'w';
127 while (fi_gets(fi, line, sizeof(line)-1))
132 yaz_log(YLOG_LOG, "safari line: %s", line);
133 if (sscanf(line, ZINT_FORMAT " " ZINT_FORMAT " " ZINT_FORMAT " %39s %n",
134 &recWord.record_id, &recWord.section_id, &recWord.seqno,
137 yaz_log(YLOG_WARN, "Bad safari record line: %s", line);
138 return RECCTRL_EXTRACT_ERROR_GENERIC;
140 for (cp = line + nor; *cp == ' '; cp++)
142 recWord.attrStr = field;
143 recWord.term_buf = cp;
144 recWord.term_len = strlen(cp);
145 (*p->tokenAdd)(&recWord);
148 return RECCTRL_EXTRACT_OK;
151 static int safari_retrieve (void *clientData, struct recRetrieveCtrl *p)
153 int r, safari_ptr = 0;
154 static char *safari_buf = NULL;
155 static int safari_size = 0;
158 const char *elementSetName = NULL;
161 if (p->comp && p->comp->which == Z_RecordComp_simple &&
162 p->comp->u.simple->which == Z_ElementSetNames_generic)
163 elementSetName = p->comp->u.simple->u.generic;
167 /* don't make header for the R(aw) element set name */
168 if (!strcmp(elementSetName, "R"))
173 /* only make header for the H(eader) element set name */
174 else if (!strcmp(elementSetName, "H"))
182 if (safari_ptr + 4096 >= safari_size)
186 safari_size = 2*safari_size + 8192;
187 nb = (char *) xmalloc (safari_size);
190 memcpy (nb, safari_buf, safari_ptr);
195 if (make_header && safari_ptr == 0)
199 sprintf (safari_buf, "Rank: %d\n", p->score);
200 safari_ptr = strlen(safari_buf);
202 sprintf (safari_buf + safari_ptr, "Local Number: " ZINT_FORMAT "\n",
204 safari_ptr = strlen(safari_buf);
207 sprintf (safari_buf + safari_ptr, "Filename: %s\n", p->fname);
208 safari_ptr = strlen(safari_buf);
210 strcpy(safari_buf+safari_ptr++, "\n");
214 r = (*p->readf)(p->fh, safari_buf + safari_ptr, 4096);
219 safari_buf[safari_ptr] = '\0';
222 if (!strcmp (elementSetName, "B"))
224 if (!strcmp (elementSetName, "M"))
229 char *p = safari_buf;
232 while (++i <= no_lines && (p = strchr (p, '\n')))
237 safari_ptr = p-safari_buf;
240 p->output_format = VAL_SUTRS;
241 p->rec_buf = safari_buf;
242 p->rec_len = safari_ptr;
246 static struct recType safari_type = {
256 #ifdef IDZEBRA_STATIC_SAFARI
257 idzebra_filter_safari