Changeset 1022

Show
Ignore:
Timestamp:
12/23/07 14:12:23 (4 years ago)
Author:
rakshasa
Message:

* Refactoring utils::Directory.

Location:
trunk
Files:
12 modified

Legend:

Unmodified
Added
Removed
  • trunk/libtorrent/rak/algorithm.h

    r939 r1022  
    123123// Count the number of elements from the start of the containers to 
    124124// the first inequal element. 
    125 template <typename _InputIter> 
    126 typename std::iterator_traits<_InputIter>::difference_type 
    127 count_base(_InputIter __first1, _InputIter __last1, 
    128            _InputIter __first2, _InputIter __last2) { 
     125template <typename _InputIter1, typename _InputIter2> 
     126typename std::iterator_traits<_InputIter1>::difference_type 
     127count_base(_InputIter1 __first1, _InputIter1 __last1, 
     128           _InputIter2 __first2, _InputIter2 __last2) { 
    129129 
    130   typename std::iterator_traits<_InputIter>::difference_type __n = 0; 
     130  typename std::iterator_traits<_InputIter1>::difference_type __n = 0; 
    131131 
    132132  for ( ;__first1 != __last1 && __first2 != __last2; ++__first1, ++__first2, ++__n) 
     
    137137} 
    138138 
    139 template <typename _InputIter> 
    140 typename std::iterator_traits<_InputIter>::value_type 
    141 make_base(_InputIter __first, _InputIter __last) { 
     139template <typename _Return, typename _InputIter, typename _Ftor> 
     140_Return 
     141make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) { 
    142142  if (__first == __last) 
    143143    return ""; 
    144144 
    145   typename std::iterator_traits<_InputIter>::value_type __base = *__first++; 
     145  _Return __base = __ftor(*__first++); 
    146146 
    147147  for ( ;__first != __last; ++__first) { 
    148148    typename std::iterator_traits<_InputIter>::difference_type __pos = count_base(__base.begin(), __base.end(), 
    149                                                                                   __first->begin(), __first->end()); 
     149                                                                                  __ftor(*__first).begin(), __ftor(*__first).end()); 
    150150 
    151151    if (__pos < (typename std::iterator_traits<_InputIter>::difference_type)__base.size()) 
     
    156156} 
    157157 
    158  
    159  
    160158} 
    161159 
  • trunk/rtorrent/rak/algorithm.h

    r938 r1022  
    123123// Count the number of elements from the start of the containers to 
    124124// the first inequal element. 
    125 template <typename _InputIter> 
    126 typename std::iterator_traits<_InputIter>::difference_type 
    127 count_base(_InputIter __first1, _InputIter __last1, 
    128            _InputIter __first2, _InputIter __last2) { 
     125template <typename _InputIter1, typename _InputIter2> 
     126typename std::iterator_traits<_InputIter1>::difference_type 
     127count_base(_InputIter1 __first1, _InputIter1 __last1, 
     128           _InputIter2 __first2, _InputIter2 __last2) { 
    129129 
    130   typename std::iterator_traits<_InputIter>::difference_type __n = 0; 
     130  typename std::iterator_traits<_InputIter1>::difference_type __n = 0; 
    131131 
    132132  for ( ;__first1 != __last1 && __first2 != __last2; ++__first1, ++__first2, ++__n) 
     
    137137} 
    138138 
    139 template <typename _InputIter> 
    140 typename std::iterator_traits<_InputIter>::value_type 
    141 make_base(_InputIter __first, _InputIter __last) { 
     139template <typename _Return, typename _InputIter, typename _Ftor> 
     140_Return 
     141make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) { 
    142142  if (__first == __last) 
    143143    return ""; 
    144144 
    145   typename std::iterator_traits<_InputIter>::value_type __base = *__first++; 
     145  _Return __base = __ftor(*__first++); 
    146146 
    147147  for ( ;__first != __last; ++__first) { 
    148148    typename std::iterator_traits<_InputIter>::difference_type __pos = count_base(__base.begin(), __base.end(), 
    149                                                                                   __first->begin(), __first->end()); 
     149                                                                                  __ftor(*__first).begin(), __ftor(*__first).end()); 
    150150 
    151151    if (__pos < (typename std::iterator_traits<_InputIter>::difference_type)__base.size()) 
     
    156156} 
    157157 
    158  
    159  
    160158} 
    161159 
  • trunk/rtorrent/src/command_network.cc

    r1013 r1022  
    3939#include <functional> 
    4040#include <rak/address_info.h> 
    41 #include <rak/file_stat.h> 
    4241#include <rak/path.h> 
    4342#include <torrent/connection_manager.h> 
  • trunk/rtorrent/src/core/download_store.cc

    r938 r1022  
    5151#include <torrent/resume.h> 
    5252#include <torrent/object_stream.h> 
     53 
     54#include "utils/directory.h" 
    5355 
    5456#include "download.h" 
     
    145147} 
    146148 
     149// This also needs to check that it isn't a directory. 
     150bool 
     151not_correct_format(const utils::directory_entry& entry) { 
     152  return !DownloadStore::is_correct_format(entry.d_name); 
     153} 
     154 
    147155utils::Directory 
    148156DownloadStore::get_formated_entries() { 
     
    150158    return utils::Directory(); 
    151159 
    152   utils::Directory d(m_path); 
     160  utils::Directory d; 
     161  d.set_path(m_path); 
    153162 
    154163  if (!d.update()) 
    155164    throw torrent::storage_error("core::DownloadStore::update() could not open directory \"" + m_path + "\""); 
    156165 
    157   d.erase(std::remove_if(d.begin(), d.end(), std::not1(std::ptr_fun(&DownloadStore::is_correct_format))), d.end()); 
     166  d.erase(std::remove_if(d.begin(), d.end(), std::ptr_fun(&not_correct_format)), d.end()); 
    158167 
    159168  return d; 
     
    161170 
    162171bool 
    163 DownloadStore::is_correct_format(std::string f) { 
     172DownloadStore::is_correct_format(const std::string& f) { 
    164173  if (f.size() != 48 || f.substr(40) != ".torrent") 
    165174    return false; 
  • trunk/rtorrent/src/core/download_store.h

    r938 r1022  
    4040#include <string> 
    4141 
    42 #include "utils/directory.h" 
    4342#include "utils/lockfile.h" 
     43 
     44namespace utils { 
     45  class Directory; 
     46} 
    4447 
    4548namespace core { 
     
    6467  utils::Directory    get_formated_entries(); 
    6568 
     69  static bool         is_correct_format(const std::string& f); 
     70 
    6671private: 
    67   static bool         is_correct_format(std::string f); 
    6872  std::string         create_filename(Download* d); 
    6973 
  • trunk/rtorrent/src/core/manager.cc

    r1013 r1022  
    5757 
    5858#include "rpc/parse_commands.h" 
     59#include "utils/directory.h" 
    5960 
    6061#include "globals.h" 
     
    418419} 
    419420 
     421utils::Directory 
     422path_expand_transform(std::string path, const utils::directory_entry& entry) { 
     423  return path + entry.d_name; 
     424} 
     425 
    420426// Move this somewhere better. 
    421427void 
     
    455461      // starts with the same. 
    456462      itr->update(r.pattern()[0] != '.'); 
    457       itr->erase(std::remove_if(itr->begin(), itr->end(), std::not1(r)), itr->end()); 
    458  
    459       std::transform(itr->begin(), itr->end(), std::back_inserter(nextCache), std::bind1st(std::plus<std::string>(), itr->get_path() + "/")); 
     463      itr->erase(std::remove_if(itr->begin(), itr->end(), rak::on(rak::mem_ref(&utils::directory_entry::d_name), std::not1(r))), itr->end()); 
     464 
     465      std::transform(itr->begin(), itr->end(), std::back_inserter(nextCache), rak::bind1st(std::ptr_fun(&path_expand_transform), itr->get_path() + "/")); 
    460466    } 
    461467 
  • trunk/rtorrent/src/input/path_input.cc

    r938 r1022  
    3939#include <functional> 
    4040#include <rak/algorithm.h> 
    41 #include <rak/file_stat.h> 
     41#include <rak/functional.h> 
    4242#include <rak/path.h> 
     43 
     44#include <sys/types.h> 
     45#include <sys/dir.h> 
    4346 
    4447#include "path_input.h" 
     
    6972 
    7073struct _transform_filename { 
    71   _transform_filename(const std::string& base) : m_base(base) {} 
    72  
    73   void operator () (std::string& filename) { 
    74     rak::file_stat fs; 
    75  
    76     if (!fs.update(rak::path_expand(m_base + filename))) 
    77       return; 
    78  
    79     else if (fs.is_directory()) 
    80       filename += '/'; 
     74  void operator () (utils::directory_entry& entry) { 
     75    if (entry.d_type == DT_DIR) 
     76      entry.d_name += '/'; 
    8177  } 
    82  
    83   const std::string& m_base; 
    8478}; 
    8579 
     
    9690  } 
    9791 
    98   std::for_each(dir.begin(), dir.end(), _transform_filename(str().substr(0, dirEnd))); 
     92  std::for_each(dir.begin(), dir.end(), _transform_filename()); 
    9993 
    10094  Range r = find_incomplete(dir, str().substr(dirEnd, get_pos())); 
     
    10397    return; // Show some nice colors here. 
    10498 
    105   std::string base = rak::make_base(r.first, r.second); 
     99  std::string base = rak::make_base<std::string>(r.first, r.second, rak::const_mem_ref(&utils::directory_entry::d_name)); 
    106100 
    107101  // Clear the path after the cursor to make this code cleaner. It's 
     
    134128} 
    135129 
     130inline bool 
     131find_complete_compare(const utils::directory_entry& complete, const std::string& base) { 
     132  return complete.d_name.compare(0, base.size(), base); 
     133} 
     134 
     135inline bool 
     136find_complete_not_compare(const utils::directory_entry& complete, const std::string& base) { 
     137  return !complete.d_name.compare(0, base.size(), base); 
     138} 
     139 
    136140PathInput::Range 
    137141PathInput::find_incomplete(utils::Directory& d, const std::string& f) { 
    138142  Range r; 
    139143 
    140   r.first  = std::find_if(d.begin(), d.end(), std::bind2nd(rak::compare_base<std::string>(), f)); 
    141   r.second = std::find_if(r.first, d.end(), std::not1(std::bind2nd(rak::compare_base<std::string>(), f))); 
     144  r.first  = std::find_if(d.begin(), d.end(), rak::bind2nd(std::ptr_fun(&find_complete_not_compare), f)); 
     145  r.second = std::find_if(r.first,   d.end(), rak::bind2nd(std::ptr_fun(&find_complete_compare), f)); 
    142146 
    143147  return r; 
  • trunk/rtorrent/src/main.cc

    r1020 r1022  
    102102load_session_torrents(Control* c) { 
    103103  // Load session torrents. 
    104   std::list<std::string> l = c->core()->download_store()->get_formated_entries().make_list(); 
    105  
    106   for (std::list<std::string>::iterator first = l.begin(), last = l.end(); first != last; ++first) { 
     104  std::vector<std::string> l = c->core()->download_store()->get_formated_entries().make_list(); 
     105 
     106  for (std::vector<std::string>::iterator first = l.begin(), last = l.end(); first != last; ++first) { 
    107107    core::DownloadFactory* f = new core::DownloadFactory(c->core()); 
    108108 
  • trunk/rtorrent/src/ui/download_list.cc

    r992 r1022  
    256256 
    257257  input->signal_show_range().connect(sigc::hide(sigc::hide(sigc::bind(sigc::mem_fun(*this, &DownloadList::activate_display), DISPLAY_STRING_LIST)))); 
    258   input->signal_show_range().connect(sigc::mem_fun(*esl, &ElementStringList::set_range<utils::Directory::iterator>)); 
     258  input->signal_show_range().connect(sigc::mem_fun(*esl, &ElementStringList::set_range_dirent<utils::Directory::iterator>)); 
    259259 
    260260  input->bindings()['\n']      = sigc::bind(sigc::mem_fun(*this, &DownloadList::receive_exit_input), type); 
  • trunk/rtorrent/src/ui/element_string_list.h

    r938 r1022  
    7474  } 
    7575 
     76  // A hack, clean this up. 
     77  template <typename InputIter> 
     78  void                set_range_dirent(InputIter first, InputIter last) { 
     79    m_list.clear(); 
     80 
     81    while (first != last) 
     82      m_list.push_back((first++)->d_name); 
     83 
     84    m_window->set_range(m_list.begin(), m_list.end()); 
     85    m_window->mark_dirty(); 
     86  } 
     87 
    7688  void                next_screen(); 
    7789 
  • trunk/rtorrent/src/utils/directory.cc

    r938 r1022  
    4747namespace utils { 
    4848 
     49// Keep this? 
    4950bool 
    5051Directory::is_valid() const { 
     
    5859} 
    5960 
     61// Update should take various flags and sort functors. 
    6062bool 
    6163Directory::update(bool hideDot) { 
     
    7072  struct dirent* ent; 
    7173 
     74  // Err... let us use getdirentries here instead. 
    7275  while ((ent = readdir(d)) != NULL) { 
     76    // Don't construct it here, check the const char. 
    7377    std::string de(ent->d_name); 
    7478 
    75     if (!de.empty() && (!hideDot || de[0] != '.')) 
    76       Base::push_back(ent->d_name); 
     79    if (!de.empty() && (!hideDot || de[0] != '.')) { 
     80      iterator itr = base_type::insert(end(), value_type()); 
     81 
     82      itr->d_fileno = ent->d_fileno; 
     83      itr->d_reclen = ent->d_reclen; 
     84      itr->d_type   = ent->d_type; 
     85      itr->d_name   = de; 
     86    } 
    7787  } 
    7888 
    7989  closedir(d); 
    80   Base::sort(std::less<std::string>()); 
     90  std::sort(begin(), end()); 
    8191 
    8292  return true; 
    8393} 
    8494 
    85 Directory::Base 
     95std::vector<std::string> 
    8696Directory::make_list() { 
    87   Base l; 
     97  std::vector<std::string> l; 
     98  l.reserve(size()); 
    8899 
    89   for (Base::iterator itr = begin(); itr != end(); ++itr) 
    90     l.push_back(m_path + *itr); 
     100  for (iterator itr = begin(); itr != end(); ++itr) 
     101    l.push_back(m_path + itr->d_name); 
    91102 
    92103  return l; 
  • trunk/rtorrent/src/utils/directory.h

    r938 r1022  
    3939 
    4040#include <string> 
    41 #include <list> 
     41#include <vector> 
    4242 
    4343namespace utils { 
    4444 
    45 class Directory : private std::list<std::string> { 
     45struct directory_entry { 
     46  // The name and types should match POSIX. 
     47  uint32_t            d_fileno; 
     48  uint16_t            d_reclen; 
     49  uint8_t             d_type; 
     50 
     51  std::string         d_name; 
     52}; 
     53 
     54class Directory : private std::vector<directory_entry> { 
    4655public: 
    47   typedef std::list<std::string> Base; 
     56  typedef std::vector<directory_entry> base_type; 
    4857 
    49   using Base::iterator; 
    50   using Base::const_iterator; 
    51   using Base::reverse_iterator; 
    52   using Base::const_reverse_iterator; 
     58  using base_type::iterator; 
     59  using base_type::const_iterator; 
     60  using base_type::reverse_iterator; 
     61  using base_type::const_reverse_iterator; 
     62  using base_type::value_type; 
    5363 
    54   using Base::begin; 
    55   using Base::end; 
    56   using Base::rbegin; 
    57   using Base::rend; 
     64  using base_type::begin; 
     65  using base_type::end; 
     66  using base_type::rbegin; 
     67  using base_type::rend; 
    5868 
    59   using Base::empty; 
    60   using Base::size; 
     69  using base_type::empty; 
     70  using base_type::size; 
    6171 
    62   using Base::erase; 
     72  using base_type::erase; 
    6373 
    6474  Directory() {} 
     
    6979  bool                update(bool hideDot = true); 
    7080 
     81  // Ergh... 
    7182  const std::string&  get_path() { return m_path; } 
     83  void                set_path(const std::string& path) { m_path = path; } 
    7284 
    7385  // Make a list with full path names. 
    74   Base                make_list(); 
     86  // 
     87  // Fix the uses of this, real bad stuff. 
     88  std::vector<std::string> make_list(); 
    7589 
    7690private: 
     
    7892}; 
    7993 
     94inline bool operator == (const directory_entry& left, const directory_entry& right) { return left.d_name == right.d_name; } 
     95inline bool operator != (const directory_entry& left, const directory_entry& right) { return left.d_name != right.d_name; } 
     96inline bool operator <  (const directory_entry& left, const directory_entry& right) { return left.d_name <  right.d_name; } 
     97inline bool operator >  (const directory_entry& left, const directory_entry& right) { return left.d_name >  right.d_name; } 
     98inline bool operator <= (const directory_entry& left, const directory_entry& right) { return left.d_name <= right.d_name; } 
     99inline bool operator >= (const directory_entry& left, const directory_entry& right) { return left.d_name >= right.d_name; } 
     100 
    80101} 
    81102