Back to index

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