1 /* $Id: rsbool.c,v 1.19 2002-08-02 19:26:57 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
33 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
34 static RSFD r_open (RSET ct, int flag);
35 static void r_close (RSFD rfd);
36 static void r_delete (RSET ct);
37 static void r_rewind (RSFD rfd);
38 static int r_count (RSET ct);
39 static int r_read_and (RSFD rfd, void *buf, int *term_index);
40 static int r_read_or (RSFD rfd, void *buf, int *term_index);
41 static int r_read_not (RSFD rfd, void *buf, int *term_index);
42 static int r_write (RSFD rfd, const void *buf);
44 static const struct rset_control control_and =
57 static const struct rset_control control_or =
70 static const struct rset_control control_not =
84 const struct rset_control *rset_kind_and = &control_and;
85 const struct rset_control *rset_kind_or = &control_or;
86 const struct rset_control *rset_kind_not = &control_not;
88 struct rset_bool_info {
93 int (*cmp)(const void *p1, const void *p2);
94 struct rset_bool_rfd *rfd_list;
97 struct rset_bool_rfd {
106 struct rset_bool_rfd *next;
107 struct rset_bool_info *info;
110 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
112 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
113 struct rset_bool_info *info;
115 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
116 info->key_size = bool_parms->key_size;
117 info->rset_l = bool_parms->rset_l;
118 info->rset_r = bool_parms->rset_r;
119 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
120 ct->flags |= RSET_FLAG_VOLATILE;
121 info->cmp = bool_parms->cmp;
122 info->rfd_list = NULL;
124 info->term_index_s = info->rset_l->no_rset_terms;
126 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
127 ct->rset_terms = (RSET_TERM *)
128 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
130 memcpy (ct->rset_terms, info->rset_l->rset_terms,
131 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
132 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
133 info->rset_r->rset_terms,
134 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
138 static RSFD r_open (RSET ct, int flag)
140 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
141 struct rset_bool_rfd *rfd;
143 if (flag & RSETF_WRITE)
145 logf (LOG_FATAL, "bool set type is read-only");
148 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
149 rfd->next = info->rfd_list;
150 info->rfd_list = rfd;
153 rfd->buf_l = xmalloc (info->key_size);
154 rfd->buf_r = xmalloc (info->key_size);
155 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
156 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
157 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
159 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
164 static void r_close (RSFD rfd)
166 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
167 struct rset_bool_rfd **rfdp;
169 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
172 xfree ((*rfdp)->buf_l);
173 xfree ((*rfdp)->buf_r);
174 rset_close (info->rset_l, (*rfdp)->rfd_l);
175 rset_close (info->rset_r, (*rfdp)->rfd_r);
176 *rfdp = (*rfdp)->next;
180 logf (LOG_FATAL, "r_close but no rfd match!");
184 static void r_delete (RSET ct)
186 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
188 assert (info->rfd_list == NULL);
189 xfree (ct->rset_terms);
190 rset_delete (info->rset_l);
191 rset_delete (info->rset_r);
195 static void r_rewind (RSFD rfd)
197 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
198 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
200 logf (LOG_DEBUG, "rsbool_rewind");
201 rset_rewind (info->rset_l, p->rfd_l);
202 rset_rewind (info->rset_r, p->rfd_r);
203 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
204 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
207 static int r_count (RSET ct)
212 static int r_read_and (RSFD rfd, void *buf, int *term_index)
214 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
215 struct rset_bool_info *info = p->info;
217 while (p->more_l && p->more_r)
221 cmp = (*info->cmp)(p->buf_l, p->buf_r);
224 memcpy (buf, p->buf_l, info->key_size);
225 *term_index = p->term_index_l;
226 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
228 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
234 memcpy (buf, p->buf_r, info->key_size);
236 *term_index = p->term_index_r + info->term_index_s;
237 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
243 memcpy (buf, p->buf_l, info->key_size);
244 *term_index = p->term_index_l;
245 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
250 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
253 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
257 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
260 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
265 static int r_read_or (RSFD rfd, void *buf, int *term_index)
267 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
268 struct rset_bool_info *info = p->info;
270 while (p->more_l || p->more_r)
274 if (p->more_l && p->more_r)
275 cmp = (*info->cmp)(p->buf_l, p->buf_r);
282 memcpy (buf, p->buf_l, info->key_size);
283 *term_index = p->term_index_l;
284 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
286 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
292 memcpy (buf, p->buf_r, info->key_size);
293 *term_index = p->term_index_r + info->term_index_s;
294 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
300 memcpy (buf, p->buf_l, info->key_size);
301 *term_index = p->term_index_l;
302 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
310 static int r_read_not (RSFD rfd, void *buf, int *term_index)
312 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
313 struct rset_bool_info *info = p->info;
315 while (p->more_l || p->more_r)
319 if (p->more_l && p->more_r)
320 cmp = (*info->cmp)(p->buf_l, p->buf_r);
327 memcpy (buf, p->buf_l, info->key_size);
328 *term_index = p->term_index_l;
329 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
334 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
338 memcpy (buf, p->buf_l, info->key_size);
341 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
345 cmp = (*info->cmp)(p->buf_l, buf);
346 } while (cmp >= -1 && cmp <= 1);
349 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
353 cmp = (*info->cmp)(p->buf_r, buf);
354 } while (cmp >= -1 && cmp <= 1);
361 static int r_write (RSFD rfd, const void *buf)
363 logf (LOG_FATAL, "bool set type is read-only");