From 9b3a0326cfc9ba5334f0ed0178a8ba6d1814bc19 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Fri, 9 Dec 2016 14:18:31 +0100 Subject: can read input - woohoo --- click-finder.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++--- click-finder.h.in | 3 +- 2 files changed, 122 insertions(+), 6 deletions(-) diff --git a/click-finder.c b/click-finder.c index ac953a5..54efb0f 100644 --- a/click-finder.c +++ b/click-finder.c @@ -1,10 +1,125 @@ #include #include +#include #include "click-finder.h" #include "version.h" -#include +// #include -int main(int argc, char** argv) { - printf("%s\n", VERSION); - return 0; -} +static int verbose_flag; +static long long int bytes_per_sample = 3; +static long long int channels = 1; + +int +main(int argc, char **argv) + { + int c; + int samples_read; + while (1) + { + static struct option long_options[] = + { + {"help", no_argument, 0, 0}, + {"version", no_argument, 0, 0}, + {"verbose", no_argument, &verbose_flag, 1}, + {"brief", no_argument, &verbose_flag, 0}, + {"bitspersample", required_argument, 0, 'b'}, + {"channels", required_argument, 0, 'c'}, + {0, 0, 0, 0} + }; + /* getopt_long stores the option index here. */ + int option_index = 0; + + c = getopt_long (argc, argv, "b:hv", + long_options, &option_index); + /* Detect the end of the options. */ + if (c == -1) + break; + + switch (c) + { + case 0: + /* If this option set a flag, do nothing else now. */ + if (long_options[option_index].flag != 0) + break; + switch (option_index) + { + case 0: + printf("Print help here.\n"); + exit(0); + case 1: + printf("This is %s, version %s\n", progname, VERSION); + exit(0); + default: + fprintf (stderr, "unknown option %s", long_options[option_index].name); + if (optarg) + fprintf (stderr, " with arg %s", optarg); + fprintf (stderr, "\n"); + exit(1); + } + break; + + case 'b': + if (atoi(optarg) % 8 != 0) + { + fprintf(stderr, "%s: bits per sample (%d) is no multiple of 8", progname, atoi(optarg)); + exit(1); + } + bytes_per_sample = atoi(optarg)/8; + break; + + case 'c': + channels = atoi(optarg); + break; + + case 'v': + verbose_flag=1; + break; + + case '?': + /* getopt_long already printed an error message. */ + exit(1); + + default: + exit(1); + } + } + + if (optind != argc) + { + fprintf(stderr, "%s: spurious option '%s'\n", progname, argv[optind]); + exit(1); + } + + if (verbose_flag) + { + fprintf(stderr, "bps: %d\n", bytes_per_sample*8); + fprintf(stderr, "channels: %d\n", channels); + } + + int8_t *content = malloc(sizeof(int8_t) * bytes_per_sample * channels * BUF_SIZE); + long long int content_length = 0; // in bytes + if (content == NULL) + { + fprintf(stderr, "%s: Failed to allocate content", progname); + exit(1); + } + while (samples_read = fread(content + content_length, bytes_per_sample * channels, BUF_SIZE, stdin)) + { + content_length += samples_read * bytes_per_sample * channels; + content = realloc(content, content_length + sizeof(int8_t) * bytes_per_sample * channels * BUF_SIZE); + } + + if(ferror(stdin)) + { + free(content); + fprintf(stderr, "%s: Error reading from stdin.", progname); + exit(1); + } + + fprintf(stderr, "%d", content_length); + + free(content); + if (verbose_flag) + fprintf(stderr, "success\n"); + return 0; + } diff --git a/click-finder.h.in b/click-finder.h.in index 8b13789..e724055 100644 --- a/click-finder.h.in +++ b/click-finder.h.in @@ -1 +1,2 @@ - +#define progname "click-finder" +#define BUF_SIZE 1024 -- cgit v1.2.3