38 inline std::string
read_file(std::filesystem::path file_path)
40 std::ifstream ifs(file_path.string(), std::ios::binary | std::ios::ate);
45 auto fileSize = ifs.tellg();
50 ifs.seekg(0, std::ios::beg);
55 std::string content(fileSize,
'\0');
56 ifs.read(&content[0], fileSize);
57 if (ifs.fail() && !ifs.eof())
65 inline void write_file(
const std::filesystem::path& file_path,
const std::string& content)
67 std::ofstream ofs(file_path.string());
81 inline void touch(
const std::filesystem::path& p)
83 std::ofstream file(p, std::ios::app);
93 std::filesystem::path unique_path;
97 unique_path = (std::filesystem::temp_directory_path() /
random::random_string(16)).replace_extension(extension);
98 }
while (std::filesystem::exists(unique_path));
106 std::filesystem::path unique_path;
111 }
while (std::filesystem::exists(unique_path));
132 std::filesystem::create_directories(unique_path);
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
Facilitates file I/O, path normalization, and directory operations in a cross-platform manner....
Definition io.hpp:36
std::filesystem::path create_unique_temp_file(const std::string &extension={})
Definition io.hpp:121
std::filesystem::path create_unique_temp_dir()
create unique temp directory, analogous to bash mktemp -d
Definition io.hpp:129
std::filesystem::path unique_temp_file(const std::string &extension={})
Definition io.hpp:91
std::string read_file(std::filesystem::path file_path)
Reads the given file as std::string. Throws cbeam::error::runtime_error in case of errors.
Definition io.hpp:38
void touch(const std::filesystem::path &p)
create a file under the given path, if it does not exist yet; otherwise, the file content is left unc...
Definition io.hpp:81
void write_file(const std::filesystem::path &file_path, const std::string &content)
Creates or overwrites the given file with the given content. Throws cbeam::error::runtime_error in ca...
Definition io.hpp:65
std::filesystem::path unique_temp_dir()
get unique and non-existing temp directory
Definition io.hpp:104
std::string random_string(std::string::size_type length, std::mt19937 &gen=default_generator())
Generates a random string of specified length.
Definition generators.hpp:67