]> granicus.if.org Git - transmission/commitdiff
(trunk) add tr_formatter_size() and tr_formatter_speed() so that all the client apps...
authorCharles Kerr <charles@transmissionbt.com>
Tue, 22 Jun 2010 04:34:16 +0000 (04:34 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Tue, 22 Jun 2010 04:34:16 +0000 (04:34 +0000)
daemon/remote.c
gtk/main.c
gtk/util.c
libtransmission/utils.c
libtransmission/utils.h
qt/app.cc
qt/utils.cc
qt/utils.h
utils/show.c

index a320d5351c9df8098b9fc43f7203fc00fb29e3a6..475a8c1cfcc1faaaae6fc56a7ac1c1d006e6a310 100644 (file)
@@ -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 );
index f3adf3ca60d73163659819482fcad39d69830032..183fa830cab776cc1196883fc16e4d9859154ebf 100644 (file)
@@ -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( ) )
index 48cb1aaeb05fe8fcf4ff10a5e4b3e99056405990..957bdb40b713dab3f2fa8312b455f6fdd3934d0a 100644 (file)
@@ -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;
 }
index 8b0ce35c60bfa3f0543780f33a6409a5b568b9c5..5b55cebc97c982ab7fa08b75d2ab6a7ae10d3588 100644 (file)
@@ -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 );
+}
index 6385353d0c01afa27785fb565b2057764d68cd83..faa68728f47e7afd534f803aab6e698f9f24d595 100644 (file)
@@ -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
index 1d8966ce9e66e3d0313c97d02a2d451a32a06083..3b244beb2f8358ba69fbf231b5042caeb78785c0 100644 (file)
--- a/qt/app.cc
+++ b/qt/app.cc
@@ -26,6 +26,7 @@
 
 #include <libtransmission/transmission.h>
 #include <libtransmission/tr-getopt.h>
+#include <libtransmission/utils.h>
 #include <libtransmission/version.h>
 
 #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" ) );
index dfa0a0c3753a6cc44f294331b962a329dd9bd0bc..d03a3b5542e6e72fca96e06b414254c50ffd6eef 100644 (file)
@@ -23,7 +23,7 @@
 #include <QStyle>
 
 #include <libtransmission/transmission.h>
-#include <libtransmission/utils.h>
+#include <libtransmission/utils.h> // 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 )
 {
index 0dc85c2b3cec23c4ddb90557ad75819ecbf07cb7..5338ac6ceb5e1163db59294f5743bd15975b3e77 100644 (file)
@@ -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 );
index ff4a7959399f018d6a7d3da921085720a9ca44e7..0dbf5270d645534cb08fa87826f1eadaf8125404 100644 (file)
@@ -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;