mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-09 07:39:21 +00:00
* Refactoring utils::Directory.
git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1022 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
+18
-7
@@ -46,6 +46,7 @@
|
||||
|
||||
namespace utils {
|
||||
|
||||
// Keep this?
|
||||
bool
|
||||
Directory::is_valid() const {
|
||||
if (m_path.empty())
|
||||
@@ -57,6 +58,7 @@ Directory::is_valid() const {
|
||||
return d;
|
||||
}
|
||||
|
||||
// Update should take various flags and sort functors.
|
||||
bool
|
||||
Directory::update(bool hideDot) {
|
||||
if (m_path.empty())
|
||||
@@ -69,25 +71,34 @@ Directory::update(bool hideDot) {
|
||||
|
||||
struct dirent* ent;
|
||||
|
||||
// Err... let us use getdirentries here instead.
|
||||
while ((ent = readdir(d)) != NULL) {
|
||||
// Don't construct it here, check the const char.
|
||||
std::string de(ent->d_name);
|
||||
|
||||
if (!de.empty() && (!hideDot || de[0] != '.'))
|
||||
Base::push_back(ent->d_name);
|
||||
if (!de.empty() && (!hideDot || de[0] != '.')) {
|
||||
iterator itr = base_type::insert(end(), value_type());
|
||||
|
||||
itr->d_fileno = ent->d_fileno;
|
||||
itr->d_reclen = ent->d_reclen;
|
||||
itr->d_type = ent->d_type;
|
||||
itr->d_name = de;
|
||||
}
|
||||
}
|
||||
|
||||
closedir(d);
|
||||
Base::sort(std::less<std::string>());
|
||||
std::sort(begin(), end());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Directory::Base
|
||||
std::vector<std::string>
|
||||
Directory::make_list() {
|
||||
Base l;
|
||||
std::vector<std::string> l;
|
||||
l.reserve(size());
|
||||
|
||||
for (Base::iterator itr = begin(); itr != end(); ++itr)
|
||||
l.push_back(m_path + *itr);
|
||||
for (iterator itr = begin(); itr != end(); ++itr)
|
||||
l.push_back(m_path + itr->d_name);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
+36
-15
@@ -38,28 +38,38 @@
|
||||
#define RTORRENT_UTILS_DIRECTORY_H
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
namespace utils {
|
||||
|
||||
class Directory : private std::list<std::string> {
|
||||
struct directory_entry {
|
||||
// The name and types should match POSIX.
|
||||
uint32_t d_fileno;
|
||||
uint16_t d_reclen;
|
||||
uint8_t d_type;
|
||||
|
||||
std::string d_name;
|
||||
};
|
||||
|
||||
class Directory : private std::vector<directory_entry> {
|
||||
public:
|
||||
typedef std::list<std::string> Base;
|
||||
typedef std::vector<directory_entry> base_type;
|
||||
|
||||
using Base::iterator;
|
||||
using Base::const_iterator;
|
||||
using Base::reverse_iterator;
|
||||
using Base::const_reverse_iterator;
|
||||
using base_type::iterator;
|
||||
using base_type::const_iterator;
|
||||
using base_type::reverse_iterator;
|
||||
using base_type::const_reverse_iterator;
|
||||
using base_type::value_type;
|
||||
|
||||
using Base::begin;
|
||||
using Base::end;
|
||||
using Base::rbegin;
|
||||
using Base::rend;
|
||||
using base_type::begin;
|
||||
using base_type::end;
|
||||
using base_type::rbegin;
|
||||
using base_type::rend;
|
||||
|
||||
using Base::empty;
|
||||
using Base::size;
|
||||
using base_type::empty;
|
||||
using base_type::size;
|
||||
|
||||
using Base::erase;
|
||||
using base_type::erase;
|
||||
|
||||
Directory() {}
|
||||
Directory(const std::string& path) : m_path(path) {}
|
||||
@@ -68,15 +78,26 @@ public:
|
||||
|
||||
bool update(bool hideDot = true);
|
||||
|
||||
// Ergh...
|
||||
const std::string& get_path() { return m_path; }
|
||||
void set_path(const std::string& path) { m_path = path; }
|
||||
|
||||
// Make a list with full path names.
|
||||
Base make_list();
|
||||
//
|
||||
// Fix the uses of this, real bad stuff.
|
||||
std::vector<std::string> make_list();
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
};
|
||||
|
||||
inline bool operator == (const directory_entry& left, const directory_entry& right) { return left.d_name == right.d_name; }
|
||||
inline bool operator != (const directory_entry& left, const directory_entry& right) { return left.d_name != right.d_name; }
|
||||
inline bool operator < (const directory_entry& left, const directory_entry& right) { return left.d_name < right.d_name; }
|
||||
inline bool operator > (const directory_entry& left, const directory_entry& right) { return left.d_name > right.d_name; }
|
||||
inline bool operator <= (const directory_entry& left, const directory_entry& right) { return left.d_name <= right.d_name; }
|
||||
inline bool operator >= (const directory_entry& left, const directory_entry& right) { return left.d_name >= right.d_name; }
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user