header_utils
Loading...
Searching...
No Matches
filesystem.h
1
4
5#pragma once
6
7#include <filesystem>
8#include "expected.h"
9
11{
12 namespace stdfs = std::filesystem;
13
14 using stdfs::path;
15 using stdfs::file_status;
16 using stdfs::file_type;
17 using stdfs::file_time_type;
18
19#define GHPL_CALL_WEC(fname) []<typename... ARGS>(ARGS&&... args) { return fname(std::forward<ARGS>(args)...); }
20
21 inline auto absolute(const stdfs::path& path) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::absolute), path); }
22 inline auto canonical(const stdfs::path& path) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::canonical), path); }
23 inline auto weakly_canonical(const stdfs::path& path) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::weakly_canonical), path); }
24 inline auto relative(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::relative), p); }
25 inline auto relative(const stdfs::path& p, const stdfs::path& base = stdfs::current_path()) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::relative), p, base); }
26 inline auto proximate(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::proximate), p); }
27 inline auto proximate(const stdfs::path& p, const stdfs::path& base = stdfs::current_path()) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::proximate), p, base); }
28 inline auto copy(const stdfs::path& from, const stdfs::path& to) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::copy), from, to); }
29 inline auto copy(const stdfs::path& from, const stdfs::path& to, stdfs::copy_options options) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::copy), from, to, options); }
30 inline auto copy_file(const stdfs::path& from, const stdfs::path& to) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::copy_file), from, to); }
31 inline auto copy_file(const stdfs::path& from, const stdfs::path& to, stdfs::copy_options options) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::copy_file), from, to, options); }
33 inline auto copy_symlink(const stdfs::path& from, const stdfs::path& to) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::copy_symlink), from, to); }
34 inline auto create_directory(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_directory), p); }
35 inline auto create_directory(const stdfs::path& p, const stdfs::path& existing_p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_directory), p, existing_p); }
36 inline auto create_directories(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_directories), p); }
37 inline auto create_hard_link(const stdfs::path& target, const stdfs::path& link) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_hard_link), target, link); }
38 inline auto create_symlink(const stdfs::path& target, const stdfs::path& link) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_symlink), target, link); }
39 inline auto create_directory_symlink(const stdfs::path& target, const stdfs::path& link) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::create_directory_symlink), target, link); }
40 inline auto current_path() { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::current_path)); }
41 inline auto current_path(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::current_path), p); }
42 inline bool exists(stdfs::file_status s) noexcept { return stdfs::exists(s); }
43 inline auto exists(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::exists), p); }
44 inline auto equivalent(const stdfs::path& p1, const stdfs::path& p2) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::equivalent), p1, p2); }
45 inline auto file_size(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::file_size), p); }
46 inline auto hard_link_count(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::hard_link_count), p); }
47 inline auto last_write_time(const stdfs::path& path) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::last_write_time), path); }
48 inline auto last_write_time(const stdfs::path& path, stdfs::file_time_type new_time) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::last_write_time), path, new_time); }
49 inline auto permissions(const stdfs::path& p, stdfs::perms prms) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::permissions), p, prms); }
50 inline auto permissions(const stdfs::path& p, stdfs::perms prms, stdfs::perm_options opts = stdfs::perm_options::replace) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::permissions), p, prms, opts); }
51 inline auto read_symlink(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::read_symlink), p); }
52 inline auto remove(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::remove), p); }
53 inline auto remove_all(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::remove_all), p); }
54 inline auto rename(const stdfs::path& from, const stdfs::path& to) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::rename), from, to); }
55 inline auto resize_file(const stdfs::path& p, uintmax_t size) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::resize_file), p, size); }
56 inline auto space(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::space), p); }
57 inline auto status(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::status), p); }
58 inline auto status_known(stdfs::file_status s) noexcept { return stdfs::status_known(s); }
59 inline auto symlink_status(const stdfs::path& p) noexcept { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::symlink_status), p); }
60 inline auto temp_directory_path() { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::temp_directory_path)); }
61
62 inline auto is_block_file(stdfs::file_status s) noexcept { return stdfs::is_block_file(s); }
63 inline auto is_block_file(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_block_file), p); }
64 inline auto is_character_file(stdfs::file_status s) noexcept { return stdfs::is_character_file(s); }
65 inline auto is_character_file(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_character_file), p); }
66 inline auto is_directory(stdfs::file_status s) noexcept { return stdfs::is_directory(s); }
67 inline auto is_directory(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_directory), p); }
68 inline auto is_empty(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_empty), p); }
69 inline auto is_fifo(stdfs::file_status s) noexcept { return stdfs::is_fifo(s); }
70 inline auto is_fifo(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_fifo), p); }
71 inline auto is_other(stdfs::file_status s) noexcept { return stdfs::is_other(s); }
72 inline auto is_other(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_other), p); }
73 inline auto is_regular_file(stdfs::file_status s) noexcept { return stdfs::is_regular_file(s); }
74 inline auto is_regular_file(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_regular_file), p); }
75 inline auto is_socket(stdfs::file_status s) noexcept { return stdfs::is_socket(s); }
76 inline auto is_socket(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_socket), p); }
77 inline auto is_symlink(stdfs::file_status s) noexcept { return stdfs::is_symlink(s); }
78 inline auto is_symlink(const stdfs::path& p) { return call_with_expected_ec(GHPL_CALL_WEC(stdfs::is_symlink), p); }
79
80 //using path_view = std::basic_string_view<stdfs::path::value_type>;
81
83 inline void path_append(std::string& to, path const& p)
84 {
85#if 1
86 static_assert(stdfs::path::preferred_separator < 128);
87
88 if (p.is_absolute() || to.empty())
89 {
90 to = p.string();
91 return;
92 }
93
94 while (to.ends_with(stdfs::path::preferred_separator))
95 to.pop_back();
96
97 auto str = p.string();
98 auto sv = std::string_view{ str };
99 while (sv.starts_with(stdfs::path::preferred_separator))
100 sv.remove_prefix(1);
101
102 to += (char)stdfs::path::preferred_separator;
103 to += sv;
104#else
105 to = (to / p).string();
106#endif
107 }
108
109#undef GHPL_CALL_WEC
110}
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
constexpr CONTAINER to(RANGE &&range, TYPES &&... args)
to<container>();
Definition ranges.h:369
auto copy_symlink(const stdfs::path &from, const stdfs::path &to)
This is supposed to be noexcept but msvc's impl isn't.
Definition filesystem.h:33
void path_append(std::string &to, path const &p)
As if to /= p
Definition filesystem.h:83
auto call_with_expected_ec(FUNC &&func, ARGS &&... args) noexcept(noexcept(func(std::forward< ARGS >(args)...))) -> expected< std::invoke_result_t< FUNC, ARGS &&... >, std::error_code >
Calls the given function with args and an std::error_code as the last argument.
Definition expected.h:38