using getopt all the same - ploot - simple plotting tools HTML git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 453cb7f82c06a904e3b5aae5ee57d9a1fd91f460 DIR parent ebff9e927a35af076a55e62732001c860c883244 HTML Author: Josuah Demangeon <mail@josuah.net> Date: Sun, 4 Feb 2018 23:31:58 +0100 using getopt all the same Diffstat: D arg.h | 25 ------------------------- M ploot.c | 58 ++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 46 deletions(-) --- DIR diff --git a/arg.h b/arg.h @@ -1,25 +0,0 @@ -#define USED(x) ((void)(x)) - -extern char *argv0; - -#define ARGBEGIN(argc, argv) \ - for (argv0 = *argv, argv++, argc--; \ - argv[0] != NULL && argv[0][0] == '-' && argv[0][1] != '\0'; \ - argc--, argv++) { \ - char **arg_v, *arg_s; \ - if (argv[0][1] == '-' && argv[0][2] == '\0') { \ - argv++, argc--; \ - break; \ - } \ - arg_v = argv; \ - for (arg_s = *argv; *arg_s != '\0'; arg_s++) { \ - if (arg_v != argv) \ - break; \ - switch (*arg_s) - -#define ARGEND \ - } \ - } - -#define EARGF(x) \ - ((argv[1] == NULL) ? ((x), NULL) : (argc--, argv++, argv[0])) DIR diff --git a/ploot.c b/ploot.c @@ -3,8 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> -#include "arg.h" #include "config.h" #define MAX_VAL 80 @@ -14,9 +14,6 @@ #define MIN(x, y) ((x) < (y) ? (x) : (y)) #define LEN(x) (sizeof(x) / sizeof(*x)) -char *argv0; -int flag_h = 20; - /* * Set `str' to a human-readable form of `num' with always a width of 7 (+ 1 * the '\0' terminator). Buffer overflow is ensured not to happen due to the @@ -63,7 +60,7 @@ title(char *str, int width) { if (str == NULL) return; - printf("%*s\n\n", (int)(width + strlen(str) + MARGIN) / 2, str); + printf("%*s\n", (int)(width + strlen(str) + MARGIN) / 2, str); } /* @@ -119,7 +116,8 @@ plot(int height, double *beg, double *end, char *str) double top, bot, max; int h; - title(str, end - beg); + if (str != NULL) + title(str, end - beg); max = maxdv(beg, end); for (h = height + height % 2; h > 0; h -= 2) { @@ -137,9 +135,9 @@ plot(int height, double *beg, double *end, char *str) * next postion. */ size_t -ring_add(double *ring, size_t len, size_t pos, double val) +ring_add(double *rbuf, size_t len, size_t pos, double val) { - *ring = val; + *rbuf = val; return (pos < len) ? pos + 1 : 0; } @@ -151,8 +149,13 @@ ring_add(double *ring, size_t len, size_t pos, double val) void ring_copy(double *buf, double *rbuf, size_t len, size_t pos) { + size_t i = 0; + memcpy(buf, rbuf + pos, (len - pos) * sizeof(*rbuf)); memcpy(buf + (len - pos), rbuf, pos * sizeof(*rbuf)); + printf("len: %zd, pos: %zd\n", len, pos); + for (i = 0; i < len; i++) + printf("%03zd: %lf\n", i, buf[i]); } /* @@ -163,16 +166,18 @@ ring_copy(double *buf, double *rbuf, size_t len, size_t pos) double * read_simple(double buf[MAX_VAL]) { - /* ring buffer to keep the last `MAX_VAL' values */ double rbuf[MAX_VAL], val; size_t p, pos, len; len = LEN(rbuf); for (p = pos = 0; scanf("%lf\n", &val) > 0; p++) - pos = ring_add(rbuf, val, len, pos); + pos = ring_add(rbuf, len, pos, val); + len = MIN(len, p); + pos = MIN(pos, p); + ring_copy(buf, rbuf, len, pos); - return buf + MIN(p, len); + return buf + len; } /* @@ -184,7 +189,6 @@ read_simple(double buf[MAX_VAL]) double * read_time_series(double *valv, time_t *timev) { - /* ring buffer to keep the last `MAX_VAL' values */ time_t time_rbuf[MAX_VAL]; double val_rbuf[MAX_VAL]; @@ -198,7 +202,7 @@ read_time_series(double *valv, time_t *timev) void usage(void) { - printf("usage: %s [-h height]\n", argv0); + printf("usage: ploot [-h height]\n"); exit(1); } @@ -206,16 +210,28 @@ int main(int argc, char **argv) { double val[MAX_VAL], *end; - - ARGBEGIN(argc, argv) { - case 'h': - flag_h = atoi(EARGF(usage())); - if (flag_h <= 0) + char c; + + int flag_h = 20; + char *flag_t = NULL; + + while ((c = getopt(argc, argv, "h:t:")) != -1) { + switch (c) { + case -1: + break; + case 'h': + if ((flag_h = atoi(optarg)) <= 0) + usage(); + break; + case 't': + flag_t = optarg; + break; + default: usage(); - break; - } ARGEND; + } + } end = read_simple(val); - plot(flag_h, val, end, "Sample data generated with jot"); + plot(flag_h, val, end, flag_t); return 0; }