#pragma once #include #include #include #include #include #include #include #include namespace Void::Csv { using Row = std::vector; using Table = std::vector; // ── parse // ───────────────────────────────────────────────────────────────────── RFC // 4180-compatible parser. Fields may be quoted ("..."). CRLF and LF both // accepted as row terminators. [[nodiscard]] inline Table parse(std::string_view text, char delim = ',') { Table result; Row row; std::string field; bool in_quote = false; for (std::size_t i = 0; i < text.size(); ++i) { const char c = text[i]; if (in_quote) { if (c == '"') { // Peek for doubled quote if (i + 1 < text.size() && text[i + 1] == '"') { field += '"'; ++i; } else { in_quote = false; } } else { field += c; } } else { if (c == '"') { in_quote = true; } else if (c == delim) { row.push_back(std::move(field)); field.clear(); } else if (c == '\n') { row.push_back(std::move(field)); field.clear(); result.push_back(std::move(row)); row.clear(); } else if (c == '\r') { // skip CR in CRLF } else { field += c; } } } // Flush last field/row row.push_back(std::move(field)); if (!row.empty() && !(row.size() == 1 && row[0].empty())) { result.push_back(std::move(row)); } return result; } // ── serialize // ───────────────────────────────────────────────────────────────── // Produces RFC 4180 CSV. Fields that contain the delimiter, quote, or // newline are wrapped in double quotes; embedded quotes are doubled. [[nodiscard]] inline std::string serialize(const Table& table, char delim = ',') { std::string out; for (const auto& row : table) { for (std::size_t i = 0; i < row.size(); ++i) { const auto& field = row[i]; const bool needs_quote = field.find(delim) != std::string::npos || field.find('"') != std::string::npos || field.find('\n') != std::string::npos || field.find('\r') != std::string::npos; if (needs_quote) { out += '"'; for (char c : field) { if (c == '"') { out += '"'; } out += c; } out += '"'; } else { out += field; } if (i + 1 < row.size()) { out += delim; } } out += '\n'; } return out; } // ── with_header convenience // ─────────────────────────────────────────────────── struct CsvWithHeader { Row header; Table rows; [[nodiscard]] std::optional col_index(std::string_view name) const { for (std::size_t i = 0; i < header.size(); ++i) { if (header[i] == name) { return i; } } return std::nullopt; } [[nodiscard]] std::optional get(const Row& row, std::string_view name) const { auto idx = col_index(name); if (!idx || *idx >= row.size()) { return std::nullopt; } return row[*idx]; } }; [[nodiscard]] inline std::optional parse_with_header(std::string_view text, char delim = ',') { auto table = parse(text, delim); if (table.empty()) { return std::nullopt; } CsvWithHeader result; result.header = std::move(table[0]); result.rows.assign(std::make_move_iterator(table.begin() + 1), std::make_move_iterator(table.end())); return result; } // ── reflective struct ↔ row // ─────────────────────────────────────────────────── Map a flat aggregate // to/from a CSV Row by reflecting its data members (column order = // declaration order; header = field names). Supported field types: // std::string, arithmetic, bool. #ifdef VOID_HAS_META_REFLECTION namespace Detail { template [[nodiscard]] std::string cell_to_string(const F& f) { if constexpr (std::same_as) { return f; } else if constexpr (std::same_as) { return f ? "true" : "false"; } else if constexpr (std::is_arithmetic_v) { return std::format("{}", f); } else { return Void::Meta::repr(f); } } template [[nodiscard]] bool cell_from_string(std::string_view s, F& f) { if constexpr (std::same_as) { f = std::string{s}; return true; } else if constexpr (std::same_as) { f = (s == "true" || s == "1"); return true; } else if constexpr (std::is_integral_v) { return std::from_chars(s.data(), s.data() + s.size(), f).ec == std::errc{}; } else if constexpr (std::is_floating_point_v) { return std::from_chars(s.data(), s.data() + s.size(), f).ec == std::errc{}; } else { return false; } } } // namespace Detail /// The header row (field names) for T. template [[nodiscard]] Row header_row() { Row h; for (auto name : Void::Meta::field_names()) { h.emplace_back(name); } return h; } /// Stringify a struct into a CSV Row (declaration order). template [[nodiscard]] Row to_row(const T& v) { Row r; Void::Meta::for_each_field(v, [&](std::string_view, const auto& f) { r.push_back(Detail::cell_to_string(f)); }); return r; } /// Parse a CSV Row back into a struct (by position). Returns nullopt if any /// cell fails to convert. template [[nodiscard]] std::optional from_row(const Row& row) { T out{}; std::size_t i = 0; bool ok = true; Void::Meta::for_each_field(out, [&](std::string_view, auto& f) { if (i < row.size()) { ok = Detail::cell_from_string(row[i], f) && ok; } ++i; }); return ok ? std::optional{std::move(out)} : std::nullopt; } #endif // VOID_HAS_META_REFLECTION } // namespace Void::Csv