Back to index

courier  0.68.2
bdbobj2.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 #include      "config.h"
00007 #include      <string.h>
00008 #include      <stdlib.h>
00009 #include      "bdbobj.h"
00010 
00011 char   *bdbobj_firstkey(struct bdbobj *obj, size_t *keylen, char **val,
00012        size_t *vallen)
00013 {
00014 DBT    key, value;
00015 
00016        if (!obj->has_dbf)   return (0);
00017 
00018        memset(&key, 0, sizeof(key));
00019        memset(&value, 0, sizeof(value));
00020 
00021 #if    DB_VERSION_MAJOR < 2
00022        if ((*obj->dbf->seq)(obj->dbf, &key, &value, R_FIRST))  return (0);
00023 #else
00024        if (obj->has_dbc)
00025        {
00026               (*obj->dbc->c_close)(obj->dbc);
00027               obj->has_dbc=0;
00028        }
00029 
00030 #if DB_VERSION_MAJOR > 2
00031        if ((*obj->dbf->cursor)(obj->dbf, 0, &obj->dbc, 0))     return (0);
00032 #else
00033 #if DB_VERSION_MINOR >= 5
00034        if ((*obj->dbf->cursor)(obj->dbf, 0, &obj->dbc, 0))     return (0);
00035 #else
00036        if ((*obj->dbf->cursor)(obj->dbf, 0, &obj->dbc)) return (0);
00037 #endif
00038 #endif
00039        obj->has_dbc=1;
00040 
00041        if ((*obj->dbc->c_get)(obj->dbc, &key, &value, DB_FIRST)) return (0);
00042 #endif
00043        *keylen=key.size;
00044        *vallen=value.size;
00045        if ((*val=(char *)malloc(*vallen)) == 0)  return (0);
00046 
00047        memcpy(*val, value.data, *vallen);
00048        return ((char *)key.data);
00049 }
00050 
00051 char   *bdbobj_nextkey(struct bdbobj *obj, size_t *keylen, char **val,
00052        size_t *vallen)
00053 {
00054 DBT    key, value;
00055 
00056        if (!obj->has_dbf)   return (0);
00057 
00058        memset(&key, 0, sizeof(key));
00059        memset(&value, 0, sizeof(value));
00060 
00061 #if    DB_VERSION_MAJOR < 2
00062        if ((*obj->dbf->seq)(obj->dbf, &key, &value, R_NEXT))   return (0);
00063 #else
00064        if (!obj->has_dbc)   return (0);
00065 
00066        if ((*obj->dbc->c_get)(obj->dbc, &key, &value, DB_NEXT))
00067        {
00068               (*obj->dbc->c_close)(obj->dbc);
00069               obj->has_dbc=0;
00070        }
00071 #endif
00072 
00073        *keylen=key.size;
00074        *vallen=value.size;
00075        if ((*val=(char *)malloc(*vallen + 1)) == 0)     return (0);
00076 
00077        memcpy(*val, value.data, *vallen);
00078        (*val)[*vallen]=0;
00079 
00080        return ((char *)key.data);
00081 }