Skip to content

Commit

Permalink
Add constructor(mode)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 25, 2024
1 parent fbbe15c commit db5b51a
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions include/tmp/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace tmp {
class file {
public:
/// Write mode for the temporary file
enum class write_mode : std::uint8_t {
enum class mode : std::uint8_t {
text, ///< Text mode
binary, ///< Binary mode
};
Expand All @@ -57,7 +57,7 @@ class file {
/// 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 = "", write_mode mode = write_mode::text) : mode(mode) {
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";

Expand All @@ -66,6 +66,9 @@ class file {
this->p = arg;
}

/// 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 @@ -108,12 +111,12 @@ class file {

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

/// 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 == write_mode::binary
return this->mode == mode::binary
? std::ofstream { this->path(), mode | std::ios::binary }
: std::ofstream { this->path(), mode };
}
Expand Down

0 comments on commit db5b51a

Please sign in to comment.