Start work on ICU based regexp searches
[idzebra-moved-to-github.git] / index / mod_grs_sgml.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
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     struct ZebraRecStream *stream;
35     off_t moffset;
36     WRBUF wrbuf;
37 };
38
39 int sgml_getc (void *clientData)
40 {
41     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
42     int res;
43     
44     if (p->off < p->size)
45         return p->buf[(p->off)++];
46     if (p->size < p->buf_size)
47         return 0;
48     p->moffset += p->off;
49     p->off = 0;
50     p->size = 0;
51     res = p->stream->readf(p->stream, p->buf, p->buf_size);
52     if (res > 0)
53     {
54         p->size += res;
55         return p->buf[(p->off)++];
56     }
57     return 0;
58 }
59
60 static data1_node *grs_read_sgml (struct grs_read_info *p)
61 {
62     struct sgml_getc_info *sgi = (struct sgml_getc_info *) p->clientData;
63     data1_node *node;
64     int res;
65     
66     sgi->moffset = p->stream->tellf(p->stream);
67     sgi->stream = p->stream;
68     sgi->off = 0;
69     sgi->size = 0;
70     res = sgi->stream->readf(sgi->stream, sgi->buf, sgi->buf_size);
71     if (res > 0)
72         sgi->size += res;
73     else
74         return 0;
75     node = data1_read_nodex(p->dh, p->mem, sgml_getc, sgi, sgi->wrbuf);
76     if (node && p->stream->endf)
77     {
78         off_t end_offset = sgi->moffset + sgi->off;
79         p->stream->endf(sgi->stream, &end_offset);
80     }
81     return node;
82 }
83
84 static void *grs_init_sgml(Res res, RecType recType)
85 {
86     struct sgml_getc_info *p = (struct sgml_getc_info *) xmalloc (sizeof(*p));
87     p->buf_size = 512;
88     p->buf = xmalloc (p->buf_size);
89     p->wrbuf = wrbuf_alloc();
90     return p;
91 }
92
93 static ZEBRA_RES grs_config_sgml(void *clientData, Res res, const char *args)
94 {
95     return ZEBRA_OK;
96 }
97
98 static void grs_destroy_sgml(void *clientData)
99 {
100     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
101
102     wrbuf_destroy(p->wrbuf);
103     xfree(p->buf);
104     xfree(p);
105 }
106
107 static int grs_extract_sgml(void *clientData, struct recExtractCtrl *ctrl)
108 {
109     return zebra_grs_extract(clientData, ctrl, grs_read_sgml);
110 }
111
112 static int grs_retrieve_sgml(void *clientData, struct recRetrieveCtrl *ctrl)
113 {
114     return zebra_grs_retrieve(clientData, ctrl, grs_read_sgml);
115 }
116
117 static struct recType grs_type_sgml =
118 {
119     0,
120     "grs.sgml",
121     grs_init_sgml,
122     grs_config_sgml,
123     grs_destroy_sgml,
124     grs_extract_sgml,
125     grs_retrieve_sgml
126 };
127
128 RecType
129 #if IDZEBRA_STATIC_GRS_SGML
130 idzebra_filter_grs_sgml
131 #else
132 idzebra_filter
133 #endif
134
135 [] = {
136     &grs_type_sgml,
137     0,
138 };
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * c-file-style: "Stroustrup"
143  * indent-tabs-mode: nil
144  * End:
145  * vim: shiftwidth=4 tabstop=8 expandtab
146  */
147