2 * UCW Library -- SDBM emulator at top of GDBM
4 * (c) 1999 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
25 sdbm_open(struct sdbm_options *o)
27 struct sdbm *d = xmalloc(sizeof(struct sdbm));
28 d->db = gdbm_open(o->name,
29 (o->page_order ? (1 << o->page_order) : 0),
30 ((o->flags & SDBM_WRITE) ? ((o->flags & SDBM_CREAT) ? GDBM_WRCREAT : GDBM_WRITER) : GDBM_READER)
31 | ((o->flags & SDBM_SYNC) ? GDBM_SYNC : 0),
35 gdbm_setopt(d->db, GDBM_CACHESIZE, &o->cache_size, sizeof(o->cache_size));
36 d->prevkey.dptr = NULL;
41 sdbm_close(struct sdbm *d)
49 sdbm_put_user(byte *D, uns Dl, byte *val, uns *vallen)
63 sdbm_store(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
72 rc = gdbm_store(d->db, K, V, GDBM_INSERT);
73 return (rc < 0) ? rc : !rc;
77 sdbm_replace(struct sdbm *d, byte *key, uns keylen, byte *val, uns vallen)
83 return sdbm_delete(d, key, keylen);
88 rc = gdbm_store(d->db, K, V, GDBM_REPLACE);
89 return (rc < 0) ? rc : !rc;
93 sdbm_delete(struct sdbm *d, byte *key, uns keylen)
99 return !gdbm_delete(d->db, K);
103 sdbm_fetch(struct sdbm *d, byte *key, uns keylen, byte *val, uns *vallen)
111 return gdbm_exists(d->db, K);
112 V = gdbm_fetch(d->db, K);
115 rc = sdbm_put_user(V.dptr, V.dsize, val, vallen);
117 return rc ? SDBM_ERROR_TOO_LARGE : 1;
121 sdbm_rewind(struct sdbm *d)
125 xfree(d->prevkey.dptr);
126 d->prevkey.dptr = NULL;
131 sdbm_get_next(struct sdbm *d, byte *key, uns *keylen, byte *val, uns *vallen)
137 K = gdbm_nextkey(d->db, d->prevkey);
138 xfree(d->prevkey.dptr);
141 K = gdbm_firstkey(d->db);
145 if (sdbm_put_user(K.dptr, K.dsize, key, keylen))
146 return SDBM_ERROR_TOO_LARGE;
148 return sdbm_fetch(d, key, *keylen, val, vallen);
153 sdbm_sync(struct sdbm *d)