| # kabc Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# dbwrapper.cpp

    00001 #include <db.h>
    00002
    00003 #include <qfile.h>
    00004
    00005 #include "dbwrapper.h"
    00006
    00007
    00008 using namespace Evolution;
    00009
    00010 struct DBIterator::Data {
    00011     DBWrapper *wrapper;
    00012     QString key;
    00013     QString data;
    00014     DBC* cursor;
    00015     bool atEnd;
    00016 };
    00017
    00018 DBIterator::DBIterator( DBWrapper* wra) {
    00019     data = new Data;
    00020     data->wrapper = wra;
    00021     data->atEnd = false;
    00022     data->cursor = 0l;
    00023 }
    00024 DBIterator::DBIterator( const DBIterator& copy ) {
    00025     data = new Data;
    00026     data->wrapper = copy.data->wrapper;
    00027     data->key = copy.data->key;
    00028     data->data = copy.data->data;
    00029     data->atEnd = copy.data->atEnd;
    00030     if (copy.data->cursor )
    00031         copy.data->cursor->c_dup(copy.data->cursor, &data->cursor, 0 );
    00032     else
    00033         data->cursor = 0l;
    00034 }
    00035 DBIterator::~DBIterator() {
    00036     if (data->cursor)
    00037         data->cursor->c_close(data->cursor);
    00038     delete data;
    00039 }
    00040 DBIterator& DBIterator::operator=( const DBIterator& rhs ) {
    00041     if ( *this == rhs )
    00042         return *this;
    00043     if (data->cursor)
    00044         data->cursor->c_close(data->cursor);
    00045     delete data;
    00046     data = new Data;
    00047     data->wrapper = rhs.data->wrapper;
    00048     data->key = rhs.data->key;
    00049     data->data = rhs.data->data;
    00050     data->atEnd = rhs.data->atEnd;
    00051     if ( rhs.data->cursor )
    00052         rhs.data->cursor->c_dup(rhs.data->cursor, &data->cursor, 0 );
    00053     else
    00054         data->cursor = 0l;
    00055
    00056     return *this;
    00057 }
    00058 QString DBIterator::key()const{
    00059     return data->key;
    00060 }
    00061 QString DBIterator::value()const {
    00062     return data->data;
    00063 }
    00064 QString DBIterator::operator*() {
    00065     return data->data;
    00066 }
    00067 DBIterator& DBIterator::operator++() {
    00068     DBT key, val;
    00069     ::memset(&key, 0, sizeof(key) );
    00070     ::memset(&val, 0, sizeof(val) );
    00071     if ( data->cursor )
    00072         if ( data->cursor->c_get(data->cursor, &key, &val,DB_NEXT ) != 0 )
    00073             data->atEnd = true;
    00074     data->key = QString::fromUtf8( (char*)key.data, key.size );
    00075     data->data = QString::fromUtf8( (char*)val.data, val.size );
    00076     return *this;
    00077 }
    00078 DBIterator& DBIterator::operator--() {
    00079     DBT key, val;
    00080     ::memset(&key, 0, sizeof(key) );
    00081     ::memset(&val, 0, sizeof(val) );
    00082     if ( data->cursor )
    00083         if ( data->cursor->c_get(data->cursor, &key, &val,DB_PREV ) != 0 )
    00084             data->atEnd = true;
    00085     data->key = QString::fromUtf8( (char*)key.data, key.size );
    00086     data->data = QString::fromUtf8( (char*)val.data, val.size );
    00087     return *this;
    00088 }
    00089 bool DBIterator::operator==( const DBIterator& rhs ) {
    00090     if ( data->atEnd && data->atEnd == rhs.data->atEnd ) return true;
    00091
    00092     return false;
    00093 }
    00094 bool DBIterator::operator!=( const DBIterator& rhs ) {
    00095     return !this->operator==(rhs );
    00096 }
    00097 struct DBWrapper::Data {
    00098     DB* db;
    00099     bool only;
    00100 };
    00101 DBWrapper::DBWrapper() {
    00102     data = new Data;
    00103     (void)db_create(&data->db, NULL, 0 );
    00104     data->only = false;
    00105 }
    00106 DBWrapper::~DBWrapper() {
    00107     data->db->close(data->db, 0 );
    00108     delete data;
    00109 }
    00110 bool DBWrapper::open( const QString& file, bool on) {
    00111     data->only = on;
    00112     return  !data->db->open(data->db, QFile::encodeName( file ), NULL, DB_HASH, 0, 0666 );
    00113 }
    00114 bool DBWrapper::save() {
    00115     return true;
    00116 }
    00117 DBIterator DBWrapper::begin() {
    00118     DBIterator it(this);
    00119     DBC* cursor;
    00120     DBT key, val;
    00121     int ret;
    00122     ret = data->db->cursor(data->db, NULL, &cursor, 0 );
    00123     if (ret ) {
    00124         it.data->atEnd = true;
    00125         return it;
    00126     }
    00127
    00128     ::memset(&key, 0, sizeof(key) );
    00129     ::memset(&val, 0, sizeof(val) );
    00130     ret = cursor->c_get(cursor, &key, &val, DB_FIRST );
    00131     if (ret ) {
    00132         it.data->atEnd = true;
    00133         return it;
    00134     }
    00135
    00136     it.data->cursor = cursor;
    00137     it.data->key = QString::fromUtf8((char*)key.data, key.size );
    00138     it.data->data = QString::fromUtf8((char*)val.data, val.size );
    00139
    00140     return it;
    00141 }
    00142 DBIterator DBWrapper::end() {
    00143     DBIterator it(this);
    00144     it.data->atEnd = true;
    00145
    00146     return it;
    00147 }
    00148 bool DBWrapper::find( const QString& _key,  QString& _val ) {
    00149     DBT key, val;
    00150     ::memset(&key, 0, sizeof(key) );
    00151     ::memset(&val, 0, sizeof(val) );
    00152
    00153     QCString db_key = _key.local8Bit();
    00154     key.data = db_key.data();
    00155     key.size = db_key.size();
    00156
    00157     int ret  = data->db->get(data->db, NULL, &key,  &val, 0 );
    00158     if (!ret) {
    00159         _val = QString::fromUtf8( (char*)val.data, val.size );
    00160         qWarning("key: %s val: %sXXX", (char*)key.data, (char*)val.data );
    00161         return true;
    00162     }
    00163     return false;
    00164 }
    00165 bool DBWrapper::add( const QString& _key, const QString& _val ) {
    00166     QCString db_key = _key.local8Bit();
    00167     QCString db_val = _val.local8Bit();
    00168     DBT key, val;
    00169     ::memset(&key, 0, sizeof(key) );
    00170     ::memset(&val, 0, sizeof(val) );
    00171
    00172     key.data = db_key.data();
    00173     key.size = db_key.size();
    00174     val.data = db_val.data();
    00175     val.size = db_val.size();
    00176
    00177     return !data->db->put(data->db, NULL, &key, &val, 0 );
    00178 }
    00179 bool DBWrapper::remove( const QString& _key ) {
    00180     QCString db_key = _key.local8Bit();
    00181     DBT key;
    00182     memset(&key, 0, sizeof(key) );
    00183     key.data = db_key.data();
    00184     key.size = db_key.size();
    00185
    00186     return !data->db->del(data->db, NULL, &key, 0 );
    00187 }

This file is part of the documentation for kabc Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:29 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003