From 1d63aa4cda6439f686a6cf58dd99df0b43423dcc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 22 Jun 2010 04:34:16 +0000 Subject: [PATCH] (trunk) add tr_formatter_size() and tr_formatter_speed() so that all the client apps don't have to reinvent the wheel --- daemon/remote.c | 76 +++++++++++++++++++------------------- gtk/main.c | 2 + gtk/util.c | 27 ++++---------- libtransmission/utils.c | 81 +++++++++++++++++++++++++++++++++++++++++ libtransmission/utils.h | 18 +++++++++ qt/app.cc | 13 +++++++ qt/utils.cc | 70 +++++++++-------------------------- qt/utils.h | 2 +- utils/show.c | 41 +++------------------ 9 files changed, 184 insertions(+), 146 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index a320d5351..475a8c1cf 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -135,31 +135,24 @@ strlratio( char * buf, double numerator, double denominator, size_t buflen ) } static char* -strlsize( char * buf, int64_t size, size_t buflen ) +strlsize( char * buf, int64_t bytes, size_t buflen ) { - if( !size ) - tr_strlcpy( buf, "None", buflen ); - else if( size < (int64_t)KiB ) - tr_snprintf( buf, buflen, "%'" PRId64 " bytes", (int64_t)size ); + if( !bytes ) + tr_strlcpy( buf, _( "None" ), buflen ); else - { - double displayed_size; - if( size < (int64_t)MiB ) - { - displayed_size = (double) size / KiB; - tr_snprintf( buf, buflen, "%'.1f KiB", displayed_size ); - } - else if( size < (int64_t)GiB ) - { - displayed_size = (double) size / MiB; - tr_snprintf( buf, buflen, "%'.1f MiB", displayed_size ); - } - else - { - displayed_size = (double) size / GiB; - tr_snprintf( buf, buflen, "%'.1f GiB", displayed_size ); - } - } + tr_formatter_size( buf, bytes, buflen ); + + return buf; +} + +static char* +strlspeed( char * buf, int64_t bytes_per_second, size_t buflen ) +{ + if( !bytes_per_second ) + tr_strlcpy( buf, _( "None" ), buflen ); + else + tr_formatter_speed( buf, bytes_per_second, buflen ); + return buf; } @@ -799,9 +792,9 @@ printDetails( tr_benc * top ) if( tr_bencDictFindInt( t, "eta", &i ) ) printf( " ETA: %s\n", tr_strltime( buf, i, sizeof( buf ) ) ); if( tr_bencDictFindInt( t, "rateDownload", &i ) ) - printf( " Download Speed: %.1f KiB/s\n", i / 1024.0 ); + printf( " Download Speed: %s\n", strlspeed( buf, i, sizeof( buf ) ) ); if( tr_bencDictFindInt( t, "rateUpload", &i ) ) - printf( " Upload Speed: %.1f KiB/s\n", i / 1024.0 ); + printf( " Upload Speed: %s\n", strlspeed( buf, i, sizeof( buf ) ) ); if( tr_bencDictFindInt( t, "haveUnchecked", &i ) && tr_bencDictFindInt( t, "haveValid", &j ) ) { @@ -1073,7 +1066,7 @@ printDetails( tr_benc * top ) { printf( " Download Limit: " ); if( boolVal ) - printf( "%" PRId64 " KiB/s\n", i ); + printf( "%s\n", strlspeed( buf, i*1024, sizeof( buf ) ) ); else printf( "Unlimited\n" ); } @@ -1082,7 +1075,7 @@ printDetails( tr_benc * top ) { printf( " Upload Limit: " ); if( boolVal ) - printf( "%" PRId64 " KiB/s\n", i ); + printf( "%s\n", strlspeed( buf, i*1024, sizeof( buf ) ) ); else printf( "Unlimited\n" ); } @@ -1388,6 +1381,8 @@ printSession( tr_benc * top ) tr_bencDictFindBool( args, "seedRatioLimited", &seedRatioLimited) ) { char buf[128]; + char buf2[128]; + char buf3[128]; printf( "LIMITS\n" ); printf( " Peer limit: %" PRId64 "\n", peerLimit ); @@ -1399,26 +1394,30 @@ printSession( tr_benc * top ) printf( " Default seed ratio limit: %s\n", buf ); if( altEnabled ) - tr_snprintf( buf, sizeof( buf ), "%"PRId64" KiB/s", altUp ); + strlspeed( buf, altUp*1024, sizeof( buf ) ); else if( upEnabled ) - tr_snprintf( buf, sizeof( buf ), "%"PRId64" KiB/s", upLimit ); + strlspeed( buf, upLimit*1024, sizeof( buf ) ); else tr_strlcpy( buf, "Unlimited", sizeof( buf ) ); - printf( " Upload speed limit: %s (%s limit: %"PRId64" KiB/s; %s turtle limit: %"PRId64" KiB/s)\n", + printf( " Upload speed limit: %s (%s limit: %s; %s turtle limit: %s)\n", buf, - (upEnabled?"Enabled":"Disabled"), upLimit, - (altEnabled?"Enabled":"Disabled"), altUp ); + upEnabled ? "Enabled" : "Disabled", + strlspeed( buf2, upLimit*1024, sizeof( buf2 ) ), + altEnabled ? "Enabled" : "Disabled", + strlspeed( buf3, altUp*1024, sizeof( buf3 ) ) ); if( altEnabled ) - tr_snprintf( buf, sizeof( buf ), "%"PRId64" KiB/s", altDown ); + strlspeed( buf, altDown*1024, sizeof( buf ) ); else if( downEnabled ) - tr_snprintf( buf, sizeof( buf ), "%"PRId64" KiB/s", downLimit ); + strlspeed( buf, downLimit*1024, sizeof( buf ) ); else tr_strlcpy( buf, "Unlimited", sizeof( buf ) ); - printf( " Download speed limit: %s (%s limit: %"PRId64" KiB/s; %s turtle limit: %"PRId64" KiB/s)\n", + printf( " Download speed limit: %s (%s limit: %s; %s turtle limit: %s)\n", buf, - (downEnabled?"Enabled":"Disabled"), downLimit, - (altEnabled?"Enabled":"Disabled"), altDown ); + downEnabled ? "Enabled" : "Disabled", + strlspeed( buf2, downLimit*1024, sizeof( buf2 ) ), + altEnabled ? "Enabled" : "Disabled", + strlspeed( buf2, altDown*1024, sizeof( buf2 ) ) ); if( altTimeEnabled ) { printf( " Turtle schedule: %02d:%02d - %02d:%02d ", @@ -2156,6 +2155,9 @@ main( int argc, char ** argv ) return EXIT_FAILURE; } + tr_formatter_size_init ( 1024, _("B"), _("KiB"), _("MiB"), _("GiB") ); + tr_formatter_speed_init ( 1024, _("B/s"), _("KiB/s"), _("MiB/s"), _("GiB/s") ); + getHostAndPort( &argc, argv, &host, &port ); if( host == NULL ) host = tr_strdup( DEFAULT_HOST ); diff --git a/gtk/main.c b/gtk/main.c index f3adf3ca6..183fa830c 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -532,6 +532,8 @@ main( int argc, char ** argv ) bind_textdomain_codeset( domain, "UTF-8" ); textdomain( domain ); g_set_application_name( _( "Transmission" ) ); + tr_formatter_size_init( 1024, _("B"), _("KiB"), _("MiB"), _("GiB") ); + tr_formatter_speed_init( 1024, _("B/s"), _("KiB/s"), _("MiB/s"), _("GiB/s") ); /* initialize gtk */ if( !g_thread_supported( ) ) diff --git a/gtk/util.c b/gtk/util.c index 48cb1aaeb..957bdb40b 100644 --- a/gtk/util.c +++ b/gtk/util.c @@ -105,39 +105,26 @@ tr_strlratio( char * buf, double ratio, size_t buflen ) return tr_strratio( buf, buflen, ratio, gtr_get_unicode_string( GTR_UNICODE_INF ) ); } -static double KiB = 1024.0; -static double MiB = ( 1024.0 * 1024.0 ); -static double GiB = ( 1024.0 * 1024.0 * 1024.0 ); - char* tr_strlsize( char * buf, guint64 bytes, size_t buflen ) { if( !bytes ) g_strlcpy( buf, _( "None" ), buflen ); - else if( bytes < KiB ) - g_snprintf( buf, buflen, ngettext( "%'u byte", "%'u bytes", (guint)bytes ), (guint)bytes ); - else if( bytes < MiB ) - g_snprintf( buf, buflen, _( "%'.1f KiB" ), bytes / KiB ); - else if( bytes < GiB ) - g_snprintf( buf, buflen, _( "%'.1f MiB" ), bytes / MiB ); else - g_snprintf( buf, buflen, _( "%'.1f GiB" ), bytes / GiB ); + tr_formatter_size( buf, bytes, buflen ); + return buf; } char* tr_strlspeed( char * buf, double kb_sec, size_t buflen ) { - const double speed = kb_sec; + const int64_t bytes_per_second = kb_sec * 1024.0; - if( speed < 1000.0 ) /* 0.0 KiB to 999.9 KiB */ - g_snprintf( buf, buflen, _( "%'.1f KiB/s" ), speed ); - else if( speed < 102400.0 ) /* 0.98 MiB to 99.99 MiB */ - g_snprintf( buf, buflen, _( "%'.2f MiB/s" ), ( speed / KiB ) ); - else if( speed < 1024000.0 ) /* 100.0 MiB to 999.9 MiB */ - g_snprintf( buf, buflen, _( "%'.1f MiB/s" ), ( speed / MiB ) ); - else /* insane speeds */ - g_snprintf( buf, buflen, _( "%'.2f GiB/s" ), ( speed / GiB ) ); + if( bytes_per_second < 1 ) + g_strlcpy( buf, _( "None" ), buflen ); + else + tr_formatter_speed( buf, bytes_per_second, buflen ); return buf; } diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 8b0ce35c6..5b55cebc9 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1502,3 +1502,84 @@ tr_realpath( const char * path, char * resolved_path ) return realpath( path, resolved_path ); #endif } + +/*** +**** +**** +**** +***/ + +struct formatter_units +{ + double KB_val; + double MB_val; + double GB_val; + const char * B_str; + const char * KB_str; + const char * MB_str; + const char * GB_str; +}; + +static void +formatter_init( struct formatter_units * units, + double kilo, + const char * b, const char * kb, + const char * mb, const char * gb ) +{ + units->B_str = tr_strdup( b ); + units->KB_str = tr_strdup( kb ); + units->MB_str = tr_strdup( mb ); + units->GB_str = tr_strdup( gb ); + + units->KB_val = kilo; + units->MB_val = kilo * kilo; + units->GB_val = kilo * kilo * kilo; +} + +static char* +formatter_get_size_str( const struct formatter_units * u, + char * buf, uint64_t bytes, size_t buflen ) +{ + int precision; + double val; + const char * units; + + if( bytes < u->KB_val ) { val = bytes; units = u->B_str; } + else if( bytes < u->MB_val ) { val = bytes / u->KB_val; units = u->KB_str; } + else if( bytes < u->GB_val ) { val = bytes / u->MB_val; units = u->MB_str; } + else { val = bytes / u->GB_val; units = u->GB_str; } + + precision = val < 100 ? 2 : 1; + tr_snprintf( buf, buflen, "%.*f %s", precision, val, units ); + return buf; +} + +static struct formatter_units size_units = { 0, 0, 0, NULL, NULL, NULL, NULL }; + +void +tr_formatter_size_init( double kilo, const char * b, const char * kb, + const char * mb, const char * gb ) +{ + formatter_init( &size_units, kilo, b, kb, mb, gb ); +} + +char* +tr_formatter_size( char * buf, uint64_t bytes, size_t buflen ) +{ + return formatter_get_size_str( &size_units, buf, bytes, buflen ); +} + +static struct formatter_units speed_units = { 0, 0, 0, NULL, NULL, NULL, NULL }; + +void +tr_formatter_speed_init( double kilo, const char * b, const char * kb, + const char * mb, const char * gb ) +{ + formatter_init( &speed_units, kilo, b, kb, mb, gb ); +} + +char* +tr_formatter_speed( char * buf, uint64_t bytes_per_second, size_t buflen ) +{ + return formatter_get_size_str( &speed_units, buf, bytes_per_second, buflen ); +} diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 6385353d0..faa68728f 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -555,6 +555,24 @@ char* tr_realpath( const char *path, char * resolved_path ); **** ***/ +/* example: tr_formatter_size_init( 1024, _("B"), _("KiB"), _("MiB"), _("GiB") ); */ + +void tr_formatter_size_init( double kilo, const char * b, const char * kb, + const char * mb, const char * gb ); + +void tr_formatter_speed_init( double kilo, const char * b, const char * kb, + const char * mb, const char * gb ); + +/* format a size into a user-readable string. */ +char* tr_formatter_size( char * buf, uint64_t bytes, size_t buflen ); + +/* format a speed into a user-readable string. */ +char* tr_formatter_speed( char * buf, uint64_t bytes_per_second, size_t buflen ); + +/*** +**** +***/ + #ifdef __cplusplus } #endif diff --git a/qt/app.cc b/qt/app.cc index 1d8966ce9..3b244beb2 100644 --- a/qt/app.cc +++ b/qt/app.cc @@ -26,6 +26,7 @@ #include #include +#include #include #include "app.h" @@ -97,6 +98,18 @@ MyApp :: MyApp( int& argc, char ** argv ): t->load( QString(MY_NAME) + "_" + QLocale::system().name() ); installTranslator( t ); + // initialize the units formatter + + tr_formatter_size_init ( 1024, qPrintable(tr("B")), + qPrintable(tr("KiB")), + qPrintable(tr("MiB")), + qPrintable(tr("GiB")) ); + + tr_formatter_speed_init( 1024, qPrintable(tr("B/s")), + qPrintable(tr("KiB/s")), + qPrintable(tr("MiB/s")), + qPrintable(tr("GiB/s")) ); + // set the default icon QIcon icon; icon.addPixmap( QPixmap( ":/icons/transmission-16.png" ) ); diff --git a/qt/utils.cc b/qt/utils.cc index dfa0a0c37..d03a3b554 100644 --- a/qt/utils.cc +++ b/qt/utils.cc @@ -23,7 +23,7 @@ #include #include -#include +#include // tr_formatter #include "qticonloader.h" #include "utils.h" @@ -46,46 +46,28 @@ Utils :: remoteFileChooser( QWidget * parent, const QString& title, const QStrin return path; } -#define KILOBYTE_FACTOR 1024.0 -#define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) -#define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 ) - QString -Utils :: sizeToString( double size ) +Utils :: sizeToString( double bytes ) { - QString str; - - if( !size ) - { - str = tr( "None" ); + if( !bytes ) + return tr( "None" ); + else { + char buf[128]; + tr_formatter_size( buf, bytes, sizeof( buf ) ); + return buf; } - else if( size < KILOBYTE_FACTOR ) - { - const int i = (int)size; - str = tr( "%Ln byte(s)", 0, i ); - } - else - { - double displayed_size; +} - if( size < (int64_t)MEGABYTE_FACTOR ) - { - displayed_size = (double)size / KILOBYTE_FACTOR; - str = tr( "%L1 KiB" ).arg( displayed_size, 0, 'f', 1 ); - } - else if( size < (int64_t)GIGABYTE_FACTOR ) - { - displayed_size = (double)size / MEGABYTE_FACTOR; - str = tr( "%L1 MiB" ).arg( displayed_size, 0, 'f', 1 ); - } - else - { - displayed_size = (double) size / GIGABYTE_FACTOR; - str = tr( "%L1 GiB" ).arg( displayed_size, 0, 'f', 1 ); - } +QString +Utils :: speedToString( const Speed& speed ) +{ + if( speed.isZero( ) ) + return tr( "None" ); + else { + char buf[128]; + tr_formatter_speed( buf, speed.bps( ), sizeof( buf ) ); + return buf; } - - return str; } QString @@ -167,22 +149,6 @@ Utils :: timeToString( int seconds ) return str; } -QString -Utils :: speedToString( const Speed& speed ) -{ - const double kbps( speed.kbps( ) ); - QString str; - - if( kbps < 1000.0 ) /* 0.0 KiB to 999.9 KiB */ - str = tr( "%L1 KiB/s" ).arg( kbps, 0, 'f', 1 ); - else if( kbps < 102400.0 ) /* 0.98 MiB to 99.99 MiB */ - str = tr( "%L1 MiB/s" ).arg( kbps / KILOBYTE_FACTOR, 0, 'f', 2 ); - else // insane speeds - str = tr( "%L1 GiB/s" ).arg( kbps / MEGABYTE_FACTOR, 0, 'f', 1 ); - - return str; -} - void Utils :: toStderr( const QString& str ) { diff --git a/qt/utils.h b/qt/utils.h index 0dc85c2b3..5338ac6ce 100644 --- a/qt/utils.h +++ b/qt/utils.h @@ -28,7 +28,7 @@ class Utils: public QObject virtual ~Utils( ) { } public: static QString remoteFileChooser( QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local ); - static QString sizeToString( double size ); + static QString sizeToString( double bytes ); static QString speedToString( const Speed& speed ); static QString ratioToString( double ratio ); static QString timeToString( int seconds ); diff --git a/utils/show.c b/utils/show.c index ff4a79593..0dbf5270d 100644 --- a/utils/show.c +++ b/utils/show.c @@ -62,39 +62,6 @@ parseCommandLine( int argc, const char ** argv ) return 0; } -static const double KiB = 1024.0; -static const double MiB = 1024.0 * 1024.0; -static const double GiB = 1024.0 * 1024.0 * 1024.0; - -static char* -strlsize( char * buf, int64_t size, size_t buflen ) -{ - if( !size ) - tr_strlcpy( buf, "None", buflen ); - else if( size < (int64_t)KiB ) - tr_snprintf( buf, buflen, "%'" PRId64 " bytes", (int64_t)size ); - else - { - double displayed_size; - if( size < (int64_t)MiB ) - { - displayed_size = (double) size / KiB; - tr_snprintf( buf, buflen, "%'.1f KiB", displayed_size ); - } - else if( size < (int64_t)GiB ) - { - displayed_size = (double) size / MiB; - tr_snprintf( buf, buflen, "%'.1f MiB", displayed_size ); - } - else - { - displayed_size = (double) size / GiB; - tr_snprintf( buf, buflen, "%'.1f GiB", displayed_size ); - } - } - return buf; -} - static void showInfo( const tr_info * inf ) { @@ -119,8 +86,8 @@ showInfo( const tr_info * inf ) if( inf->comment && *inf->comment ) printf( " Comment: %s\n", inf->comment ); printf( " Piece Count: %d\n", inf->pieceCount ); - printf( " Piece Size: %s\n", strlsize( buf, inf->pieceSize, sizeof( buf ) ) ); - printf( " Total Size: %s\n", strlsize( buf, inf->totalSize, sizeof( buf ) ) ); + printf( " Piece Size: %s\n", tr_formatter_size( buf, inf->pieceSize, sizeof( buf ) ) ); + printf( " Total Size: %s\n", tr_formatter_size( buf, inf->totalSize, sizeof( buf ) ) ); printf( " Privacy: %s\n", inf->isPrivate ? "Private torrent" : "Public torrent" ); /** @@ -145,7 +112,7 @@ showInfo( const tr_info * inf ) printf( "\nFILES\n\n" ); for( i=0; i<(int)inf->fileCount; ++i ) - printf( " %s (%s)\n", inf->files[i].name, strlsize( buf, inf->files[i].length, sizeof( buf ) ) ); + printf( " %s (%s)\n", inf->files[i].name, tr_formatter_size( buf, inf->files[i].length, sizeof( buf ) ) ); } static size_t @@ -264,6 +231,8 @@ main( int argc, char * argv[] ) tr_ctor * ctor; tr_setMessageLevel( TR_MSG_ERR ); + tr_formatter_size_init ( 1024, _("B"), _("KiB"), _("MiB"), _("GiB") ); + tr_formatter_speed_init( 1024, _("B/s"), _("KiB/s"), _("MiB/s"), _("GiB/s") ); if( parseCommandLine( argc, (const char**)argv ) ) return EXIT_FAILURE; -- 2.40.0