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

◆ open()

bool MayaFlux::IO::DelimitedTextWriter::open ( const std::string &  filepath,
const Kakshya::CompositeLayout &  layout 
)
overridevirtual

Create or truncate a CSV or TSV file.

Parameters
filepathOutput path; a relative path resolves through resolve_write_path().
layoutField names and exact types for every written element.
Returns
True when ready to accept row slices.

Implements MayaFlux::IO::CompositeWriter.

Definition at line 187 of file DelimitedTextWriter.cpp.

190{
191 if (is_open()) {
192 m_last_error = "Close the current file before opening another";
193 return false;
194 }
195
196 m_last_error.clear();
197
198 if (!can_write(filepath)) {
199 m_last_error = "Expected a .csv or .tsv output path";
200 return false;
201 }
202
203 if (layout.fields().empty()) {
204 m_last_error = "Composite output requires at least one field";
205 return false;
206 }
207
208 for (const auto& field : layout.fields()) {
209 if (!supported_type(field.type)) {
210 m_last_error = "Unsupported Composite field: " + field.name;
211 return false;
212 }
213 }
214
215 Kakshya::CompositeArray empty(layout);
216 m_layout = empty.layout();
219 : (extension_of(filepath) == ".tsv" ? '\t' : ',');
220
221 m_file.open(resolve_write_path(filepath),
222 std::ios::binary | std::ios::out | std::ios::trunc);
223
224 if (!m_file) {
225 m_last_error = "Cannot open delimited text output";
226 m_layout.reset();
227 return false;
228 }
229
230 m_rows_written = 0;
231
232 if (m_write_header) {
233 const auto& fields = m_layout->fields();
234
235 for (size_t i = 0; i < fields.size(); ++i) {
236 if (i != 0)
238
239 if (!write_cell(fields[i].name, false)) {
240 close();
241 return false;
242 }
243 }
244
245 m_file.put('\n');
246
247 if (!m_file) {
248 m_last_error = "Failed to write delimited text header";
249 close();
250 return false;
251 }
252 }
253
254 return true;
255}
std::string name
Definition VKDevice.cpp:123
bool write_cell(std::string_view text, bool force_quote)
bool close() override
Finalize and close the current file.
bool is_open() const override
Whether an output file is open.
bool can_write(const std::string &filepath) const override
Check for a .csv or .tsv output path.
std::optional< Kakshya::CompositeLayout > m_layout
std::string resolve_write_path(const std::string &filepath, std::string_view default_directory=Config::OUT_DIR)
Resolve a writable path against the configured output directory.

References can_write(), close(), MayaFlux::Kakshya::CompositeLayout::fields(), is_open(), MayaFlux::Kakshya::CompositeArray::layout(), m_active_delimiter, m_delimiter, m_file, m_last_error, m_layout, m_rows_written, m_write_header, name, MayaFlux::IO::resolve_write_path(), and write_cell().

+ Here is the call graph for this function: