(trunk) some units formatter cleanup.

This commit is contained in:
Charles Kerr 2010-07-09 20:53:38 +00:00
parent 649ebcd6c3
commit 52c04a0d06
7 changed files with 27 additions and 63 deletions

View File

@ -11,7 +11,6 @@
*/
#include <assert.h>
#include <math.h> /* ceil() */
#include <stddef.h>
#include <stdio.h> /* sscanf */
#include <stdlib.h>
@ -1192,54 +1191,38 @@ refreshPeerRow( GtkListStore * store,
GtkTreeIter * iter,
const tr_peer_stat * peer )
{
char up_speed[64];
char down_speed[64];
char up_count[64];
char down_count[64];
char blocks_to_peer[64];
char blocks_to_client[64];
char cancelled_by_peer[64];
char cancelled_by_client[64];
char up_speed[64] = { '\0' };
char down_speed[64] = { '\0' };
char up_count[64] = { '\0' };
char down_count[64] = { '\0' };
char blocks_to_peer[64] = { '\0' };
char blocks_to_client[64] = { '\0' };
char cancelled_by_peer[64] = { '\0' };
char cancelled_by_client[64] = { '\0' };
if( peer->rateToPeer_KBps > 0.01 )
tr_formatter_speed_KBps( up_speed, peer->rateToPeer_KBps, sizeof( up_speed ) );
else
*up_speed = '\0';
if( peer->rateToClient_KBps > 0 )
tr_formatter_speed_KBps( down_speed, peer->rateToClient_KBps, sizeof( down_speed ) );
else
*down_speed = '\0';
if( peer->pendingReqsToPeer > 0 )
g_snprintf( down_count, sizeof( down_count ), "%d", peer->pendingReqsToPeer );
else
*down_count = '\0';
if( peer->pendingReqsToClient > 0 )
g_snprintf( up_count, sizeof( down_count ), "%d", peer->pendingReqsToClient );
else
*up_count = '\0';
if( peer->blocksToPeer > 0 )
g_snprintf( blocks_to_peer, sizeof( blocks_to_peer ), "%"PRIu32, peer->blocksToPeer );
else
*blocks_to_peer = '\0';
if( peer->blocksToClient > 0 )
g_snprintf( blocks_to_client, sizeof( blocks_to_client ), "%"PRIu32, peer->blocksToClient );
else
*blocks_to_client = '\0';
if( peer->cancelsToPeer > 0 )
g_snprintf( cancelled_by_client, sizeof( cancelled_by_client ), "%"PRIu32, peer->cancelsToPeer );
else
*cancelled_by_client = '\0';
if( peer->cancelsToClient > 0 )
g_snprintf( cancelled_by_peer, sizeof( cancelled_by_peer ), "%"PRIu32, peer->cancelsToClient );
else
*cancelled_by_peer = '\0';
gtk_list_store_set( store, iter,
PEER_COL_PROGRESS, (int)( 100.0 * peer->progress ),
@ -1407,7 +1390,7 @@ refreshWebseedList( struct DetailsImpl * di, tr_torrent ** torrents, int n )
p = gtk_tree_row_reference_get_path( ref );
gtk_tree_model_get_iter( model, &iter, p );
if( speeds_KBps[j] > 0 )
tr_formatter_speed_Bps( buf, speeds_KBps[j], sizeof( buf ) );
tr_formatter_speed_KBps( buf, speeds_KBps[j], sizeof( buf ) );
else
*buf = '\0';
gtk_list_store_set( store, &iter, WEBSEED_COL_DOWNLOAD_RATE_DOUBLE, speeds_KBps[j],

View File

@ -403,7 +403,7 @@ createSpeedMenu( PrivateData * p, tr_direction dir )
{
int i, n;
GtkWidget *w, *m;
const int speeds[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750 };
const int speeds_KBps[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750 };
m = gtk_menu_new( );
@ -424,13 +424,13 @@ createSpeedMenu( PrivateData * p, tr_direction dir )
w = gtk_separator_menu_item_new( );
gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
for( i=0, n=G_N_ELEMENTS(speeds); i<n; ++i )
for( i=0, n=G_N_ELEMENTS(speeds_KBps); i<n; ++i )
{
char buf[128];
tr_strlspeed( buf, speeds[i] * speed_K, sizeof( buf ) );
tr_formatter_speed_KBps( buf, speeds_KBps[i], sizeof( buf ) );
w = gtk_menu_item_new_with_label( buf );
g_object_set_data( G_OBJECT( w ), DIRECTION_KEY, GINT_TO_POINTER( dir ) );
g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( speeds[i] ) );
g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( speeds_KBps[i] ) );
g_signal_connect( w, "activate", G_CALLBACK(onSpeedSet), p );
gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
}

View File

@ -164,17 +164,6 @@ tr_strlsize( char * buf, guint64 bytes, size_t buflen )
return buf;
}
char*
tr_strlspeed( char * buf, int bytes_per_second, size_t buflen )
{
if( bytes_per_second < 1 )
g_strlcpy( buf, _( "None" ), buflen );
else
tr_formatter_speed_Bps( buf, bytes_per_second, buflen );
return buf;
}
char*
tr_strltime( char * buf, int seconds, size_t buflen )
{

View File

@ -65,9 +65,6 @@ char* tr_strlpercent( char * buf, double x, size_t buflen );
/* return a human-readable string for the size given in bytes. */
char* tr_strlsize( char * buf, guint64 size, size_t buflen );
/* return a human-readable string for the transfer rate given in Bps. */
char* tr_strlspeed( char * buf, int bytes_per_second, size_t buflen );
/* return a human-readable string for the given ratio. */
char* tr_strlratio( char * buf, double ratio, size_t buflen );

View File

@ -1643,10 +1643,10 @@ tr_formatter_speed_init( unsigned int kilo,
}
char*
tr_formatter_speed_Bps( char * buf, uint64_t bytes_per_second, size_t buflen )
tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen )
{
const double K = speed_units.units[TR_FMT_KB].value;
double speed = bytes_per_second / K;
double speed = KBps;
if( speed <= 999.95 ) /* 0.0 KB to 999.9 KB */
tr_snprintf( buf, buflen, "%.2f %s", speed, speed_units.units[TR_FMT_KB].name );

View File

@ -572,26 +572,21 @@ void tr_formatter_speed_init( unsigned int kilo, const char * kb, const char * m
void tr_formatter_mem_init( unsigned int kilo, const char * kb, const char * mb,
const char * gb, const char * tb );
/* format a speed into a user-readable string. */
extern unsigned int tr_speed_K;
char* tr_formatter_speed_Bps( char * buf, uint64_t Bps, size_t buflen );
static inline char* tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen ) { return tr_formatter_speed_Bps( buf, KBps * tr_speed_K, buflen ); }
static inline char* tr_formatter_speed_MBps( char * buf, double MBps, size_t buflen ) { return tr_formatter_speed_Bps( buf, MBps * tr_speed_K * tr_speed_K, buflen ); }
static inline char* tr_formatter_speed_GBps( char * buf, double GBps, size_t buflen ) { return tr_formatter_speed_Bps( buf, GBps * tr_speed_K * tr_speed_K * tr_speed_K, buflen ); }
/* format a memory size into a user-readable string. */
extern unsigned int tr_mem_K;
char* tr_formatter_mem_B( char * buf, uint64_t bytes, size_t buflen );
static inline char* tr_formatter_mem_KB( char * buf, double KBps, size_t buflen ) { return tr_formatter_mem_B( buf, KBps * tr_mem_K, buflen ); }
static inline char* tr_formatter_mem_MB( char * buf, double MBps, size_t buflen ) { return tr_formatter_mem_B( buf, MBps * tr_mem_K * tr_mem_K, buflen ); }
static inline char* tr_formatter_mem_GB( char * buf, double GBps, size_t buflen ) { return tr_formatter_mem_B( buf, GBps * tr_mem_K * tr_mem_K * tr_mem_K, buflen ); }
/* format a file size into a user-readable string. */
extern unsigned int tr_size_K;
/* format a speed from KBps into a user-readable string. */
char* tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen );
/* format a memory size from bytes into a user-readable string. */
char* tr_formatter_mem_B( char * buf, uint64_t bytes, size_t buflen );
/* format a memory size from MB into a user-readable string. */
static inline char* tr_formatter_mem_MB( char * buf, double MBps, size_t buflen ) { return tr_formatter_mem_B( buf, MBps * tr_mem_K * tr_mem_K, buflen ); }
/* format a file size from bytes into a user-readable string. */
char* tr_formatter_size_B( char * buf, uint64_t bytes, size_t buflen );
static inline char* tr_formatter_size_KB( char * buf, double KBps, size_t buflen ) { return tr_formatter_size_B( buf, KBps * tr_size_K, buflen ); }
static inline char* tr_formatter_size_MB( char * buf, double MBps, size_t buflen ) { return tr_formatter_size_B( buf, MBps * tr_size_K * tr_size_K, buflen ); }
static inline char* tr_formatter_size_GB( char * buf, double GBps, size_t buflen ) { return tr_formatter_size_B( buf, GBps * tr_size_K * tr_size_K * tr_size_K, buflen ); }
/***

View File

@ -122,7 +122,7 @@ Formatter :: speedToString( const Speed& speed )
return tr( "None" );
else {
char buf[128];
tr_formatter_speed_KBps( buf, speed.Bps( ), sizeof( buf ) );
tr_formatter_speed_KBps( buf, speed.KBps( ), sizeof( buf ) );
return buf;
}
}