summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorJoseph Werle <joseph.werle@gmail.com>2014-05-05 11:30:00 -0400
committerJoseph Werle <joseph.werle@gmail.com>2014-05-05 11:31:45 -0400
commitcbfbbbb6b0b7a5441aa0414a73f4a2cda00bd957 (patch)
treed76b573b8005aad83df719b9f3afbf683036ef6a /main.c
parent2d25dad1f814ddbdb1c02a4d648970cb36d8f71e (diff)
Add exec and man
Diffstat (limited to 'main.c')
-rw-r--r--main.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..4b03e38
--- /dev/null
+++ b/main.c
@@ -0,0 +1,145 @@
+
+/**
+ * `main.c' - libbeaufort
+ *
+ * copyright (c) 2014 joseph werle <joseph.werle@gmail.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include "murmurhash.h"
+
+static void
+usage () {
+ fprintf(stderr, "usage: murmur [-hV] [options]\n");
+}
+
+static void
+help () {
+ fprintf(stderr, "\noptions:\n");
+ fprintf(stderr, "\n --seed=[seed] hash seed (optional)");
+ fprintf(stderr, "\n");
+}
+
+static char *
+read_stdin () {
+ size_t bsize = 1024;
+ size_t size = 1;
+ char buf[bsize];
+ char *res = (char *) malloc(sizeof(char) * bsize);
+ char *tmp = NULL;
+
+ // memory issue
+ if (NULL == res) { return NULL; }
+
+ // cap
+ res[0] = '\0';
+
+ // read
+ if (NULL != fgets(buf, bsize, stdin)) {
+ // store
+ tmp = res;
+ // resize
+ size += (size_t) strlen(buf);
+ // realloc
+ res = (char *) realloc(res, size);
+
+ // memory issues
+ if (NULL == res) {
+ free(tmp);
+ return NULL;
+ }
+
+ // yield
+ strcat(res, buf);
+
+ return res;
+ }
+
+ free(res);
+
+ return NULL;
+}
+
+#define isopt(opt, str) (0 == strncmp(opt, str, strlen(str)))
+
+#define setopt(opt, key, var) { \
+ char tmp = 0; \
+ size_t len = strlen(key) + 1; \
+ for (int i = 0; i < len; ++i) { tmp = *opt++; } \
+ var = opt; \
+}
+
+int
+main (int argc, char **argv) {
+ char *buf = NULL;
+ char *key = NULL;
+ char *seed = NULL;
+ uint32_t h = 0;
+
+ // parse opts
+ {
+ char *opt = NULL;
+ char tmp = 0;
+
+ opt = *argv++; // unused
+
+ while ((opt = *argv++)) {
+
+ // flags
+ if ('-' == *opt++) {
+ switch (*opt++) {
+ case 'h':
+ return usage(), help(), 0;
+
+ case 'V':
+ fprintf(stderr, "%s\n", MURMURHASH_VERSION);
+ return 0;
+
+ case '-':
+ if (isopt(opt, "seed")) {
+ setopt(opt, "seed", seed);
+ }
+ break;
+
+ default:
+ tmp = *opt--;
+ // error
+ fprintf(stderr, "unknown option: `%s'\n", opt);
+ usage();
+ return 1;
+ }
+ }
+ }
+ }
+
+ if (NULL == seed) {
+ seed = "0";
+ }
+
+#define hash(key) murmurhash(key, (uint32_t) strlen(key), (uint32_t) atoi(seed));
+
+ if (1 == isatty(0)) { return 1; }
+ else if (ferror(stdin)) { return 1; }
+ else {
+ buf = read_stdin();
+ if (NULL == buf) { return 1; }
+ else if (0 == strlen(buf)) { buf = ""; }
+ h = hash(buf);
+ printf("%" PRIu32 "\n", h);
+ do {
+ key = read_stdin();
+ if (NULL == key) { break; }
+ else if (0 == strlen(buf)) { buf = ""; }
+ h = hash(buf);
+ printf("%d" PRIu32 "\n", h);
+ } while (NULL != key);
+ }
+
+#undef hash
+
+ return 0;
+}