Reformat: delete trailing whitespace
[idzebra-moved-to-github.git] / rset / rsisamb.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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <assert.h>
25 #include <idzebra/util.h>
26 #include <rset.h>
27 #include <string.h>
28
29 static RSFD r_open(RSET ct, int flag);
30 static void r_close(RSFD rfd);
31 static void r_delete(RSET ct);
32 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
33 static void r_pos(RSFD rfd, double *current, double *total);
34 static int r_read(RSFD rfd, void *buf, TERMID *term);
35 static int r_read_filter(RSFD rfd, void *buf, TERMID *term);
36 static int r_write(RSFD rfd, const void *buf);
37
38 static const struct rset_control control =
39 {
40     "isamb",
41     r_delete,
42     rset_get_one_term,
43     r_open,
44     r_close,
45     r_forward,
46     r_pos,
47     r_read,
48     r_write,
49 };
50
51 static const struct rset_control control_filter =
52 {
53     "isamb",
54     r_delete,
55     rset_get_one_term,
56     r_open,
57     r_close,
58     r_forward,
59     r_pos,
60     r_read_filter,
61     r_write,
62 };
63
64 struct rfd_private {
65     ISAMB_PP pt;
66     void *buf;
67 };
68
69 struct rset_private {
70     ISAMB   is;
71     ISAM_P pos;
72 };
73
74 static int log_level = 0;
75 static int log_level_initialized = 0;
76
77 RSET rsisamb_create(NMEM nmem, struct rset_key_control *kcontrol,
78                     int scope,
79                     ISAMB is, ISAM_P pos, TERMID term)
80 {
81     RSET rnew = rset_create_base(
82         kcontrol->filter_func ? &control_filter : &control,
83         nmem, kcontrol, scope, term, 0, 0);
84     struct rset_private *info;
85     assert(pos);
86     if (!log_level_initialized)
87     {
88         log_level = yaz_log_module_level("rsisamb");
89         log_level_initialized = 1;
90     }
91     info = (struct rset_private *) nmem_malloc(rnew->nmem, sizeof(*info));
92     info->is = is;
93     info->pos = pos;
94     rnew->priv = info;
95     yaz_log(log_level, "rsisamb_create");
96     return rnew;
97 }
98
99 static void r_delete(RSET ct)
100 {
101     yaz_log(log_level, "rsisamb_delete");
102 }
103
104 RSFD r_open(RSET ct, int flag)
105 {
106     struct rset_private *info = (struct rset_private *) ct->priv;
107     RSFD rfd;
108     struct rfd_private *ptinfo;
109
110     if (flag & RSETF_WRITE)
111     {
112         yaz_log(YLOG_FATAL, "ISAMB set type is read-only");
113         return NULL;
114     }
115     rfd = rfd_create_base(ct);
116     if (rfd->priv)
117         ptinfo = (struct rfd_private *) (rfd->priv);
118     else {
119         ptinfo = (struct rfd_private *) nmem_malloc(ct->nmem,sizeof(*ptinfo));
120         ptinfo->buf = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
121         rfd->priv = ptinfo;
122     }
123     ptinfo->pt = isamb_pp_open(info->is, info->pos, ct->scope );
124     yaz_log(log_level, "rsisamb_open");
125     return rfd;
126 }
127
128 static void r_close(RSFD rfd)
129 {
130     struct rfd_private *ptinfo = (struct rfd_private *)(rfd->priv);
131     isamb_pp_close (ptinfo->pt);
132     yaz_log(log_level, "rsisamb_close");
133 }
134
135
136 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
137 {
138     struct rfd_private *pinfo = (struct rfd_private *)(rfd->priv);
139     int rc;
140     rc = isamb_pp_forward(pinfo->pt, buf, untilbuf);
141     if (rc && term)
142         *term = rfd->rset->term;
143     yaz_log(log_level, "rsisamb_forward");
144     return rc;
145 }
146
147 static void r_pos(RSFD rfd, double *current, double *total)
148 {
149     struct rfd_private *pinfo = (struct rfd_private *)(rfd->priv);
150     assert(rfd);
151     isamb_pp_pos(pinfo->pt, current, total);
152     yaz_log(log_level, "isamb.r_pos returning %0.1f/%0.1f",
153               *current, *total);
154 }
155
156 static int r_read(RSFD rfd, void *buf, TERMID *term)
157 {
158     struct rfd_private *pinfo = (struct rfd_private *)(rfd->priv);
159     int rc;
160     rc = isamb_pp_read(pinfo->pt, buf);
161     if (rc && term)
162         *term = rfd->rset->term;
163     yaz_log(log_level, "isamb.r_read ");
164     return rc;
165 }
166
167 static int r_read_filter(RSFD rfd, void *buf, TERMID *term)
168 {
169     struct rfd_private *pinfo = (struct rfd_private *)rfd->priv;
170     const struct rset_key_control *kctrl = rfd->rset->keycontrol;
171     int rc;
172     while((rc = isamb_pp_read(pinfo->pt, buf)))
173     {
174         int incl = (*kctrl->filter_func)(buf, kctrl->filter_data);
175         if (incl)
176             break;
177     }
178     if (rc && term)
179         *term = rfd->rset->term;
180     yaz_log(log_level, "isamb.r_read_filter");
181     return rc;
182 }
183
184 static int r_write(RSFD rfd, const void *buf)
185 {
186     yaz_log(YLOG_FATAL, "ISAMB set type is read-only");
187     return -1;
188 }
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * c-file-style: "Stroustrup"
193  * indent-tabs-mode: nil
194  * End:
195  * vim: shiftwidth=4 tabstop=8 expandtab
196  */
197