Remove path::walker_t

The intent of this class was to allow ad-hoc recursive/lazy walking of a directory structure and having links only visited once.

It was however only used once, and for that task, path::entries() is sufficient.
This commit is contained in:
Allan Odgaard
2013-05-14 21:38:11 +07:00
parent 6e4926177e
commit 58dc0b0491
3 changed files with 12 additions and 124 deletions

View File

@@ -210,26 +210,26 @@ namespace document
}
}
path::walker_ptr walker = path::open_for_walk(session_dir());
iterate(path, *walker)
for(auto dirEntry : path::entries(session_dir()))
{
std::string const& attr = path::get_attr(*path, "com.macromates.backup.identifier");
std::string const path = path::join(session_dir(), dirEntry->d_name);
std::string const attr = path::get_attr(path, "com.macromates.backup.identifier");
if(attr != NULL_STR && uuid == oak::uuid_t(attr))
{
document_ptr res = document_ptr(new document_t);
res->_identifier = uuid;
res->_backup_path = *path;
res->_backup_path = path;
res->_path = path::get_attr(*path, "com.macromates.backup.path");
res->_path = path::get_attr(path, "com.macromates.backup.path");
res->_key = path::identifier_t(res->_path);
res->_file_type = path::get_attr(*path, "com.macromates.backup.file-type");
res->_disk_encoding = path::get_attr(*path, "com.macromates.backup.encoding");
res->_disk_bom = path::get_attr(*path, "com.macromates.backup.bom") == "YES";
res->_disk_newlines = path::get_attr(*path, "com.macromates.backup.newlines");
res->_untitled_count = atoi(path::get_attr(*path, "com.macromates.backup.untitled-count").c_str());
res->_custom_name = path::get_attr(*path, "com.macromates.backup.custom-name");
res->_modified = path::get_attr(*path, "com.macromates.backup.modified") == "YES";
res->_file_type = path::get_attr(path, "com.macromates.backup.file-type");
res->_disk_encoding = path::get_attr(path, "com.macromates.backup.encoding");
res->_disk_bom = path::get_attr(path, "com.macromates.backup.bom") == "YES";
res->_disk_newlines = path::get_attr(path, "com.macromates.backup.newlines");
res->_untitled_count = atoi(path::get_attr(path, "com.macromates.backup.untitled-count").c_str());
res->_custom_name = path::get_attr(path, "com.macromates.backup.custom-name");
res->_modified = path::get_attr(path, "com.macromates.backup.modified") == "YES";
add(res);
return res;

View File

@@ -712,74 +712,10 @@ namespace path
return NULL_STR;
}
// ==========
// = Walker =
// ==========
void walker_t::rebalance () const
{
while(files.empty() && !paths.empty())
{
struct dirent** entries;
int size = scandir(paths.front().c_str(), &entries, NULL, NULL);
if(size != -1)
{
for(int i = 0; i < size; ++i)
{
std::string const& name = entries[i]->d_name;
if(name != "." && name != "..")
{
std::string const& path = join(paths.front(), name);
if(seen.insert(identifier(path)).second)
files.push_back(path);
}
free(entries[i]);
}
free(entries);
}
paths.erase(paths.begin());
}
}
void walker_t::push_back (std::string const& dir)
{
paths.push_back(dir);
rebalance();
}
bool walker_t::equal (size_t lhs, size_t rhs) const
{
if(paths.empty())
return std::min(lhs, files.size()) == std::min(rhs, files.size());
else return lhs == rhs;
}
std::string const& walker_t::at (size_t index) const
{
ASSERT_LT(index, files.size());
return files[index];
}
size_t walker_t::advance_from (size_t index) const
{
if(index + 1 == files.size())
{
files.clear();
rebalance();
return 0;
}
return index + 1;
}
// ===========
// = Actions =
// ===========
walker_ptr open_for_walk (std::string const& path, std::string const& glob)
{
return walker_ptr(new walker_t(path, glob));
}
std::string content (std::string const& path)
{
int fd = open(path.c_str(), O_RDONLY|O_CLOEXEC);

View File

@@ -89,54 +89,6 @@ namespace path
// = Actions =
// ===========
struct walker_t;
typedef std::shared_ptr<walker_t> walker_ptr;
typedef std::shared_ptr<walker_t const> walker_const_ptr;
struct PUBLIC walker_t : std::enable_shared_from_this<walker_t>
{
struct iterator_t
{
iterator_t (walker_const_ptr const& walker, size_t index) : walker(walker), index(index) { }
std::string const& operator* () const { return walker->at(index); }
std::string const* operator-> () const { return &walker->at(index); }
iterator_t& operator++ () { index = walker->advance_from(index); return *this; }
bool operator== (iterator_t const& rhs) const { return walker->equal(index, rhs.index); }
bool operator!= (iterator_t const& rhs) const { return !walker->equal(index, rhs.index); }
private:
walker_const_ptr walker;
size_t index;
};
typedef iterator_t iterator;
walker_t (std::string const& path, std::string const& glob = "*") { push_back(path); }
void push_back (std::string const& dir); // add a directory to the queue of what will be scanned (can be called between begin/end)
iterator_t begin () const { return iterator_t(shared_from_this(), 0); }
iterator_t end () const { return iterator_t(shared_from_this(), SIZE_T_MAX); }
private:
mutable std::vector<std::string> paths;
mutable std::vector<std::string> files;
mutable std::set<identifier_t> seen;
void rebalance () const;
// not implemented/allowed
walker_t ();
walker_t (walker_t const& rhs);
walker_t& operator= (walker_t const& rhs);
friend struct walker_t::iterator_t;
bool equal (size_t lhs, size_t rhs) const;
std::string const& at (size_t index) const;
size_t advance_from (size_t index) const;
};
PUBLIC walker_ptr open_for_walk (std::string const& path, std::string const& glob = "*"); // TODO support glob argument
PUBLIC std::string content (std::string const& path);
PUBLIC bool set_content (std::string const& path, char const* first, char const* last);
inline bool set_content (std::string const& path, std::string const& content) { return set_content(path, content.data(), content.data() + content.size()); }