ploot

plot images or real-time plain text from a .csv file
Log | Files | Refs | README

commit a5dff2608625c0e39fdaa989e59c59c6caf9ac97
parent db65109a51ccebe406eb2a708d003fe68ad0af20
Author: Josuah Demangeon <mail@josuah.net>
Date:   Fri, 29 Jun 2018 13:30:06 +0200

start rewrite as a library

Diffstat:
Alibploot/Makefile | 17+++++++++++++++++
Alibploot/bin/test | 4++++
Alibploot/ploot.h | 2++
Mlibploot/ploot_humanize.c | 33+++++++++++++++++++--------------
Alibploot/test/ploot_humanize.c | 14++++++++++++++
5 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/libploot/Makefile b/libploot/Makefile @@ -0,0 +1,17 @@ + +# ls *.c | tr '\n' ' ' | fold -w 78 -s | sed 's,$, \\,' | sed '$ s, \\,,' +SRC = \ +ploot_braille.c ploot_humanize.c + +# ls test/*.c | tr '\n' ' ' | fold -w 78 -s | sed 's,$, \\,' | sed '$ s, \\,,' +TEST_SRC = \ +test/ploot_humanize.c +TEST_BIN = $(TEST_SRC:.c=) + +test: $(TEST_BIN) + +.c.o: + $(CC) $(CFLAGS) -o $@ $< + +$(TEST_BIN): + ./test $@ diff --git a/libploot/bin/test b/libploot/bin/test @@ -0,0 +1,4 @@ +#!/bin/sh -e + +cc "$CFLAGS" -o "$1" "${1#test/}.c" "$1.c" +./$1 diff --git a/libploot/ploot.h b/libploot/ploot.h @@ -0,0 +1,2 @@ +void ploot_braille (double *, int, char *, int, int); +int ploot_humanize (char *, double); diff --git a/libploot/ploot_humanize.c b/libploot/ploot_humanize.c @@ -1,3 +1,5 @@ + + #include "ploot.h" #define ABS(x) ((x) > 0 ? (x) : -(x)) @@ -24,30 +26,33 @@ sys_int_suf(double *val) /* * Return the precision to use for val so that it takes 6 characters to print. */ -int -precision(double *val) +static int +precision(double val) { - return (*val == 0) ? 0 : /* 0 */ - (*val < 1.0) ? 4 : /* 0.0001u */ - (*val < 1000) ? 2 : /* 1.00u - 10.00u - 100.00u */ - (*val < 1000000) ? 1 : /* 100000u */ + return (val == 0) ? 0 : /* 0 */ + (val < 1.0) ? 4 : /* 0.0001u */ + (val < 1000) ? 2 : /* 1.00u - 10.00u - 100.00u */ + (val < 1000000) ? 1 : /* 100000u */ -1; /* a lot */ } /* - * Set 'str' to a human-readable form of 'num' with always a width of 8 (+ 1 + * Set <s> to a human-readable form of <num> with always a width of 8 (+ 1 for * the '\0' terminator). Buffer overflow is ensured not to happen due to the * max size of a double. Return the exponent. */ int -ploot_humanize(char *str, double val) +ploot_humanize(char *s, double val) { int exp, precision; + char suf; - snprintf(str, 9, "%+.*f %c", precision, val, label[exp]); - str[8] = '\0'; - if (val >= 0) - str[0] = ' '; - - return exp * 3; + if (val < 0) { + val = -val; + *s++ = '-'; + } else { + *s++ = ' '; + } + suf = sys_int_suf(*val); + s += snprintf(s, 9, "%+.*f%c", precision(val), val, suf); } diff --git a/libploot/test/ploot_humanize.c b/libploot/test/ploot_humanize.c @@ -0,0 +1,14 @@ + +int main(void) +{ + double d; + char line[1024], s[20]; + + while (fgets(line, sizeof(line), stdin) != NULL) { + d = atof(line); + ploot_humanize(s, d); + puts(s); + } + + return 0; +}