-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdb-export.c
71 lines (56 loc) · 1.18 KB
/
db-export.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "db.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int i;
db_t db;
db_iter_t iter;
db_option_t option;
uint32_t item;
uint32_t klen;
uint32_t vlen;
char key[1024];
char val[1024];
FILE *fp;
if (argc != 4) {
fprintf(stderr, "usage: %s [datafile] [indexfile] [file]\n", argv[0]);
return 0;
}
option.rdonly = 1;
if (db_open(&db, argv[1], argv[2], &option) != DB_OK) {
fprintf(stderr, "open db %s failed\n", argv[1]);
return 0;
}
if (db_iter(&db, &iter, NULL, 0) != DB_OK) {
fprintf(stderr, "iter db %s failed\n", argv[1]);
return 0;
}
fp = fopen(argv[3], "w");
if (fp == NULL) {
fprintf(stderr, "open file %s failed\n", argv[3]);
return 0;
}
item = 0;
klen = sizeof(key);
vlen = sizeof(val);
while (db_iter_next(&db, &iter, key, &klen, val, &vlen) == DB_OK) {
for (i = 0; i < klen; i++)
fprintf(fp, "%02x", key[i]);
fprintf(fp, "\n");
for (i = 0; i < vlen; i++)
fprintf(fp, "%02x", val[i]);
fprintf(fp, "\n");
klen = sizeof(key);
vlen = sizeof(val);
item += 1;
}
printf("%u", item);
fclose(fp);
db_close(&db);
return 0;
}