From 34d02d4b413943f0b7c0950244a710be14b97a48 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Wed, 27 Jan 2016 20:41:58 +0100 Subject: [PATCH] Group print functions --- cmdline/device.c | 54 ------------------------------------ cmdline/state.c | 28 ------------------- cmdline/support.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ cmdline/support.h | 24 ++++++++++++++++ 4 files changed, 94 insertions(+), 82 deletions(-) diff --git a/cmdline/device.c b/cmdline/device.c index d02fd2fd..cc20740a 100644 --- a/cmdline/device.c +++ b/cmdline/device.c @@ -596,60 +596,6 @@ static double raid_prob_of_one_or_more_failures(double array_failure_rate, doubl return poisson_prob_n_or_more_failures(raid_failure_rate, 1); } -/** - * Print a string with space padding. - */ -static void printl(const char* str, size_t pad) -{ - size_t len; - - printf("%s", str); - - len = strlen(str); - - while (len < pad) { - putchar(' '); - ++len; - } -} - -/** - * Print a probability with space padding. - */ -static void printp(double v, size_t pad) -{ - char buf[64]; - const char* s = "%"; - - if (v > 0.1) - snprintf(buf, sizeof(buf), "%5.2f%s", v, s); - else if (v > 0.01) - snprintf(buf, sizeof(buf), "%6.3f%s", v, s); - else if (v > 0.001) - snprintf(buf, sizeof(buf), "%7.4f%s", v, s); - else if (v > 0.0001) - snprintf(buf, sizeof(buf), "%8.5f%s", v, s); - else if (v > 0.00001) - snprintf(buf, sizeof(buf), "%9.6f%s", v, s); - else if (v > 0.000001) - snprintf(buf, sizeof(buf), "%10.7f%s", v, s); - else if (v > 0.0000001) - snprintf(buf, sizeof(buf), "%11.8f%s", v, s); - else if (v > 0.00000001) - snprintf(buf, sizeof(buf), "%12.9f%s", v, s); - else if (v > 0.000000001) - snprintf(buf, sizeof(buf), "%13.10f%s", v, s); - else if (v > 0.0000000001) - snprintf(buf, sizeof(buf), "%14.11f%s", v, s); - else if (v > 0.00000000001) - snprintf(buf, sizeof(buf), "%15.12f%s", v, s); - else if (v > 0.000000000001) - snprintf(buf, sizeof(buf), "%16.13f%s", v, s); - else - snprintf(buf, sizeof(buf), "%17.14f%s", v, s); - printl(buf, pad); -} - static void state_smart(unsigned n, tommy_list* low) { tommy_node* i; diff --git a/cmdline/state.c b/cmdline/state.c index 57d7a9c0..a93dfa53 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -4136,34 +4136,6 @@ void state_usage_parity(struct snapraid_state* state, unsigned* waiting_map, uns state->tick_last = now; } -/** - * Print a repeated char. - */ -static void printc(char c, size_t pad) -{ - while (pad) { - putchar(c); - --pad; - } -} - -/** - * Print a string with space padding. - */ -static void printr(const char* str, size_t pad) -{ - size_t len; - - len = strlen(str); - - while (len < pad) { - putchar(' '); - ++len; - } - - printf("%s", str); -} - #define USAGE_COLUMN 60 void state_usage_graph(struct snapraid_state* state, struct snapraid_io* io, unsigned current, unsigned oldest) diff --git a/cmdline/support.c b/cmdline/support.c index f29646a4..18074553 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -302,6 +302,76 @@ void msg_flush(void) unlock_msg(); } +void printc(char c, size_t pad) +{ + while (pad) { + putchar(c); + --pad; + } +} + +void printr(const char* str, size_t pad) +{ + size_t len; + + len = strlen(str); + + while (len < pad) { + putchar(' '); + ++len; + } + + printf("%s", str); +} + +void printl(const char* str, size_t pad) +{ + size_t len; + + printf("%s", str); + + len = strlen(str); + + while (len < pad) { + putchar(' '); + ++len; + } +} + +void printp(double v, size_t pad) +{ + char buf[64]; + const char* s = "%"; + + if (v > 0.1) + snprintf(buf, sizeof(buf), "%5.2f%s", v, s); + else if (v > 0.01) + snprintf(buf, sizeof(buf), "%6.3f%s", v, s); + else if (v > 0.001) + snprintf(buf, sizeof(buf), "%7.4f%s", v, s); + else if (v > 0.0001) + snprintf(buf, sizeof(buf), "%8.5f%s", v, s); + else if (v > 0.00001) + snprintf(buf, sizeof(buf), "%9.6f%s", v, s); + else if (v > 0.000001) + snprintf(buf, sizeof(buf), "%10.7f%s", v, s); + else if (v > 0.0000001) + snprintf(buf, sizeof(buf), "%11.8f%s", v, s); + else if (v > 0.00000001) + snprintf(buf, sizeof(buf), "%12.9f%s", v, s); + else if (v > 0.000000001) + snprintf(buf, sizeof(buf), "%13.10f%s", v, s); + else if (v > 0.0000000001) + snprintf(buf, sizeof(buf), "%14.11f%s", v, s); + else if (v > 0.00000000001) + snprintf(buf, sizeof(buf), "%15.12f%s", v, s); + else if (v > 0.000000000001) + snprintf(buf, sizeof(buf), "%16.13f%s", v, s); + else + snprintf(buf, sizeof(buf), "%17.14f%s", v, s); + printl(buf, pad); +} + /** * Number of buffers for esc(). */ diff --git a/cmdline/support.h b/cmdline/support.h index d7162304..6a3ca997 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -186,6 +186,30 @@ void msg_verbose(const char* format, ...) __attribute__((format(attribute_printf */ void msg_flush(void); + +/****************************************************************************/ +/* print */ + +/** + * Print a repeated char. + */ +void printc(char c, size_t pad); + +/** + * Print a string with right space padding. + */ +void printr(const char* str, size_t pad); + +/** + * Print a string with left space padding. + */ +void printl(const char* str, size_t pad); + +/** + * Print a probability with space padding. + */ +void printp(double v, size_t pad); + /****************************************************************************/ /* escape */