}
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;
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)
{
/* 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]);
tr_ptrArray * setme)
{
size_t i;
+ size_t n;
tr_torrent * tor;
struct TorrentAndPosition * candidates;
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)))
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: ");
#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