File: Clean up directory_iterator construction, preserve pattern.

FatFs requires pattern pointer to be stable during search.
This commit is contained in:
Jared Boone 2016-09-07 20:46:45 -07:00
parent 2740761ed7
commit 79330015ed
2 changed files with 9 additions and 7 deletions

View File

@ -126,7 +126,7 @@ Optional<File::Error> File::sync() {
static std::filesystem::path find_last_file_matching_pattern(const std::filesystem::path& pattern) { static std::filesystem::path find_last_file_matching_pattern(const std::filesystem::path& pattern) {
std::filesystem::path last_match; std::filesystem::path last_match;
for(const auto& entry : std::filesystem::directory_iterator(u"", pattern.c_str())) { for(const auto& entry : std::filesystem::directory_iterator(u"", pattern)) {
if( std::filesystem::is_regular_file(entry.status()) ) { if( std::filesystem::is_regular_file(entry.status()) ) {
const auto match = entry.path(); const auto match = entry.path();
if( match > last_match ) { if( match > last_match ) {
@ -211,11 +211,12 @@ std::string filesystem_error::what() const {
} }
directory_iterator::directory_iterator( directory_iterator::directory_iterator(
const std::filesystem::path::value_type* path, std::filesystem::path path,
const std::filesystem::path::value_type* wild std::filesystem::path wild
) { ) : pattern { wild }
{
impl = std::make_shared<Impl>(); impl = std::make_shared<Impl>();
const auto result = f_findfirst(&impl->dir, &impl->filinfo, reinterpret_cast<const TCHAR*>(path), reinterpret_cast<const TCHAR*>(wild)); const auto result = f_findfirst(&impl->dir, &impl->filinfo, reinterpret_cast<const TCHAR*>(path.c_str()), reinterpret_cast<const TCHAR*>(pattern.c_str()));
if( result != FR_OK ) { if( result != FR_OK ) {
impl.reset(); impl.reset();
// TODO: Throw exception if/when I enable exceptions... // TODO: Throw exception if/when I enable exceptions...

View File

@ -97,6 +97,7 @@ class directory_iterator {
}; };
std::shared_ptr<Impl> impl; std::shared_ptr<Impl> impl;
const path pattern;
friend bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs); friend bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs);
@ -108,7 +109,7 @@ public:
using iterator_category = std::input_iterator_tag; using iterator_category = std::input_iterator_tag;
directory_iterator() noexcept { }; directory_iterator() noexcept { };
directory_iterator(const std::filesystem::path::value_type* path, const std::filesystem::path::value_type* wild); directory_iterator(std::filesystem::path path, std::filesystem::path wild);
~directory_iterator() { } ~directory_iterator() { }