Cbeam
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
29#include <cbeam/random/generators.hpp> // for cbeam::random::random_string
30
31#include <filesystem> // for std::filesystem::path, std::filesystem::exists, std::filesystem::operator/, std::filesystem::temp_directory_path, std::filesystem::create_directories
32#include <fstream> // for std::ios, std::operator|, std::basic_ostream, std::ofstream, std::basic_istream::read, std::basic_istream::seekg, std::basic_istream::tellg, std::ifstream
33#include <string> // for std::operator+, std::allocator, std::char_traits, std::string, std::operator<<
34
36{
38 inline std::string read_file(std::filesystem::path file_path)
39 {
40 std::ifstream ifs(file_path.string(), std::ios::binary | std::ios::ate);
41 if (ifs.fail())
42 {
43 throw cbeam::error::runtime_error("Could not open file '" + file_path.string() + "' for reading.");
44 }
45 auto fileSize = ifs.tellg();
46 if (fileSize == -1)
47 {
48 throw cbeam::error::runtime_error("Could not determine the size of file '" + file_path.string() + "'.");
49 }
50 ifs.seekg(0, std::ios::beg);
51 if (ifs.fail())
52 {
53 throw cbeam::error::runtime_error("Could not seek to the beginning of file '" + file_path.string() + "'.");
54 }
55 std::string content(fileSize, '\0');
56 ifs.read(&content[0], fileSize);
57 if (ifs.fail() && !ifs.eof())
58 {
59 throw cbeam::error::runtime_error("Could not read from file '" + file_path.string() + "'.");
60 }
61 return content;
62 }
63
65 inline void write_file(const std::filesystem::path& file_path, const std::string& content)
66 {
67 std::ofstream ofs(file_path.string());
68 if (ofs.fail())
69 {
70 throw cbeam::error::runtime_error("Could not open file '" + file_path.string() + "' for writing.");
71 }
72 ofs << content;
73 if (ofs.fail())
74 {
75 throw cbeam::error::runtime_error("Could not write to file '" + file_path.string() + "'.");
76 }
77 ofs.close();
78 }
79
81 inline void touch(const std::filesystem::path& p)
82 {
83 std::ofstream file(p, std::ios::app);
84 }
85
91 inline std::filesystem::path unique_temp_file(const std::string& extension = {})
92 {
93 std::filesystem::path unique_path;
94
95 do
96 {
97 unique_path = (std::filesystem::temp_directory_path() / random::random_string(16)).replace_extension(extension);
98 } while (std::filesystem::exists(unique_path));
99
100 return unique_path;
101 }
102
104 inline std::filesystem::path unique_temp_dir()
105 {
106 std::filesystem::path unique_path;
107
108 do
109 {
110 unique_path = std::filesystem::temp_directory_path() / random::random_string(16);
111 } while (std::filesystem::exists(unique_path));
112
113 return unique_path;
114 }
115
121 inline std::filesystem::path create_unique_temp_file(const std::string& extension = {})
122 {
123 std::filesystem::path unique_path = unique_temp_file(extension);
124 touch(unique_path);
125 return unique_path;
126 }
127
129 inline std::filesystem::path create_unique_temp_dir()
130 {
131 std::filesystem::path unique_path = unique_temp_dir();
132 std::filesystem::create_directories(unique_path);
133 return unique_path;
134 }
135}
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