1 #include <tcutil.h>
2 #include <tchdb.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <stdint.h>
6
main(int argc,char ** argv)7 int main(int argc, char **argv){
8 TCHDB *hdb;
9 int ecode;
10 char *key, *value;
11
12 /* create the object */
13 hdb = tchdbnew();
14
15 /* open the database */
16 if(!tchdbopen(hdb, "casket.tch", HDBOWRITER | HDBOCREAT)){
17 ecode = tchdbecode(hdb);
18 fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode));
19 }
20
21 /* store records */
22 if(!tchdbput2(hdb, "foo", "hop") ||
23 !tchdbput2(hdb, "bar", "step") ||
24 !tchdbput2(hdb, "baz", "jump")){
25 ecode = tchdbecode(hdb);
26 fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode));
27 }
28
29 /* retrieve records */
30 value = tchdbget2(hdb, "foo");
31 if(value){
32 printf("%s\n", value);
33 free(value);
34 } else {
35 ecode = tchdbecode(hdb);
36 fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode));
37 }
38
39 /* traverse records */
40 tchdbiterinit(hdb);
41 while((key = tchdbiternext2(hdb)) != NULL){
42 value = tchdbget2(hdb, key);
43 if(value){
44 printf("%s:%s\n", key, value);
45 free(value);
46 }
47 free(key);
48 }
49
50 /* close the database */
51 if(!tchdbclose(hdb)){
52 ecode = tchdbecode(hdb);
53 fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode));
54 }
55
56 /* delete the object */
57 tchdbdel(hdb);
58
59 return 0;
60 }
61