15 typedef unsigned int uint;
17 static int bm_compare_key_trans(const DBT *a, const DBT *b) {
18 uint* tmpa = (uint*)(a->data);
19 uint* tmpb = (uint*)(b->data);
21 if((*tmpa) == (*tmpb)) return 0;
22 if(*tmpa > *tmpb) return 1;
25 //------------------------------------------------------------------------------
26 static int bm_compare_key_words(const DBT *a, const DBT *b) {
27 char* tmpa = g_utf8_casefold((char*)(a->data),-1);
28 char* tmpb = g_utf8_casefold((char*)(b->data),-1);
29 int result = g_utf8_collate(tmpa,tmpb);
35 //------------------------------------------------------------------------------
36 int read_berkeley_in_string_mode(DB* dbp) {
37 DBT key = { NULL , 0 };
38 DBT val = { NULL , 0 };
40 int result = dbp->seq(dbp, &key, &val, R_FIRST);
49 printf("---Error! While sequentiall reading database\n");
52 memcpy(&a, val.data, sizeof(uint));
53 t = (uint*)(val.data + sizeof(uint));
54 printf("%4d. word = %-30s [%-2d record(s)] => \n",count, (char*)(key.data), a);
56 for(i=0; i < a; ++i) {
59 printf("\t%4u.%u. ID = %u [hash = %#x]\n",count,i+1,b,c);
62 result = dbp->seq(dbp, &key, &val, R_NEXT);
67 //------------------------------------------------------------------------------
68 int read_berkeley_in_binary_mode(DB* dbp) {
69 DBT key = { NULL , 0 };
70 DBT val = { NULL , 0 };
75 int result = dbp->seq(dbp, &key, &val, R_FIRST);
78 printf("---Error! While sequentiall reading database\n");
81 memcpy(&a, key.data, sizeof(uint));
82 s = (char*) (val.data);
85 memcpy(&b , val.data, sizeof(uint));
86 printf("%4u. ID = %-4u value(freeID)=%u\n",count,a,b);
89 printf("%4u. ID = %-4u string=\'%s\'\n",count,a,s);
92 result = dbp->seq(dbp, &key, &val, R_NEXT);
97 //------------------------------------------------------------------------------
98 int berkeley_del_record(DB* dbp,char mode,char* id) {
100 DBT key = { NULL , 0 };
102 printf("Delete key: %s...",id);
104 key.size = strlen(id) + 1;
106 else if('b' == mode) {
107 if(1 != sscanf(id,"%u",&a)) {
108 printf("ID has wrong format - it should be unsigned int!\n");
111 printf("Deleting record with id=%u\n",a);
112 key.size = sizeof(uint);
116 printf("not supported mode for delete action!\n");
120 int result = dbp->del(dbp,&key,0);
122 printf("... there was no such an element!\n");
124 else if(-1 == result) {
125 printf("---Error while deleteing record!\n");
129 printf("... record deleted!\n");
134 //------------------------------------------------------------------------------
135 int berkeley_fnd_record(DB* dbp,char mode,char* id) {
137 DBT key = { NULL , 0 };
138 DBT val = { NULL , 0 };
140 printf("Searching for key: %s...\n",id);
142 key.size = strlen(id) + 1;
144 else if('b'== mode) {
145 if(1 != sscanf(id,"%u",&a)) {
146 printf("ID has wrong format - it should be unsigned int!\n");
149 printf("Searching for record with id=%u\n",a);
150 key.size = sizeof(uint);
154 printf("not supported mode for find action!\n");
158 int result = dbp->get(dbp,&key,&val,0);
160 printf("... there was no such a record\n");
162 else if(-1 == result) {
163 printf("---Error while searching for the record!\n");
168 uint b = 0, c = 0, i = 0;
172 // ------------------------------
173 memcpy(&a, val.data, sizeof(uint));
174 t = (uint*)(val.data + sizeof(uint));
175 printf("Element word = %-30s [%-2d record(s)] => \n", (char*)(key.data), a);
177 for(i=0; i < a; ++i) {
180 printf("\t%2u. ID = %u [hash = %#xu]\n",i+1,b,c);
183 // ------------------------------
185 // ------------------------------
186 s = (char*) (val.data);
188 memcpy(&b , val.data, sizeof(uint));
189 printf("Element ID = %-4u value(freeID)=%u\n",a,b);
192 printf("Element ID = %-4u string=\'%s\'\n",a,s);
195 // ------------------------------
197 printf("Not supported mode!\n");
203 //------------------------------------------------------------------------------
204 int berkeley_fnd_pattern_record(DB* dbp,char mode,char* id) {
205 DBT key = { NULL , 0 };
206 DBT val = { NULL , 0 };
209 int result = dbp->seq(dbp, &key, &val, R_FIRST);
216 char* tmp = g_utf8_casefold(id,-1);
217 uint len = strlen(tmp);
218 printf("dlugosc: %u\n",len);
222 printf("---Error! While sequentiall reading database\n");
225 tmp2 = g_utf8_casefold((char*)(key.data),len);
226 if(0 == g_utf8_collate(tmp2,tmp) || len == 0) {
227 memcpy(&a, val.data, sizeof(uint));
228 t = (uint*)(val.data + sizeof(uint));
229 printf("%4d. word = %-30s [%-2d record(s)] => \n",count, (char*)(key.data), a);
231 for(i=0; i < a; ++i) {
234 printf("\t%4u.%u. ID = %u [hash = %#xu]\n",count,i+1,b,c);
237 result = dbp->seq(dbp, &key, &val, R_NEXT);
244 printf("Searching for pattern is not supported in binary mode!");
248 //------------------------------------------------------------------------------
249 int berkeley_del_pattern_record(DB* dbp,char mode,char *id) {
250 DBT key = { NULL , 0 };
251 DBT val = { NULL , 0 };
254 int result = dbp->seq(dbp, &key, &val, R_FIRST);
261 char* tmp = g_utf8_casefold(id,-1);
262 uint len = strlen(tmp);
266 printf("---Error! While sequentiall reading database\n");
269 tmp2 = g_utf8_casefold((char*)(key.data),len);
270 if(0 == g_utf8_collate(tmp2,tmp) || len == 0) {
271 // delete actual record
273 memcpy(&a, val.data, sizeof(uint));
274 t = (uint*)(val.data + sizeof(uint));
275 printf("Deleting record %4d. word = %-30s [%-2d record(s)] => \n",count, (char*)(key.data), a);
277 for(i=0; i < a; ++i) {
280 printf("\t%4u.%u. ID = %u [hash = %#xu]\n",count,i+1,b,c);
282 int result = dbp->del(dbp,&key,0);
284 printf("---Error find element but could not delete it!\n");
286 else if(-1 == result) {
287 printf("---Error while deleteing record!\n");
291 printf("... record deleted!\n");
295 free(tmp2); tmp2 = NULL;
296 result = dbp->seq(dbp, &key, &val, R_NEXT);
298 printf("%u record were deleted totaly.\n",count);
303 printf("Searching for pattern is not supported in binary mode!");
307 //------------------------------------------------------------------------------
308 int berkeley_add_record(DB* dbp,char mode,char* id, char* value) {
309 DBT key = { NULL , 0 };
310 DBT val = { NULL , 0 };
313 uint a = 0, i = 0, error = 0, b = 0, c = 0;
315 char* tmp = malloc(200);
316 char* tmp_original = tmp;
318 printf("Adding record with key = %s...\n",id);
320 key.size = strlen(id) + 1;
322 if(2 != sscanf(value,"%u%[ 0-9]",&a,tmp)) {
326 val.size = sizeof(uint) * (a*2 + 1);
327 t = (uint*)malloc( val.size );
329 for(i=0; i< a; ++i) {
330 if(2 != sscanf(tmp,"%u%[ 0-9]",&b,tmp)) {
335 int n = sscanf(tmp,"%u%[ 0-9]",&b,tmp);
336 if(( (2 != n) && (i!=(a-1)) ) || ((1!=n) && (i==(a-1)))) {
343 printf("Wrong value for string database!\n");
350 else if('b'== mode) {
351 if(1 != sscanf(id,"%u",&a)) {
352 printf("ID has wrong format - it should be unsigned int!\n");
355 printf("Adding record with id=%u\n",a);
356 key.size = sizeof(uint);
359 if(1 != sscanf(value,"%u",&c)) {
360 printf("Value has wrong format for record with ID=0 (freeID - unsinged int)\n");
367 val.size = strlen(value) + 1;
372 printf("---Not supported mode for add action!\n");
382 result = dbp->put(dbp, &key, &val, R_NOOVERWRITE);
384 printf("Such a key already exist in database!\n");
387 else if(-1 == result) {
388 printf("---Error while adding new record!\n");
392 printf("New record has been added.\n");
393 if(-1 == dbp->sync(dbp, 0)) {
394 printf("---Error while saving data to file! Record could be lost.\n");
401 //------------------------------------------------------------------------------
402 int main(int argc, char* argv[]) {
404 for(i=1;i<argc;++i) {
405 if((0 == strcmp("help",argv[i])) ||
406 (0 == strcmp("--help",argv[i])) ||
407 (0 == strcmp("-help",argv[i])) ||
408 (0 == strcmp("-h",argv[i])) ||
409 (0 == strcmp("/h",argv[i])) ||
410 (0 == strcmp("/help",argv[i]))) {
415 if((argc < 3) || help) {
416 printf("[Usage: bdbprint s|b file_name.db (action identifier_of_record (optional_value))]\n"
417 "\tfile_name.db - database to open and print\n"
418 "\t s - open in string mode, every key is treated as a string\n"
419 "\t b - open in binary mode, every key is treated as a unsigned integer\n"
420 "\t action - optional action to do on the base\n"
421 "\t\t del - delete record with key=identifier_of_record\n"
422 "\t\t add - add new record with key=identifier_of_record\n"
423 "\t\t fnd - find record with key=identifier_of_record\n"
424 "\t\t\t optional_value - value for add action\n");
428 char* filename = argv[2];
429 char* _mode = argv[1];
430 if((strlen(_mode) > 1) || ( ('s' != _mode[0]) && ('b' != _mode[0]) )) {
431 printf("Wrong mode! Mode could be only s or b [\'s\' , \'b\']!\n");
434 char mode = _mode[0];
437 0, /* permit duplicate keys? */
438 0, /* cache size; 0 - default size */
439 0, /* page size; 0 - default */
440 0, /* byte order; 0 - use host order */
441 0, /* min page number per page; 0 - default=2 */
442 bm_compare_key_words, /* comparision function */
443 NULL /* prefix comparision function */
446 info.compare = bm_compare_key_trans;
448 u_int32_t flags = O_CREAT | O_RDWR;
449 DB *dbp = dbopen(filename, flags, 0755, DB_BTREE, &info);
451 printf("Could not open database: %s!\n",filename);
457 printf("Printing database: \'%s\' in mode %s :\n", filename, _mode);
459 result = read_berkeley_in_string_mode(dbp);
461 else if('b' == mode) {
462 result = read_berkeley_in_binary_mode(dbp);
466 printf("Do not print - only action... [mode=%s]\n",_mode);
468 printf("Wrong number of parameters! probably You did not give id of record for action.\n");
471 char* action = argv[3];
472 if(strcmp(action,"del") == 0) {
473 if('*' == argv[4][0]) {
474 result = berkeley_del_pattern_record(dbp,mode,&(argv[4][1]));
477 result = berkeley_del_record(dbp,mode,argv[4]);
480 else if(strcmp(action,"fnd") == 0) {
481 if('*' == argv[4][0]) {
482 result = berkeley_fnd_pattern_record(dbp,mode,argv[4]+1);
485 result = berkeley_fnd_record(dbp,mode,argv[4]);
488 else if(strcmp(action,"add") == 0) {
490 printf("You did not give value for new record!\n");
493 result = berkeley_add_record(dbp,mode,argv[4],argv[5]);
496 printf("Not supported action: %s! The only good values are: add, del, fnd.\n",action);
504 //------------------------------------------------------------------------------