File: cgdbm.cc

package info (click to toggle)
sms-pl 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 376 kB
  • ctags: 355
  • sloc: cpp: 2,618; perl: 109; makefile: 103; sh: 26
file content (224 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <exception>
#include <string.h>
#include "cgdbm.h"

using namespace std;

Cdatum::Cdatum()
{
    dat.dptr = NULL;
    dat.dsize = 0;
}

Cdatum::Cdatum(const Cdatum &cd)
{
    dat.dsize = cd.dat.dsize;
    dat.dptr = NULL;
    if (cd.dat.dptr != NULL) {
	    dat.dptr = (char*)malloc(dat.dsize);
	    memcpy(dat.dptr, cd.dat.dptr, dat.dsize);
    }
}

Cdatum &Cdatum::operator=(const Cdatum &cd)
{
    Clear();
    dat.dsize = cd.dat.dsize;
    if (cd.dat.dptr != NULL) {
	    dat.dptr = (char*)malloc(dat.dsize);
	    memcpy(dat.dptr, cd.dat.dptr, dat.dsize);
    }
    return *this;
}

Cdatum &Cdatum::operator=(const datum &d)
{
    Clear();
    dat.dsize = d.dsize;
    if (d.dptr != NULL) {
	    dat.dptr = (char*)malloc(dat.dsize);
	    memcpy(dat.dptr, d.dptr, dat.dsize);
    }
    return *this;
}

Cdatum::Cdatum(const datum &d)
{
    dat.dsize = d.dsize;
    dat.dptr = NULL;
    if (d.dptr != NULL) {
	    dat.dptr = (char*)malloc(dat.dsize);
	    memcpy(dat.dptr, d.dptr, dat.dsize);
    }
}

Cdatum::Cdatum(const void *ptr, int size)
{
    dat.dsize = size;
    dat.dptr = (char*)malloc(dat.dsize);
    memcpy(dat.dptr, ptr, size);
}

Cdatum::Cdatum(const string &str)
{
    dat.dsize = str.length() + 1;
    dat.dptr = (char*)malloc(dat.dsize);
    strcpy(dat.dptr, str.c_str());
}

Cdatum::Cdatum(long val)
{
    dat.dptr = (char*)malloc(dat.dsize = sizeof(val));
    memcpy(dat.dptr, &val, dat.dsize);
}

Cdatum::Cdatum(float val)
{
    dat.dptr = (char*)malloc(dat.dsize = sizeof(val));
    memcpy(dat.dptr, &val, dat.dsize);
}

void Cdatum::Clear()
{
	if (dat.dptr != NULL && dat.dsize) free(dat.dptr);
	dat.dptr = NULL;
	dat.dsize = 0;
}

Cdatum::~Cdatum()
{
	Clear();
}

Cdatum::operator datum&()
{
    return dat;
}

Cdatum::operator string()
{
    string str = (const char*)dat.dptr;
    return str;
}

Cdatum::operator const void*()
{
    return dat.dptr;
}

//////////////////////////////////////

inline const int Cgdbm::Status()
{
    return (db != NULL);
}

Cgdbm::Cgdbm(const string &_filename, int block_size, int read_write, int mode)
{
    db = NULL;
    Open(_filename, block_size, read_write, mode);
}

Cgdbm::~Cgdbm()
{
    Close();
}
 
bool Cgdbm::Open(const string &_filename, int block_size, int read_write, int mode)
{
    if (db != NULL) Close();
    filename = _filename;
    db = gdbm_open((char*)(filename.c_str()), block_size, read_write, mode, NULL);
    return db != NULL;
}

void Cgdbm::Close()
{
       if (db != NULL) {
	       gdbm_close(db);
	       db = NULL;
       }
}
   
bool Cgdbm::store(Cdatum &key, Cdatum &content, int flag)
{
	if (!Status()) throw Cgdbm_exception();
    return gdbm_store(db, key, content, flag);
}

bool Cgdbm::insert(Cdatum &key, Cdatum &content)
{
	if (!Status()) throw Cgdbm_exception();
    return store(key, content, GDBM_INSERT);
}
 
bool Cgdbm::replace(Cdatum &key, Cdatum &content)
{
	if (!Status()) throw Cgdbm_exception();
    return store(key, content, GDBM_REPLACE);
}

Cdatum Cgdbm::operator[](Cdatum &key)
{
	if (!Status()) throw Cgdbm_exception();
	Cdatum tmp_key = gdbm_fetch(db, key);
	if (tmp_key.isNull()) throw "gdbm_fetch()"; 
	return tmp_key;
}

Cdatum Cgdbm::begin()
{
	if (!Status()) throw Cgdbm_exception();
	tmp_key = gdbm_firstkey(db);
	if (tmp_key.isNull()) throw "gdbm_firstkey()"; 
	return tmp_key;
}

Cdatum Cgdbm::next()
{
	if (!Status()) throw Cgdbm_exception();
	tmp_key = gdbm_nextkey(db, tmp_key);
	if (tmp_key.isNull()) throw "gdbm_nextkey()"; 
	return tmp_key;
}

bool Cgdbm::Exists(Cdatum &key)
{
	if (!Status()) throw Cgdbm_exception();
    return gdbm_exists(db, key) != 0;
}

bool Cgdbm::Delete(Cdatum &key)
{
	if (!Status()) throw Cgdbm_exception();
    return gdbm_delete(db, key) != 0;
}

bool Cgdbm::Reorganize()
{
	if (!Status()) throw Cgdbm_exception();
    return gdbm_reorganize(db) != 0;
}

void Cgdbm::Sync()
{
	if (!Status()) throw Cgdbm_exception();
    gdbm_sync(db);
}
 
int Cgdbm::SetOpt(int option, int *value, int size)
{
	if (!Status()) throw Cgdbm_exception();
    return gdbm_setopt(db, option, value, size);
}

const char *Cgdbm::StrError(gdbm_error err)
{
    if ((err = (gdbm_error)-1)) err = gdbm_errno;
    return gdbm_strerror(err);
}

const char *Cgdbm::Version()
{
    return gdbm_version;
}