MCPcopy Create free account
hub / github.com/Tracktion/choc / createAndWriteToFile

Function createAndWriteToFile

choc/text/choc_Files.h:186–211  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184
185template <typename WriteFn>
186void 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
213inline size_t attemptToRead (std::istream& source, std::istream::char_type* buffer, size_t size)
214{

Callers 1

replaceFileWithContentFunction · 0.85

Calls 3

whatMethod · 0.80
ErrorClass · 0.70
openMethod · 0.45

Tested by

no test coverage detected