8 using namespace Evolution;
10 struct DBIterator::Data {
18 DBIterator::DBIterator( DBWrapper* wra) {
24 DBIterator::DBIterator(
const DBIterator& copy ) {
26 data->wrapper =
copy.data->wrapper;
27 data->key =
copy.data->key;
28 data->data =
copy.data->data;
29 data->atEnd =
copy.data->atEnd;
30 if (
copy.data->cursor )
31 copy.data->cursor->c_dup(
copy.data->cursor, &data->cursor, 0 );
35 DBIterator::~DBIterator() {
37 data->cursor->c_close(data->cursor);
40 DBIterator& DBIterator::operator=(
const DBIterator& rhs ) {
44 data->cursor->c_close(data->cursor);
47 data->wrapper = rhs.data->wrapper;
48 data->key = rhs.data->key;
49 data->data = rhs.data->data;
50 data->atEnd = rhs.data->atEnd;
51 if ( rhs.data->cursor )
52 rhs.data->cursor->c_dup(rhs.data->cursor, &data->cursor, 0 );
58 TQString DBIterator::key()
const{
61 TQString DBIterator::value()
const {
64 TQString DBIterator::operator*() {
67 DBIterator& DBIterator::operator++() {
69 ::memset(&key, 0,
sizeof(key) );
70 ::memset(&val, 0,
sizeof(val) );
72 if ( data->cursor->c_get(data->cursor, &key, &val,DB_NEXT ) != 0 )
74 data->key = TQString::fromUtf8( (
char*)
key.data,
key.size );
75 data->data = TQString::fromUtf8( (
char*)val.data, val.size );
78 DBIterator& DBIterator::operator--() {
80 ::memset(&key, 0,
sizeof(key) );
81 ::memset(&val, 0,
sizeof(val) );
83 if ( data->cursor->c_get(data->cursor, &key, &val,DB_PREV ) != 0 )
85 data->key = TQString::fromUtf8( (
char*)
key.data,
key.size );
86 data->data = TQString::fromUtf8( (
char*)val.data, val.size );
89 bool DBIterator::operator==(
const DBIterator& rhs ) {
90 if ( data->atEnd && data->atEnd == rhs.data->atEnd )
return true;
94 bool DBIterator::operator!=(
const DBIterator& rhs ) {
95 return !this->operator==(rhs );
97 struct DBWrapper::Data {
101 DBWrapper::DBWrapper() {
103 (void)db_create(&data->db, NULL, 0 );
106 DBWrapper::~DBWrapper() {
107 data->db->close(data->db, 0 );
110 bool DBWrapper::open(
const TQString& file,
bool on) {
112 return !data->db->open(data->db, TQFile::encodeName( file ), NULL, DB_HASH, 0, 0666 );
114 bool DBWrapper::save() {
117 DBIterator DBWrapper::begin() {
122 ret = data->db->cursor(data->db, NULL, &cursor, 0 );
124 it.data->atEnd =
true;
128 ::memset(&key, 0,
sizeof(key) );
129 ::memset(&val, 0,
sizeof(val) );
130 ret = cursor->c_get(cursor, &key, &val, DB_FIRST );
132 it.data->atEnd =
true;
136 it.data->cursor = cursor;
137 it.data->key = TQString::fromUtf8((
char*)
key.data,
key.size );
138 it.data->data = TQString::fromUtf8((
char*)val.data, val.size );
142 DBIterator DBWrapper::end() {
144 it.data->atEnd =
true;
148 bool DBWrapper::find(
const TQString& _key, TQString& _val ) {
150 ::memset(&key, 0,
sizeof(key) );
151 ::memset(&val, 0,
sizeof(val) );
153 TQCString db_key = _key.local8Bit();
154 key.data = db_key.data();
155 key.size = db_key.size();
157 int ret = data->db->get(data->db, NULL, &key, &val, 0 );
159 _val = TQString::fromUtf8( (
char*)val.data, val.size );
160 tqWarning(
"key: %s val: %sXXX", (
char*)
key.data, (
char*)val.data );
165 bool DBWrapper::add(
const TQString& _key,
const TQString& _val ) {
166 TQCString db_key = _key.local8Bit();
167 TQCString db_val = _val.local8Bit();
169 ::memset(&key, 0,
sizeof(key) );
170 ::memset(&val, 0,
sizeof(val) );
172 key.data = db_key.data();
173 key.size = db_key.size();
174 val.data = db_val.data();
175 val.size = db_val.size();
177 return !data->db->put(data->db, NULL, &key, &val, 0 );
179 bool DBWrapper::remove(
const TQString& _key ) {
180 TQCString db_key = _key.local8Bit();
182 memset(&key, 0,
sizeof(key) );
183 key.data = db_key.data();
184 key.size = db_key.size();
186 return !data->db->del(data->db, NULL, &key, 0 );
const TDEShortcut & copy()