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-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
|
|
|
|
|
|
|
#include "ostd/range.hh"
|
2017-03-10 17:19:59 +00:00
|
|
|
#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-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
|
|
|
|
|
|
|
/** @} */
|