}
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;
}
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 ) )
{
{
printf( " Download Limit: " );
if( boolVal )
- printf( "%" PRId64 " KiB/s\n", i );
+ printf( "%s\n", strlspeed( buf, i*1024, sizeof( buf ) ) );
else
printf( "Unlimited\n" );
}
{
printf( " Upload Limit: " );
if( boolVal )
- printf( "%" PRId64 " KiB/s\n", i );
+ printf( "%s\n", strlspeed( buf, i*1024, sizeof( buf ) ) );
else
printf( "Unlimited\n" );
}
tr_bencDictFindBool( args, "seedRatioLimited", &seedRatioLimited) )
{
char buf[128];
+ char buf2[128];
+ char buf3[128];
printf( "LIMITS\n" );
printf( " Peer limit: %" PRId64 "\n", peerLimit );
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 ",
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 );
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( ) )
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;
}
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 );
+}
****
***/
+/* 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
#include <libtransmission/transmission.h>
#include <libtransmission/tr-getopt.h>
+#include <libtransmission/utils.h>
#include <libtransmission/version.h>
#include "app.h"
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" ) );
#include <QStyle>
#include <libtransmission/transmission.h>
-#include <libtransmission/utils.h>
+#include <libtransmission/utils.h> // tr_formatter
#include "qticonloader.h"
#include "utils.h"
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
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 )
{
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 );
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 )
{
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" );
/**
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
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;