214 lines
5.7 KiB
C++
214 lines
5.7 KiB
C++
#include "engine/io_csv.hpp"
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
namespace htab {
|
|
|
|
namespace {
|
|
|
|
bool parse_rfc4180_csv(std::string_view text,
|
|
std::vector<std::vector<std::string>>& rows_out,
|
|
std::string* err) {
|
|
rows_out.clear();
|
|
if (text.size() >= 3 &&
|
|
static_cast<unsigned char>(text[0]) == 0xEF &&
|
|
static_cast<unsigned char>(text[1]) == 0xBB &&
|
|
static_cast<unsigned char>(text[2]) == 0xBF) {
|
|
text.remove_prefix(3);
|
|
}
|
|
if (text.empty()) return true;
|
|
|
|
size_t i = 0;
|
|
std::vector<std::string> current_row;
|
|
|
|
while (i < text.size()) {
|
|
std::string field;
|
|
if (text[i] == '"') {
|
|
++i;
|
|
bool closed = false;
|
|
while (i < text.size()) {
|
|
if (text[i] == '"') {
|
|
if (i + 1 < text.size() && text[i + 1] == '"') {
|
|
field.push_back('"');
|
|
i += 2;
|
|
} else {
|
|
++i;
|
|
closed = true;
|
|
break;
|
|
}
|
|
} else {
|
|
field.push_back(text[i++]);
|
|
}
|
|
}
|
|
if (!closed) {
|
|
if (err) *err = "Unterminated quoted CSV field";
|
|
return false;
|
|
}
|
|
if (i < text.size() && text[i] != ',' && text[i] != '\r' && text[i] != '\n') {
|
|
if (err) *err = "Unexpected character after closing CSV quote";
|
|
return false;
|
|
}
|
|
} else {
|
|
while (i < text.size() && text[i] != ',' && text[i] != '\r' && text[i] != '\n') {
|
|
if (text[i] == '"') {
|
|
if (err) *err = "Unexpected quote inside unquoted CSV field";
|
|
return false;
|
|
}
|
|
field.push_back(text[i++]);
|
|
}
|
|
}
|
|
|
|
current_row.push_back(std::move(field));
|
|
|
|
if (i >= text.size()) {
|
|
rows_out.push_back(std::move(current_row));
|
|
current_row.clear();
|
|
break;
|
|
}
|
|
|
|
if (text[i] == ',') {
|
|
++i;
|
|
if (i >= text.size()) {
|
|
// Trailing comma at EOF -> final empty field
|
|
current_row.emplace_back("");
|
|
rows_out.push_back(std::move(current_row));
|
|
current_row.clear();
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// End of line (\r\n, \n, or \r)
|
|
if (text[i] == '\r') {
|
|
++i;
|
|
if (i < text.size() && text[i] == '\n') ++i;
|
|
} else if (text[i] == '\n') {
|
|
++i;
|
|
}
|
|
rows_out.push_back(std::move(current_row));
|
|
current_row.clear();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
std::string escape_csv_field(const std::string& s) {
|
|
bool needs_quote = false;
|
|
for (char ch : s) {
|
|
if (ch == ',' || ch == '"' || ch == '\n' || ch == '\r') {
|
|
needs_quote = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!needs_quote) return s;
|
|
std::string out;
|
|
out.reserve(s.size() + 4);
|
|
out.push_back('"');
|
|
for (char ch : s) {
|
|
if (ch == '"') out += "\"\"";
|
|
else out.push_back(ch);
|
|
}
|
|
out.push_back('"');
|
|
return out;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool import_csv_string(Workbook& wb,
|
|
int sheet_idx,
|
|
std::string_view csv_text,
|
|
std::string* err) {
|
|
if (sheet_idx < 0 || sheet_idx >= wb.sheet_count()) {
|
|
if (err) *err = "Invalid sheet index";
|
|
return false;
|
|
}
|
|
std::vector<std::vector<std::string>> rows;
|
|
if (!parse_rfc4180_csv(csv_text, rows, err)) {
|
|
return false;
|
|
}
|
|
std::unordered_map<CellKey, Cell, CellKeyHash> new_cells;
|
|
for (size_t r = 0; r < rows.size(); ++r) {
|
|
if (r >= static_cast<size_t>(kMaxRows)) break;
|
|
for (size_t c = 0; c < rows[r].size(); ++c) {
|
|
if (c >= static_cast<size_t>(kMaxCols)) break;
|
|
if (rows[r][c].empty()) continue;
|
|
Cell cell;
|
|
cell.raw_input = rows[r][c];
|
|
new_cells.emplace(CellKey{static_cast<int>(r), static_cast<int>(c)}, std::move(cell));
|
|
}
|
|
}
|
|
wb.replace_sheet_cells(sheet_idx, std::move(new_cells));
|
|
return true;
|
|
}
|
|
|
|
bool import_csv_file(Workbook& wb,
|
|
int sheet_idx,
|
|
const std::string& path,
|
|
std::string* err) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
if (!in) {
|
|
if (err) *err = "Cannot open CSV file for reading: " + path;
|
|
return false;
|
|
}
|
|
std::ostringstream oss;
|
|
oss << in.rdbuf();
|
|
return import_csv_string(wb, sheet_idx, oss.str(), err);
|
|
}
|
|
|
|
std::string export_csv_string(const Workbook& wb,
|
|
int sheet_idx,
|
|
bool export_formulas) {
|
|
auto bounds = wb.get_used_bounds(sheet_idx);
|
|
if (!bounds) return "";
|
|
|
|
std::string out;
|
|
for (int r = 0; r <= bounds->r2; ++r) {
|
|
int row_last_col = -1;
|
|
std::vector<std::string> row_vals(static_cast<size_t>(bounds->c2 + 1));
|
|
for (int c = 0; c <= bounds->c2; ++c) {
|
|
std::string val;
|
|
if (export_formulas) {
|
|
const Cell* cell = wb.get_cell(sheet_idx, r, c);
|
|
if (cell && !cell->raw_input.empty()) {
|
|
val = cell->raw_input;
|
|
}
|
|
} else {
|
|
val = wb.get_formatted_value(sheet_idx, r, c);
|
|
}
|
|
if (!val.empty()) row_last_col = c;
|
|
row_vals[static_cast<size_t>(c)] = std::move(val);
|
|
}
|
|
|
|
int limit = (row_last_col >= 0) ? row_last_col : -1;
|
|
for (int c = 0; c <= limit; ++c) {
|
|
if (c > 0) out.push_back(',');
|
|
out += escape_csv_field(row_vals[static_cast<size_t>(c)]);
|
|
}
|
|
out += "\r\n";
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool export_csv_file(const Workbook& wb,
|
|
int sheet_idx,
|
|
const std::string& path,
|
|
bool export_formulas,
|
|
std::string* err) {
|
|
std::ofstream out(path, std::ios::binary | std::ios::trunc);
|
|
if (!out) {
|
|
if (err) *err = "Cannot open CSV file for writing: " + path;
|
|
return false;
|
|
}
|
|
std::string data = export_csv_string(wb, sheet_idx, export_formulas);
|
|
out.write(data.data(), static_cast<std::streamsize>(data.size()));
|
|
if (!out) {
|
|
if (err) *err = "Failed writing CSV file: " + path;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace htab
|