Код: Выделить всё
template
void for_dirents_in_path(const std::filesystem::path& path, T callback) {
std::error_code ec;
for (auto& dirent : DirectoryIterator(path, ec)) {
if (!dirent.is_regular_file()) {
continue;
}
callback(std::move(dirent));
}
}
template
void for_dirents_with_filename(const std::filesystem::path& path,
const std::string& filename,
T callback) {
for_dirents_in_path(
path, [&](const std::filesystem::directory_entry&& dirent) {
// you may also filter on e.g. dirent.path().extension()
if (dirent.path().filename() != filename) {
return;
}
callback(std::move(dirent));
});
}
Код: Выделить всё
_NODISCARD inline wstring_view _Parse_filename(const wstring_view _Str) {
// attempt to parse _Str as a path and return the filename if it exists; otherwise, an empty view
const auto _First = _Str.data();
const auto _Last = _First + _Str.size();
const auto _Filename = _Find_filename(_First, _Last);
return wstring_view(_Filename, static_cast(_Last - _Filename));
}
_NODISCARD path filename() const {
// parse the filename from *this and return a copy if present; otherwise, return the empty path
return _Parse_filename(_Text);
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... ithout-cop
Мобильная версия