* Refactoring utils::Directory.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1022 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-12-23 19:12:23 +00:00
parent ccb3713a57
commit 7b053b969e
11 changed files with 126 additions and 62 deletions
+18 -7
View File
@@ -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
View File
@@ -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