Ignore:
Timestamp:
Jun 22, 2010, 1:34:46 PM (11 years ago)
Author:
charles
Message:

(trunk libT) add an accessor for the formatter unit strings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r10818 r10819  
    15101510***/
    15111511
     1512struct formatter_unit
     1513{
     1514    char * name;
     1515    double value;
     1516};
     1517 
    15121518struct formatter_units
    15131519{
    1514     double KB_val;
    1515     double MB_val;
    1516     double GB_val;
    1517     const char * B_str;
    1518     const char * KB_str;
    1519     const char * MB_str;
    1520     const char * GB_str;
     1520    struct formatter_unit units[4];
    15211521};
    15221522
     
    15271527                const char * mb, const char * gb )
    15281528{
    1529     units->B_str  = tr_strdup( b );
    1530     units->KB_str = tr_strdup( kb );
    1531     units->MB_str = tr_strdup( mb );
    1532     units->GB_str = tr_strdup( gb );
    1533 
    1534     units->KB_val = kilo;
    1535     units->MB_val = kilo * kilo;
    1536     units->GB_val = kilo * kilo * kilo;
     1529    units->units[TR_FMT_B].name = tr_strdup( b );
     1530    units->units[TR_FMT_B].value = 1;
     1531
     1532    units->units[TR_FMT_KB].name = tr_strdup( kb );
     1533    units->units[TR_FMT_KB].value = kilo;
     1534
     1535    units->units[TR_FMT_MB].name = tr_strdup( mb );
     1536    units->units[TR_FMT_MB].value = pow( kilo, 2 );
     1537
     1538    units->units[TR_FMT_GB].name = tr_strdup( gb );
     1539    units->units[TR_FMT_GB].value = pow( kilo, 3 );
     1540}
     1541
     1542static const char*
     1543tr_formatter_units( const struct formatter_units * u, int size )
     1544{
     1545    assert( u != NULL );
     1546    assert( 0<=size && size<4 );
     1547
     1548    return u->units[size].name;
    15371549}
    15381550
     
    15421554{
    15431555    int precision;
    1544     double val;
     1556    double value;
    15451557    const char * units;
    1546 
    1547          if( bytes < u->KB_val ) { val = bytes;             units = u->B_str; }
    1548     else if( bytes < u->MB_val ) { val = bytes / u->KB_val; units = u->KB_str; }
    1549     else if( bytes < u->GB_val ) { val = bytes / u->MB_val; units = u->MB_str; }
    1550     else                         { val = bytes / u->GB_val; units = u->GB_str; }
    1551 
    1552     precision = val < 100 ? 2 : 1;
    1553     tr_snprintf( buf, buflen, "%.*f %s", precision, val, units );
     1558    const struct formatter_unit * unit;
     1559
     1560         if( bytes < u->units[1].value ) unit = &u->units[0];
     1561    else if( bytes < u->units[2].value ) unit = &u->units[1];
     1562    else if( bytes < u->units[3].value ) unit = &u->units[2];
     1563    else                                 unit = &u->units[3];
     1564
     1565    value = bytes / unit->value;
     1566    units = unit->name;
     1567    precision = value < 100 ? 2 : 1;
     1568    tr_snprintf( buf, buflen, "%.*f %s", precision, value, units );
    15541569    return buf;
    15551570}
    15561571
    1557 static struct formatter_units size_units = { 0, 0, 0, NULL, NULL, NULL, NULL };
     1572static struct formatter_units size_units;
    15581573
    15591574void
     
    15701585}
    15711586
    1572 static struct formatter_units speed_units = { 0, 0, 0, NULL, NULL, NULL, NULL };
     1587const char*
     1588tr_formatter_size_units( int i )
     1589{
     1590    return tr_formatter_units( &size_units, i );
     1591}
     1592
     1593static struct formatter_units speed_units;
    15731594
    15741595void
     
    15841605    return formatter_get_size_str( &speed_units, buf, bytes_per_second, buflen );
    15851606}
     1607
     1608const char*
     1609tr_formatter_speed_units( int i )
     1610{
     1611    return tr_formatter_units( &speed_units, i );
     1612}
Note: See TracChangeset for help on using the changeset viewer.