From cbfbbbb6b0b7a5441aa0414a73f4a2cda00bd957 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Mon, 5 May 2014 11:30:00 -0400 Subject: Add exec and man --- main.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 main.c (limited to 'main.c') 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 + */ + +#include +#include +#include +#include +#include +#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; +} -- cgit v1.2.3