diff --git a/lib/directory.cpp b/lib/directory.cpp index 9496f43..1ba6c37 100644 --- a/lib/directory.cpp +++ b/lib/directory.cpp @@ -1,22 +1,24 @@ #include +#include +#include +#include #include +namespace fs = std::filesystem; + namespace { /// Creates a temporary directory with the given prefix in the system's /// temporary directory, and returns its path /// @param prefix The prefix to use for the temporary file name /// @returns A path to the created temporary file -/// @throws std::filesystem::filesystem_error if the temporary directory -/// cannot be created -std::filesystem::path create(std::string_view prefix) { - auto pattern = tmp::path::make_pattern(prefix); +/// @throws fs::filesystem_error if the temporary directory cannot be created +fs::path create(std::string_view prefix) { + std::string pattern = tmp::path::make_pattern(prefix); if (mkdtemp(pattern.data()) == nullptr) { - throw std::filesystem::filesystem_error( - "Cannot create temporary directory", - std::error_code(errno, std::system_category()) - ); + std::error_code ec = std::error_code(errno, std::system_category()); + throw fs::filesystem_error("Cannot create temporary directory", ec); } return pattern; @@ -28,8 +30,8 @@ namespace tmp { directory::directory(std::string_view prefix) : path(create(prefix)) {} -std::filesystem::path directory::operator/(std::string_view source) const { - return static_cast(*this) / source; +fs::path directory::operator/(std::string_view source) const { + return static_cast(*this) / source; } directory::~directory() noexcept = default; diff --git a/lib/file.cpp b/lib/file.cpp index 3ecdc78..8d1bcda 100644 --- a/lib/file.cpp +++ b/lib/file.cpp @@ -1,7 +1,12 @@ #include #include +#include #include +#include +#include + +namespace fs = std::filesystem; namespace { @@ -9,15 +14,12 @@ namespace { /// temporary directory, and returns its path /// @param prefix The prefix to use for the temporary file name /// @returns A path to the created temporary file -/// @throws std::filesystem::filesystem_error if the temporary file -/// cannot be created -std::filesystem::path create(std::string_view prefix) { - auto pattern = tmp::path::make_pattern(prefix); +/// @throws fs::filesystem_error if the temporary file cannot be created +fs::path create(std::string_view prefix) { + std::string pattern = tmp::path::make_pattern(prefix); if (mkstemp(pattern.data()) == -1) { - throw std::filesystem::filesystem_error( - "Cannot create temporary file", - std::error_code(errno, std::system_category()) - ); + std::error_code ec = std::error_code(errno, std::system_category()); + throw fs::filesystem_error("Cannot create temporary file", ec); } return pattern; @@ -34,8 +36,8 @@ std::ofstream stream(const tmp::file& file, bool binary, bool append) noexcept { mode |= std::ios::binary; } - const std::filesystem::path& path = file; - return std::ofstream { path, mode }; + const fs::path& path = file; + return std::ofstream(path, mode); } } // namespace diff --git a/lib/path.cpp b/lib/path.cpp index 96b6e85..2a7b641 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -1,8 +1,11 @@ #include +#include #include #include +namespace fs = std::filesystem; + namespace { /// Deletes the given path recursively, ignoring any errors @@ -10,20 +13,24 @@ namespace { void remove(const tmp::path& path) noexcept { if (!path->empty()) { std::error_code ec; - std::filesystem::remove_all(path, ec); + fs::remove_all(path, ec); } } } // namespace namespace tmp { -path::path(path&& other) noexcept : underlying(std::move(other.underlying)) { +path::path(fs::path path) + : underlying(std::move(path)) {} + +path::path(path&& other) noexcept + : underlying(std::move(other.underlying)) { other.underlying.clear(); } path& path::operator=(path&& other) noexcept { remove(*this); - this->underlying = std::move(other.underlying); + underlying = std::move(other.underlying); other.underlying.clear(); return *this; } @@ -32,22 +39,18 @@ path::~path() noexcept { remove(*this); } -path::operator const std::filesystem::path&() const noexcept { - return this->underlying; +path::operator const fs::path&() const noexcept { + return underlying; } -const std::filesystem::path* path::operator->() const noexcept { - return std::addressof(this->underlying); -} - -path::path(std::filesystem::path path) : underlying(std::move(path)) { +const fs::path* path::operator->() const noexcept { + return std::addressof(underlying); } std::string path::make_pattern(std::string_view prefix) { - const auto parent = std::filesystem::temp_directory_path() / prefix; - std::filesystem::create_directories(parent); + fs::path parent = fs::temp_directory_path() / prefix; + fs::create_directories(parent); return parent / "XXXXXX"; } - } // namespace tmp