-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvwriter.cpp
186 lines (152 loc) · 3.77 KB
/
csvwriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "infra/csvwriter.h"
#include "infra/exception.h"
#include "infra/string.h"
#include <array>
#include <iomanip>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
static constexpr std::array<std::uint8_t, 3> utf8Bom{{0xEF, 0xBB, 0xBF}};
namespace inf {
static char locale_requires_number_quoting(const std::locale& loc, char separator)
{
if (std::has_facet<std::numpunct<char>>(loc)) {
return std::use_facet<std::numpunct<char>>(loc).decimal_point() == separator ||
std::use_facet<std::numpunct<char>>(loc).thousands_sep() == separator;
} else {
return false;
}
}
char CsvWriter::system_list_separator()
{
#ifdef _WIN32
char szBuf[8];
if (::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, szBuf, sizeof(szBuf)) > 0) {
return szBuf[0];
}
#endif
return ',';
}
CsvWriter::CsvWriter(const fs::path& outputPath)
: CsvWriter(outputPath, Settings())
{
}
CsvWriter::CsvWriter(const fs::path& outputPath, Settings settings)
: _stream(outputPath)
, _settings(settings)
{
if (!_stream.is_open()) {
throw RuntimeError("Failed to open file for csv writing: {}", outputPath);
}
if (_settings.utf8Bom) {
_stream.write(reinterpret_cast<const char*>(utf8Bom.data()), utf8Bom.size());
if (_stream.bad()) {
throw RuntimeError("Failed to write Utf8 BOM");
}
}
_stream.imbue(_settings.locale);
if (_settings.numberFormat == NumberFormat::Scientific) {
_stream << std::scientific;
} else {
_stream << std::fixed;
}
if (_settings.precision.has_value()) {
_stream << std::setprecision(*_settings.precision);
}
_quoteNumbers = locale_requires_number_quoting(_settings.locale, _settings.separator);
}
void CsvWriter::write_header(std::span<const std::string> names)
{
write_line(names);
}
void CsvWriter::write_header(std::span<std::string_view> names)
{
write_line(names);
}
void CsvWriter::write_header(std::initializer_list<std::string_view> names)
{
write_line(names);
}
void CsvWriter::write_line(std::span<const std::string> names)
{
for (auto name : names) {
write_column_value(name);
}
new_line();
}
void CsvWriter::write_line(std::initializer_list<std::string_view> names)
{
for (auto name : names) {
write_column_value(name);
}
new_line();
}
void CsvWriter::write_line(std::span<std::string_view> names)
{
for (auto name : names) {
write_column_value(name);
}
new_line();
}
void CsvWriter::write_empty_column()
{
if (_fieldOffset != 0) {
_stream << _settings.separator;
}
++_fieldOffset;
}
void CsvWriter::write_column_value(std::string_view value)
{
bool quotesNeeded = value.find(_settings.separator) != std::string_view::npos;
if (quotesNeeded) {
write_quoted_value(value);
} else {
write_value(value);
}
}
void CsvWriter::write_column_value(int32_t value)
{
write_value(value);
}
void CsvWriter::write_column_value(int64_t value)
{
write_value(value);
}
void CsvWriter::write_column_value(float value)
{
write_value(value);
}
void CsvWriter::write_column_value(double value)
{
write_value(value);
}
template <typename T>
void CsvWriter::write_value(T value)
{
if constexpr (std::is_scalar_v<T>) {
if (_quoteNumbers) {
return write_quoted_value(value);
}
}
if (_fieldOffset != 0) {
_stream << _settings.separator;
}
_stream << value;
++_fieldOffset;
}
template <typename T>
void CsvWriter::write_quoted_value(T value)
{
if (_fieldOffset != 0) {
_stream << _settings.separator;
}
_stream << '"' << value << '"';
++_fieldOffset;
}
void CsvWriter::new_line()
{
_stream << "\n";
_fieldOffset = 0;
}
}