add tr_sessionGetTorrents(), a private utility to avoid code duplication in libtransmission

This commit is contained in:
Jordan Lee 2013-07-20 15:37:13 +00:00
parent acb1fb80dd
commit fe79ad39bc
4 changed files with 38 additions and 24 deletions

View File

@ -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;

View File

@ -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; i<n; ++i)
torrents[i] = tor = tr_torrentNext (session, tor);
return torrents;
}
static int
compareTorrentByCur (const void * va, const void * vb)
{
@ -1789,11 +1811,7 @@ sessionCloseImpl (void * vsession)
/* Close the torrents. Get the most active ones first so that
* if we can't get them all closed in a reasonable amount of time,
* at least we get the most important ones first. */
tor = NULL;
n = session->torrentCount;
torrents = tr_new (tr_torrent *, session->torrentCount);
for (i=0; i<n; ++i)
torrents[i] = tor = tr_torrentNext (session, tor);
torrents = tr_sessionGetTorrents (session, &n);
qsort (torrents, n, sizeof (tr_torrent*), compareTorrentByCur);
for (i=0; i<n; ++i)
tr_torrentFree (torrents[i]);
@ -2805,6 +2823,7 @@ tr_sessionGetNextQueuedTorrents (tr_session * session,
tr_ptrArray * setme)
{
size_t i;
size_t n;
tr_torrent * tor;
struct TorrentAndPosition * candidates;
@ -2812,7 +2831,8 @@ tr_sessionGetNextQueuedTorrents (tr_session * session,
assert (tr_isDirection (direction));
/* build an array of the candidates */
candidates = tr_new (struct TorrentAndPosition, session->torrentCount);
n = tr_sessionCountTorrents (session);
candidates = tr_new (struct TorrentAndPosition, n);
i = 0;
tor = NULL;
while ((tor = tr_torrentNext (session, tor)))

View File

@ -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,

View File

@ -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 && i<n; ++i)
if (tmp[i]->queuePosition != i)
if (torrents[i]->queuePosition != i)
is_sequenced = false;
tr_free (tmp);
tr_free (torrents);
return is_sequenced;
}
#endif