File: bdbobj.h

package info (click to toggle)
maildrop 2.9.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,104 kB
  • sloc: ansic: 26,822; cpp: 9,085; sh: 4,868; makefile: 753; perl: 94
file content (183 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download | duplicates (22)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef	bdbobj_h
#define	bdbobj_h

/*
** Copyright 1998 - 2007 Double Precision, Inc.  See COPYING for
** distribution information.
*/


#if HAVE_CONFIG_H
#include	"config.h"
#endif

#include	<sys/types.h>

#if HAVE_LIMITS_H
#include	<limits.h>
#endif

#include	<db.h>
#include	<stdlib.h>

#ifdef	__cplusplus
extern "C" {
#endif

struct bdbobj {
	DB	*dbf;
	int	has_dbf;

#if	DB_VERSION_MAJOR >= 2
	DBC	*dbc;
	int	has_dbc;
#endif
	} ;

void bdbobj_init(struct bdbobj *);

int bdbobj_open(struct bdbobj *, const char *, const char *);
void bdbobj_close(struct bdbobj *);

#define	bdbobj_isopen(p)	(!!(p)->has_dbf)

char	*bdbobj_fetch(struct bdbobj *, const char *, size_t, size_t *, const char *);

int	bdbobj_exists(struct bdbobj *, const char *, size_t);
int	bdbobj_delete(struct bdbobj *, const char *, size_t);
int	bdbobj_store(struct bdbobj *, const char *, size_t, const char *,
		size_t, const char *);

char	*bdbobj_firstkey(struct bdbobj *, size_t *, char **, size_t *);
char	*bdbobj_nextkey(struct bdbobj *, size_t *, char **, size_t *);

#ifdef	__cplusplus
} ;

#include	<string>


class BDbObj {
	struct bdbobj	obj;

	BDbObj(const BDbObj &);			// Undefined
	BDbObj	&operator=(const BDbObj &);		// Undefined
	char	*do_fetch(const char *, size_t, size_t &);
	char	*do_query(const char *, size_t, size_t &, const char *);

public:
	BDbObj() { bdbobj_init(&obj); }
	~BDbObj()	{ bdbobj_close(&obj); }
	int	Open(std::string filename, const char *mode)
	{
		return (bdbobj_open(&obj, filename.c_str(), mode));
	}

	int	IsOpen() { return (bdbobj_isopen(&obj)); }
	void	Close() { bdbobj_close(&obj); }

	std::string Fetch(std::string key, std::string mode)
	{
		size_t l;
		char *p=Fetch(key.c_str(), key.size(), l, mode.c_str());

		if (!p) return "";

		std::string v(p, p+l);

		free(p);
		return v;
	}

	bool	Exists(std::string key)
	{
		return !!Exists(key.c_str(), key.size());
	}

	bool	Delete(std::string key)
	{
		return !!Delete(key.c_str(), key.size());
	}

	bool	Store(std::string key, std::string val, std::string mode)
	{
		return Store(key.c_str(), key.size(),
			     val.c_str(), val.size(), mode.c_str());
	}

	std::string FetchFirstKeyVal(std::string &valRet)
	{
		char *key;
		size_t keyLen;
		char *val;
		size_t valLen;

		key=FetchFirstKeyVal(keyLen, val, valLen);

		if (!key)
			return "";

		std::string r(key, key+keyLen);

		valRet=std::string(val, val+valLen);
		free(val);
		return r;
	}

	std::string FetchNextKeyVal(std::string &valRet)
	{
		char *key;
		size_t keyLen;
		char *val;
		size_t valLen;

		key=FetchNextKeyVal(keyLen, val, valLen);

		if (!key)
			return "";

		std::string r(key, key+keyLen);

		valRet=std::string(val, val+valLen);
		free(val);
		return r;
	}





	char	*Fetch(const char *key, size_t keylen, size_t &vallen,
		const char *mode)
		{
			return (bdbobj_fetch(&obj, key, keylen, &vallen, mode));
		}
	int	Exists(const char *key, size_t keylen)
		{
			return (bdbobj_exists(&obj, key, keylen));
		}
	int	Delete(const char *key, size_t keylen)
		{
			return (bdbobj_delete(&obj, key, keylen));
		}

	int	Store(const char *key, size_t keylen, const char *val,
			size_t vallen, const char *mode)
		{
			return (bdbobj_store(&obj, key, keylen, val, vallen,
				mode));
		}

	char	*FetchFirstKeyVal(size_t &keylen, char *&val, size_t &vallen)
		{
			return (bdbobj_firstkey(&obj, &keylen, &val, &vallen));
		}
	char	*FetchNextKeyVal(size_t &keylen, char *&val, size_t &vallen)
		{
			return (bdbobj_nextkey(&obj, &keylen, &val, &vallen));
		}
} ;

#endif

#endif