Object | +---DDbm
The Dbm class implements a wrapper around the gdbm database manager. The naming of the methods is identical to the hashtable class, but this class is not a collection. It stores data, not objects. Open modes: r = read, w = write, n = write in new database, c = write in new database if not exist.
#include <stdio.h> #include "ofc/DDbm.h" int main(int argc, char *argv[]) { DDbm *dbm1 = [DDbm alloc]; DDbm *dbm2 = [DDbm new ]; DData *dat; DText *str; [dbm1 init :"file.dbm" :"c"]; // Init and open with filename, writer if ([dbm1 isOpen]) // Check if the file is open { if ([dbm1 insert :"key1" :4 :"data1" :5]) printf("Data: key1,data1 succesfull inserted.\n"); else printf("Error inserting: key1,data1:%d.\n", [dbm1 error]); if ([dbm1 insert :"key2" :4 :"data2" :5]) printf("Data: key2,data2 succesfull inserted.\n"); else printf("Error inserting: key2,data2:%d.\n", [dbm1 error]); [dbm1 close]; } else printf("Error opening file.dbm:%d.\n", [dbm1 error]); if ([dbm2 open :"file.dbm" :"r"]) // Open the file for reading { dat = [dbm2 get :"key2" :4]; // Get the data for key2 if (dat != nil) { str = [dat toText]; printf("Data for key2:%s.\n", [str cstring]); [str free]; [dat free]; } else printf("Key2 is not present in the file.\n"); if ([dbm2 get :"key3" :4]) // Check for key3 printf("Key3 is present in the file.\n"); else printf("Key3 is not present in the file.\n"); [dbm2 close]; } else printf("Error opening file.dbm:%d.\n", [dbm2 error]); [dbm1 free]; // Cleanup [dbm2 free]; return 0; }