Commit 582f4485 authored by rswindell's avatar rswindell
Browse files

Introduce duration_to_str() and byte_count_to_str() to genwrap.c (the inverse

of parse_byte_count() and parse_duration()) and use them in ini_file.c
parent 99b1ea4a
......@@ -43,6 +43,7 @@
#include <errno.h> /* errno */
#include <ctype.h> /* toupper/tolower */
#include <limits.h> /* CHAR_BIT */
#include <math.h> /* fmod */
#if defined(__unix__)
#include <sys/ioctl.h> /* ioctl() */
......@@ -231,6 +232,26 @@ int64_t DLLCALL parse_byte_count(const char* str, ulong unit)
return((int64_t)(unit>1 ? (bytes/unit):bytes));
}
/* Convert a byte count to a string with a floating point value
and a single letter multiplier/suffix.
For values evenly divisible by 1024, no suffix otherwise.
*/
char* DLLCALL byte_count_to_str(int64_t bytes, char* str, size_t size)
{
if(fmod((double)bytes,1024.0*1024.0*1024.0*1024.0)==0)
safe_snprintf(str, size, "%gT",bytes/(1024.0*1024.0*1024.0*1024.0));
else if(fmod((double)bytes,1024.0*1024.0*1024.0)==0)
safe_snprintf(str, size, "%gG",bytes/(1024.0*1024.0*1024.0));
else if(fmod((double)bytes,1024.0*1024.0)==0)
safe_snprintf(str, size, "%gM",bytes/(1024.0*1024.0));
else if(fmod((double)bytes,1024.0)==0)
safe_snprintf(str, size, "%gK",bytes/1024.0);
else
safe_snprintf(str, size, "%"PRIi64, bytes);
return str;
}
/* Parse a duration string, default unit is in seconds */
/* (Y)ears, (W)eeks, (D)ays, (H)ours, and (M)inutes */
/* suffixes/multipliers are supported.
......@@ -253,6 +274,28 @@ double DLLCALL parse_duration(const char* str)
return t;
}
/* Convert a duration (in seconds) to a string
* with a single letter multiplier/suffix:
* (Y)ears, (W)eeks, (D)ays, (H)ours, and (M)inutes
*/
char* DLLCALL duration_to_str(double value, char* str, size_t size)
{
if(fmod(value,365.0*24.0*60.0*60.0)==0)
safe_snprintf(str, size, "%gY",value/(365.0*24.0*60.0*60.0));
else if(fmod(value,7.0*24.0*60.0*60.0)==0)
safe_snprintf(str, size, "%gW",value/(7.0*24.0*60.0*60.0));
else if(fmod(value,24.0*60.0*60.0)==0)
safe_snprintf(str, size, "%gD",value/(24.0*60.0*60.0));
else if(fmod(value,60.0*60.0)==0)
safe_snprintf(str, size, "%gH",value/(60.0*60.0));
else if(fmod(value,60.0)==0)
safe_snprintf(str, size, "%gM",value/60.0);
else
safe_snprintf(str, size, "%gS",value);
return str;
}
/****************************************************************************/
/* Convert ASCIIZ string to upper case */
/****************************************************************************/
......
......@@ -370,6 +370,8 @@ DLLEXPORT char DLLCALL c_unescape_char(char ch);
/* Power-of-2 byte count string parser (e.g. "100K" returns 102400 if unit is 1) */
DLLEXPORT int64_t DLLCALL parse_byte_count(const char*, ulong unit);
DLLEXPORT double DLLCALL parse_duration(const char*);
DLLEXPORT char* DLLCALL duration_to_str(double value, char* str, size_t size);
DLLEXPORT char* DLLCALL byte_count_to_str(int64_t bytes, char* str, size_t size);
/* Microsoft (e.g. DOS/Win32) real-time system clock API (ticks since process started) */
typedef clock_t msclock_t;
......
......@@ -549,7 +549,6 @@ char* DLLCALL iniSetBytes(str_list_t* list, const char* section, const char* key
,int64_t value, ini_style_t* style)
{
char str[INI_MAX_VALUE_LEN];
double bytes;
if(value==0)
SAFECOPY(str,"0");
......@@ -567,18 +566,7 @@ char* DLLCALL iniSetBytes(str_list_t* list, const char* section, const char* key
default:
if(unit<1)
unit=1;
bytes=(double)(value*unit);
if(fmod(bytes,1024.0*1024.0*1024.0*1024.0)==0)
SAFEPRINTF(str,"%gT",bytes/(1024.0*1024.0*1024.0*1024.0));
else if(fmod(bytes,1024*1024*1024)==0)
SAFEPRINTF(str,"%gG",bytes/(1024*1024*1024));
else if(fmod(bytes,1024*1024)==0)
SAFEPRINTF(str,"%gM",bytes/(1024*1024));
else if(fmod(bytes,1024)==0)
SAFEPRINTF(str,"%gK",bytes/1024);
else
SAFEPRINTF(str,"%"PRIi64, (int64_t)bytes);
byte_count_to_str(value*unit, str, sizeof(str));
}
return iniSetString(list, section, key, str, style);
......@@ -589,20 +577,7 @@ char* DLLCALL iniSetDuration(str_list_t* list, const char* section, const char*
{
char str[INI_MAX_VALUE_LEN];
if(fmod(value,365.0*24.0*60.0*60.0)==0)
SAFEPRINTF(str,"%gY",value/(365.0*24.0*60.0*60.0));
else if(fmod(value,7.0*24.0*60.0*60.0)==0)
SAFEPRINTF(str,"%gW",value/(7.0*24.0*60.0*60.0));
else if(fmod(value,24.0*60.0*60.0)==0)
SAFEPRINTF(str,"%gD",value/(24.0*60.0*60.0));
else if(fmod(value,60.0*60.0)==0)
SAFEPRINTF(str,"%gH",value/(60.0*60.0));
else if(fmod(value,60.0)==0)
SAFEPRINTF(str,"%gM",value/60.0);
else
SAFEPRINTF(str,"%gS",value);
return iniSetString(list, section, key, str, style);
return iniSetString(list, section, key, duration_to_str(value, str, sizeof(str)), style);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment