header_utils
Loading...
Searching...
No Matches
filesystem.h
1
4
5#pragma once
6
7#include "min-cpp-version/cpp17.h"
8#include <filesystem>
9#include "expected.h"
10
12{
14
15 namespace stdfs = std::filesystem;
16
17 using stdfs::path;
18 using stdfs::file_status;
19 using stdfs::file_type;
20 using stdfs::file_time_type;
21
24
29
30 [[nodiscard]] inline auto absolute(const stdfs::path& path) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::absolute(path, ec); if (ec) return unexpected(ec); return result; }
31 [[nodiscard]] inline auto canonical(const stdfs::path& path) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::canonical(path, ec); if (ec) return unexpected(ec); return result; }
32 [[nodiscard]] inline auto weakly_canonical(const stdfs::path& path) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::weakly_canonical(path, ec); if (ec) return unexpected(ec); return result; }
33 [[nodiscard]] inline auto relative(const stdfs::path& p) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::relative(p, ec); if (ec) return unexpected(ec); return result; }
34 [[nodiscard]] inline auto relative(const stdfs::path& p, const stdfs::path& base) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::relative(p, base, ec); if (ec) return unexpected(ec); return result; }
35 [[nodiscard]] inline auto proximate(const stdfs::path& p) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::proximate(p, ec); if (ec) return unexpected(ec); return result; }
36 [[nodiscard]] inline auto proximate(const stdfs::path& p, const stdfs::path& base) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::proximate(p, base, ec); if (ec) return unexpected(ec); return result; }
37 inline auto copy(const stdfs::path& from, const stdfs::path& to) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::copy(from, to, ec); if (ec) return unexpected(ec); return {}; }
38 inline auto copy(const stdfs::path& from, const stdfs::path& to, stdfs::copy_options options) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::copy(from, to, options, ec); if (ec) return unexpected(ec); return {}; }
39 inline auto copy_file(const stdfs::path& from, const stdfs::path& to) -> expected<uintmax_t, std::error_code> { std::error_code ec{}; auto result = stdfs::copy_file(from, to, ec); if (ec) return unexpected(ec); return result; }
40 inline auto copy_file(const stdfs::path& from, const stdfs::path& to, stdfs::copy_options options) -> expected<uintmax_t, std::error_code> { std::error_code ec{}; auto result = stdfs::copy_file(from, to, options, ec); if (ec) return unexpected(ec); return result; }
41 inline auto copy_symlink(const stdfs::path& from, const stdfs::path& to) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::copy_symlink(from, to, ec); if (ec) return unexpected(ec); return {}; }
42 inline auto create_directory(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::create_directory(p, ec); if (ec) return unexpected(ec); return result; }
43 inline auto create_directory(const stdfs::path& p, const stdfs::path& existing_p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::create_directory(p, existing_p, ec); if (ec) return unexpected(ec); return result; }
44 inline auto create_directories(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::create_directories(p, ec); if (ec) return unexpected(ec); return result; }
45 inline auto create_hard_link(const stdfs::path& target, const stdfs::path& link) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::create_hard_link(target, link, ec); if (ec) return unexpected(ec); return {}; }
46 inline auto create_symlink(const stdfs::path& target, const stdfs::path& link) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::create_symlink(target, link, ec); if (ec) return unexpected(ec); return {}; }
47 inline auto create_directory_symlink(const stdfs::path& target, const stdfs::path& link) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::create_directory_symlink(target, link, ec); if (ec) return unexpected(ec); return {}; }
48 [[nodiscard]] inline auto current_path() -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::current_path(ec); if (ec) return unexpected(ec); return result; }
49 inline auto current_path(const stdfs::path& p) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::current_path(p, ec); if (ec) return unexpected(ec); return {}; }
50 [[nodiscard]] inline auto exists(stdfs::file_status s) noexcept { return stdfs::exists(s); }
51 [[nodiscard]] inline auto exists(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::exists(p, ec); if (ec) return unexpected(ec); return result; }
52 [[nodiscard]] inline auto equivalent(const stdfs::path& p1, const stdfs::path& p2) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::equivalent(p1, p2, ec); if (ec) return unexpected(ec); return result; }
53 [[nodiscard]] inline auto file_size(const stdfs::path& p) -> expected<uintmax_t, std::error_code> { std::error_code ec{}; auto result = stdfs::file_size(p, ec); if (ec) return unexpected(ec); return result; }
54 [[nodiscard]] inline auto hard_link_count(const stdfs::path& p) -> expected<uintmax_t, std::error_code> { std::error_code ec{}; auto result = stdfs::hard_link_count(p, ec); if (ec) return unexpected(ec); return result; }
55 [[nodiscard]] inline auto last_write_time(const stdfs::path& path) -> expected<stdfs::file_time_type, std::error_code> { std::error_code ec{}; auto result = stdfs::last_write_time(path, ec); if (ec) return unexpected(ec); return result; }
56 inline auto last_write_time(const stdfs::path& path, stdfs::file_time_type new_time) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::last_write_time(path, new_time, ec); if (ec) return unexpected(ec); return {}; }
57 [[nodiscard]] inline auto permissions(const stdfs::path& p, stdfs::perms prms) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::permissions(p, prms, ec); if (ec) return unexpected(ec); return {}; }
58 inline auto permissions(const stdfs::path& p, stdfs::perms prms, stdfs::perm_options opts) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::permissions(p, prms, opts, ec); if (ec) return unexpected(ec); return {}; }
59 [[nodiscard]] inline auto read_symlink(const stdfs::path& p) -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::read_symlink(p, ec); if (ec) return unexpected(ec); return result; }
60 inline auto remove(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::remove(p, ec); if (ec) return unexpected(ec); return result; }
61 inline auto remove_all(const stdfs::path& p) -> expected<uintmax_t, std::error_code> { std::error_code ec{}; auto result = stdfs::remove_all(p, ec); if (ec) return unexpected(ec); return result; }
62 inline auto rename(const stdfs::path& from, const stdfs::path& to) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::rename(from, to, ec); if (ec) return unexpected(ec); return {}; }
63 inline auto resize_file(const stdfs::path& p, uintmax_t size) -> expected<void, std::error_code> { std::error_code ec{}; stdfs::resize_file(p, size, ec); if (ec) return unexpected(ec); return {}; }
64 [[nodiscard]] inline auto space(const stdfs::path& p) -> expected<stdfs::space_info, std::error_code> { std::error_code ec{}; auto result = stdfs::space(p, ec); if (ec) return unexpected(ec); return result; }
65 [[nodiscard]] inline auto status(const stdfs::path& p) -> expected<stdfs::file_status, std::error_code> { std::error_code ec{}; auto result = stdfs::status(p, ec); if (ec) return unexpected(ec); return result; }
66 [[nodiscard]] inline auto status_known(stdfs::file_status s) noexcept { return stdfs::status_known(s); }
67 [[nodiscard]] inline auto symlink_status(const stdfs::path& p) -> expected<stdfs::file_status, std::error_code> { std::error_code ec{}; auto result = stdfs::symlink_status(p, ec); if (ec) return unexpected(ec); return result; }
68 [[nodiscard]] inline auto temp_directory_path() -> expected<stdfs::path, std::error_code> { std::error_code ec{}; auto result = stdfs::temp_directory_path(ec); if (ec) return unexpected(ec); return result; }
69
70 [[nodiscard]] inline auto is_block_file(stdfs::file_status s) noexcept { return stdfs::is_block_file(s); }
71 [[nodiscard]] inline auto is_character_file(stdfs::file_status s) noexcept { return stdfs::is_character_file(s); }
72 [[nodiscard]] inline auto is_directory(stdfs::file_status s) noexcept { return stdfs::is_directory(s); }
73 [[nodiscard]] inline auto is_fifo(stdfs::file_status s) noexcept { return stdfs::is_fifo(s); }
74 [[nodiscard]] inline auto is_other(stdfs::file_status s) noexcept { return stdfs::is_other(s); }
75 [[nodiscard]] inline auto is_regular_file(stdfs::file_status s) noexcept { return stdfs::is_regular_file(s); }
76 [[nodiscard]] inline auto is_socket(stdfs::file_status s) noexcept { return stdfs::is_socket(s); }
77 [[nodiscard]] inline auto is_symlink(stdfs::file_status s) noexcept { return stdfs::is_symlink(s); }
78
79 [[nodiscard]] inline auto is_block_file(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_block_file(p, ec); if (ec) return unexpected(ec); return result; }
80 [[nodiscard]] inline auto is_character_file(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_character_file(p, ec); if (ec) return unexpected(ec); return result; }
81 [[nodiscard]] inline auto is_directory(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_directory(p, ec); if (ec) return unexpected(ec); return result; }
82 [[nodiscard]] inline auto is_empty(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_empty(p, ec); if (ec) return unexpected(ec); return result; }
83 [[nodiscard]] inline auto is_fifo(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_fifo(p, ec); if (ec) return unexpected(ec); return result; }
84 [[nodiscard]] inline auto is_other(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_other(p, ec); if (ec) return unexpected(ec); return result; }
85 [[nodiscard]] inline auto is_regular_file(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_regular_file(p, ec); if (ec) return unexpected(ec); return result; }
86 [[nodiscard]] inline auto is_socket(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_socket(p, ec); if (ec) return unexpected(ec); return result; }
87 [[nodiscard]] inline auto is_symlink(const stdfs::path& p) -> expected<bool, std::error_code> { std::error_code ec{}; auto result = stdfs::is_symlink(p, ec); if (ec) return unexpected(ec); return result; }
88
90
93 inline void path_append(std::string& to, path const& p)
94 {
95#if 1
96 static_assert(stdfs::path::preferred_separator < 128);
97
98 if (p.is_absolute() || to.empty())
99 {
100 to = p.string();
101 return;
102 }
103
104 while (!to.empty() && to.back() == stdfs::path::preferred_separator)
105 to.pop_back();
106
107 auto str = p.string();
108 auto sv = std::string_view{ str };
109 while (!sv.empty() && sv[0] == stdfs::path::preferred_separator)
110 sv.remove_prefix(1);
111
112 to += (char)stdfs::path::preferred_separator;
113 to += sv;
114#else
115 to = (to / p).string();
116#endif
117 }
118}
void path_append(std::string &to, path const &p)
As if to /= p.
Definition filesystem.h:93
constexpr CONTAINER to(RANGE &&range, TYPES &&... args)
to<container>();
Definition ranges.h:386