Add argument as_bits to format_byte_humanreadable function

master
Mike Massonnet 2014-11-15 23:27:36 +01:00
parent 65df9dacc8
commit 2e2d905b28
4 changed files with 27 additions and 15 deletions

View File

@ -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);

View File

@ -27,6 +27,7 @@
#include "utils.h"
#include "global.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <libxfce4util/libxfce4util.h>
@ -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));

View File

@ -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;
}

View File

@ -19,6 +19,8 @@
#ifndef UTILS_H
#define UTILS_H
#include <glib.h>
/**
* 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 <code>null</code>
*/
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.