Skip to content

Commit

Permalink
Refine constructors and factories
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 27, 2024
1 parent db5b51a commit 4401384
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions include/tmp/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,20 @@ namespace tmp {
/// object goes out of scope and the temporary file is deleted.
class file {
public:
/// Write mode for the temporary file
enum class mode : std::uint8_t {
text, ///< Text mode
binary, ///< Binary mode
};
/// Creates a unique temporary binary file using the system's default
/// location for temporary files. If a prefix is provided to the
/// constructor, the directory is created in the path <temp dir>/prefix/.
/// The prefix can be a path consisting of multiple segments.
file(std::string_view prefix = "") : file(prefix, /*binary=*/true) {}

/// Creates a unique temporary file using the system's default location
/// Creates a unique temporary text file using the system's default location
/// for temporary files. If a prefix is provided to the constructor, the
/// directory is created in the path <temp dir>/prefix/. The prefix can be
/// a path consisting of multiple segments.
explicit file(std::string_view prefix = "", mode mode = mode::text) : mode(mode) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::string arg = parent / "XXXXXX";

std::filesystem::create_directories(parent);
::mkstemp(arg.data());
this->p = arg;
static file text(std::string_view prefix = "") {
return file(prefix, /*binary=*/false);
}

/// Creates a unique temporary file without prefixes
explicit file(mode mode) : file("", mode) {}

/// Creates a file from a moved @p other
file(file&& other) noexcept : p(std::move(other.p)) {
other.p.clear();
Expand Down Expand Up @@ -111,12 +103,25 @@ class file {

private:
std::filesystem::path p; ///< This file path
mode mode; ///< This file write mode
bool binary; ///< This file write mode

/// Creates a unique temporary file using the system's default location
/// for temporary files. If a prefix is provided to the constructor, the
/// directory is created in the path <temp dir>/prefix/. The prefix can be
/// a path consisting of multiple segments.
explicit file(std::string_view prefix, bool binary) : binary(binary) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::string arg = parent / "XXXXXX";

std::filesystem::create_directories(parent);
::mkstemp(arg.data());
this->p = arg;
}

/// Returns a stream for this file
std::ofstream stream(bool append) const noexcept {
std::ios::openmode mode = append ? std::ios::app : std::ios::trunc;
return this->mode == mode::binary
return this->binary
? std::ofstream { this->path(), mode | std::ios::binary }
: std::ofstream { this->path(), mode };
}
Expand Down

0 comments on commit 4401384

Please sign in to comment.