Changeset 8017


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

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/remote.c

    r8016 r8017  
    659659#define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 )
    660660
    661 static void
    662 printf_double_without_rounding( char * buf, int buflen, double d, int places )
    663 {
    664     char * pch;
    665     char tmp[128];
    666     int len;
    667     tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d );
    668     pch = strchr( tmp, '.' );
    669     pch += places + 1;
    670     len = MIN( buflen - 1, pch - tmp );
    671     memcpy( buf, tmp, len );
    672     buf[len] = '\0';
    673 }
    674 
    675661static char*
    676662strlratio2( char * buf, double ratio, size_t buflen )
    677663{
    678     if( (int)ratio == TR_RATIO_NA )
    679         tr_strlcpy( buf, "None", buflen );
    680     else if( (int)ratio == TR_RATIO_INF )
    681         tr_strlcpy( buf, "Inf", buflen );
    682     else if( ratio < 10.0 )
    683         printf_double_without_rounding( buf, buflen, ratio, 2 );
    684     else if( ratio < 100.0 )
    685         printf_double_without_rounding( buf, buflen, ratio, 1 );
    686     else
    687         tr_snprintf( buf, buflen, "%'.0f", ratio );
    688     return buf;
     664    return tr_strratio( buf, buflen, ratio, "Inf" );
    689665}
    690666
  • trunk/gtk/util.c

    r8015 r8017  
    4949#include "util.h"
    5050
    51 static void
    52 printf_double_without_rounding( char * buf, int buflen, double d, int places )
    53 {
    54     char * pch;
    55     char tmp[128];
    56     int len;
    57     g_snprintf( tmp, sizeof( tmp ), "%'.64f", d );
    58     pch = strchr( tmp, '.' );
    59     pch += places + 1;
    60     len = MIN( buflen - 1, pch - tmp );
    61     memcpy( buf, tmp, len );
    62     buf[len] = '\0';
    63 }
    64 
    6551char*
    6652tr_strlratio( char * buf, double ratio, size_t buflen )
    6753{
    68     if( (int)ratio == TR_RATIO_NA )
    69         g_strlcpy( buf, _( "None" ), buflen );
    70     else if( (int)ratio == TR_RATIO_INF )
    71         g_strlcpy( buf, "\xE2\x88\x9E", buflen );
    72     else if( ratio < 10.0 )
    73         printf_double_without_rounding( buf, buflen, ratio, 2 );
    74     else if( ratio < 100.0 )
    75         printf_double_without_rounding( buf, buflen, ratio, 1 );
    76     else
    77         g_snprintf( buf, buflen, "%'.0f", ratio );
    78     return buf;
     54    tr_strratio( buf, buflen, ratio, "\xE2\x88\x9E" );
    7955}
    8056
  • 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}
  • trunk/libtransmission/utils.h

    r7854 r8017  
    459459void* tr_int2ptr( int );
    460460
     461/**
     462 * @param buf the buffer to write the string to
     463 * @param buflef buf's size
     464 * @param ratio the ratio to convert to a string
     465 * @param the string represntation of "infinity"
     466 */
     467char* tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity );
     468
     469
    461470#ifdef __cplusplus
    462471}
Note: See TracChangeset for help on using the changeset viewer.