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