Skip to content

Commit

Permalink
Simplify stream function
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 28, 2024
1 parent 7da9b92 commit d327d01
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ namespace tmp {
namespace {

/// Opens a temporary file for writing and returns an output file stream
/// @param file The file to open
/// @param binary Whether to open the file in binary mode (true) or text mode (false)
/// @param append Whether to append to the end of the file (true) or truncate it (false)
/// @param file The file to open
/// @param binary Whether to open the file in binary mode
/// @param append Whether to append to the end of the file
/// @return An output file stream
std::ofstream stream(const file& file, bool binary, bool append) noexcept {
std::ios::openmode mode = append ? std::ios::app : std::ios::trunc;
auto path = static_cast<const std::filesystem::path&>(file);
return binary
? std::ofstream { path, mode | std::ios::binary }
: std::ofstream { path, mode };
}
if (binary) {
mode |= std::ios::binary;
}

const std::filesystem::path& path = file;
return std::ofstream { path, mode };
}
} // namespace

file::file(std::string_view prefix) : file(prefix, /*binary=*/true) {
}
Expand Down
1 change: 0 additions & 1 deletion lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ void remove(const tmp::path& path) noexcept {
std::filesystem::remove_all(path, ec);
}
}

} // namespace

namespace tmp {
Expand Down

0 comments on commit d327d01

Please sign in to comment.