mirror of
https://github.com/weechat/weechat.git
synced 2026-07-02 15:53:12 +02:00
Add new plugin "relay", new function "string_format_size" in plugin API
This commit is contained in:
@@ -1244,3 +1244,39 @@ string_iconv_fprintf (FILE *file, const char *data, ...)
|
||||
if (buf2)
|
||||
free (buf2);
|
||||
}
|
||||
|
||||
/*
|
||||
* string_format_size: format a string with size and unit name (bytes, KB, MB, GB)
|
||||
* note: returned value has to be free() after use
|
||||
*/
|
||||
|
||||
char *
|
||||
string_format_size (unsigned long size)
|
||||
{
|
||||
char *unit_name[] = { N_("bytes"), N_("KB"), N_("MB"), N_("GB") };
|
||||
char *unit_format[] = { "%.0f", "%.1f", "%.02f", "%.02f" };
|
||||
float unit_divide[] = { 1, 1024, 1024*1024, 1024*1024*1024 };
|
||||
char format_size[128], str_size[128];
|
||||
int num_unit;
|
||||
|
||||
str_size[0] = '\0';
|
||||
|
||||
if (size < 1024*10)
|
||||
num_unit = 0;
|
||||
else if (size < 1024*1024)
|
||||
num_unit = 1;
|
||||
else if (size < 1024*1024*1024)
|
||||
num_unit = 2;
|
||||
else
|
||||
num_unit = 3;
|
||||
|
||||
snprintf (format_size, sizeof (format_size),
|
||||
"%s %%s",
|
||||
unit_format[num_unit]);
|
||||
snprintf (str_size, sizeof (str_size),
|
||||
format_size,
|
||||
((float)size) / ((float)(unit_divide[num_unit])),
|
||||
(size <= 1) ? _("byte") : _(unit_name[num_unit]));
|
||||
|
||||
return strdup (str_size);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user