Added forward functions to rsbool, and a few tests for them.
[idzebra-moved-to-github.git] / rset / rset.c
1 /* $Id: rset.c,v 1.20 2004-01-30 11:43:41 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
25 #include <stdio.h>
26 #include <string.h>
27 #include <zebrautl.h>
28
29 #include <rset.h>
30 #include <../index/index.h> /* for log_keydump. Debugging only */
31
32 RSET rset_create(const struct rset_control *sel, void *parms)
33 {
34     RSET rnew;
35     int i;
36
37     logf (LOG_DEBUG, "rs_create(%s)", sel->desc);
38     rnew = (RSET) xmalloc(sizeof(*rnew));
39     rnew->control = sel;
40     rnew->flags = 0;
41     rnew->count = 1;
42     rnew->rset_terms = NULL;
43     rnew->no_rset_terms = 0;
44     rnew->buf = (*sel->f_create)(rnew, sel, parms);
45     logf (LOG_DEBUG, "no_rset_terms: %d", rnew->no_rset_terms);
46     for (i = 0; i<rnew->no_rset_terms; i++)
47         logf (LOG_DEBUG, " %s", rnew->rset_terms[i]->name);
48     return rnew;
49 }
50
51 void rset_delete (RSET rs)
52 {
53     (rs->count)--;
54     if (!rs->count)
55     {
56         (*rs->control->f_delete)(rs);
57         xfree(rs);
58     }
59 }
60
61 RSET rset_dup (RSET rs)
62 {
63     (rs->count)++;
64     return rs;
65 }
66
67 int rset_default_forward(RSET ct, RSFD rfd, void *buf, int *term_index, 
68                            int (*cmpfunc)(const void *p1, const void *p2), 
69                            const void *untilbuf)
70 {
71     int more=1;
72     int cmp=2;
73     logf (LOG_DEBUG, "rset_default_forward starting '%s' (ct=%p rfd=%p)",
74                     ct->control->desc, ct,rfd);
75     key_logdump(LOG_DEBUG, untilbuf);
76     while ( (cmp==2) && (more))
77     {
78         logf (LOG_DEBUG, "rset_default_forward looping m=%d c=%d",more,cmp);
79         more=rset_read(ct, rfd, buf, term_index);
80         if (more)
81             cmp=(*cmpfunc)(untilbuf,buf);
82         if (more)
83             key_logdump(LOG_DEBUG,buf);
84     }
85     logf (LOG_DEBUG, "rset_default_forward exiting m=%d c=%d",more,cmp);
86
87     return more;
88 }
89
90 RSET_TERM *rset_terms(RSET rs, int *no)
91 {
92     *no = rs->no_rset_terms;
93     return rs->rset_terms;
94 }
95
96 RSET_TERM rset_term_create (const char *name, int length, const char *flags,
97                             int type)
98 {
99     RSET_TERM t = (RSET_TERM) xmalloc (sizeof(*t));
100     if (!name)
101         t->name = NULL;
102     else if (length == -1)
103         t->name = xstrdup (name);
104     else
105     {
106         t->name = (char*) xmalloc (length+1);
107         memcpy (t->name, name, length);
108         t->name[length] = '\0';
109     }
110     if (!flags)
111         t->flags = NULL;
112     else
113         t->flags = xstrdup (flags);
114     t->nn = -1;
115     t->count = 0;
116     t->type = type;
117     return t;
118 }
119
120 void rset_term_destroy (RSET_TERM t)
121 {
122     xfree (t->name);
123     xfree (t->flags);
124     xfree (t);
125 }
126
127 RSET_TERM rset_term_dup (RSET_TERM t)
128 {
129     RSET_TERM nt = (RSET_TERM) xmalloc (sizeof(*nt));
130     if (t->name)
131         nt->name = xstrdup (t->name);
132     else
133         nt->name = NULL;
134     if (t->flags)
135         nt->flags = xstrdup (t->flags);
136     else
137         nt->flags = NULL;
138     nt->nn = t->nn;
139     return nt;
140 }