Update copyright year + FSF address
[idzebra-moved-to-github.git] / index / sgmlread.c
1 /* $Id: sgmlread.c,v 1.2 2006-08-14 10:40:15 adam Exp $
2    Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23
24 #include <assert.h>
25 #include <yaz/log.h>
26
27 #include <idzebra/recgrs.h>
28
29 struct sgml_getc_info {
30     char *buf;
31     int buf_size;
32     int size;
33     int off;
34     off_t 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(Res res, RecType recType)
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 ZEBRA_RES grs_config_sgml(void *clientData, Res res, const char *args)
93 {
94     return ZEBRA_OK;
95 }
96
97 static void grs_destroy_sgml(void *clientData)
98 {
99     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
100
101     wrbuf_free(p->wrbuf, 1);
102     xfree (p->buf);
103     xfree (p);
104 }
105
106 static int grs_extract_sgml(void *clientData, struct recExtractCtrl *ctrl)
107 {
108     return zebra_grs_extract(clientData, ctrl, grs_read_sgml);
109 }
110
111 static int grs_retrieve_sgml(void *clientData, struct recRetrieveCtrl *ctrl)
112 {
113     return zebra_grs_retrieve(clientData, ctrl, grs_read_sgml);
114 }
115
116 static struct recType grs_type_sgml =
117 {
118     0,
119     "grs.sgml",
120     grs_init_sgml,
121     grs_config_sgml,
122     grs_destroy_sgml,
123     grs_extract_sgml,
124     grs_retrieve_sgml
125 };
126
127 RecType
128 #ifdef IDZEBRA_STATIC_GRS_SGML
129 idzebra_filter_grs_sgml
130 #else
131 idzebra_filter
132 #endif
133
134 [] = {
135     &grs_type_sgml,
136     0,
137 };
138 /*
139  * Local variables:
140  * c-basic-offset: 4
141  * indent-tabs-mode: nil
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */
145