tinycdb-python (2.)
1. Convert between strings and binary data
class cdb_make (Structure):
_fields_ = [('cdb_fd', c_int),
('cdb_dpos', c_uint),
('cdb_cdb_rcnt', c_uint),
('cdb_cdb_buf', c_ubyte * 4096),
('cdb_bpos', POINTER(c_ubyte)),
('cdb_rec', POINTER(cdb_rl) * 256)]
cdblib.cdb_make_add.argtypes = [POINTER(cdb_make), c_void_p, c_uint, c_void_p, c_uint]
def add(self, key, val):
#cdb_make_add(struct cdb_make *cdbmp,const void *key, unsigned klen,const void *val, unsigned vlen);
cdblib.cdb_make_add(self.cdbm, key, len(key), val, len(val))
>>> import ctypes
>>> p1= ctypes.c_char_p("hi mom")
>>> ctypes.cast( p1, ctypes.c_void_p )
c_void_p(11133300)
def find(self, key):
#cdb_find(struct cdb *cdbp, const void *key, unsigned klen)
ret = cdblib.cdb_find(self.cdb, key, len(key))
if ret > 0:
vpos = cdb_datapos(self.cdb)
vlen = cdb_datalen(self.cdb)
buf = (c_char * vlen)()
val = cast(buf, c_void_p)
#int cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos);
cdblib.cdb_read(self.cdb, val, vlen, vpos)
return buf.value
else:
return None