Imported upstream version 0.59.3
[hcoop/debian/courier-authlib.git] / bdbobj / bdbobj.h
1 #ifndef bdbobj_h
2 #define bdbobj_h
3
4 /*
5 ** Copyright 1998 - 2007 Double Precision, Inc. See COPYING for
6 ** distribution information.
7 */
8
9 static const char bdbobj_h_rcsid[]="$Id: bdbobj.h,v 1.6 2007/03/01 04:32:03 mrsam Exp $";
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <sys/types.h>
16
17 #if HAVE_LIMITS_H
18 #include <limits.h>
19 #endif
20
21 #include <db.h>
22 #include <stdlib.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 struct bdbobj {
29 DB *dbf;
30 int has_dbf;
31
32 #if DB_VERSION_MAJOR >= 2
33 DBC *dbc;
34 int has_dbc;
35 #endif
36 } ;
37
38 void bdbobj_init(struct bdbobj *);
39
40 int bdbobj_open(struct bdbobj *, const char *, const char *);
41 void bdbobj_close(struct bdbobj *);
42
43 #define bdbobj_isopen(p) (!!(p)->has_dbf)
44
45 char *bdbobj_fetch(struct bdbobj *, const char *, size_t, size_t *, const char *);
46
47 int bdbobj_exists(struct bdbobj *, const char *, size_t);
48 int bdbobj_delete(struct bdbobj *, const char *, size_t);
49 int bdbobj_store(struct bdbobj *, const char *, size_t, const char *,
50 size_t, const char *);
51
52 char *bdbobj_firstkey(struct bdbobj *, size_t *, char **, size_t *);
53 char *bdbobj_nextkey(struct bdbobj *, size_t *, char **, size_t *);
54
55 #ifdef __cplusplus
56 } ;
57
58 #include <string>
59
60
61 class BDbObj {
62 struct bdbobj obj;
63
64 BDbObj(const BDbObj &); // Undefined
65 BDbObj &operator=(const BDbObj &); // Undefined
66 char *do_fetch(const char *, size_t, size_t &);
67 char *do_query(const char *, size_t, size_t &, const char *);
68
69 public:
70 BDbObj() { bdbobj_init(&obj); }
71 ~BDbObj() { bdbobj_close(&obj); }
72 int Open(std::string filename, const char *mode)
73 {
74 return (bdbobj_open(&obj, filename.c_str(), mode));
75 }
76
77 int IsOpen() { return (bdbobj_isopen(&obj)); }
78 void Close() { bdbobj_close(&obj); }
79
80 std::string Fetch(std::string key, std::string mode)
81 {
82 size_t l;
83 char *p=Fetch(key.c_str(), key.size(), l, mode.c_str());
84
85 if (!p) return "";
86
87 std::string v(p, p+l);
88
89 free(p);
90 return v;
91 }
92
93 bool Exists(std::string key)
94 {
95 return !!Exists(key.c_str(), key.size());
96 }
97
98 bool Delete(std::string key)
99 {
100 return !!Delete(key.c_str(), key.size());
101 }
102
103 bool Store(std::string key, std::string val, std::string mode)
104 {
105 return Store(key.c_str(), key.size(),
106 val.c_str(), val.size(), mode.c_str());
107 }
108
109 std::string FetchFirstKeyVal(std::string &valRet)
110 {
111 char *key;
112 size_t keyLen;
113 char *val;
114 size_t valLen;
115
116 key=FetchFirstKeyVal(keyLen, val, valLen);
117
118 if (!key)
119 return "";
120
121 std::string r(key, key+keyLen);
122
123 valRet=std::string(val, val+valLen);
124 free(val);
125 return r;
126 }
127
128 std::string FetchNextKeyVal(std::string &valRet)
129 {
130 char *key;
131 size_t keyLen;
132 char *val;
133 size_t valLen;
134
135 key=FetchNextKeyVal(keyLen, val, valLen);
136
137 if (!key)
138 return "";
139
140 std::string r(key, key+keyLen);
141
142 valRet=std::string(val, val+valLen);
143 free(val);
144 return r;
145 }
146
147
148
149
150
151 char *Fetch(const char *key, size_t keylen, size_t &vallen,
152 const char *mode)
153 {
154 return (bdbobj_fetch(&obj, key, keylen, &vallen, mode));
155 }
156 int Exists(const char *key, size_t keylen)
157 {
158 return (bdbobj_exists(&obj, key, keylen));
159 }
160 int Delete(const char *key, size_t keylen)
161 {
162 return (bdbobj_delete(&obj, key, keylen));
163 }
164
165 int Store(const char *key, size_t keylen, const char *val,
166 size_t vallen, const char *mode)
167 {
168 return (bdbobj_store(&obj, key, keylen, val, vallen,
169 mode));
170 }
171
172 char *FetchFirstKeyVal(size_t &keylen, char *&val, size_t &vallen)
173 {
174 return (bdbobj_firstkey(&obj, &keylen, &val, &vallen));
175 }
176 char *FetchNextKeyVal(size_t &keylen, char *&val, size_t &vallen)
177 {
178 return (bdbobj_nextkey(&obj, &keylen, &val, &vallen));
179 }
180 } ;
181
182 #endif
183
184 #endif