From 93b3d1ad7e035966850841dea441d8bd91c2cc32 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 4 Mar 2009 16:16:02 +0000 Subject: [PATCH] (trunk) #1881: promote tr_strratio() to libtransmission, so that its code doesn't have to be repeated in all the clients --- daemon/remote.c | 26 +------------------------- gtk/util.c | 26 +------------------------- libtransmission/utils.c | 34 ++++++++++++++++++++++++++++++++++ libtransmission/utils.h | 9 +++++++++ 4 files changed, 45 insertions(+), 50 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index 4711c2ea2..cd9c65af5 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -658,34 +658,10 @@ etaToString( char * buf, #define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) #define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 ) -static void -printf_double_without_rounding( char * buf, int buflen, double d, int places ) -{ - char * pch; - char tmp[128]; - int len; - tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d ); - pch = strchr( tmp, '.' ); - pch += places + 1; - len = MIN( buflen - 1, pch - tmp ); - memcpy( buf, tmp, len ); - buf[len] = '\0'; -} - static char* strlratio2( char * buf, double ratio, size_t buflen ) { - if( (int)ratio == TR_RATIO_NA ) - tr_strlcpy( buf, "None", buflen ); - else if( (int)ratio == TR_RATIO_INF ) - tr_strlcpy( buf, "Inf", buflen ); - else if( ratio < 10.0 ) - printf_double_without_rounding( buf, buflen, ratio, 2 ); - else if( ratio < 100.0 ) - printf_double_without_rounding( buf, buflen, ratio, 1 ); - else - tr_snprintf( buf, buflen, "%'.0f", ratio ); - return buf; + return tr_strratio( buf, buflen, ratio, "Inf" ); } static char* diff --git a/gtk/util.c b/gtk/util.c index bca6c7434..848adb311 100644 --- a/gtk/util.c +++ b/gtk/util.c @@ -48,34 +48,10 @@ #include "tr-prefs.h" #include "util.h" -static void -printf_double_without_rounding( char * buf, int buflen, double d, int places ) -{ - char * pch; - char tmp[128]; - int len; - g_snprintf( tmp, sizeof( tmp ), "%'.64f", d ); - pch = strchr( tmp, '.' ); - pch += places + 1; - len = MIN( buflen - 1, pch - tmp ); - memcpy( buf, tmp, len ); - buf[len] = '\0'; -} - char* tr_strlratio( char * buf, double ratio, size_t buflen ) { - if( (int)ratio == TR_RATIO_NA ) - g_strlcpy( buf, _( "None" ), buflen ); - else if( (int)ratio == TR_RATIO_INF ) - g_strlcpy( buf, "\xE2\x88\x9E", buflen ); - else if( ratio < 10.0 ) - printf_double_without_rounding( buf, buflen, ratio, 2 ); - else if( ratio < 100.0 ) - printf_double_without_rounding( buf, buflen, ratio, 1 ); - else - g_snprintf( buf, buflen, "%'.0f", ratio ); - return buf; + tr_strratio( buf, buflen, ratio, "\xE2\x88\x9E" ); } #define KILOBYTE_FACTOR 1024.0 diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 2e42784bd..1ee52ded6 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1518,3 +1518,37 @@ tr_parseNumberRange( const char * str_in, int len, int * setmeCount ) *setmeCount = n; return uniq; } + +/*** +**** +***/ + +static void +printf_double_without_rounding( char * buf, int buflen, double d, int places ) +{ + char * pch; + char tmp[128]; + int len; + tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d ); + pch = strchr( tmp, '.' ); + pch += places + 1; + len = MIN( buflen - 1, pch - tmp ); + memcpy( buf, tmp, len ); + buf[len] = '\0'; +} + +char* +tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity ) +{ + if( (int)ratio == TR_RATIO_NA ) + tr_strlcpy( buf, _( "None" ), buflen ); + else if( (int)ratio == TR_RATIO_INF ) + tr_strlcpy( buf, infinity, buflen ); + else if( ratio < 10.0 ) + printf_double_without_rounding( buf, buflen, ratio, 2 ); + else if( ratio < 100.0 ) + printf_double_without_rounding( buf, buflen, ratio, 1 ); + else + tr_snprintf( buf, buflen, "%'.0f", ratio ); + return buf; +} diff --git a/libtransmission/utils.h b/libtransmission/utils.h index ddcd8c1d5..555b20b2e 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -458,6 +458,15 @@ int tr_ptr2int( void* ); void* tr_int2ptr( int ); +/** + * @param buf the buffer to write the string to + * @param buflef buf's size + * @param ratio the ratio to convert to a string + * @param the string represntation of "infinity" + */ +char* tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity ); + + #ifdef __cplusplus } #endif