1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00

(trunk) #1881: promote tr_strratio() to libtransmission, so that its code doesn't have to be repeated in all the clients

This commit is contained in:
Charles Kerr 2009-03-04 16:16:02 +00:00
parent c30983ff96
commit 93b3d1ad7e
4 changed files with 45 additions and 50 deletions

View file

@ -658,34 +658,10 @@ etaToString( char * buf,
#define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) #define MEGABYTE_FACTOR ( 1024.0 * 1024.0 )
#define GIGABYTE_FACTOR ( 1024.0 * 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* static char*
strlratio2( char * buf, double ratio, size_t buflen ) strlratio2( char * buf, double ratio, size_t buflen )
{ {
if( (int)ratio == TR_RATIO_NA ) return tr_strratio( buf, buflen, ratio, "Inf" );
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;
} }
static char* static char*

View file

@ -48,34 +48,10 @@
#include "tr-prefs.h" #include "tr-prefs.h"
#include "util.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* char*
tr_strlratio( char * buf, double ratio, size_t buflen ) tr_strlratio( char * buf, double ratio, size_t buflen )
{ {
if( (int)ratio == TR_RATIO_NA ) tr_strratio( buf, buflen, ratio, "\xE2\x88\x9E" );
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;
} }
#define KILOBYTE_FACTOR 1024.0 #define KILOBYTE_FACTOR 1024.0

View file

@ -1518,3 +1518,37 @@ tr_parseNumberRange( const char * str_in, int len, int * setmeCount )
*setmeCount = n; *setmeCount = n;
return uniq; 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;
}

View file

@ -458,6 +458,15 @@ int tr_ptr2int( void* );
void* tr_int2ptr( int ); 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 #ifdef __cplusplus
} }
#endif #endif