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