1 /* $Id: danbibr.c,v 1.10 2005-03-31 12:42:06 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/recgrs.h>
31 #include <yaz/xmalloc.h>
33 #define READ_CHUNK 200
37 char read_buf[READ_CHUNK+1]; /* space for \0 */
40 static void *init_danbib(Res res, RecType rt)
42 struct danbibr_info *p = (struct danbibr_info *) xmalloc (sizeof(*p));
44 p->rec_buf = wrbuf_alloc();
45 wrbuf_puts(p->rec_buf, "");
49 static int read_rec(struct grs_read_info *p)
51 struct danbibr_info *info = p->clientData;
53 wrbuf_rewind(info->rec_buf);
57 int r = (*p->readf)(p->fh, info->read_buf, READ_CHUNK);
60 if (wrbuf_len(info->rec_buf) > 0)
65 info->read_buf[r] = '\0';
66 wrbuf_puts(info->rec_buf, info->read_buf);
68 cp_split = strstr(wrbuf_buf(info->rec_buf), "\n$");
71 cp_split++; /* now at $ */
73 (*p->endf)(p->fh, p->offset +
74 (cp_split - wrbuf_buf(info->rec_buf)));
82 static data1_node *mk_tree(struct grs_read_info *p, const char *rec_buf)
84 data1_node *root = data1_mk_root(p->dh, p->mem, "danbib");
85 data1_node *root_tag = data1_mk_tag(p->dh, p->mem, "danbib", 0, root);
86 const char *cp = rec_buf;
88 if (1) /* <text> all </text> */
90 data1_node *text_node = data1_mk_tag(p->dh, p->mem, "text", 0, root_tag);
91 data1_mk_text_n(p->dh, p->mem, rec_buf, strlen(rec_buf), text_node);
95 const char *start_tag = cp;
96 const char *start_text;
102 else if (*cp == ' ') /* bad continuation */
104 while (*cp && *cp != '\n')
107 else if (*cp == '$') /* header */
112 for(start_text = cp; *cp && *cp != '\n'; cp++)
115 if (start_text != cp)
119 sprintf(elemstr, "head%d", no);
121 hnode = data1_mk_tag(p->dh, p->mem, elemstr, 0, root_tag);
122 data1_mk_text_n(p->dh, p->mem, start_text,
123 cp - start_text, hnode);
131 while (*cp != ' ' && *cp && *cp != '\n')
135 data1_node *tag_node =
136 data1_mk_tag_n(p->dh, p->mem,
137 start_tag, cp - start_tag, 0, root_tag);
140 while (*cp != '\n' && *cp)
142 if (*cp == '*' && cp[1]) /* subfield */
144 data1_node *sub_tag_node;
145 if (start_text != cp)
146 data1_mk_text_n(p->dh, p->mem, start_text,
147 cp-start_text, tag_node);
150 data1_mk_tag_n(p->dh, p->mem, cp, 1, 0, tag_node);
155 if (*cp == '\n' && cp[1] == ' ')
158 if (start_text != cp)
159 data1_mk_text_n(p->dh, p->mem, start_text,
160 cp-start_text, sub_tag_node);
165 else if (*cp == '\n')
172 if (start_text != cp)
173 data1_mk_text_n(p->dh, p->mem, start_text,
174 cp-start_text, sub_tag_node);
180 if (start_text != cp)
181 data1_mk_text_n(p->dh, p->mem, start_text,
182 cp-start_text, tag_node);
189 static data1_node *read_danbib (struct grs_read_info *p)
191 struct danbibr_info *info = p->clientData;
194 return mk_tree(p, wrbuf_buf(info->rec_buf));
198 static void destroy_danbib(void *clientData)
200 struct danbibr_info *p = (struct danbibr_info *) clientData;
202 wrbuf_free(p->rec_buf, 1);
207 static int extract_danbib(void *clientData, struct recExtractCtrl *ctrl)
209 return zebra_grs_extract(clientData, ctrl, read_danbib);
212 static int retrieve_danbib(void *clientData, struct recRetrieveCtrl *ctrl)
214 return zebra_grs_retrieve(clientData, ctrl, read_danbib);
217 static struct recType danbib_type = {
228 #ifdef IDZEBRA_STATIC_GRS_DANBIB
229 idzebra_filter_grs_danbib