Organize source tree
[blerg.git] / tools / blergtool.c
diff --git a/tools/blergtool.c b/tools/blergtool.c
new file mode 100644 (file)
index 0000000..d5aef50
--- /dev/null
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "database.h"
+#include "tags.h"
+
+void help() {
+       printf("Usage: blergtool <store|fetch> <storename> [record]\n");
+}
+
+int main(int argc, char *argv[]) {
+       if (argc < 2) {
+               help();
+               exit(1);
+       }
+
+       if (strncmp(argv[1], "store", 5) == 0) {
+               char *store = argv[2];
+               struct blerg *f = blerg_open(store);
+               if (!f) {
+                       printf("Blerg open failed\n");
+                       exit(1);
+               }
+
+               size_t bytes_read = 0;
+               char *data = malloc(65536);
+               do {
+                       bytes_read += fread(data + bytes_read, 1, 65536 - bytes_read, stdin);
+               } while (bytes_read < 65536 && !feof(stdin));
+               int record = blerg_store(f, data, bytes_read);
+               blerg_close(f);
+               free(data);
+
+               if (record < 0) {
+                       fprintf(stderr, "Could not store\n");
+                       exit(1);
+               } else {
+                       fprintf(stderr, "Stored record %d\n", record);
+               }
+       } else if (strncmp(argv[1], "fetch", 5) == 0) {
+               char *store = argv[2];
+               int record = atoi(argv[3]);
+               struct blerg *f = blerg_open(store);
+               if (!f) {
+                       printf("Blerg open failed\n");
+                       exit(1);
+               }
+
+               char *data;
+               int size;
+               blerg_fetch(f, record, &data, &size);
+               blerg_close(f);
+               fwrite(data, 1, size, stdout);
+               free(data);
+       } else if (strncmp(argv[1], "list", 4) == 0) {
+               char *tag = argv[2];
+               uint64_t count = 50;
+               struct tag *list = tag_list(tag, 0, &count, -1);
+               if (list == NULL) {
+                       printf("No entries");
+               } else {
+                       int i;
+                       for (i = 0; i < count; i++) {
+                               printf("%s %d\n", list[i].author, list[i].record);
+                       }
+                       free(list);
+               }
+       } else {
+               help();
+       }
+
+       return 0;
+}