char *tr_strratio(char *buf, size_t buflen, double ratio, const char *infinity) { if ((int) ratio == TR_RATIO_NA) tr_strlcpy(buf, _("None"), buflen); else if ((int) ratio == TR_RATIO_INF) tr_strlcpy(buf, infinity, buflen); else if (ratio < 10.0) tr_snprintf(buf, buflen, "%.2f", tr_truncd(ratio, 2)); else if (ratio < 100.0) tr_snprintf(buf, buflen, "%.1f", tr_truncd(ratio, 1)); else tr_snprintf(buf, buflen, "%'.0f", ratio); return buf; }
static int test_truncd( void ) { char buf[32]; const double nan = sqrt( -1 ); tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 ); check_streq("100.00%", buf); tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) ); check_streq("99.99%", buf); tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) ); check_streq("403650.6562", buf); tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) ); check_streq( "2.15", buf ); tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.05, 2 ) ); check_streq( "2.05", buf ); tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 3.3333, 2 ) ); check_streq( "3.33", buf ); tr_snprintf( buf, sizeof( buf ), "%.0f", tr_truncd( 3.3333, 0 ) ); check_streq( "3", buf ); tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( nan, 2 ) ); check( strstr( buf, "nan" ) != NULL ); return 0; }
char *tr_strlpercent(char *buf, double x, size_t buflen) { int precision; if (x < 10.0) precision = 2; else if (x < 100.0) precision = 1; else precision = 0; tr_snprintf(buf, buflen, "%.*f%%", precision, tr_truncd(x, precision)); return buf; }
static char* getShortStatusString( const tr_torrent * tor, const tr_stat * st, double uploadSpeed_KBps, double downloadSpeed_KBps ) { GString * gstr = g_string_new( NULL ); switch( st->activity ) { case TR_STATUS_STOPPED: if( st->finished ) g_string_assign( gstr, _( "Finished" ) ); else g_string_assign( gstr, _( "Paused" ) ); break; case TR_STATUS_CHECK_WAIT: g_string_assign( gstr, _( "Waiting to verify local data" ) ); break; case TR_STATUS_CHECK: g_string_append_printf( gstr, _( "Verifying local data (%.1f%% tested)" ), tr_truncd( st->recheckProgress * 100.0, 1 ) ); break; case TR_STATUS_DOWNLOAD: case TR_STATUS_SEED: { char buf[512]; if( st->activity != TR_STATUS_DOWNLOAD ) { tr_strlratio( buf, st->ratio, sizeof( buf ) ); g_string_append_printf( gstr, _( "Ratio %s" ), buf ); g_string_append( gstr, ", " ); } getShortTransferString( tor, st, uploadSpeed_KBps, downloadSpeed_KBps, buf, sizeof( buf ) ); g_string_append( gstr, buf ); break; } default: break; } return g_string_free( gstr, FALSE ); }
static void getShortStatusString( GString * gstr, const tr_torrent * tor, const tr_stat * st, double uploadSpeed_KBps, double downloadSpeed_KBps ) { switch( st->activity ) { case TR_STATUS_STOPPED: g_string_append( gstr, st->finished ? _( "Finished" ) : _( "Paused" ) ); break; case TR_STATUS_CHECK_WAIT: g_string_append( gstr, _( "Queued for verification" ) ); break; case TR_STATUS_DOWNLOAD_WAIT: g_string_append( gstr, _( "Queued for download" ) ); break; case TR_STATUS_SEED_WAIT: g_string_append( gstr, _( "Queued for seeding" ) ); break; case TR_STATUS_CHECK: g_string_append_printf( gstr, _( "Verifying local data (%.1f%% tested)" ), tr_truncd( st->recheckProgress * 100.0, 1 ) ); break; case TR_STATUS_DOWNLOAD: case TR_STATUS_SEED: { char buf[512]; if( st->activity != TR_STATUS_DOWNLOAD ) { tr_strlratio( buf, st->ratio, sizeof( buf ) ); g_string_append_printf( gstr, _( "Ratio %s" ), buf ); g_string_append( gstr, ", " ); } getShortTransferString( tor, st, uploadSpeed_KBps, downloadSpeed_KBps, buf, sizeof( buf ) ); g_string_append( gstr, buf ); break; } default: break; } }
static void getShortStatusString (GString * gstr, const tr_torrent * tor, const tr_stat * st, double uploadSpeed_KBps, double downloadSpeed_KBps) { switch (st->activity) { case TR_STATUS_STOPPED: g_string_append (gstr, st->finished ? _("Finished") : _("Paused")); break; case TR_STATUS_CHECK_WAIT: g_string_append (gstr, _("Queued for verification")); break; case TR_STATUS_DOWNLOAD_WAIT: g_string_append (gstr, _("Queued for download")); break; case TR_STATUS_SEED_WAIT: g_string_append (gstr, _("Queued for seeding")); break; case TR_STATUS_CHECK: g_string_append_printf (gstr, _("Verifying local data (%.1f%% tested)"), tr_truncd (st->recheckProgress * 100.0, 1)); break; case TR_STATUS_DOWNLOAD: case TR_STATUS_SEED: { char speedStr[64]; char ratioStr[64]; tr_strlratio (ratioStr, st->ratio, sizeof (ratioStr)); getShortTransferString (tor, st, uploadSpeed_KBps, downloadSpeed_KBps, speedStr, sizeof (speedStr)); /* download/upload speed, ratio */ g_string_append_printf (gstr, "%1$s Ratio: %2$s", speedStr, ratioStr); break; } default: break; } }
static int test_truncd (void) { char buf[32]; const double nan = sqrt (-1); tr_snprintf (buf, sizeof (buf), "%.2f%%", 99.999); check_streq ("100.00%", buf); tr_snprintf (buf, sizeof (buf), "%.2f%%", tr_truncd (99.999, 2)); check_streq ("99.99%", buf); tr_snprintf (buf, sizeof (buf), "%.4f", tr_truncd (403650.656250, 4)); check_streq ("403650.6562", buf); tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.15, 2)); check_streq ("2.15", buf); tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.05, 2)); check_streq ("2.05", buf); tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (3.3333, 2)); check_streq ("3.33", buf); tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0)); check_streq ("3", buf); #if !(defined (_MSC_VER) || (defined (__MINGW32__) && defined (__MSVCRT__))) /* FIXME: MSCVRT behaves differently in case of nan */ tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2)); check (strstr (buf, "nan") != NULL); #else (void) nan; #endif return 0; }