From fe79ad39bcec738c63675390f7b3db2888c77ad1 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Sat, 20 Jul 2013 15:37:13 +0000 Subject: [PATCH] add tr_sessionGetTorrents(), a private utility to avoid code duplication in libtransmission --- libtransmission/rpcimpl.c | 6 +----- libtransmission/session.c | 32 ++++++++++++++++++++++++++------ libtransmission/session.h | 2 ++ libtransmission/torrent.c | 22 +++++++++------------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index 934ea5b23..29b173ece 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -181,11 +181,7 @@ getTorrents (tr_session * session, } else /* all of them */ { - tr_torrent * tor = NULL; - const int n = tr_sessionCountTorrents (session); - torrents = tr_new0 (tr_torrent *, n); - while ((tor = tr_torrentNext (session, tor))) - torrents[torrentCount++] = tor; + torrents = tr_sessionGetTorrents (session, &torrentCount); } *setmeCount = torrentCount; diff --git a/libtransmission/session.c b/libtransmission/session.c index a0512b43b..289a56210 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -1740,6 +1740,28 @@ tr_sessionCountTorrents (const tr_session * session) return tr_isSession (session) ? session->torrentCount : 0; } +tr_torrent ** +tr_sessionGetTorrents (tr_session * session, int * setme_n) +{ + int i; + int n; + tr_torrent ** torrents; + tr_torrent * tor; + + assert (tr_isSession (session)); + assert (setme_n != NULL); + + n = tr_sessionCountTorrents (session); + *setme_n = n; + + torrents = tr_new (tr_torrent *, n); + tor = NULL; + for (i=0; itorrentCount; - torrents = tr_new (tr_torrent *, session->torrentCount); - for (i=0; itorrentCount); + n = tr_sessionCountTorrents (session); + candidates = tr_new (struct TorrentAndPosition, n); i = 0; tor = NULL; while ((tor = tr_torrentNext (session, tor))) diff --git a/libtransmission/session.h b/libtransmission/session.h index 847df363f..6a08ebe5f 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -263,6 +263,8 @@ struct tr_bindsockets * tr_sessionGetBindSockets (tr_session *); int tr_sessionCountTorrents (const tr_session * session); +tr_torrent ** tr_sessionGetTorrents (tr_session * session, int * setme_n); + enum { SESSION_MAGIC_NUMBER = 3845, diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 36a8996a4..ae2afc077 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -3390,20 +3390,15 @@ compareTorrentByQueuePosition (const void * va, const void * vb) static bool queueIsSequenced (tr_session * session) { - int i ; - int n ; - bool is_sequenced = true; + int i; + int n; + bool is_sequenced; tr_torrent * tor; - tr_torrent ** tmp = tr_new (tr_torrent *, session->torrentCount); + tr_torrent ** torrents; - /* get all the torrents */ n = 0; - tor = NULL; - while ((tor = tr_torrentNext (session, tor))) - tmp[n++] = tor; - - /* sort them by position */ - qsort (tmp, n, sizeof (tr_torrent *), compareTorrentByQueuePosition); + torrents = tr_sessionGetTorrents (session, &n); + qsort (torrents, n, sizeof (tr_torrent *), compareTorrentByQueuePosition); #if 0 fprintf (stderr, "%s", "queue: "); @@ -3413,11 +3408,12 @@ queueIsSequenced (tr_session * session) #endif /* test them */ + is_sequenced = true; for (i=0; is_sequenced && iqueuePosition != i) + if (torrents[i]->queuePosition != i) is_sequenced = false; - tr_free (tmp); + tr_free (torrents); return is_sequenced; } #endif