b0322a85 |
1 | #include "config.h" |
d9898ee8 |
2 | #include "bdbobj.h" |
3 | #include <stdio.h> |
4 | #include <string.h> |
5 | |
6 | static void fw(char *p, size_t a, size_t b, FILE *f) |
7 | { |
8 | size_t i, j=a*b; |
9 | |
10 | for (i=0; i<j; i++) |
11 | putc(p[i], f); |
12 | } |
13 | |
14 | struct kd { |
15 | struct kd *next; |
16 | char *key, *data; |
17 | } ; |
18 | |
19 | static int kdcmp (const void *a, const void *b) |
20 | { |
21 | return (strcmp( (*(const struct kd **)a)->key, |
22 | (*(const struct kd **)b)->key)); |
23 | } |
24 | |
25 | int main(int argc, char **argv) |
26 | { |
27 | if (argc < 2) exit(1); |
28 | |
29 | if (argc < 3) |
30 | { |
31 | BDbObj dbw; |
32 | char *key, *data; |
33 | size_t keylen, datalen; |
34 | struct kd *kdlist, **kdarray; |
35 | size_t kdcnt; |
36 | |
37 | if (dbw.Open(argv[1], "R")) |
38 | { |
39 | perror("open"); |
40 | exit(1); |
41 | } |
42 | |
43 | printf("Dumping %s:\n", argv[1]); |
44 | kdlist=0; |
45 | kdcnt=0; |
46 | for (key=dbw.FetchFirstKeyVal(keylen, data, datalen); |
47 | key; key=dbw.FetchNextKeyVal(keylen, data, datalen)) |
48 | { |
49 | struct kd *k=(struct kd *)malloc(sizeof(struct kd)); |
50 | |
51 | if (!k) |
52 | { |
53 | perror("malloc"); |
54 | exit(1); |
55 | } |
56 | if ((k->key=(char *)malloc(keylen+1)) == 0 || |
57 | (k->data=(char *)malloc(datalen+1)) == 0) |
58 | { |
59 | perror("malloc"); |
60 | exit(1); |
61 | } |
62 | memcpy(k->key, key, keylen); |
63 | k->key[keylen]=0; |
64 | memcpy(k->data, data, datalen); |
65 | k->data[datalen]=0; |
66 | free(data); |
67 | ++kdcnt; |
68 | k->next=kdlist; |
69 | kdlist=k; |
70 | } |
71 | |
72 | if ((kdarray=(struct kd **) |
73 | malloc( (kdcnt+1)*sizeof(struct kd *))) == 0) |
74 | { |
75 | perror("malloc"); |
76 | exit(1); |
77 | } |
78 | kdcnt=0; |
79 | while ( kdlist ) |
80 | { |
81 | kdarray[kdcnt++]=kdlist; |
82 | kdlist=kdlist->next; |
83 | } |
84 | kdarray[kdcnt]=0; |
85 | |
86 | qsort( kdarray, kdcnt, sizeof(kdarray[0]), &kdcmp); |
87 | |
88 | for (kdcnt=0; kdarray[kdcnt]; kdcnt++) |
89 | { |
90 | printf("Key: "); |
91 | fw(kdarray[kdcnt]->key, strlen(kdarray[kdcnt]->key), |
92 | 1, stdout); |
93 | printf(", Data: "); |
94 | fw(kdarray[kdcnt]->data, strlen(kdarray[kdcnt]->data), |
95 | 1, stdout); |
96 | printf("\n"); |
97 | free(kdarray[kdcnt]->key); |
98 | free(kdarray[kdcnt]->data); |
99 | free(kdarray[kdcnt]); |
100 | } |
101 | free(kdarray); |
102 | |
103 | dbw.Close(); |
104 | } else if (argc < 4 && argv[2][0] == '-') |
105 | { |
106 | BDbObj dbw; |
107 | |
108 | if (dbw.Open(argv[1], "W")) |
109 | { |
110 | perror("open"); |
111 | exit(1); |
112 | } |
113 | |
114 | printf("Deleting %s from %s:\n", argv[2], argv[1]); |
115 | if (dbw.Delete(argv[2]+1, strlen(argv[2]+1))) |
116 | fprintf(stderr, "Not found.\n"); |
117 | |
118 | dbw.Close(); |
119 | } else if (argc < 4) |
120 | { |
121 | BDbObj dbw; |
122 | |
123 | if (dbw.Open(argv[1], "R")) |
124 | { |
125 | perror("open"); |
126 | exit(1); |
127 | } |
128 | |
129 | size_t len; |
130 | char *val=dbw.Fetch(argv[2], strlen(argv[2]), len, 0); |
131 | |
132 | if (!val) |
133 | { |
134 | fprintf(stderr, "%s: not found.\n", argv[2]); |
135 | exit(1); |
136 | } |
137 | printf("Fetching %s from %s: ", argv[2], argv[1]); |
138 | fw(val, len, 1, stdout); |
139 | printf("\n"); |
140 | free(val); |
141 | dbw.Close(); |
142 | } |
143 | else |
144 | { |
145 | BDbObj dbw; |
146 | |
147 | if (dbw.Open(argv[1], "C")) |
148 | { |
149 | perror("open"); |
150 | exit(1); |
151 | } |
152 | |
153 | printf("Storing %s/%s into %s:\n", argv[2], argv[3], argv[1]); |
154 | if (dbw.Store(argv[2], strlen(argv[2]), |
155 | argv[3], strlen(argv[3]), "R")) |
156 | { |
157 | perror("write"); |
158 | exit(1); |
159 | } |
160 | |
161 | dbw.Close(); |
162 | } |
163 | exit(0); |
164 | return (0); |
165 | } |