1 /* $Id: danbibr.c,v 1.4 2004-05-26 13:47:08 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 data1_node *root_tag = data1_mk_tag(p->dh, p->mem, "danbib", 0, root);
88 const char *cp = rec_buf;
90 if (1) /* <text> all </text> */
92 data1_node *text_node = data1_mk_tag(p->dh, p->mem, "text", 0, root_tag);
93 data1_mk_text_n(p->dh, p->mem, rec_buf, strlen(rec_buf), text_node);
97 const char *start_tag = cp;
98 const char *start_text;
104 else if (*cp == ' ') /* bad continuation */
106 while (*cp && *cp != '\n')
109 else if (*cp == '$') /* header */
114 for(start_text = cp; *cp && *cp != '\n'; cp++)
117 if (start_text != cp)
121 sprintf(elemstr, "head%d", no);
123 hnode = data1_mk_tag(p->dh, p->mem, elemstr, 0, root_tag);
124 data1_mk_text_n(p->dh, p->mem, start_text,
125 cp - start_text, hnode);
133 while (*cp != ' ' && *cp && *cp != '\n')
137 data1_node *tag_node =
138 data1_mk_tag_n(p->dh, p->mem,
139 start_tag, cp - start_tag, 0, root_tag);
142 while (*cp != '\n' && *cp)
144 if (*cp == '*' && cp[1]) /* subfield */
146 data1_node *sub_tag_node;
147 if (start_text != cp)
148 data1_mk_text_n(p->dh, p->mem, start_text,
149 cp-start_text, tag_node);
152 data1_mk_tag_n(p->dh, p->mem, cp, 1, 0, tag_node);
157 if (*cp == '\n' && cp[1] == ' ')
160 if (start_text != cp)
161 data1_mk_text_n(p->dh, p->mem, start_text,
162 cp-start_text, sub_tag_node);
167 else if (*cp == '\n')
174 if (start_text != cp)
175 data1_mk_text_n(p->dh, p->mem, start_text,
176 cp-start_text, sub_tag_node);
182 if (start_text != cp)
183 data1_mk_text_n(p->dh, p->mem, start_text,
184 cp-start_text, tag_node);
191 static data1_node *grs_read_danbib (struct grs_read_info *p)
193 struct danbibr_info *info = p->clientData;
196 return mk_tree(p, wrbuf_buf(info->rec_buf));
200 static void grs_destroy_danbib(void *clientData)
202 struct danbibr_info *p = (struct danbibr_info *) clientData;
204 wrbuf_free(p->rec_buf, 1);
208 static struct recTypeGrs danbib_type = {
215 RecTypeGrs recTypeGrs_danbib = &danbib_type;