Permalink
Browse files

Iterators are now returned by begin() and end()

This also fixes a couple of issues:

1. When we reached the end of a directory were were creating an empty
directory_entry, which doesn't make sense since the end iterator has
those set to 0's anyway and thus comparing a null ptr to an otherwise
empty object doesn't make much sense.

2. Fixes the operator!=, removes the old one that was created as a
temporary place holder, and makes sure that proper checking is done for
inequality.
  • Loading branch information...
1 parent 5dd2bc0 commit 87ba3fee8799fac9cff169412e8e2778f1f93888 @bertjwregeer committed Sep 6, 2011
Showing with 22 additions and 13 deletions.
  1. +18 −6 src/directory.cc
  2. +4 −7 src/directory.h
View
@@ -57,11 +57,11 @@ namespace dspawn
}
directory::iterator directory::begin() {
- return iterator();
+ return iterator(_full_path);
}
directory::iterator directory::end() {
- return iterator();
+ return iterator(0);
}
bool directory::_verify_is_dir(std::string dir) {
@@ -115,10 +115,6 @@ namespace dspawn
return *this;
}
- bool directory_iterator::operator!=(const directory_iterator other) {
- return (_entry != other._entry);
- }
-
void directory_iterator::_read_next() {
errno = 0;
@@ -132,7 +128,23 @@ namespace dspawn
delete _dir_entry;
}
+ // End of a directory.
+ if (_entry == 0) {
+ _dir_entry = 0;
+
+ return;
+ }
+
_dir_entry = new directory_entry(_entry);
+
+ return;
+ }
+
+ bool directory_iterator::operator!=(const directory_iterator& other) {
+ if (_dir_entry == 0 && other._dir_entry == 0) return false;
+ if (other._dir_entry == 0 && _dir_entry != 0) return true;
+
+ return !(*_dir_entry == *(other._dir_entry));
}
} /* namespace dspawn */
View
@@ -46,7 +46,7 @@ namespace dspawn
* @brief Takes care of certain operations to do with directories
* such as reading them and providing a list of files in them.
*/
- class directory
+ class directory
{
friend class directory_iterator;
@@ -63,7 +63,6 @@ namespace dspawn
bool is_dir(std::string dir);
std::vector<std::string> all_entries();
-
iterator begin();
iterator end();
@@ -79,7 +78,7 @@ namespace dspawn
* @class directory_iterator
* @brief An iterator that returns directory_entry's
*/
- class directory_iterator : public std::iterator<std::forward_iterator_tag, directory_entry>
+ class directory_iterator : public std::iterator<std::forward_iterator_tag, directory_entry>
{
friend class directory;
@@ -88,8 +87,8 @@ namespace dspawn
directory_entry& operator*();
const directory_iterator& operator++();
- bool operator!=(const directory_iterator other);
-
+ bool operator!=(const directory_iterator& other);
+
private:
/* data */
directory_iterator();
@@ -102,8 +101,6 @@ namespace dspawn
struct dirent *_entry;
directory_entry *_dir_entry;
};
-
-
} /* namespace dspawn */

0 comments on commit 87ba3fe

Please sign in to comment.