1 /* $Id: safari.c,v 1.1 2004-11-29 21:55:28 adam Exp $
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, "%lld %lld %lld %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++)
143 recWord.length = strlen(cp);
144 (*p->tokenAdd)(&recWord);
147 return RECCTRL_EXTRACT_OK;
150 static int safari_retrieve (void *clientData, struct recRetrieveCtrl *p)
152 int r, safari_ptr = 0;
153 static char *safari_buf = NULL;
154 static int safari_size = 0;
157 const char *elementSetName = NULL;
160 if (p->comp && p->comp->which == Z_RecordComp_simple &&
161 p->comp->u.simple->which == Z_ElementSetNames_generic)
162 elementSetName = p->comp->u.simple->u.generic;
166 /* don't make header for the R(aw) element set name */
167 if (!strcmp(elementSetName, "R"))
172 /* only make header for the H(eader) element set name */
173 else if (!strcmp(elementSetName, "H"))
181 if (safari_ptr + 4096 >= safari_size)
185 safari_size = 2*safari_size + 8192;
186 nb = (char *) xmalloc (safari_size);
189 memcpy (nb, safari_buf, safari_ptr);
194 if (make_header && safari_ptr == 0)
198 sprintf (safari_buf, "Rank: %d\n", p->score);
199 safari_ptr = strlen(safari_buf);
201 sprintf (safari_buf + safari_ptr, "Local Number: " ZINT_FORMAT "\n",
203 safari_ptr = strlen(safari_buf);
206 sprintf (safari_buf + safari_ptr, "Filename: %s\n", p->fname);
207 safari_ptr = strlen(safari_buf);
209 strcpy(safari_buf+safari_ptr++, "\n");
213 r = (*p->readf)(p->fh, safari_buf + safari_ptr, 4096);
218 safari_buf[safari_ptr] = '\0';
221 if (!strcmp (elementSetName, "B"))
223 if (!strcmp (elementSetName, "M"))
228 char *p = safari_buf;
231 while (++i <= no_lines && (p = strchr (p, '\n')))
236 safari_ptr = p-safari_buf;
239 p->output_format = VAL_SUTRS;
240 p->rec_buf = safari_buf;
241 p->rec_len = safari_ptr;
245 static struct recType safari_type = {
255 #ifdef IDZEBRA_STATIC_SAFARI
256 idzebra_filter_safari