From 2e2d905b28d31c6065183ee7a52d238f0be410f5 Mon Sep 17 00:00:00 2001 From: Mike Massonnet Date: Sat, 15 Nov 2014 23:27:36 +0100 Subject: [PATCH] Add argument as_bits to format_byte_humanreadable function --- panel-plugin/commandline.c | 6 +++--- panel-plugin/netload.c | 7 ++++--- panel-plugin/utils.c | 25 +++++++++++++++++-------- panel-plugin/utils.h | 4 +++- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/panel-plugin/commandline.c b/panel-plugin/commandline.c index 5624845..8dfb4fb 100644 --- a/panel-plugin/commandline.c +++ b/panel-plugin/commandline.c @@ -73,9 +73,9 @@ int main(int argc, char* argv[]) for (;;) { get_current_netload(&data, &in, &out, &tot); - format_byte_humanreadable(bufIn, 20, (double)in, 0); - format_byte_humanreadable(bufOut, 20, (double)out, 0); - format_byte_humanreadable(bufTot, 20, (double)tot, 0); + format_byte_humanreadable(bufIn, 20, (double)in, 0, FALSE); + format_byte_humanreadable(bufOut, 20, (double)out, 0, FALSE); + format_byte_humanreadable(bufTot, 20, (double)tot, 0, FALSE); printf("Current netload:\nIN : %s\nOUT: %s\nTOT: %s\nIP: %s\n", bufIn, bufOut, bufTot, get_ip_address(&data)); sleep(1); diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index 79d6bdb..990a75d 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -27,6 +27,7 @@ #include "utils.h" #include "global.h" +#include #include #include @@ -240,11 +241,11 @@ static gboolean update_monitors(t_global_monitor *global) if (global->monitor->options.show_bars) gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(global->monitor->status[i]), temp); - format_byte_humanreadable( buffer[i], BUFSIZ - 1, display[i], 2 ); - format_byte_humanreadable( buffer_panel[i], BUFSIZ - 1, display[i], 2 ); + format_byte_humanreadable( buffer[i], BUFSIZ - 1, display[i], 2, FALSE ); + format_byte_humanreadable( buffer_panel[i], BUFSIZ - 1, display[i], 2, FALSE ); } - format_byte_humanreadable( buffer[TOT], BUFSIZ - 1, (display[IN]+display[OUT]), 2 ); + format_byte_humanreadable( buffer[TOT], BUFSIZ - 1, (display[IN]+display[OUT]), 2, FALSE ); { char* ip = get_ip_address(&(global->monitor->data)); diff --git a/panel-plugin/utils.c b/panel-plugin/utils.c index c2824ba..c262f87 100644 --- a/panel-plugin/utils.c +++ b/panel-plugin/utils.c @@ -70,19 +70,28 @@ unsigned long max_array( unsigned long array[], int size ) /* ---------------------------------------------------------------------------------------------- */ -char* format_byte_humanreadable(char* string, int stringsize, double number, int digits) +char* format_byte_humanreadable(char* string, int stringsize, double number, int digits, gboolean as_bits) { char* str = string; char buffer[BUFSIZ], formatstring[BUFSIZ]; char* bufptr = buffer; char* unit_names[] = { N_("B"), N_("KiB"), N_("MiB"), N_("GiB") }; + char* unit_names_bits[] = { N_("bps"), N_("Kbps"), N_("Mbps"), N_("Gbps") }; unsigned int uidx = 0; - double number_displayed = number / 1024.0; + double number_displayed = 0; + double thousand_divider = as_bits ? 1000 : 1024; unsigned int i; int numberOfIntegerChars, count; struct lconv* localeinfo = localeconv(); int grouping = (int)localeinfo->grouping[0] == 0 ? INT_MAX : (int)localeinfo->grouping[0]; + /* Start with kilo and adapt to bits values*/ + uidx = 1; + number_displayed = number / thousand_divider; + if (as_bits) + { + number_displayed *= 8; + } /* sensible value for digits */ if (digits < 0 || digits >= 10) @@ -90,16 +99,16 @@ char* format_byte_humanreadable(char* string, int stringsize, double number, int digits = 2; } - /* no digits for values under MiB/s unit size */ - if (number < 1024.0 * 1024.0) + /* 1 digit for values above MiB/s unit size */ + if (digits > 1 && number_displayed > thousand_divider * thousand_divider) { - digits = 0; + digits = 1; } /* calculate number and appropriate unit size for display */ - while(number_displayed >= 1024.0 && uidx < (sizeof(unit_names) / sizeof(char*) - 1)) + while(number_displayed >= thousand_divider && uidx < (sizeof(unit_names) / sizeof(char*) - 1)) { - number_displayed /= 1024.0; + number_displayed /= thousand_divider; uidx++; } @@ -148,7 +157,7 @@ char* format_byte_humanreadable(char* string, int stringsize, double number, int *str = 0; /* Add the unit name */ - g_strlcat(string, _(unit_names[uidx]), stringsize); + g_strlcat(string, as_bits ? _(unit_names_bits[uidx]) : _(unit_names[uidx]), stringsize); return string; } diff --git a/panel-plugin/utils.h b/panel-plugin/utils.h index 1da6a9b..749de87 100644 --- a/panel-plugin/utils.h +++ b/panel-plugin/utils.h @@ -19,6 +19,8 @@ #ifndef UTILS_H #define UTILS_H +#include + /** * Formats the number into a number of the appropriate byte unit with * a thousands separator, respecting the current locale. It appends @@ -32,7 +34,7 @@ * @param digits the number of digits after the decimal point * @return the string to allow concatening buffers or null */ -char* format_byte_humanreadable( char* string, int stringsize, double number, int digits ); +char* format_byte_humanreadable( char* string, int stringsize, double number, int digits, gboolean as_bits ); /** * Returns the minimum of the array. The array must contain at least one element.