#include <tcutil.h>
#include <tchdb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
int main(int argc, char * argv[])
{
TCHDB *hdb;
int ecode;
char *key,*value;
char dbpath[255];
hdb = tchdbnew();
if(!tchdbopen(hdb,"/data/database1.tch",HDBOWRITER | HDBOCREAT))
{
ecode = tchdbecode(hdb);
fprintf(stderr,"open error:%s\n",tchdberrmsg(ecode));exit;
}
/* store records */
if(!tchdbput2(hdb,"hello","world") || !tchdbput2(hdb,"foo","bar"))
{
ecode = tchdbecode(hdb);
fprintf(stderr,"put error:%s\n",tchdberrmsg(ecode));exit;
}
value = tchdbget2(hdb,"hello");
if(value)
{
printf("%s\n",value);
free(value);
}
else
{
ecode = tchdbecode(hdb);
fprintf(stderr,"get error:%s\n",tchdberrmsg(ecode));exit;
}
/* traverse records */
tchdbiterinit(hdb);
while((key = tchdbiternext2(hdb)) != NULL)
{
value = tchdbget2(hdb, key);
if(value)
{
printf("%s:%s\n", key, value);
free(value);
}
free(key);
}
/* remove records */
if(!tchdbout2(hdb,"hello") || !tchdbout2(hdb,"foo"))
{
ecode = tchdbecode(hdb);
fprintf(stderr,"put error:%s\n",tchdberrmsg(ecode));exit;
}
if(!tchdbclose(hdb))
{
ecode = tchdbecode(hdb);
fprintf(stderr,"close error:%s\n",tchdberrmsg(ecode));exit;
}
tchdbdel(hdb);
}