c22cd51ba2f7d124ed36f13af6bfd14a9975391e
[idzebra-moved-to-github.git] / recctrl / sgmlread.c
1 /* $Id: sgmlread.c,v 1.11 2002-08-02 19:26:56 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24 #include <assert.h>
25 #include <yaz/log.h>
26
27 #include "grsread.h"
28
29 struct sgml_getc_info {
30     char *buf;
31     int buf_size;
32     int size;
33     int off;
34     int moffset;
35     void *fh;
36     int (*readf)(void *, char *, size_t);
37     WRBUF wrbuf;
38 };
39
40 int sgml_getc (void *clientData)
41 {
42     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
43     int res;
44     
45     if (p->off < p->size)
46         return p->buf[(p->off)++];
47     if (p->size < p->buf_size)
48         return 0;
49     p->moffset += p->off;
50     p->off = 0;
51     p->size = 0;
52     res = (*p->readf)(p->fh, p->buf, p->buf_size);
53     if (res > 0)
54     {
55         p->size += res;
56         return p->buf[(p->off)++];
57     }
58     return 0;
59 }
60
61 static data1_node *grs_read_sgml (struct grs_read_info *p)
62 {
63     struct sgml_getc_info *sgi = (struct sgml_getc_info *) p->clientData;
64     data1_node *node;
65     int res;
66     
67     sgi->moffset = p->offset;
68     sgi->fh = p->fh;
69     sgi->readf = p->readf;
70     sgi->off = 0;
71     sgi->size = 0;
72     res = (*sgi->readf)(sgi->fh, sgi->buf, sgi->buf_size);
73     if (res > 0)
74         sgi->size += res;
75     else
76         return 0;
77     node = data1_read_nodex (p->dh, p->mem, sgml_getc, sgi, sgi->wrbuf);
78     if (node && p->endf)
79         (*p->endf)(sgi->fh, sgi->moffset + sgi->off);
80     return node;
81 }
82
83 static void *grs_init_sgml(void)
84 {
85     struct sgml_getc_info *p = (struct sgml_getc_info *) xmalloc (sizeof(*p));
86     p->buf_size = 512;
87     p->buf = xmalloc (p->buf_size);
88     p->wrbuf = wrbuf_alloc();
89     return p;
90 }
91
92 static void grs_destroy_sgml(void *clientData)
93 {
94     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
95
96     wrbuf_free(p->wrbuf, 1);
97     xfree (p->buf);
98     xfree (p);
99 }
100
101 static struct recTypeGrs sgml_type = {
102     "sgml",
103     grs_init_sgml,
104     grs_destroy_sgml,
105     grs_read_sgml
106 };
107
108 RecTypeGrs recTypeGrs_sgml = &sgml_type;
109