Back to index

courier  0.68.2
bdbobj.h
Go to the documentation of this file.
00001 #ifndef       bdbobj_h
00002 #define       bdbobj_h
00003 
00004 /*
00005 ** Copyright 1998 - 2007 Double Precision, Inc.  See COPYING for
00006 ** distribution information.
00007 */
00008 
00009 
00010 #if HAVE_CONFIG_H
00011 #include      "config.h"
00012 #endif
00013 
00014 #include      <sys/types.h>
00015 
00016 #if HAVE_LIMITS_H
00017 #include      <limits.h>
00018 #endif
00019 
00020 #include      <db.h>
00021 #include      <stdlib.h>
00022 
00023 #ifdef __cplusplus
00024 extern "C" {
00025 #endif
00026 
00027 struct bdbobj {
00028        DB     *dbf;
00029        int    has_dbf;
00030 
00031 #if    DB_VERSION_MAJOR >= 2
00032        DBC    *dbc;
00033        int    has_dbc;
00034 #endif
00035        } ;
00036 
00037 void bdbobj_init(struct bdbobj *);
00038 
00039 int bdbobj_open(struct bdbobj *, const char *, const char *);
00040 void bdbobj_close(struct bdbobj *);
00041 
00042 #define       bdbobj_isopen(p)     (!!(p)->has_dbf)
00043 
00044 char   *bdbobj_fetch(struct bdbobj *, const char *, size_t, size_t *, const char *);
00045 
00046 int    bdbobj_exists(struct bdbobj *, const char *, size_t);
00047 int    bdbobj_delete(struct bdbobj *, const char *, size_t);
00048 int    bdbobj_store(struct bdbobj *, const char *, size_t, const char *,
00049               size_t, const char *);
00050 
00051 char   *bdbobj_firstkey(struct bdbobj *, size_t *, char **, size_t *);
00052 char   *bdbobj_nextkey(struct bdbobj *, size_t *, char **, size_t *);
00053 
00054 #ifdef __cplusplus
00055 } ;
00056 
00057 #include      <string>
00058 
00059 
00060 class BDbObj {
00061        struct bdbobj obj;
00062 
00063        BDbObj(const BDbObj &);                   // Undefined
00064        BDbObj &operator=(const BDbObj &);        // Undefined
00065        char   *do_fetch(const char *, size_t, size_t &);
00066        char   *do_query(const char *, size_t, size_t &, const char *);
00067 
00068 public:
00069        BDbObj() { bdbobj_init(&obj); }
00070        ~BDbObj()     { bdbobj_close(&obj); }
00071        int    Open(std::string filename, const char *mode)
00072        {
00073               return (bdbobj_open(&obj, filename.c_str(), mode));
00074        }
00075 
00076        int    IsOpen() { return (bdbobj_isopen(&obj)); }
00077        void   Close() { bdbobj_close(&obj); }
00078 
00079        std::string Fetch(std::string key, std::string mode)
00080        {
00081               size_t l;
00082               char *p=Fetch(key.c_str(), key.size(), l, mode.c_str());
00083 
00084               if (!p) return "";
00085 
00086               std::string v(p, p+l);
00087 
00088               free(p);
00089               return v;
00090        }
00091 
00092        bool   Exists(std::string key)
00093        {
00094               return !!Exists(key.c_str(), key.size());
00095        }
00096 
00097        bool   Delete(std::string key)
00098        {
00099               return !!Delete(key.c_str(), key.size());
00100        }
00101 
00102        bool   Store(std::string key, std::string val, std::string mode)
00103        {
00104               return Store(key.c_str(), key.size(),
00105                           val.c_str(), val.size(), mode.c_str());
00106        }
00107 
00108        std::string FetchFirstKeyVal(std::string &valRet)
00109        {
00110               char *key;
00111               size_t keyLen;
00112               char *val;
00113               size_t valLen;
00114 
00115               key=FetchFirstKeyVal(keyLen, val, valLen);
00116 
00117               if (!key)
00118                      return "";
00119 
00120               std::string r(key, key+keyLen);
00121 
00122               valRet=std::string(val, val+valLen);
00123               free(val);
00124               return r;
00125        }
00126 
00127        std::string FetchNextKeyVal(std::string &valRet)
00128        {
00129               char *key;
00130               size_t keyLen;
00131               char *val;
00132               size_t valLen;
00133 
00134               key=FetchNextKeyVal(keyLen, val, valLen);
00135 
00136               if (!key)
00137                      return "";
00138 
00139               std::string r(key, key+keyLen);
00140 
00141               valRet=std::string(val, val+valLen);
00142               free(val);
00143               return r;
00144        }
00145 
00146 
00147 
00148 
00149 
00150        char   *Fetch(const char *key, size_t keylen, size_t &vallen,
00151               const char *mode)
00152               {
00153                      return (bdbobj_fetch(&obj, key, keylen, &vallen, mode));
00154               }
00155        int    Exists(const char *key, size_t keylen)
00156               {
00157                      return (bdbobj_exists(&obj, key, keylen));
00158               }
00159        int    Delete(const char *key, size_t keylen)
00160               {
00161                      return (bdbobj_delete(&obj, key, keylen));
00162               }
00163 
00164        int    Store(const char *key, size_t keylen, const char *val,
00165                      size_t vallen, const char *mode)
00166               {
00167                      return (bdbobj_store(&obj, key, keylen, val, vallen,
00168                             mode));
00169               }
00170 
00171        char   *FetchFirstKeyVal(size_t &keylen, char *&val, size_t &vallen)
00172               {
00173                      return (bdbobj_firstkey(&obj, &keylen, &val, &vallen));
00174               }
00175        char   *FetchNextKeyVal(size_t &keylen, char *&val, size_t &vallen)
00176               {
00177                      return (bdbobj_nextkey(&obj, &keylen, &val, &vallen));
00178               }
00179 } ;
00180 
00181 #endif
00182 
00183 #endif