MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DelimitedTextWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "CompositeWriter.hpp"
4
5#include <fstream>
6
7namespace MayaFlux::IO {
8
9/**
10 * @class DelimitedTextWriter
11 * @brief Streaming CSV and TSV writer for Composite arrays.
12 *
13 * The output delimiter defaults to the file extension. A header is
14 * written by default, and every record ends with LF. Missing fields
15 * become unquoted empty cells; present empty text becomes a quoted empty
16 * cell so DelimitedTextReader can restore its presence bit. Numbers use
17 * the shortest decimal form that parses back to the same value, and text
18 * is quoted when CSV/TSV syntax requires it. A non-finite floating-point
19 * value has no CSV form and fails the write.
20 *
21 * Usage:
22 * @code
23 * DelimitedTextWriter writer;
24 * bool saved = writer.write("measurements.csv", array);
25 * @endcode
26 *
27 * For large arrays, call open(), write_rows() with bounded slices,
28 * and close(). Every slice must use the layout passed to open().
29 */
30class MAYAFLUX_API DelimitedTextWriter : public CompositeWriter {
31public:
33 ~DelimitedTextWriter() override;
34
35 /**
36 * @brief Select whether a header record is written.
37 * @param enabled True by default.
38 * @return False while a file is open.
39 */
40 bool set_write_header(bool enabled) noexcept;
41
42 /**
43 * @brief Override the extension-selected delimiter.
44 * @param delimiter A nonzero character other than quote, CR, or LF.
45 * @return False for an invalid delimiter or while a file is open.
46 */
47 bool set_delimiter(char delimiter) noexcept;
48
49 /**
50 * @brief Check for a .csv or .tsv output path.
51 * @param filepath Candidate path.
52 * @return True for a supported extension.
53 */
54 [[nodiscard]] bool can_write(const std::string& filepath) const override;
55
56 /**
57 * @brief Create or truncate a CSV or TSV file.
58 * @param filepath Output path; a relative path resolves through
59 * resolve_write_path().
60 * @param layout Field names and exact types for every written element.
61 * @return True when ready to accept row slices.
62 */
63 bool open(
64 const std::string& filepath,
65 const Kakshya::CompositeLayout& layout) override;
66
67 /**
68 * @brief Encode a bounded slice using the open file's layout.
69 * @param rows Elements to append; field names, types, and order must
70 * match the layout passed to open().
71 * @return True when every element was accepted. On failure, elements
72 * before the failing one have already been written.
73 */
74 bool write_rows(const Kakshya::CompositeSlice& rows) override;
75
76 /** @brief Flush the current file without closing it. */
77 bool flush() override;
78
79 /** @brief Finalize and close the current file. */
80 bool close() override;
81
82 /** @brief Whether an output file is open. */
83 [[nodiscard]] bool is_open() const override { return m_file.is_open(); }
84
85 /** @brief Supported extensions without dots. */
86 [[nodiscard]] std::vector<std::string>
87 get_supported_extensions() const override
88 {
89 return { "csv", "tsv" };
90 }
91
92 /** @brief Last encoding or file error, if any. */
93 [[nodiscard]] std::string get_last_error() const override
94 {
95 return m_last_error;
96 }
97
98 /** @brief Number of data elements accepted since open(). */
99 [[nodiscard]] size_t rows_written() const noexcept { return m_rows_written; }
100
101private:
102 std::ofstream m_file;
103 std::optional<Kakshya::CompositeLayout> m_layout;
104 std::string m_last_error;
105 char m_delimiter {};
106 char m_active_delimiter {};
107 bool m_write_header { true };
108 size_t m_rows_written {};
109
110 [[nodiscard]] bool write_cell(std::string_view text, bool force_quote);
111 [[nodiscard]] bool write_element(const Kakshya::Composite& element);
112};
113
114}
uint32_t rows
Typed writer contract for schema-bearing Composite files.
std::string get_last_error() const override
Last encoding or file error, if any.
std::vector< std::string > get_supported_extensions() const override
Supported extensions without dots.
bool is_open() const override
Whether an output file is open.
size_t rows_written() const noexcept
Number of data elements accepted since open().
std::optional< Kakshya::CompositeLayout > m_layout
Streaming CSV and TSV writer for Composite arrays.
Shared field description and byte layout for a CompositeArray.
Definition Composite.hpp:52
Borrowed consecutive elements, optionally selected by a Region.
Read view of one value in a CompositeArray.