Skip to content

Commit

Permalink
Final tidy up for *.cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 28, 2024
1 parent 6c8dccb commit 629ff20
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
22 changes: 12 additions & 10 deletions lib/directory.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include <tmp/directory.hpp>

#include <filesystem>
#include <string_view>
#include <system_error>
#include <unistd.h>

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;
Expand All @@ -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<const std::filesystem::path&>(*this) / source;
fs::path directory::operator/(std::string_view source) const {
return static_cast<const fs::path&>(*this) / source;
}

directory::~directory() noexcept = default;
Expand Down
22 changes: 12 additions & 10 deletions lib/file.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#include <tmp/file.hpp>

#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string_view>
#include <system_error>

namespace fs = std::filesystem;

namespace {

/// Creates a temporary file 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 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;
Expand All @@ -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

Expand Down
29 changes: 16 additions & 13 deletions lib/path.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
#include <tmp/path.hpp>

#include <filesystem>
#include <system_error>
#include <utility>

namespace fs = std::filesystem;

namespace {

/// Deletes the given path recursively, ignoring any errors
/// @param path The path of the directory to remove
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;
}
Expand All @@ -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

0 comments on commit 629ff20

Please sign in to comment.