MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open()

bool MayaFlux::IO::TextFileWriter::open ( const std::string &  filepath,
FileWriteOptions  options 
)
overridevirtual

Open a file for writing.

Parameters
filepathPath to the file
optionsWrite options (append, create, truncate, etc.)
Returns
true if successful

Implements MayaFlux::IO::FileWriter.

Definition at line 26 of file TextFileWriter.cpp.

27{
28 std::lock_guard lock(m_mutex);
29
30 if (m_is_open) {
31 close();
32 }
33
34 m_filepath = filepath;
35 m_options = options;
36
37 std::ios_base::openmode mode = std::ios_base::out;
38
40 mode |= std::ios_base::app;
41 }
42
44 mode |= std::ios_base::trunc;
45 }
46
47 try {
48 std::filesystem::path path(filepath);
49 if (auto parent = path.parent_path(); !parent.empty()) {
50 std::filesystem::create_directories(parent);
51 }
52 } catch (const std::filesystem::filesystem_error& e) {
53 m_last_error = std::string("Failed to create directories: ") + e.what();
54 return false;
55 }
56
57 m_file.open(filepath, mode);
58
59 if (!m_file.is_open()) {
60 m_last_error = "Failed to open file: " + filepath;
61 return false;
62 }
63
64 m_is_open = true;
66
68 try {
69 m_bytes_written = std::filesystem::file_size(filepath);
70 } catch (...) {
72 }
73 }
74
75 return true;
76}
void close() override
Close the currently open file.
@ TRUNCATE
Truncate existing file.
@ APPEND
Append to existing file.

References MayaFlux::IO::APPEND, close(), m_bytes_written, m_file, m_filepath, m_is_open, m_last_error, m_mutex, m_options, MayaFlux::IO::NONE, and MayaFlux::IO::TRUNCATE.

+ Here is the call graph for this function: