Vince's CSV Parser
Loading...
Searching...
No Matches
csv_writer.hpp
Go to the documentation of this file.
1
5#pragma once
6#include "common.hpp"
7#include <cmath>
8#include <fstream>
9#include <iostream>
10#include <memory>
11#ifdef CSV_HAS_CXX20
12#include <ranges>
13#endif
14#include <stdexcept>
15#include <string>
16#include <tuple>
17#include <type_traits>
18#include <vector>
19
21#include "checked_field_range.hpp"
22#include "common.hpp"
23#include "csv_exceptions.hpp"
24#include "data_frame/fwd.hpp"
25
26namespace csv {
27namespace internals {
28 static int DECIMAL_PLACES = 5;
29
31 template<typename T>
32 CSV_CONST CONSTEXPR_14
33 long double pow10(const T& n) noexcept {
34 static_assert(std::is_integral<T>::value, "pow10 only supports integral exponents");
35
36 long double multiplicand = n > 0 ? 10 : 0.1,
37 ret = 1;
38 T iterations = n > 0 ? n : -n;
39
40 for (T i = 0; i < iterations; i++) {
42 }
43
44 return ret;
45 }
46
47 template<>
48 CSV_CONST CONSTEXPR_14
49 long double pow10(const unsigned& n) noexcept {
50 long double multiplicand = n > 0 ? 10 : 0.1,
51 ret = 1;
52
53 for (unsigned i = 0; i < n; i++) {
54 ret *= multiplicand;
55 }
56
57 return ret;
58 }
59
63 template<typename T = int>
64 inline T csv_abs(T x) {
65 return abs(x);
66 }
67
68 template<>
69 inline int csv_abs(int x) {
70 return abs(x);
71 }
72
73 template<>
74 inline long int csv_abs(long int x) {
75 return labs(x);
76 }
77
78 template<>
79 inline long long int csv_abs(long long int x) {
80 return llabs(x);
81 }
82
83 template<>
84 inline float csv_abs(float x) {
85 return fabsf(x);
86 }
87
88 template<>
89 inline double csv_abs(double x) {
90 return fabs(x);
91 }
92
93 template<>
94 inline long double csv_abs(long double x) {
95 return fabsl(x);
96 }
97
101 template<
102 typename T,
103 csv::enable_if_t<std::is_arithmetic<T>::value, int> = 0
104 >
106 {
107 x = csv_abs(x);
108 int digits = 0;
109 for (; x >= 1; digits++)
110 x /= 10;
111
112 return (x == 0) ? 1 : digits;
113 }
114
116 template<typename T,
117 csv::enable_if_t<std::is_unsigned<T>::value, int> = 0>
118 inline std::string to_string(T value) {
119 std::string digits_reverse = "";
120 if (value == 0) return "0";
121
122 for (; value > 0; value /= 10)
123 digits_reverse += (char)('0' + (value % 10));
124
125 return std::string(digits_reverse.rbegin(), digits_reverse.rend());
126 }
127
129 template<
130 typename T,
131 csv::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, int> = 0
132 >
133 inline std::string to_string(T value) {
134 return (value >= 0) ? to_string(static_cast<unsigned long long>(value))
135 : "-" + to_string(0ULL - static_cast<unsigned long long>(value));
136 }
137
139 template<
140 typename T,
141 csv::enable_if_t<std::is_floating_point<T>::value, int> = 0
142 >
143 inline std::string to_string(T value) {
144 std::string result = "";
145
146 long double integral_part;
147 long double fractional_part = csv_abs(std::modf((long double)value, &integral_part));
148
149 const long double scale = pow10(DECIMAL_PLACES);
150 long double rounded_fractional = std::round(fractional_part * scale);
151
152 // Work with the absolute value of the integral part so digit extraction
153 // and carry both work correctly for negative numbers.
154 long double abs_integral = csv_abs(integral_part);
155
156 // Carry rounding overflow from fractional digits into integral digits.
157 if (rounded_fractional >= scale) {
158 abs_integral += 1;
159 rounded_fractional = 0;
160 }
161
162 // Integral part
163 if (value < 0) result = "-";
164
165 if (abs_integral == 0) {
166 result += "0";
167 }
168 else {
169 for (int n_digits = num_digits(abs_integral); n_digits > 0; n_digits --) {
170 int digit = (int)(std::fmod(abs_integral, pow10(n_digits)) / pow10(n_digits - 1));
171 result += (char)('0' + digit);
172 }
173 }
174
175 // Decimal part
176 result += ".";
177
178 if (rounded_fractional > 0) {
179 for (int n_digits = DECIMAL_PLACES; n_digits > 0; n_digits--) {
180 int digit = (int)(std::fmod(rounded_fractional, pow10(n_digits)) / pow10(n_digits - 1));
181 result += (char)('0' + digit);
182 }
183 }
184 else {
185 result += "0";
186 }
187
188 return result;
189 }
190}
191
193inline static void set_decimal_places(int precision) {
194 internals::DECIMAL_PLACES = precision;
195}
196
197namespace internals {
198 template<typename T, typename = void>
199 struct has_checked_csv_fields : std::false_type {};
200
201 template<typename T>
202 struct has_checked_csv_fields<T, csv::void_t<
203 decltype(std::declval<const typename std::remove_reference<T>::type&>().size()),
204 decltype(std::declval<const typename std::remove_reference<T>::type&>()
205 .try_field(size_t{0}, std::declval<CSVError&>()).get_sv())>> : std::true_type {};
206
207 template<typename T>
208 struct is_data_frame : std::false_type {};
209
210 template<typename KeyType>
211 struct is_data_frame<DataFrame<KeyType>> : std::true_type {};
212
214 template<typename T, typename = void>
215 struct is_iterable : std::false_type {};
216
217 template<typename T>
218 struct is_iterable<T, typename std::enable_if<true>::type> {
219 private:
220 template<typename U>
221 static auto test(int) -> decltype(
222 std::begin(std::declval<const U&>()),
223 std::end(std::declval<const U&>()),
224 std::true_type{}
225 );
226 template<typename>
227 static std::false_type test(...);
228 public:
229 static constexpr bool value = decltype(test<T>(0))::value;
230 };
231
233 template<typename T, typename = void>
234 struct is_tuple : std::false_type {};
235
236 template<typename T>
237 struct is_tuple<T, typename std::enable_if<true>::type> {
238 private:
239 template<typename U>
240 static auto test(int) -> decltype(std::tuple_size<U>::value, std::true_type{});
241 template<typename>
242 static std::false_type test(...);
243 public:
244 static constexpr bool value = decltype(test<T>(0))::value;
245 };
246
247}
248
251
272 template<class OutputStream, char Delim, char Quote>
274 public:
277 DelimWriter(OutputStream& _out, bool _quote_minimal = true)
278 : out(&_out), quote_minimal(_quote_minimal) {}
279
281#if CSV_EXCEPTIONS_ENABLED
282 template<typename T = OutputStream,
283 csv::enable_if_t<std::is_same<T, std::ofstream>::value, int> = 0>
284 DelimWriter(const std::string& filename, bool _quote_minimal = true)
285 : DelimWriter(std::unique_ptr<OutputStream>(new OutputStream(filename, std::ios::out)),
286 _quote_minimal) {
287 if (!owned_out->is_open())
288 internals::throw_failed_open_for_writing(filename);
289 }
290#endif
291
295 template<typename T = OutputStream,
296 csv::enable_if_t<std::is_same<T, std::ofstream>::value, int> = 0>
297 static std::unique_ptr<DelimWriter> try_open(
298 const std::string& filename, CSVError& error, bool quote_minimal = true
299 ) {
300 error.clear();
301 std::unique_ptr<OutputStream> stream(new OutputStream(filename, std::ios::out));
302 if (!stream->is_open()) {
303 internals::fail(error, CSVErrorCode::OpenFailed,
304 std::string(internals::ERROR_FAILED_OPEN_WRITE) + filename);
305 return nullptr;
306 }
307 return std::unique_ptr<DelimWriter>(new DelimWriter(std::move(stream), quote_minimal));
308 }
309
310 DelimWriter(const DelimWriter&) = delete;
311 DelimWriter& operator=(const DelimWriter&) = delete;
312
313 DelimWriter(DelimWriter&& other) noexcept
314 : owned_out(std::move(other.owned_out)),
315 out(other.out),
316 quote_minimal(other.quote_minimal),
317 auto_flush_(other.auto_flush_),
318 batch_buffer_(std::move(other.batch_buffer_)) {
319 if (owned_out && out) {
320 out = owned_out.get();
321 }
322 other.out = nullptr;
323 other.quote_minimal = true;
324 }
325
326 DelimWriter& operator=(DelimWriter&& other) noexcept {
327 if (this == &other) return *this;
328
329 owned_out = std::move(other.owned_out);
330 out = other.out;
331 quote_minimal = other.quote_minimal;
332 auto_flush_ = other.auto_flush_;
333 batch_buffer_ = std::move(other.batch_buffer_);
334
335 if (owned_out && out) {
336 out = owned_out.get();
337 }
338
339 other.out = nullptr;
340 other.quote_minimal = true;
341 return *this;
342 }
343
345 DelimWriter& set_auto_flush(bool value) & noexcept {
346 this->auto_flush_ = value;
347 return *this;
348 }
349
351 DelimWriter&& set_auto_flush(bool value) && noexcept {
352 this->auto_flush_ = value;
353 return std::move(*this);
354 }
355
357 bool get_auto_flush() const noexcept {
358 return this->auto_flush_;
359 }
360
363 // A failed write can have emitted a prefix. Never retry its buffered bytes
364 // during destruction; the caller has already received the I/O failure.
365 if (!out || !ready_for_destruction(*out, 0)) return;
366 CSV_TRY {
367 flush_batch();
368 out->flush();
369 }
370 CSV_CATCH((...), {
371 // Destruction must not replace an active exception or terminate a caller
372 // that already handled a checked I/O failure.
373 })
374 }
375
377 template<typename T, size_t N>
378 DelimWriter& operator<<(const T (&record)[N]) {
379 write_range_impl(record);
380 return *this;
381 }
382
384 template<typename T, size_t N>
385 DelimWriter& operator<<(const std::array<T, N>& record) {
386 write_range_impl(record);
387 return *this;
388 }
389
395#if CSV_EXCEPTIONS_ENABLED
396 template<typename KeyType>
398 append_range_fields(frame.columns());
399 end_record();
400 flush_batch_if_needed();
401
402#ifdef CSV_HAS_CXX20
403 return write_rows(frame);
404#else
405 // Older standards use the existing visible-row conversion path.
406 for (const auto& row : frame) {
407 append_range_fields(std::vector<std::string>(row));
408 end_record();
409 flush_batch_if_needed();
410 }
411 finish_write_call();
412 return *this;
413#endif
414 }
415
416#endif
417
431 template<typename T,
432 csv::enable_if_t<!internals::is_data_frame<typename std::decay<T>::type>::value, int> = 0>
433 auto write_row(T&& record) -> decltype(*this << std::forward<T>(record)) {
434 return *this << std::forward<T>(record);
435 }
436
446 template<typename T, typename U, typename... Rest>
447 DelimWriter& write_row(T&& first, U&& second, Rest&&... rest) {
448 this->write_tuple<0>(std::forward_as_tuple(
449 std::forward<T>(first), std::forward<U>(second), std::forward<Rest>(rest)...));
450 return *this;
451 }
452
453#ifdef CSV_HAS_CXX20
469 template<std::ranges::input_range Rows>
471 DelimWriter& write_rows(Rows&& rows) {
472 for (auto&& row : rows) {
473 append_row_like(row);
474 flush_batch_if_needed();
475 }
476
477 finish_write_call();
478 return *this;
479 }
480
487 template<std::ranges::input_range Range>
488 DelimWriter& operator<<(Range&& container)
489 requires std::ranges::input_range<Range>
490 && std::convertible_to<std::ranges::range_reference_t<Range>, csv::string_view> {
491 write_range_impl(container);
492 return *this;
493 }
494
496 template<typename RowLike>
497 DelimWriter& operator<<(const RowLike& row)
500 append_row_like(row);
501 finish_write_call();
502 return *this;
503 }
504#else
513 template<typename Range>
514 typename std::enable_if<
517 && !std::is_same<Range, std::string>::value
518 && !std::is_same<Range, csv::string_view>::value,
520 >::type operator<<(const Range& record) {
521 write_range_impl(record);
522 return *this;
523 }
524#endif
525
527 template<typename... T>
528 DelimWriter& operator<<(const std::tuple<T...>& record) {
529 this->write_tuple<0, T...>(record);
530 return *this;
531 }
532
534 void flush() {
535 flush_batch();
536 out->flush();
537 }
538
548 template<typename T,
549 csv::enable_if_t<internals::has_checked_csv_fields<T>::value, int> = 0>
550 bool try_write_row(CSVError& error, T&& record) {
551 return this->checked_write(error, std::forward<T>(record));
552 }
553
554 template<typename... Args>
555 auto try_write_row(CSVError& error, Args&&... args)
556 -> decltype(this->write_row(std::forward<Args>(args)...), bool()) {
557 return this->checked_write(error, std::forward<Args>(args)...);
558 }
559
564 template<typename KeyType>
565 bool try_write_frame(const DataFrame<KeyType>& frame, CSVError& error) {
566 return this->checked_write(error, frame);
567 }
568
569#ifdef CSV_HAS_CXX20
571 template<std::ranges::input_range Rows>
574 bool try_write_rows(Rows&& rows, CSVError& error) {
575 return this->checked_operation(error, [this, &rows, &error]() {
576 for (auto&& row : rows) {
577 if (!this->append_checked_row(row, error, 0)) return false;
578 this->flush_batch_if_needed();
579 if (!this->check_write_status(error)) return false;
580 }
581 this->finish_write_call();
582 return true;
583 });
584 }
585#endif
586
588 bool try_flush(CSVError& error) {
589 return this->checked_operation(error, [this]() {
590 this->flush();
591 return true;
592 });
593 }
594
599 template<typename T = OutputStream,
600 csv::enable_if_t<std::is_same<T, std::ofstream>::value, int> = 0>
601 bool try_close(CSVError& error) {
602 error.clear();
603 if (!owned_out || !out) {
604 return internals::fail_static(error, CSVErrorCode::InvalidArgument,
605 "try_close() requires an open, owned output file.");
606 }
607
608 const bool flushed = this->try_flush(error);
609 // Detach before closing so neither close errors nor a failed flush can
610 // trigger a second write from the writer's destructor.
611 out = nullptr;
612 CSV_TRY {
613 owned_out->close();
614 }
615 CSV_CATCH((const std::ios_base::failure& failure), {
616 if (flushed) internals::fail(error, CSVErrorCode::WriteFailed, failure.what());
617 return false;
618 })
619 if (!flushed) return false;
620 if (owned_out->fail()) {
621 return internals::fail_static(error, CSVErrorCode::WriteFailed, "Failed to close CSV output.");
622 }
623 return true;
624 }
625
626 private:
627 DelimWriter(std::unique_ptr<OutputStream> stream, bool quote_minimal)
628 : owned_out(std::move(stream)), out(owned_out.get()), quote_minimal(quote_minimal) {}
629
630 // These probes preserve support for lightweight custom output streams in
631 // the existing unchecked API. Only checked calls require fail().
632 template<typename Stream>
633 static auto exceptions_disabled(const Stream& stream, int)
634 -> decltype(stream.exceptions(), bool()) {
635 return stream.exceptions() == std::ios_base::goodbit;
636 }
637
638 template<typename Stream>
639 static bool exceptions_disabled(const Stream&, long) { return true; }
640
641 template<typename Stream>
642 static auto ready_for_destruction(const Stream& stream, int)
643 -> decltype(stream.fail(), bool()) {
644#if CSV_EXCEPTIONS_ENABLED
645 return !stream.fail();
646#else
647 return !stream.fail() && exceptions_disabled(stream, 0);
648#endif
649 }
650
651 template<typename Stream>
652 static bool ready_for_destruction(const Stream&, long) { return true; }
653
654 template<typename Fn>
655 bool checked_operation(CSVError& error, Fn&& operation) {
656 error.clear();
657 if (!out) {
658 return internals::fail_static(error, CSVErrorCode::InvalidArgument, "CSV output is closed.");
659 }
660#if !CSV_EXCEPTIONS_ENABLED
661 if (!exceptions_disabled(*out, 0)) {
662 return internals::fail_static(error, CSVErrorCode::InvalidArgument,
663 "Checked CSV output requires a stream with exceptions disabled.");
664 }
665#endif
666 if (out->fail()) {
667 return internals::fail_static(error, CSVErrorCode::WriteFailed, "CSV output stream has failed.");
668 }
669 CSV_TRY {
670 if (!std::forward<Fn>(operation)()) return false;
671 }
672 CSV_CATCH((const std::ios_base::failure& failure), {
673 return internals::fail(error, CSVErrorCode::WriteFailed, failure.what());
674 })
675 return this->check_write_status(error);
676 }
677
678 bool check_write_status(CSVError& error) const {
679 return !out->fail() || internals::fail_static(error, CSVErrorCode::WriteFailed,
680 "CSV output stream has failed.");
681 }
682
683 template<typename... Args>
684 bool checked_write(CSVError& error, Args&&... args) {
685 return this->checked_operation(error, [&]() {
686 return this->checked_write_call(error, std::forward<Args>(args)...);
687 });
688 }
689
690 template<typename T>
691 bool checked_write_call(CSVError& error, T&& record) {
692 return this->checked_write_record(std::forward<T>(record), error, 0);
693 }
694
695 template<typename T, typename U, typename... Rest>
696 bool checked_write_call(CSVError&, T&& first, U&& second, Rest&&... rest) {
697 this->write_row(std::forward<T>(first), std::forward<U>(second), std::forward<Rest>(rest)...);
698 return true;
699 }
700
701 template<typename KeyType>
702 bool checked_write_record(const DataFrame<KeyType>& frame, CSVError& error, int) {
703 append_range_fields(frame.columns());
704 end_record();
705 flush_batch_if_needed();
706 if (!this->check_write_status(error)) return false;
707 for (const auto& row : frame) {
708 if (!this->append_checked_row(row, error, 0)) return false;
709 flush_batch_if_needed();
710 if (!this->check_write_status(error)) return false;
711 }
712 finish_write_call();
713 return true;
714 }
715
716 template<typename Row>
717 auto checked_write_record(Row&& row, CSVError& error, int)
718 -> decltype(row.try_field(size_t{0}, error).get_sv(), bool()) {
719 if (!this->append_checked_row(row, error, 0)) return false;
720 finish_write_call();
721 return true;
722 }
723
724 template<typename Row>
725 bool checked_write_record(Row&& row, CSVError&, long) {
726 this->write_row(std::forward<Row>(row));
727 return true;
728 }
729
730 template<typename Row>
731 auto append_checked_row(Row&& row, CSVError& error, int)
732 -> decltype(row.try_field(size_t{0}, error).get_sv(), bool()) {
733 const size_t row_start = batch_buffer_.size();
734 append_range_fields(internals::checked_field_range(row, error));
735 if (error) {
736 // No bytes from this row have been sent yet. Discard its
737 // incomplete buffered record while retaining earlier rows.
738 batch_buffer_.resize(row_start);
739 return false;
740 }
741 end_record();
742 return true;
743 }
744
745#ifdef CSV_HAS_CXX20
746 template<typename Row>
747 bool append_checked_row(Row&& row, CSVError&, long) {
748 append_row_like(std::forward<Row>(row));
749 return true;
750 }
751#endif
752
758 template<typename Range>
759 inline void append_range_fields(Range&& record) {
760 auto it = std::begin(record);
761 auto end = std::end(record);
762
763 if (it != end) {
764 const size_t field_start = batch_buffer_.size();
765 write_field(*it);
766 ++it;
767 if (it == end) quote_lone_empty_field(field_start);
768 }
769
770 for (; it != end; ++it) {
771 batch_buffer_.push_back(Delim);
772 write_field(*it);
773 }
774 }
775
779 void quote_lone_empty_field(size_t field_start) {
780 if (batch_buffer_.size() == field_start) {
781 batch_buffer_.push_back(Quote);
782 batch_buffer_.push_back(Quote);
783 }
784 }
785
787 size_t find_first_special_for_writer(csv::string_view in) const {
788 size_t pos = internals::find_next_non_special(in, 0, simd_sentinels_);
789
790 for (; pos < in.size(); ++pos) {
791 char ch = in[pos];
792 if (ch == Quote || ch == Delim || ch == '\r' || ch == '\n')
793 return pos;
794 }
795
796 return in.size();
797 }
798
802 template<typename Range>
803 inline void write_range_impl(const Range& record) {
804 append_range_fields(record);
805
806 end_record();
807 finish_write_call();
808 }
809
810#ifdef CSV_HAS_CXX20
811 template<typename Row>
812 void append_row_like(Row&& row) {
813 IF_CONSTEXPR(internals::csv_string_field_range<Row>) {
814 append_range_fields(std::forward<Row>(row));
815 }
816 else {
817 append_range_fields(row.to_sv_range());
818 }
819
820 end_record();
821 }
822#endif
823
824 // Preserve the owning checked field through serialization. This also
825 // avoids instantiating the string-conversion fallback in C++11/14.
826 template<typename Field>
827 void write_field(const internals::CheckedFieldValue<Field>& in) {
828 in.with_view([this](csv::string_view value) { write_escaped_field(value); });
829 }
830
831 template<
832 typename T,
833 csv::enable_if_t<
834 !std::is_convertible<T, std::string>::value
835 && !std::is_convertible<T, csv::string_view>::value
836 , int> = 0
837 >
838 void write_field(T in) {
839 const std::string serialized = internals::to_string(in);
840 write_raw(serialized);
841 }
842
843 template<
844 typename T,
845 csv::enable_if_t<
846 std::is_convertible<T, std::string>::value
847 || std::is_convertible<T, csv::string_view>::value
848 , int> = 0
849 >
850 void write_field(T in) {
851 IF_CONSTEXPR(std::is_convertible<T, csv::string_view>::value) {
852 write_escaped_field(in);
853 }
854 else {
855 const std::string serialized(in);
856 write_escaped_field(serialized);
857 }
858 }
859
860 void write_raw(csv::string_view in) {
861 if (!in.empty())
862 batch_buffer_.append(in.data(), in.size());
863 }
864
865 void write_escaped_field(csv::string_view in) {
866 const size_t first_special = find_first_special_for_writer(in);
867
868 if (first_special == in.size()) {
869 if (!quote_minimal) {
870 batch_buffer_.push_back(Quote);
871 write_raw(in);
872 batch_buffer_.push_back(Quote);
873 } else {
874 write_raw(in);
875 }
876 return;
877 }
878
879 write_quoted_field(in, first_special);
880 }
881
882 void write_quoted_field(csv::string_view in, size_t first_special) {
883 batch_buffer_.push_back(Quote);
884
885 size_t chunk_start = 0;
886 size_t pos = first_special;
887 while (pos < in.size()) {
888 if (in[pos] == Quote) {
889 write_raw(in.substr(chunk_start, pos - chunk_start));
890 batch_buffer_.push_back(Quote);
891 batch_buffer_.push_back(Quote);
892 chunk_start = pos + 1;
893 }
894
895 ++pos;
896 }
897
898 write_raw(in.substr(chunk_start));
899 batch_buffer_.push_back(Quote);
900 }
901
903 template<size_t Index = 0, typename... T>
904 typename std::enable_if<Index < sizeof...(T), void>::type write_tuple(const std::tuple<T...>& record) {
905 const size_t field_start = batch_buffer_.size();
906 write_field(std::get<Index>(record));
907
908 CSV_MSVC_PUSH_DISABLE(4127)
909 IF_CONSTEXPR (sizeof...(T) == 1) quote_lone_empty_field(field_start);
910 CSV_MSVC_POP
911
912 CSV_MSVC_PUSH_DISABLE(4127)
913 IF_CONSTEXPR (Index + 1 < sizeof...(T)) batch_buffer_.push_back(Delim);
914 CSV_MSVC_POP
915
916 this->write_tuple<Index + 1>(record);
917 }
918
920 template<size_t Index = 0, typename... T>
921 typename std::enable_if<Index == sizeof...(T), void>::type write_tuple(const std::tuple<T...>& record) {
922 (void)record;
923 end_record();
924 finish_write_call();
925 }
926
928 void end_record() {
929 batch_buffer_.push_back('\n');
930 }
931
932 void finish_write_call() {
933 if (this->auto_flush_) {
934 flush_batch();
935 out->flush();
936 return;
937 }
938
939 flush_batch_if_needed();
940 }
941
942 void flush_batch() {
943 if (batch_buffer_.empty()) return;
944
945 out->write(batch_buffer_.data(), static_cast<std::streamsize>(batch_buffer_.size()));
946 batch_buffer_.clear();
947 }
948
949 void flush_batch_if_needed() {
950 if (batch_buffer_.size() >= batch_flush_threshold_)
951 flush_batch();
952 }
953
959 std::unique_ptr<OutputStream> owned_out;
960
962 OutputStream* out;
963
964 bool quote_minimal;
965 bool auto_flush_ = true;
966 static constexpr size_t batch_flush_threshold_ = 64 * 1024;
967 std::string batch_buffer_;
968 internals::SentinelVecs simd_sentinels_{Delim, Quote};
969 };
970
978 template<class OutputStream>
979 using CSVWriter = DelimWriter<OutputStream, ',', '"'>;
980
989 template<class OutputStream>
990 using TSVWriter = DelimWriter<OutputStream, '\t', '"'>;
991
993 template<class OutputStream>
994 inline CSVWriter<OutputStream> make_csv_writer(OutputStream& out, bool quote_minimal=true) {
995 return CSVWriter<OutputStream>(out, quote_minimal);
996 }
997
999 template<class OutputStream>
1000 inline TSVWriter<OutputStream> make_tsv_writer(OutputStream& out, bool quote_minimal=true) {
1001 return TSVWriter<OutputStream>(out, quote_minimal);
1002 }
1004}
SIMD-accelerated skip for runs of non-special CSV bytes.
const std::vector< std::string > & columns() const noexcept
Get the column names in order.
Class for writing delimiter separated values files.
DelimWriter & write_row(T &&first, U &&second, Rest &&... rest)
Write a row from a variadic list of mixed-type values.
~DelimWriter()
Best-effort flush.
DelimWriter & operator<<(const DataFrame< KeyType > &frame)
Write a complete DataFrame: visible column names followed by visible rows.
DelimWriter & operator<<(const std::array< T, N > &record)
Write a std::array of strings as one delimited row.
static std::unique_ptr< DelimWriter > try_open(const std::string &filename, CSVError &error, bool quote_minimal=true)
Open an owned output file, returning null on failure.
DelimWriter && set_auto_flush(bool value) &&noexcept
Configure whether each write operation flushes the underlying stream.
bool try_write_row(CSVError &error, T &&record)
Write the same inputs accepted by write_row(), reporting stream failures.
DelimWriter & set_auto_flush(bool value) &noexcept
Configure whether each write operation flushes the underlying stream.
DelimWriter(const std::string &filename, bool _quote_minimal=true)
Construct a DelimWriter that owns an output file stream.
bool try_flush(CSVError &error)
Flush buffered data and the stream, reporting I/O errors.
DelimWriter & write_rows(Rows &&rows)
Write many rows using a shared batch buffer.
DelimWriter & operator<<(const T(&record)[N])
Write a C-style array of strings as one delimited row.
DelimWriter & operator<<(const RowLike &row)
Write a row-like object that exposes to_sv_range().
bool get_auto_flush() const noexcept
Return whether each write operation flushes the underlying stream.
DelimWriter & operator<<(const std::tuple< T... > &record)
Write a C-style array of strings as one delimited row.
bool try_close(CSVError &error)
Flush and close an owned output file, including after a flush failure.
auto write_row(T &&record) -> decltype(*this<< std::forward< T >(record))
Write a row from any single-row argument accepted by operator<< (std::vector, std::array,...
void flush()
Flushes the written data.
bool try_write_frame(const DataFrame< KeyType > &frame, CSVError &error)
Write a complete DataFrame, including its header, and report failures.
bool try_write_rows(Rows &&rows, CSVError &error)
Write a batch using the same accepted row ranges and buffering policy as write_rows().
DelimWriter(OutputStream &_out, bool _quote_minimal=true)
Construct a DelimWriter over the specified output stream.
DelimWriter & operator<<(Range &&container)
Write a range of string-like fields as one delimited row.
Borrow a row and error state for synchronous, single-pass field access.
A standalone header file containing shared code.
#define IF_CONSTEXPR
Expands to if constexpr in C++17 and if otherwise.
Definition common.hpp:191
Shared exception message templates and throw helpers.
T csv_abs(T x)
Calculate the absolute value of a number.
CSV_CONST CONSTEXPR_14 long double pow10(const T &n) noexcept
Compute 10 to the power of an integral exponent.
int num_digits(T x)
Calculate the number of digits in a number.
std::string to_string(T value)
to_string() for unsigned integers
The all encompassing namespace.
TSVWriter< OutputStream > make_tsv_writer(OutputStream &out, bool quote_minimal=true)
Return a csv::TSVWriter over the output stream.
@ InvalidArgument
Invalid operation input, such as a null stream or zero batch size.
@ WriteFailed
Writing, flushing, or closing the output failed.
@ OpenFailed
A native file could not be opened, including initial input mapping.
CSVWriter< OutputStream > make_csv_writer(OutputStream &out, bool quote_minimal=true)
Return a csv::CSVWriter over the output stream.
std::string_view string_view
The string_view class used by this library.
Definition common.hpp:182
A diagnostic whose text remains valid for the lifetime of the error.
Definition csv_error.hpp:94
SFINAE trait: detects if a type is iterable (has std::begin/end).
SFINAE trait: detects if a type is a std::tuple.