Changeset 14130


Ignore:
Timestamp:
Jul 20, 2013, 3:37:13 PM (9 years ago)
Author:
jordan
Message:

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

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/rpcimpl.c

    r14116 r14130  
    182182  else /* all of them */
    183183    {
    184       tr_torrent * tor = NULL;
    185       const int n = tr_sessionCountTorrents (session);
    186       torrents = tr_new0 (tr_torrent *, n);
    187       while ((tor = tr_torrentNext (session, tor)))
    188         torrents[torrentCount++] = tor;
     184      torrents = tr_sessionGetTorrents (session, &torrentCount);
    189185    }
    190186
  • trunk/libtransmission/session.c

    r14114 r14130  
    17411741}
    17421742
     1743tr_torrent **
     1744tr_sessionGetTorrents (tr_session * session, int * setme_n)
     1745{
     1746  int i;
     1747  int n;
     1748  tr_torrent ** torrents;
     1749  tr_torrent * tor;
     1750
     1751  assert (tr_isSession (session));
     1752  assert (setme_n != NULL);
     1753
     1754  n = tr_sessionCountTorrents (session);
     1755  *setme_n = n;
     1756
     1757  torrents = tr_new (tr_torrent *, n);
     1758  tor = NULL;
     1759  for (i=0; i<n; ++i)
     1760    torrents[i] = tor = tr_torrentNext (session, tor);
     1761
     1762  return torrents;
     1763}
     1764
    17431765static int
    17441766compareTorrentByCur (const void * va, const void * vb)
     
    17901812   * if we can't get them all closed in a reasonable amount of time,
    17911813   * at least we get the most important ones first. */
    1792   tor = NULL;
    1793   n = session->torrentCount;
    1794   torrents = tr_new (tr_torrent *, session->torrentCount);
    1795   for (i=0; i<n; ++i)
    1796     torrents[i] = tor = tr_torrentNext (session, tor);
     1814  torrents = tr_sessionGetTorrents (session, &n);
    17971815  qsort (torrents, n, sizeof (tr_torrent*), compareTorrentByCur);
    17981816  for (i=0; i<n; ++i)
     
    28062824{
    28072825  size_t i;
     2826  size_t n;
    28082827  tr_torrent * tor;
    28092828  struct TorrentAndPosition * candidates;
     
    28132832
    28142833  /* build an array of the candidates */
    2815   candidates = tr_new (struct TorrentAndPosition, session->torrentCount);
     2834  n = tr_sessionCountTorrents (session);
     2835  candidates = tr_new (struct TorrentAndPosition, n);
    28162836  i = 0;
    28172837  tor = NULL;
  • trunk/libtransmission/session.h

    r14023 r14130  
    264264int tr_sessionCountTorrents (const tr_session * session);
    265265
     266tr_torrent ** tr_sessionGetTorrents (tr_session * session, int * setme_n);
     267
    266268enum
    267269{
  • trunk/libtransmission/torrent.c

    r14125 r14130  
    33913391queueIsSequenced (tr_session * session)
    33923392{
    3393   int i ;
    3394   int n ;
    3395   bool is_sequenced = true;
     3393  int i;
     3394  int n;
     3395  bool is_sequenced;
    33963396  tr_torrent * tor;
    3397   tr_torrent ** tmp = tr_new (tr_torrent *, session->torrentCount);
    3398 
    3399   /* get all the torrents */
     3397  tr_torrent ** torrents;
     3398
    34003399  n = 0;
    3401   tor = NULL;
    3402   while ((tor = tr_torrentNext (session, tor)))
    3403     tmp[n++] = tor;
    3404 
    3405   /* sort them by position */
    3406   qsort (tmp, n, sizeof (tr_torrent *), compareTorrentByQueuePosition);
     3400  torrents = tr_sessionGetTorrents (session, &n);
     3401  qsort (torrents, n, sizeof (tr_torrent *), compareTorrentByQueuePosition);
    34073402
    34083403#if 0
     
    34143409
    34153410  /* test them */
     3411  is_sequenced = true;
    34163412  for (i=0; is_sequenced && i<n; ++i)
    3417     if (tmp[i]->queuePosition != i)
     3413    if (torrents[i]->queuePosition != i)
    34183414      is_sequenced = false;
    34193415
    3420   tr_free (tmp);
     3416  tr_free (torrents);
    34213417  return is_sequenced;
    34223418}
Note: See TracChangeset for help on using the changeset viewer.