1 /* $Id: danbibr.c,v 1.2 2004-05-21 13:25:07 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 root = data1_mk_tag(p->dh, p->mem, "danbib", 0, root);
91 if (1) /* <text> all </text> */
93 data1_node *text_node = data1_mk_tag(p->dh, p->mem, "text", 0, root);
94 data1_mk_text_n(p->dh, p->mem, rec_buf, strlen(rec_buf), text_node);
98 const char *start_tag = cp;
99 const char *start_text;
105 else if (*cp == ' ') /* bad continuation */
107 while (*cp && *cp != '\n')
110 else if (*cp == '$') /* header */
115 for(start_text = cp; *cp && *cp != '\n'; cp++)
118 if (start_text != cp)
122 sprintf(elemstr, "head%d", no);
124 hnode = data1_mk_tag(p->dh, p->mem, elemstr, 0, root);
125 data1_mk_text_n(p->dh, p->mem, start_text,
126 cp - start_text, hnode);
134 while (*cp != ' ' && *cp && *cp != '\n')
138 data1_node *tag_node =
139 data1_mk_tag_n(p->dh, p->mem,
140 start_tag, cp - start_tag, 0, root);
143 while (*cp != '\n' && *cp)
145 if (*cp == '*' && cp[1]) /* subfield */
147 data1_node *sub_tag_node;
148 if (start_text != cp)
149 data1_mk_text_n(p->dh, p->mem, start_text,
150 cp-start_text, tag_node);
153 data1_mk_tag_n(p->dh, p->mem, cp, 1, 0, tag_node);
158 if (*cp == '\n' && cp[1] == ' ')
161 if (start_text != cp)
162 data1_mk_text_n(p->dh, p->mem, start_text,
163 cp-start_text, sub_tag_node);
168 else if (*cp == '\n')
173 if (start_text != cp)
174 data1_mk_text_n(p->dh, p->mem, start_text,
175 cp-start_text, sub_tag_node);
181 if (start_text != cp)
182 data1_mk_text_n(p->dh, p->mem, start_text,
183 cp-start_text, tag_node);
190 static data1_node *grs_read_danbib (struct grs_read_info *p)
192 struct danbibr_info *info = p->clientData;
195 return mk_tree(p, wrbuf_buf(info->rec_buf));
199 static void grs_destroy_danbib(void *clientData)
201 struct danbibr_info *p = (struct danbibr_info *) clientData;
203 wrbuf_free(p->rec_buf, 1);
207 static struct recTypeGrs danbib_type = {
214 RecTypeGrs recTypeGrs_danbib = &danbib_type;