From 15ffb4232e0f730f3c8f50fae5be649070058f21 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 4 Jan 2008 19:13:30 +0000 Subject: [PATCH] tr_stat.ratio now returns TR_RATIO_INF when appropriate --- libtransmission/stats.c | 7 +------ libtransmission/torrent.c | 6 ++---- libtransmission/transmission.h | 3 ++- libtransmission/utils.c | 19 +++++++++++++++++++ libtransmission/utils.h | 2 ++ 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/libtransmission/stats.c b/libtransmission/stats.c index 8f66e2ce8..b1a581ca7 100644 --- a/libtransmission/stats.c +++ b/libtransmission/stats.c @@ -135,12 +135,7 @@ tr_statsClose( tr_handle * handle ) static void updateRatio( tr_session_stats * setme ) { - if( setme->downloadedBytes ) - setme->ratio = setme->uploadedBytes / (double)setme->downloadedBytes; - else if( setme->uploadedBytes ) - setme->ratio = TR_RATIO_INF; - else - setme->ratio = TR_RATIO_NA; + setme->ratio = tr_getRatio( setme->uploadedBytes, setme->downloadedBytes ); } void diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 5c3db7056..1d4e125cb 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -661,10 +661,8 @@ tr_torrentStat( tr_torrent * tor ) tr_bitfieldFree( availablePieces ); } - - s->ratio = ( s->downloadedEver || s->haveValid ) - ? s->uploadedEver / (float)(MAX(s->downloadedEver,s->haveValid)) - : TR_RATIO_NA; + + s->ratio = tr_getRatio( s->uploadedEver, MAX( s->downloadedEver, s->haveValid ) ); tr_torrentUnlock( tor ); diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 6e1899e16..5b37718a6 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -133,7 +133,7 @@ typedef struct tr_session_stats { uint64_t uploadedBytes; /* total up */ uint64_t downloadedBytes; /* total down */ - double ratio; /* total up / total down */ + double ratio; /* TR_RATIO_INF, TR_RATIO_NA, or total up/down */ uint64_t filesAdded; /* number of files added */ uint64_t sessionCount; /* program started N times */ uint64_t secondsActive; /* how long Transmisson's been running */ @@ -764,6 +764,7 @@ struct tr_stat #define TR_RATIO_NA -1 #define TR_RATIO_INF -2 + /* TR_RATIO_INF, TR_RATIO_NA, or a regular ratio */ float ratio; uint64_t startDate; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 49a8c8a84..5a6a38237 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -842,3 +842,22 @@ strlcat(char *dst, const char *src, size_t siz) } #endif /* HAVE_STRLCAT */ + +/*** +**** +***/ + +double +tr_getRatio( double numerator, double denominator ) +{ + double ratio; + + if( denominator ) + ratio = numerator / denominator; + else if( numerator ) + ratio = TR_RATIO_INF; + else + ratio = TR_RATIO_NA; + + return ratio; +} diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 4c0d7e85a..d717c9352 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -150,4 +150,6 @@ size_t tr_bitfieldCountTrueBits( const tr_bitfield* ); tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); +double tr_getRatio( double numerator, double denominator ); + #endif