Skip to content

Commit

Permalink
Add error-code constructors for file (#144)
Browse files Browse the repository at this point in the history
Add:
- `file::file(std::error_code&)`
- `file::file(std::string_view, std::error_code&)`
- `file::file(std::string_view, std::string_view, std::error_code&)`
- `file::text(std::error_code&)`
- `file::text(std::string_view, std::error_code&)`
- `file::text(std::string_view, std::string_view, std::error_code&)`
  • Loading branch information
bugdea1er authored Jan 21, 2025
1 parent c0ab457 commit be38178
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
41 changes: 39 additions & 2 deletions include/tmp/file
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,30 @@ class TMP_EXPORT file : public entry {
public:
/// Creates a unique temporary file and opens it for reading and writing
/// in binary mode
/// @param label A label to attach to the temporary file path
/// @param extension An extension of the temporary file path
/// @param[in] label A label to attach to the temporary file path
/// @param[in] extension An extension of the temporary file path
/// @throws std::filesystem::filesystem_error if cannot create a file
/// @throws std::invalid_argument if arguments are ill-formatted
explicit file(std::string_view label = "", std::string_view extension = "");

/// Creates a unique temporary file and opens it for reading and writing
/// in binary mode
/// @param[out] ec Parameter for error reporting
explicit file(std::error_code& ec);

/// Creates a unique temporary file and opens it for reading and writing
/// in binary mode
/// @param[in] label A label to attach to the temporary file path
/// @param[out] ec Parameter for error reporting
file(std::string_view label, std::error_code& ec);

/// Creates a unique temporary file and opens it for reading and writing
/// in binary mode
/// @param[in] label A label to attach to the temporary file path
/// @param[in] extension An extension of the temporary file path
/// @param[out] ec Parameter for error reporting
file(std::string_view label, std::string_view extension, std::error_code& ec);

/// Creates a unique temporary file and opens it for reading and writing
/// in text mode
/// @param label A label to attach to the temporary file path
Expand All @@ -61,6 +79,25 @@ public:
static file text(std::string_view label = "",
std::string_view extension = "");

/// Creates a unique temporary file and opens it for reading and writing
/// in text mode
/// @param[out] ec Parameter for error reporting
static file text(std::error_code& ec);

/// Creates a unique temporary file and opens it for reading and writing
/// in text mode
/// @param[in] label A label to attach to the temporary file path
/// @param[out] ec Parameter for error reporting
static file text(std::string_view label, std::error_code& ec);

/// Creates a unique temporary file and opens it for reading and writing
/// in text mode
/// @param[in] label A label to attach to the temporary file path
/// @param[in] extension An extension of the temporary file path
/// @param[out] ec Parameter for error reporting
static file text(std::string_view label, std::string_view extension,
std::error_code& ec);

/// Creates a unique temporary copy from the given path
/// @param path A path to make a temporary copy from
/// @param label A label to attach to the temporary file path
Expand Down
29 changes: 29 additions & 0 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,42 @@ file::file(std::string_view label, std::string_view extension)
: entry(create_file(label, extension)),
binary(true) {}

file::file(std::error_code& ec)
: entry(create_file("", "", ec)),
binary(true) {}

file::file(std::string_view label, std::error_code& ec)
: entry(create_file(label, "", ec)),
binary(true) {}

file::file(std::string_view label, std::string_view extension,
std::error_code& ec)
: entry(create_file(label, extension, ec)),
binary(true) {}

file file::text(std::string_view label, std::string_view extension) {
file result = file(label, extension);
result.binary = false;

return result;
}

file file::text(std::error_code& ec) {
return text("", ec);
}

file file::text(std::string_view label, std::error_code& ec) {
return text(label, "", ec);
}

file file::text(std::string_view label, std::string_view extension,
std::error_code& ec) {
file result = file(label, extension, ec);
result.binary = false;

return result;
}

file file::copy(const fs::path& path, std::string_view label,
std::string_view extension) {
file tmpfile = file(label, extension);
Expand Down

0 comments on commit be38178

Please sign in to comment.