Cleaned up the creation of rsets, added nmem
[idzebra-moved-to-github.git] / include / rset.h
1 /* $Id: rset.h,v 1.31 2004-08-24 14:25:15 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 void *RSFD;       /* Rset "file descriptor" */
35 typedef struct rset *RSET;
36
37 struct rset_control
38 {
39     char *desc; /* text description of set type (for debugging) */
40 /* void *(*f_create)(RSET ct, const struct rset_control *sel, void *parms); */
41     RSFD (*f_open)(RSET ct, int wflag);
42     void (*f_close)(RSFD rfd);
43     void (*f_delete)(RSET ct);
44     void (*f_rewind)(RSFD rfd);
45     int (*f_forward)(RSET ct, RSFD rfd, void *buf,
46                      int (*cmpfunc)(const void *p1, const void *p2), 
47                      const void *untilbuf);
48     void (*f_pos)(RSFD rfd, double *current, double *total);
49        /* returns -1,-1 if pos function not implemented for this type */
50     int (*f_read)(RSFD rfd, void *buf);
51     int (*f_write)(RSFD rfd, const void *buf);
52 };
53
54 int rset_default_forward(RSET ct, RSFD rfd, void *buf, 
55                      int (*cmpfunc)(const void *p1, const void *p2), 
56                      const void *untilbuf);
57 void rset_default_pos(RSFD rfd, double *current, double *total);
58
59
60 typedef struct rset
61 {
62     const struct rset_control *control;
63     int  count;  /* reference count */
64     void *priv;  /* stuff private to the given type of rset */
65     NMEM nmem;    /* nibble memory for various allocs */
66     char my_nmem; /* Should the nmem be destroyed with the rset?  */
67                   /* 1 if created with it, 0 if passed from above */
68 } rset;
69 /* rset is a "virtual base class", which will never exist on its own */
70 /* all instances are rsets of some specific type, like rsisamb, or rsbool */
71 /* They keep their own stuff behind the priv pointer. */
72
73 #define RSETF_READ       0
74 #define RSETF_WRITE      1
75
76 RSET rset_create_base(const struct rset_control *sel, NMEM nmem);
77
78 RSET rset_create_OLD(const struct rset_control *sel, void *parms); 
79 /* parameters? */
80
81 void rset_delete(RSET rs);
82
83 RSET rset_dup (RSET rs);
84
85
86 /* RSFD rset_open(RSET rs, int wflag); */
87 #define rset_open(rs, wflag) (*(rs)->control->f_open)((rs), (wflag))
88
89 /* void rset_close(RSET rs); */
90 #define rset_close(rs, rfd) (*(rs)->control->f_close)(rfd)
91
92 /* void rset_rewind(RSET rs); */
93 #define rset_rewind(rs, rfd) (*(rs)->control->f_rewind)((rfd))
94
95 /* int rset_forward(RSET rs, void *buf, void *untilbuf); */
96 #define rset_forward(rs, fd, buf, cmpfunc, untilbuf) \
97     (*(rs)->control->f_forward)((rs), (fd), (buf), (cmpfunc), (untilbuf))
98
99 /* int rset_pos(RSET rs, RSFD fd, double *current, double *total); */
100 #define rset_pos(rs,fd,cur,tot) \
101     (*(rs)->control->f_pos)( (fd),(cur),(tot))
102
103 /* int rset_read(RSET rs, void *buf); */
104 #define rset_read(rs, fd, buf) (*(rs)->control->f_read)((fd), (buf))
105
106 /* int rset_write(RSET rs, const void *buf); */
107 #define rset_write(rs, fd, buf) (*(rs)->control->f_write)((fd), (buf))
108
109 /* int rset_type (RSET) */
110 #define rset_type(rs) ((rs)->control->desc)
111
112
113 #ifdef __cplusplus
114 }
115 #endif
116
117 #endif