1 /* $Id: danbibr.c,v 1.1 2004-05-21 11:58:56 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
31 #include <yaz/xmalloc.h>
35 #define READ_CHUNK 200
39 char read_buf[READ_CHUNK+1]; /* space for \0 */
42 static void *grs_init_danbib(void)
44 struct danbibr_info *p = (struct danbibr_info *) xmalloc (sizeof(*p));
46 p->rec_buf = wrbuf_alloc();
47 wrbuf_puts(p->rec_buf, "");
51 static int read_rec(struct grs_read_info *p)
53 struct danbibr_info *info = p->clientData;
55 wrbuf_rewind(info->rec_buf);
59 int r = (*p->readf)(p->fh, info->read_buf, READ_CHUNK);
62 if (wrbuf_len(info->rec_buf) > 0)
67 info->read_buf[r] = '\0';
68 wrbuf_puts(info->rec_buf, info->read_buf);
70 cp_split = strstr(wrbuf_buf(info->rec_buf), "\n$");
73 cp_split++; /* now at $ */
75 (*p->endf)(p->fh, p->offset +
76 (cp_split - wrbuf_buf(info->rec_buf)));
84 static data1_node *mk_tree(struct grs_read_info *p, const char *rec_buf)
86 data1_node *root = data1_mk_root(p->dh, p->mem, "danbib");
87 const char *cp = rec_buf;
89 if (1) /* <text> all </text> */
91 data1_node *text_node = data1_mk_tag(p->dh, p->mem, "text", 0, root);
92 data1_mk_text_n(p->dh, p->mem, rec_buf, strlen(rec_buf), text_node);
96 const char *start_tag = cp;
97 const char *start_text;
103 if (*cp == ' ') /* continuation */
105 while (*cp && *cp != '\n')
108 else if (*cp == '$') /* header */
113 for(start_text = cp; *cp && *cp != '\n'; cp++)
116 if (start_text != cp)
120 sprintf(elemstr, "head%d", no);
122 hnode = data1_mk_tag(p->dh, p->mem, elemstr, 0, root);
123 data1_mk_text_n(p->dh, p->mem, start_text,
124 cp - start_text, hnode);
132 while (*cp != ' ' && *cp && *cp != '\n')
136 data1_node *tag_node =
137 data1_mk_tag_n(p->dh, p->mem,
138 start_tag, cp - start_tag, 0, root);
141 while (*cp != '\n' && *cp)
143 if (*cp == '*' && cp[1]) /* subfield */
145 data1_node *sub_tag_node;
146 if (start_text != cp)
147 data1_mk_text_n(p->dh, p->mem, start_text,
148 cp-start_text, tag_node);
151 data1_mk_tag_n(p->dh, p->mem, cp, 1, 0, tag_node);
154 while (*cp && *cp != '\n'&& *cp != '*')
156 if (start_text != cp)
157 data1_mk_text_n(p->dh, p->mem, start_text,
158 cp-start_text, sub_tag_node);
164 if (start_text != cp)
165 data1_mk_text_n(p->dh, p->mem, start_text,
166 cp-start_text, tag_node);
173 static data1_node *grs_read_danbib (struct grs_read_info *p)
175 struct danbibr_info *info = p->clientData;
178 return mk_tree(p, wrbuf_buf(info->rec_buf));
182 static void grs_destroy_danbib(void *clientData)
184 struct danbibr_info *p = (struct danbibr_info *) clientData;
186 wrbuf_free(p->rec_buf, 1);
190 static struct recTypeGrs danbib_type = {
197 RecTypeGrs recTypeGrs_danbib = &danbib_type;