MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextFileWriter.cpp
Go to the documentation of this file.
1#include "TextFileWriter.hpp"
2
3#include <filesystem>
4
5namespace MayaFlux::IO {
6
8
13
14bool TextFileWriter::can_write(const std::string& filepath) const
15{
16 std::filesystem::path path(filepath);
17
18 auto parent = path.parent_path();
19 if (parent.empty()) {
20 parent = std::filesystem::current_path();
21 }
22
23 return std::filesystem::exists(parent) && !std::filesystem::is_directory(path);
24}
25
26bool TextFileWriter::open(const std::string& filepath, FileWriteOptions options)
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}
77
79{
80 std::lock_guard lock(m_mutex);
81
82 if (m_file.is_open()) {
83 m_file.flush();
84 m_file.close();
85 }
86
87 m_is_open = false;
88}
89
91{
92 std::lock_guard lock(m_mutex);
93 return m_is_open && m_file.is_open();
94}
95
96bool TextFileWriter::write_bytes(const void* data, size_t size)
97{
98 std::lock_guard lock(m_mutex);
99
100 if (!m_is_open || !m_file.is_open()) {
101 m_last_error = "File not open";
102 return false;
103 }
104
105 if (should_rotate()) {
106 if (!rotate_file()) {
107 return false;
108 }
109 }
110
111 m_file.write(static_cast<const char*>(data), size);
112
113 if (!m_file.good()) {
114 m_last_error = "Write failed";
115 return false;
116 }
117
118 m_bytes_written += size;
119
121 m_file.flush();
122 }
123
124 return true;
125}
126
127bool TextFileWriter::write_string(std::string_view str)
128{
129 return write_bytes(str.data(), str.size());
130}
131
132bool TextFileWriter::write_line(std::string_view line)
133{
134 if (!write_string(line)) {
135 return false;
136 }
137 return write_bytes("\n", 1);
138}
139
141{
142 std::lock_guard lock(m_mutex);
143
144 if (!m_is_open || !m_file.is_open()) {
145 return false;
146 }
147
148 m_file.flush();
149 return m_file.good();
150}
151
153{
154 std::lock_guard lock(m_mutex);
155 return m_bytes_written;
156}
157
159{
160 std::lock_guard lock(m_mutex);
161 return m_last_error;
162}
163
165{
166 std::lock_guard lock(m_mutex);
167 m_max_file_size = max_bytes;
168}
169
171{
172 std::lock_guard lock(m_mutex);
173 return m_bytes_written;
174}
175
180
182{
183 m_file.flush();
184 m_file.close();
185
186 auto now = std::chrono::system_clock::now();
187 auto time_t = std::chrono::system_clock::to_time_t(now);
188
189 std::ostringstream oss;
190 oss << m_filepath << "."
191 << std::put_time(std::localtime(&time_t), "%Y%m%d_%H%M%S");
192
193 std::string backup_path = oss.str();
194
195 try {
196 std::filesystem::rename(m_filepath, backup_path);
197 } catch (const std::filesystem::filesystem_error& e) {
198 m_last_error = std::string("Failed to rotate file: ") + e.what();
199 return false;
200 }
201
202 std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc;
203 m_file.open(m_filepath, mode);
204
205 if (!m_file.is_open()) {
206 m_last_error = "Failed to reopen file after rotation";
207 return false;
208 }
209
210 m_bytes_written = 0;
211 return true;
212}
213
214} // namespace MayaFlux::IO
bool flush() override
Flush buffered writes to disk.
bool write_string(std::string_view str) override
Write a string.
void set_max_file_size(size_t max_bytes)
Set maximum file size before rotation.
bool open(const std::string &filepath, FileWriteOptions options) override
Open a file for writing.
bool can_write(const std::string &filepath) const override
Check if this writer can handle the given file path.
std::string get_last_error() const override
Get last error message.
bool write_line(std::string_view line) override
Write a line (appends newline)
void close() override
Close the currently open file.
size_t get_write_position() const override
Get current write position (bytes written)
bool write_bytes(const void *data, size_t size) override
Write raw bytes.
bool is_open() const override
Check if a file is currently open for writing.
size_t get_file_size() const
Get current file size.
@ TRUNCATE
Truncate existing file.
@ SYNC
Sync after each write (slow but safe)
@ APPEND
Append to existing file.