| 184 | |
| 185 | template <typename WriteFn> |
| 186 | void createAndWriteToFile (const std::filesystem::path& path, WriteFn&& write) |
| 187 | { |
| 188 | try |
| 189 | { |
| 190 | try |
| 191 | { |
| 192 | if (path.has_parent_path()) |
| 193 | if (auto parent = path.parent_path(); ! exists (parent)) |
| 194 | create_directories (parent); |
| 195 | } |
| 196 | catch (const std::ios_base::failure&) {} |
| 197 | |
| 198 | std::ofstream stream; |
| 199 | stream.exceptions (std::ofstream::failbit | std::ofstream::badbit); |
| 200 | stream.open (path, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary); |
| 201 | write (stream); |
| 202 | } |
| 203 | catch (const std::ios_base::failure& e) |
| 204 | { |
| 205 | throw Error ("Failed to write to file: " + path.string() + ": " + e.what()); |
| 206 | } |
| 207 | catch (...) |
| 208 | { |
| 209 | throw Error ("Failed to write to file: " + path.string()); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | inline size_t attemptToRead (std::istream& source, std::istream::char_type* buffer, size_t size) |
| 214 | { |
no test coverage detected