Ignore:
Timestamp:
Mar 4, 2009, 4:16:02 PM (13 years ago)
Author:
charles
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r7901 r8017  
    15191519    return uniq;
    15201520}
     1521
     1522/***
     1523****
     1524***/
     1525
     1526static void
     1527printf_double_without_rounding( char * buf, int buflen, double d, int places )
     1528{
     1529    char * pch;
     1530    char tmp[128];
     1531    int len;
     1532    tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d );
     1533    pch = strchr( tmp, '.' );
     1534    pch += places + 1;
     1535    len = MIN( buflen - 1, pch - tmp );
     1536    memcpy( buf, tmp, len );
     1537    buf[len] = '\0';
     1538}
     1539
     1540char*
     1541tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity )
     1542{
     1543    if( (int)ratio == TR_RATIO_NA )
     1544        tr_strlcpy( buf, _( "None" ), buflen );
     1545    else if( (int)ratio == TR_RATIO_INF )
     1546        tr_strlcpy( buf, infinity, buflen );
     1547    else if( ratio < 10.0 )
     1548        printf_double_without_rounding( buf, buflen, ratio, 2 );
     1549    else if( ratio < 100.0 )
     1550        printf_double_without_rounding( buf, buflen, ratio, 1 );
     1551    else
     1552        tr_snprintf( buf, buflen, "%'.0f", ratio );
     1553    return buf;
     1554}
Note: See TracChangeset for help on using the changeset viewer.