1 /* $Id: cdb.h,v 1.7 2005/04/18 09:46:50 mjt Exp $
2 * public cdb include file
4 * This file is a part of tinycdb package by Michael Tokarev, mjt@corpit.ru.
8 #ifndef TINYCDB_VERSION
9 #define TINYCDB_VERSION 0.75
15 typedef unsigned int cdbi_t
; /* compatibility */
18 unsigned cdb_hash(const void *buf
, unsigned len
);
19 unsigned cdb_unpack(const unsigned char buf
[4]);
20 void cdb_pack(unsigned num
, unsigned char buf
[4]);
23 int cdb_fd
; /* file descriptor */
25 unsigned cdb_fsize
; /* datafile size */
26 unsigned cdb_dend
; /* end of data ptr */
27 const unsigned char *cdb_mem
; /* mmap'ed file memory */
28 unsigned cdb_vpos
, cdb_vlen
; /* found data */
29 unsigned cdb_kpos
, cdb_klen
; /* found key */
32 #define CDB_STATIC_INIT {0,0,0,0,0,0,0,0}
34 #define cdb_datapos(c) ((c)->cdb_vpos)
35 #define cdb_datalen(c) ((c)->cdb_vlen)
36 #define cdb_keypos(c) ((c)->cdb_kpos)
37 #define cdb_keylen(c) ((c)->cdb_klen)
38 #define cdb_fileno(c) ((c)->cdb_fd)
40 int cdb_init(struct cdb
*cdbp
, int fd
);
41 void cdb_free(struct cdb
*cdbp
);
43 int cdb_read(const struct cdb
*cdbp
,
44 void *buf
, unsigned len
, unsigned pos
);
45 #define cdb_readdata(cdbp, buf) \
46 cdb_read((cdbp), (buf), cdb_datalen(cdbp), cdb_datapos(cdbp))
47 #define cdb_readkey(cdbp, buf) \
48 cdb_read((cdbp), (buf), cdb_keylen(cdbp), cdb_keypos(cdbp))
50 const void *cdb_get(const struct cdb
*cdbp
, unsigned len
, unsigned pos
);
51 #define cdb_getdata(cdbp) \
52 cdb_get((cdbp), cdb_datalen(cdbp), cdb_datapos(cdbp))
53 #define cdb_getkey(cdbp) \
54 cdb_get((cdbp), cdb_keylen(cdbp), cdb_keypos(cdbp))
56 int cdb_find(struct cdb
*cdbp
, const void *key
, unsigned klen
);
61 const unsigned char *cdb_htp
, *cdb_htab
, *cdb_htend
;
67 int cdb_findinit(struct cdb_find
*cdbfp
, struct cdb
*cdbp
,
68 const void *key
, unsigned klen
);
69 int cdb_findnext(struct cdb_find
*cdbfp
);
71 #define cdb_seqinit(cptr, cdbp) ((*(cptr))=2048)
72 int cdb_seqnext(unsigned *cptr
, struct cdb
*cdbp
);
74 /* old simple interface */
75 /* open file using standard routine, then: */
76 int cdb_seek(int fd
, const void *key
, unsigned klen
, unsigned *dlenp
);
77 int cdb_bread(int fd
, void *buf
, int len
);
82 int cdb_fd
; /* file descriptor */
84 unsigned cdb_dpos
; /* data position so far */
85 unsigned cdb_rcnt
; /* record count so far */
86 char cdb_buf
[4096]; /* write buffer */
87 char *cdb_bpos
; /* current buf position */
88 struct cdb_rl
*cdb_rec
[256]; /* list of arrays of record infos */
92 CDB_PUT_ADD
= 0, /* add unconditionnaly, like cdb_make_add() */
93 #define CDB_PUT_ADD CDB_PUT_ADD
94 CDB_FIND
= CDB_PUT_ADD
,
95 CDB_PUT_REPLACE
, /* replace: do not place to index OLD record */
96 #define CDB_PUT_REPLACE CDB_PUT_REPLACE
97 CDB_FIND_REMOVE
= CDB_PUT_REPLACE
,
98 CDB_PUT_INSERT
, /* add only if not already exists */
99 #define CDB_PUT_INSERT CDB_PUT_INSERT
100 CDB_PUT_WARN
, /* add unconditionally but ret. 1 if exists */
101 #define CDB_PUT_WARN CDB_PUT_WARN
102 CDB_PUT_REPLACE0
, /* if a record exists, fill old one with zeros */
103 #define CDB_PUT_REPLACE0 CDB_PUT_REPLACE0
104 CDB_FIND_FILL0
= CDB_PUT_REPLACE0
107 int cdb_make_start(struct cdb_make
*cdbmp
, int fd
);
108 int cdb_make_add(struct cdb_make
*cdbmp
,
109 const void *key
, unsigned klen
,
110 const void *val
, unsigned vlen
);
111 int cdb_make_exists(struct cdb_make
*cdbmp
,
112 const void *key
, unsigned klen
);
113 int cdb_make_find(struct cdb_make
*cdbmp
,
114 const void *key
, unsigned klen
,
115 enum cdb_put_mode mode
);
116 int cdb_make_put(struct cdb_make
*cdbmp
,
117 const void *key
, unsigned klen
,
118 const void *val
, unsigned vlen
,
119 enum cdb_put_mode mode
);
120 int cdb_make_finish(struct cdb_make
*cdbmp
);
123 // } /* extern "C" */
126 #endif /* include guard */