-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mil_tools/TemporaryFile.hpp: Add TemporaryFile class
- Loading branch information
Showing
3 changed files
with
103 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/mil_common/mil_tools/include/mil_tools/os/TemporaryFile.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#pragma once | ||
|
||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "mil_tools/os/FileDescriptor.hpp" | ||
#include "mil_tools/string.hpp" | ||
|
||
namespace mil_tools::os | ||
{ | ||
|
||
class TemporaryFile | ||
{ | ||
private: | ||
std::string name_; | ||
std::string tmp_path_ = std::filesystem::temp_directory_path().string(); | ||
void create() | ||
{ | ||
char* name = path().data(); | ||
int raw_fd = mkstemp(name); | ||
if (raw_fd < 0) | ||
{ | ||
throw std::runtime_error("Failed to create temporary file " + path() + ": " + strerror(errno)); | ||
} | ||
name_ = mil_tools::string::removeprefix(name, tmp_path_); | ||
fd_ = FileDescriptor(raw_fd); | ||
} | ||
FileDescriptor fd_; | ||
|
||
public: | ||
TemporaryFile() : name_("tmpXXXXXX") | ||
{ | ||
create(); | ||
}; | ||
explicit TemporaryFile(std::string const& name) : name_(name) | ||
{ | ||
create(); | ||
}; | ||
inline std::string name() const | ||
{ | ||
return name_; | ||
} | ||
inline std::string path() const | ||
{ | ||
return tmp_path_ + "/" + name_; | ||
} | ||
~TemporaryFile() | ||
{ | ||
close(); | ||
}; | ||
inline bool valid() | ||
{ | ||
return fd_.valid(); | ||
} | ||
void close() | ||
{ | ||
::unlink(path().data()); | ||
fd_.close(); | ||
}; | ||
void write(std::string const& data) | ||
{ | ||
fd_.write(data); | ||
fsync(fd_.get()); | ||
}; | ||
void write(std::vector<char> const& data) | ||
{ | ||
fd_.write(data); | ||
fsync(fd_.get()); | ||
}; | ||
std::string read_as_string(int size) | ||
{ | ||
return fd_.read_as_string(size); | ||
}; | ||
std::vector<char> read(int size) | ||
{ | ||
return fd_.read(size); | ||
}; | ||
}; | ||
|
||
} // namespace mil_tools::os |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters