Add filesystem directory enumeration.

Borrow API from std::filesystem -- Boost and C++17.
This commit is contained in:
Jared Boone 2016-04-20 09:56:35 -07:00
parent 8119980370
commit 7492984144
2 changed files with 89 additions and 0 deletions

View File

@ -78,3 +78,33 @@ bool File::sync() {
const auto result = f_sync(&f);
return (result == FR_OK);
}
namespace std {
namespace filesystem {
directory_iterator::directory_iterator(
const char* path,
const char* wild
) {
impl = std::make_shared<Impl>();
const auto result = f_findfirst(&impl->dir, &impl->filinfo, path, wild);
if( result != FR_OK ) {
impl.reset();
// TODO: Throw exception if/when I enable exceptions...
}
}
directory_iterator& directory_iterator::operator++() {
const auto result = f_findnext(&impl->dir, &impl->filinfo);
if( (result != FR_OK) || (impl->filinfo.fname[0] == 0) ) {
impl.reset();
}
return *this;
}
bool is_regular_file(const file_status s) {
return !(s & AM_DIR);
}
} /* namespace filesystem */
} /* namespace std */

View File

@ -27,6 +27,8 @@
#include <cstddef>
#include <string>
#include <array>
#include <memory>
#include <iterator>
class File {
public:
@ -55,4 +57,61 @@ private:
FIL f;
};
namespace std {
namespace filesystem {
using file_status = BYTE;
struct directory_entry : public FILINFO {
file_status status() const {
return fattrib;
}
const std::string path() const noexcept { return fname; };
};
class directory_iterator {
struct Impl {
DIR dir;
directory_entry filinfo;
~Impl() {
f_closedir(&dir);
}
};
std::shared_ptr<Impl> impl;
friend bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs);
public:
using difference_type = std::ptrdiff_t;
using value_type = directory_entry;
using pointer = const directory_entry*;
using reference = const directory_entry&;
using iterator_category = std::input_iterator_tag;
directory_iterator() noexcept { };
directory_iterator(const char* path, const char* wild);
~directory_iterator() { }
directory_iterator& operator++();
reference operator*() const {
// TODO: Exception or assert if impl == nullptr.
return impl->filinfo;
}
};
inline const directory_iterator& begin(const directory_iterator& iter) noexcept { return iter; };
inline directory_iterator end(const directory_iterator&) noexcept { return { }; };
inline bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs) { return lhs.impl != rhs.impl; };
bool is_regular_file(const file_status s);
} /* namespace filesystem */
} /* namespace std */
#endif/*__FILE_H__*/