10#if !__cpp_lib_to_chars
18 requires(std::floating_point<F> || std::integral<F>)
21 std::array<char, bufmaxsize + 1>
s;
24 static constexpr char end =
'\n';
26 [[nodiscard]] F
read(std::istream &is,
char sep) {
32 char *bufend =
s.data() +
bufidx;
35 if (ptr != bufend && *ptr != sep)
37 std::string{*ptr} +
"'");
40 std::copy(ptr + 1,
static_cast<const char *
>(bufend),
s.data());
49 using namespace std::string_literals;
52 (is.bad() ?
" bad" :
"") +
53 (is.fail() ?
" fail" :
"") +
54 (is.eof() ?
" eof" :
""));
59 (is.bad() ?
" bad" :
"") +
60 (is.fail() ?
" fail" :
"") +
61 (is.eof() ?
" eof" :
""));
69 if (is.eof() || is.peek() ==
end)
73 if (
bufidx == 0 ||
s.front() !=
'#')
85 static const char *read_single(
const char *bufbegin,
const char *bufend,
87 if (bufbegin != bufend && *bufbegin ==
'+')
89 const auto [ptr, ec] = std::from_chars(bufbegin, bufend, v);
90 const auto bufvw = std::string_view(bufbegin, bufend);
91 if (ec != std::errc{})
92 throw csv_read_error(
"io::csv_read_row conversion failed '" +
94 "': " + std::make_error_code(ec).message());
98 static void strtod_ovl(
const char *str,
char **str_end,
float &v) {
99 v = std::strtof(str, str_end);
101 static void strtod_ovl(
const char *str,
char **str_end,
double &v) {
102 v = std::strtod(str, str_end);
104 static void strtod_ovl(
const char *str,
char **str_end,
long double &v) {
105 v = std::strtold(str, str_end);
107 static void strtod_ovl(
const char *str,
char **str_end,
long long &v) {
108 v = std::strtoll(str, str_end, 10);
110 static void strtod_ovl(
const char *str,
char **str_end,
long &v) {
111 v = std::strtol(str, str_end, 10);
113 static void strtod_ovl(
const char *str,
char **str_end,
int &v) {
114 v =
static_cast<int>(std::strtol(str, str_end, 10));
116 static void strtod_ovl(
const char *str,
char **str_end,
short &v) {
117 v =
static_cast<short>(std::strtol(str, str_end, 10));
119 static const char *
read_single(
const char *bufbegin,
char *bufend, F &v) {
124 if (errno || ptr == bufbegin)
126 std::string(bufbegin) +
127 "': " + std::to_string(errno));
133 if (
bufidx > 0 || (!is.eof() && is.get() !=
end))
137 [[nodiscard]]
bool done(std::istream &is)
const {
143#ifdef GUANAQO_WITH_QUAD_PRECISION
150 v =
static_cast<__float128
>(ld);
156 requires(std::floating_point<F> || std::integral<F>)
161 vv = reader.
read(is, sep);
166 requires(std::floating_point<F> || std::integral<F>)
170 for (ptrdiff_t r = 0; r < v.
rows; ++r) {
172 for (ptrdiff_t c = 0; c < v.
cols; ++c)
173 v(r, c) = reader.
read(is, sep);
179 requires(std::floating_point<F> || std::integral<F>)
184 while (!reader.
done(is))
185 v.push_back(reader.
read(is, sep));
Reading matrices and vectors from CSV files.
void csv_read_row(std::istream &is, std::span< F > v, char sep)
std::vector< F > csv_read_row_std_vector(std::istream &is, char sep)
void csv_read(std::istream &is, MatrixView< F, ptrdiff_t, ptrdiff_t > v, char sep)
A lightweight view of a 2D matrix.
static void strtod_ovl(const char *str, char **str_end, int &v)
void skip_comments(std::istream &is)
static const char * read_single(const char *bufbegin, char *bufend, F &v)
static constexpr char end
static void strtod_ovl(const char *str, char **str_end, long long &v)
static void strtod_ovl(const char *str, char **str_end, long &v)
static void strtod_ovl(const char *str, char **str_end, short &v)
static void strtod_ovl(const char *str, char **str_end, float &v)
void read_chunk(std::istream &is)
static void strtod_ovl(const char *str, char **str_end, double &v)
F read(std::istream &is, char sep)
bool done(std::istream &is) const
void next_line(std::istream &is) const
static constexpr std::streamsize bufmaxsize
std::array< char, bufmaxsize+1 > s
static void strtod_ovl(const char *str, char **str_end, long double &v)