Ignore:
Timestamp:
Jun 22, 2010, 4:34:16 AM (12 years ago)
Author:
charles
Message:

(trunk) add tr_formatter_size() and tr_formatter_speed() so that all the client apps don't have to reinvent the wheel

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r10814 r10818  
    15031503#endif
    15041504}
     1505
     1506/***
     1507****
     1508****
     1509****
     1510***/
     1511
     1512struct formatter_units
     1513{
     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;
     1521};
     1522
     1523static void
     1524formatter_init( struct formatter_units * units,
     1525                double kilo,
     1526                const char * b, const char * kb,
     1527                const char * mb, const char * gb )
     1528{
     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;
     1537}
     1538
     1539static char*
     1540formatter_get_size_str( const struct formatter_units * u,
     1541                        char * buf, uint64_t bytes, size_t buflen )
     1542{
     1543    int precision;
     1544    double val;
     1545    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 );
     1554    return buf;
     1555}
     1556
     1557static struct formatter_units size_units = { 0, 0, 0, NULL, NULL, NULL, NULL };
     1558
     1559void
     1560tr_formatter_size_init( double kilo, const char * b, const char * kb,
     1561                                     const char * mb, const char * gb )
     1562{
     1563    formatter_init( &size_units, kilo, b, kb, mb, gb );
     1564}
     1565
     1566char*
     1567tr_formatter_size( char * buf, uint64_t bytes, size_t buflen )
     1568{
     1569    return formatter_get_size_str( &size_units, buf, bytes, buflen );
     1570}
     1571
     1572static struct formatter_units speed_units = { 0, 0, 0, NULL, NULL, NULL, NULL };
     1573
     1574void
     1575tr_formatter_speed_init( double kilo, const char * b, const char * kb,
     1576                                      const char * mb, const char * gb )
     1577{
     1578    formatter_init( &speed_units, kilo, b, kb, mb, gb );
     1579}
     1580
     1581char*
     1582tr_formatter_speed( char * buf, uint64_t bytes_per_second, size_t buflen )
     1583{
     1584    return formatter_get_size_str( &speed_units, buf, bytes_per_second, buflen );
     1585}
Note: See TracChangeset for help on using the changeset viewer.