Major restructuring in rsets.
[idzebra-moved-to-github.git] / include / rset.h
1 /* $Id: rset.h,v 1.32 2004-08-31 10:43:35 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 #ifndef RSET_H
26 #define RSET_H
27
28 #include <stdlib.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 typedef struct rsfd *RSFD; /* Rset "file descriptor" */
35 typedef struct rset *RSET; /* Result set */
36
37 struct rsfd {  /* the stuff common to all rsfd's. */
38     RSET rset;  /* ptr to the rset this FD is opened to */
39     void *priv; /* private parameters for this type */
40     RSFD next;  /* to keep lists of used/free rsfd's */
41 };
42
43
44 struct rset_control
45 {
46     char *desc; /* text description of set type (for debugging) */
47 /* RSET rs_something_create(const struct rset_control *sel, ...); */
48     void (*f_delete)(RSET ct);
49     RSFD (*f_open)(RSET ct, int wflag);
50     void (*f_close)(RSFD rfd);
51     void (*f_rewind)(RSFD rfd);
52     int (*f_forward)(RSFD rfd, void *buf,
53                      int (*cmpfunc)(const void *p1, const void *p2), 
54                      const void *untilbuf);
55     void (*f_pos)(RSFD rfd, double *current, double *total);
56        /* returns -1,-1 if pos function not implemented for this type */
57     int (*f_read)(RSFD rfd, void *buf);
58     int (*f_write)(RSFD rfd, const void *buf);
59 };
60
61 int rset_default_forward(RSFD rfd, void *buf, 
62                      int (*cmpfunc)(const void *p1, const void *p2), 
63                      const void *untilbuf);
64
65
66 typedef struct rset
67 {
68     const struct rset_control *control;
69     int  count;  /* reference count */
70     void *priv;  /* stuff private to the given type of rset */
71     NMEM nmem;    /* nibble memory for various allocs */
72     char my_nmem; /* Should the nmem be destroyed with the rset?  */
73                   /* 1 if created with it, 0 if passed from above */
74     RSFD free_list; /* all rfd's allocated but not currently in use */
75 } rset;
76 /* rset is a "virtual base class", which will never exist on its own */
77 /* all instances are rsets of some specific type, like rsisamb, or rsbool */
78 /* They keep their own stuff behind the priv pointer. */
79
80 RSFD rfd_create_base(RSET rs);
81 void rfd_delete_base(RSFD rfd);
82
83 RSET rset_create_base(const struct rset_control *sel, NMEM nmem);
84 void rset_delete(RSET rs);
85 RSET rset_dup (RSET rs);
86
87
88 #define RSETF_READ       0
89 #define RSETF_WRITE      1
90 /* RSFD rset_open(RSET rs, int wflag); */
91 #define rset_open(rs, wflag) (*(rs)->control->f_open)((rs), (wflag))
92
93 /* void rset_close(RSFD rfd); */
94 #define rset_close(rfd) (*(rfd)->rset->control->f_close)(rfd)
95
96 /* void rset_rewind(RSFD rfd); */
97 #define rset_rewind(rfd) (*(rfd)->rset->control->f_rewind)((rfd))
98
99 /* int rset_forward(RSFD rfd, void *buf, void *untilbuf); */
100 #define rset_forward(rfd, buf, cmpfunc, untilbuf) \
101     (*(rfd)->rset->control->f_forward)((rfd),(buf),(cmpfunc),(untilbuf))
102 /*FIXME - get rid of the cmp function here, keep it in a general */
103 /*        key_control block */
104
105 /* int rset_pos(RSFD fd, double *current, double *total); */
106 #define rset_pos(rfd,cur,tot) \
107     (*(rfd)->rset->control->f_pos)( (rfd),(cur),(tot))
108
109 /* int rset_read(RSFD rfd, void *buf); */
110 #define rset_read(rfd, buf) (*(rfd)->rset->control->f_read)((rfd), (buf))
111
112 /* int rset_write(RSFD rfd, const void *buf); */
113 #define rset_write(rfd, buf) (*(rfd)->rset->control->f_write)((rfd), (buf))
114
115 /* int rset_type (RSET) */
116 #define rset_type(rs) ((rs)->control->desc)
117
118
119 #ifdef __cplusplus
120 }
121 #endif
122
123 #endif