2017-04-05 18:53:14 +00:00
|
|
|
/** @addtogroup Utilities
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file filesystem.hh
|
|
|
|
*
|
|
|
|
* @brief Filesystem abstraction module.
|
|
|
|
*
|
|
|
|
* This module defines the namespace ostd::filesystem, which is either
|
|
|
|
* std::experimental::filesystem or std::filesystem depending on which
|
|
|
|
* is available. For portable applications, only use the subset of the
|
|
|
|
* module covered by both versions.
|
|
|
|
*
|
|
|
|
* It also provides range integration for directory iterators and
|
|
|
|
* ostd::format_traits specialization for std::filesystem::path.
|
2015-09-04 17:25:17 +00:00
|
|
|
*
|
2017-06-10 15:47:26 +00:00
|
|
|
* Additionally, it implements glob matching following POSIX with its
|
|
|
|
* own extensions (mainly recursive glob matching via `**`).
|
|
|
|
*
|
2017-06-11 16:51:05 +00:00
|
|
|
* @include glob.cc
|
2017-05-03 00:14:27 +00:00
|
|
|
* @include listdir.cc
|
|
|
|
*
|
2017-04-05 18:53:14 +00:00
|
|
|
* @copyright See COPYING.md in the project tree for further information.
|
2015-09-04 17:25:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSTD_FILESYSTEM_HH
|
|
|
|
#define OSTD_FILESYSTEM_HH
|
|
|
|
|
2017-03-10 17:19:59 +00:00
|
|
|
#if __has_include(<filesystem>)
|
|
|
|
# include <filesystem>
|
|
|
|
namespace ostd {
|
|
|
|
namespace filesystem = std::filesystem;
|
|
|
|
}
|
|
|
|
#elif __has_include(<experimental/filesystem>)
|
|
|
|
# include <experimental/filesystem>
|
|
|
|
namespace ostd {
|
|
|
|
namespace filesystem = std::experimental::filesystem;
|
|
|
|
}
|
2016-07-06 17:31:21 +00:00
|
|
|
#else
|
2017-03-10 17:19:59 +00:00
|
|
|
# error "Unsupported platform"
|
2015-09-06 15:07:14 +00:00
|
|
|
#endif
|
2015-09-04 17:25:17 +00:00
|
|
|
|
2017-06-19 14:59:36 +00:00
|
|
|
#include <ostd/platform.hh>
|
|
|
|
#include <ostd/range.hh>
|
|
|
|
#include <ostd/format.hh>
|
2015-09-04 17:25:17 +00:00
|
|
|
|
|
|
|
namespace ostd {
|
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @addtogroup Utilities
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @brief Range integration for std::filesystem::directory_iterator.
|
|
|
|
*
|
|
|
|
* Allows directory iterators to be made into ranges via ostd::iter().
|
|
|
|
*
|
|
|
|
* @see ostd::ranged_traits<filesystem::recursive_directory_iterator>
|
|
|
|
*/
|
2017-03-10 17:19:59 +00:00
|
|
|
template<>
|
|
|
|
struct ranged_traits<filesystem::directory_iterator> {
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief The range type for the iterator. */
|
2017-03-10 17:19:59 +00:00
|
|
|
using range = iterator_range<filesystem::directory_iterator>;
|
2015-09-05 02:35:07 +00:00
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief Creates a range for the iterator. */
|
2017-03-10 17:19:59 +00:00
|
|
|
static range iter(filesystem::directory_iterator const &r) {
|
|
|
|
return range{filesystem::begin(r), filesystem::end(r)};
|
2015-09-05 02:35:07 +00:00
|
|
|
}
|
2015-09-04 17:25:17 +00:00
|
|
|
};
|
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief Range integration for std::filesystem::recursive_directory_iterator.
|
|
|
|
*
|
|
|
|
* Allows recursive directory iterators to be made into ranges via ostd::iter().
|
|
|
|
*
|
|
|
|
* @see ostd::ranged_traits<filesystem::directory_iterator>
|
|
|
|
*/
|
2017-03-10 17:19:59 +00:00
|
|
|
template<>
|
|
|
|
struct ranged_traits<filesystem::recursive_directory_iterator> {
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief The range type for the iterator. */
|
2017-03-10 17:19:59 +00:00
|
|
|
using range = iterator_range<filesystem::recursive_directory_iterator>;
|
2015-09-08 00:03:56 +00:00
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief Creates a range for the iterator. */
|
2017-03-10 17:19:59 +00:00
|
|
|
static range iter(filesystem::recursive_directory_iterator const &r) {
|
|
|
|
return range{filesystem::begin(r), filesystem::end(r)};
|
2015-09-08 00:03:56 +00:00
|
|
|
}
|
|
|
|
};
|
2015-09-04 17:25:17 +00:00
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief ostd::format_traits specialization for std::filesystem::path.
|
|
|
|
*
|
|
|
|
* This allows paths to be formatted as strings. The value is formatted
|
|
|
|
* as if `path.string()` was formatted, using the exact ostd::format_spec.
|
|
|
|
*/
|
2017-03-10 17:19:59 +00:00
|
|
|
template<>
|
|
|
|
struct format_traits<filesystem::path> {
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @brief Formats the path's string value.
|
|
|
|
*
|
|
|
|
* This calls exactly
|
|
|
|
*
|
|
|
|
* ~~~{.cc}
|
|
|
|
* fs.format_value(writer, p.string());
|
|
|
|
* ~~~
|
|
|
|
*/
|
2017-03-10 17:19:59 +00:00
|
|
|
template<typename R>
|
|
|
|
static void to_format(
|
|
|
|
filesystem::path const &p, R &writer, format_spec const &fs
|
|
|
|
) {
|
|
|
|
fs.format_value(writer, p.string());
|
2015-09-04 17:25:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-10 15:47:26 +00:00
|
|
|
namespace detail {
|
2017-06-16 16:54:59 +00:00
|
|
|
OSTD_EXPORT bool glob_match_filename_impl(
|
2017-06-10 15:47:26 +00:00
|
|
|
typename filesystem::path::value_type const *fname,
|
|
|
|
typename filesystem::path::value_type const *wname
|
2017-06-16 16:54:59 +00:00
|
|
|
) noexcept;
|
|
|
|
|
|
|
|
OSTD_EXPORT void glob_match_impl(
|
|
|
|
void (*out)(filesystem::path const &, void *),
|
|
|
|
typename filesystem::path::iterator beg,
|
|
|
|
typename filesystem::path::iterator &end,
|
|
|
|
filesystem::path pre, void *data
|
|
|
|
);
|
2017-06-10 18:30:48 +00:00
|
|
|
} /* namespace detail */
|
2017-06-10 15:47:26 +00:00
|
|
|
|
2017-06-11 16:09:57 +00:00
|
|
|
/** @brief Checks if the given path matches the given glob pattern.
|
|
|
|
*
|
|
|
|
* This matches the given filename against POSIX-style glob patterns.
|
|
|
|
* The following patterns are supported:
|
|
|
|
*
|
|
|
|
* | Pattern | Description |
|
|
|
|
* |---------|----------------------------------------------------|
|
|
|
|
* | * | 0 or more characters |
|
|
|
|
* | ? | any single character |
|
|
|
|
* | [abc] | one character in the brackets |
|
|
|
|
* | [a-z] | one character within the range in the brackets |
|
|
|
|
* | [!abc] | one character not in the brackets |
|
|
|
|
* | [!a-z] | one character not within the range in the brackets |
|
|
|
|
*
|
|
|
|
* The behavior is the same as in POSIX. You can combine ranges and
|
|
|
|
* individual characters in the `[]` pattern together as well as define
|
|
|
|
* multiple ranges in one (e.g. `[a-zA-Z_?]` matching alphabetics,
|
|
|
|
* an underscore and a question mark). The behavior of the range varies
|
|
|
|
* by locale. If the second character in the range is lower in value
|
|
|
|
* than the first one, a match will never happen. To match the `]`
|
|
|
|
* character in the brackets, make it the first one. To match the
|
|
|
|
* dash character, make it the first or the last.
|
|
|
|
*
|
|
|
|
* You can also use the brackets to escape metacharacters. So to
|
|
|
|
* match a literal `*`, use `[*]`.
|
|
|
|
*
|
|
|
|
* Keep in mind that an invalid bracket syntax (unterminated) will
|
|
|
|
* always cause this to return `false`.
|
|
|
|
*
|
|
|
|
* This function is used in ostd::glob_match().
|
|
|
|
*/
|
2017-06-10 18:30:48 +00:00
|
|
|
inline bool glob_match_filename(
|
|
|
|
filesystem::path const &filename, filesystem::path const &pattern
|
2017-06-11 16:09:57 +00:00
|
|
|
) noexcept {
|
2017-06-10 18:30:48 +00:00
|
|
|
return detail::glob_match_filename_impl(filename.c_str(), pattern.c_str());
|
|
|
|
}
|
|
|
|
|
2017-06-11 16:09:57 +00:00
|
|
|
/** @brief Expands a path with glob patterns.
|
|
|
|
*
|
|
|
|
* Individual expanded paths are put in `out` and are of the standard
|
|
|
|
* std::filesystem::path type. It supports standard patterns as defined
|
|
|
|
* in ostd::glob_match_filename().
|
|
|
|
*
|
|
|
|
* So for example, `*.cc` will expand to `one.cc`, `two.cc` and so on.
|
|
|
|
* A pattern like `foo/[cb]at.txt` will match `foo/cat.txt` and `foo/bat.txt`
|
|
|
|
* but not `foo/Cat.txt`. The `foo/?at.txt` will match `foo/cat.txt`,
|
|
|
|
* `foo/Cat.txt`, `foo/pat.txt`, `foo/vat.txt` or any other character
|
|
|
|
* in the place.
|
|
|
|
*
|
|
|
|
* Additionally, a special `**` pattern is also supported which is not
|
|
|
|
* matched by ostd::glob_match_filename(). It's only allowed if the entire
|
|
|
|
* filename or directory name is `**`. When used as a directory name, it
|
|
|
|
* will expand to all directories in the location and all subdirectories
|
|
|
|
* of those directories. If used as a filename (at the end of the path),
|
|
|
|
* then it expands to directories and subdirectories aswell as all files
|
|
|
|
* in the location and in the directories or subdirectories. Keep in mind
|
|
|
|
* that it is not a regular pattern and a `**` when found in a regular
|
|
|
|
* context (i.e. not as entire filename/directory name) will be treated
|
|
|
|
* as two regular `*` patterns.
|
|
|
|
*
|
|
|
|
* @throws std::filesystem_error if a filesystem error occurs.
|
|
|
|
* @returns The forwarded `out`.
|
|
|
|
*/
|
|
|
|
template<typename OutputRange>
|
|
|
|
inline OutputRange &&glob_match(
|
|
|
|
OutputRange &&out, filesystem::path const &path
|
|
|
|
) {
|
2017-06-10 15:47:26 +00:00
|
|
|
auto pend = path.end();
|
2017-06-16 16:54:59 +00:00
|
|
|
detail::glob_match_impl([](filesystem::path const &p, void *outp) {
|
|
|
|
static_cast<std::remove_reference_t<OutputRange> *>(outp)->put(p);
|
|
|
|
}, path.begin(), pend, filesystem::path{}, &out);
|
2017-06-11 16:09:57 +00:00
|
|
|
return std::forward<OutputRange>(out);
|
2017-06-10 15:47:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 18:53:14 +00:00
|
|
|
/** @} */
|
|
|
|
|
2015-09-04 17:25:17 +00:00
|
|
|
} /* namespace ostd */
|
|
|
|
|
2016-02-07 21:17:15 +00:00
|
|
|
#endif
|
2017-04-05 18:53:14 +00:00
|
|
|
|
|
|
|
/** @} */
|