2020-12-10 19:26:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "converter.hpp"
|
|
|
|
#include "extract.hpp"
|
|
|
|
#include "restrictions.hpp"
|
2021-01-10 23:51:20 +01:00
|
|
|
#include <cstdlib>
|
2020-12-10 19:26:56 +01:00
|
|
|
#include <cstring>
|
2020-12-12 23:32:06 +01:00
|
|
|
#include <optional>
|
2020-12-10 19:26:56 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace ss {
|
|
|
|
|
2021-01-17 21:46:36 +01:00
|
|
|
template <typename... Matchers>
|
2020-12-10 19:26:56 +01:00
|
|
|
class parser {
|
2021-01-17 21:46:36 +01:00
|
|
|
struct none {};
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
public:
|
|
|
|
parser(const std::string& file_name, const std::string& delimiter)
|
|
|
|
: file_name_{file_name}, delim_{delimiter},
|
|
|
|
file_{fopen(file_name_.c_str(), "rb")} {
|
|
|
|
if (file_) {
|
|
|
|
read_line();
|
|
|
|
} else {
|
|
|
|
set_error_file_not_open();
|
|
|
|
eof_ = true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~parser() {
|
2021-01-17 21:46:36 +01:00
|
|
|
if (file_) {
|
|
|
|
fclose(file_);
|
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool valid() const {
|
2021-01-01 21:57:14 +01:00
|
|
|
return (error_mode_ == error_mode::error_string) ? string_error_.empty()
|
|
|
|
: bool_error_ == false;
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_mode(error_mode mode) {
|
|
|
|
error_mode_ = mode;
|
2021-01-31 23:08:46 +01:00
|
|
|
reader_.set_error_mode(mode);
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& error_msg() const {
|
|
|
|
return string_error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool eof() const {
|
|
|
|
return eof_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignore_next() {
|
2021-01-31 23:08:46 +01:00
|
|
|
return reader_.read(file_);
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Ts>
|
|
|
|
T get_object() {
|
|
|
|
return to_object<T>(get_next<Ts...>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Ts>
|
|
|
|
no_void_validator_tup_t<T, Ts...> get_next() {
|
2021-01-31 23:08:46 +01:00
|
|
|
reader_.update();
|
2020-12-26 00:46:42 +01:00
|
|
|
clear_error();
|
|
|
|
if (eof_) {
|
|
|
|
set_error_eof_reached();
|
|
|
|
return {};
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
auto value = reader_.get_converter().template convert<T, Ts...>();
|
2020-12-12 23:32:06 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
if (!reader_.get_converter().valid()) {
|
2020-12-26 00:46:42 +01:00
|
|
|
set_error_invalid_conversion();
|
2020-12-12 23:32:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
read_line();
|
|
|
|
return value;
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
////////////////
|
|
|
|
// composite conversion
|
|
|
|
////////////////
|
|
|
|
template <typename... Ts>
|
|
|
|
class composite {
|
|
|
|
public:
|
|
|
|
composite(std::tuple<Ts...>&& values, parser& parser)
|
|
|
|
: values_{std::move(values)}, parser_{parser} {
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
// tries to convert the same line with a different output type
|
|
|
|
// only if the previous conversion was not successful,
|
|
|
|
// returns composite containing itself and the new output
|
|
|
|
// as optional, additionally, if a parameter is passed, and
|
|
|
|
// that parameter can be invoked using the converted value,
|
|
|
|
// than it will be invoked in the case of a valid conversion
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename... Us, typename Fun = none>
|
2020-12-26 00:46:42 +01:00
|
|
|
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>> or_else(
|
2020-12-26 00:56:39 +01:00
|
|
|
Fun&& fun = none{}) {
|
2020-12-26 00:46:42 +01:00
|
|
|
using Value = no_void_validator_tup_t<Us...>;
|
|
|
|
std::optional<Value> value;
|
|
|
|
try_convert_and_invoke<Value, Us...>(value, fun);
|
|
|
|
return composite_with(std::move(value));
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
// same as or_else, but saves the result into a 'U' object
|
|
|
|
// instead of a tuple
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename U, typename... Us, typename Fun = none>
|
2021-01-02 02:31:45 +01:00
|
|
|
composite<Ts..., std::optional<U>> or_object(Fun&& fun = none{}) {
|
2020-12-26 00:46:42 +01:00
|
|
|
std::optional<U> value;
|
|
|
|
try_convert_and_invoke<U, Us...>(value, fun);
|
|
|
|
return composite_with(std::move(value));
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
std::tuple<Ts...> values() {
|
|
|
|
return values_;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
template <typename Fun>
|
|
|
|
auto on_error(Fun&& fun) {
|
|
|
|
if (!parser_.valid()) {
|
|
|
|
if constexpr (std::is_invocable_v<Fun>) {
|
|
|
|
fun();
|
|
|
|
} else {
|
|
|
|
std::invoke(std::forward<Fun>(fun), parser_.error_msg());
|
2020-12-12 23:32:06 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2020-12-12 23:32:06 +01:00
|
|
|
|
2020-12-10 19:26:56 +01:00
|
|
|
private:
|
2020-12-26 00:46:42 +01:00
|
|
|
template <typename T>
|
|
|
|
composite<Ts..., T> composite_with(T&& new_value) {
|
|
|
|
auto merged_values =
|
|
|
|
std::tuple_cat(std::move(values_),
|
2021-01-10 23:51:20 +01:00
|
|
|
std::tuple<T>{parser_.valid()
|
2021-01-17 21:46:36 +01:00
|
|
|
? std::forward<T>(new_value)
|
|
|
|
: std::nullopt});
|
2020-12-26 00:46:42 +01:00
|
|
|
return {std::move(merged_values), parser_};
|
2020-12-15 22:32:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename U, typename... Us, typename Fun = none>
|
2020-12-26 00:46:42 +01:00
|
|
|
void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
|
|
|
|
if (!parser_.valid()) {
|
|
|
|
auto tuple_output = try_same<Us...>();
|
2021-01-03 17:27:21 +01:00
|
|
|
if (!parser_.valid()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
|
2021-01-03 17:27:21 +01:00
|
|
|
value = to_object<U>(std::move(tuple_output));
|
2020-12-26 00:46:42 +01:00
|
|
|
} else {
|
2021-01-03 17:27:21 +01:00
|
|
|
value = std::move(tuple_output);
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2021-01-03 17:27:21 +01:00
|
|
|
parser_.try_invoke(*value, std::forward<Fun>(fun));
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-15 22:32:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
template <typename U, typename... Us>
|
|
|
|
no_void_validator_tup_t<U, Us...> try_same() {
|
|
|
|
parser_.clear_error();
|
|
|
|
auto value =
|
2021-01-31 23:08:46 +01:00
|
|
|
parser_.reader_.get_converter().template convert<U, Us...>();
|
|
|
|
if (!parser_.reader_.get_converter().valid()) {
|
2020-12-26 00:46:42 +01:00
|
|
|
parser_.set_error_invalid_conversion();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
std::tuple<Ts...> values_;
|
|
|
|
parser& parser_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// tries to convert a line and returns a composite which is
|
|
|
|
// able to try additional conversions in case of failure
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename... Ts, typename Fun = none>
|
2020-12-26 00:46:42 +01:00
|
|
|
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
|
2020-12-26 00:56:39 +01:00
|
|
|
Fun&& fun = none{}) {
|
2021-01-03 17:27:21 +01:00
|
|
|
using Ret = no_void_validator_tup_t<Ts...>;
|
|
|
|
return try_invoke_and_make_composite<
|
|
|
|
std::optional<Ret>>(get_next<Ts...>(), std::forward<Fun>(fun));
|
2021-01-19 20:26:36 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
|
2021-01-01 23:52:14 +01:00
|
|
|
// identical to try_next but returns composite with object instead of a
|
|
|
|
// tuple
|
|
|
|
template <typename T, typename... Ts, typename Fun = none>
|
|
|
|
composite<std::optional<T>> try_object(Fun&& fun = none{}) {
|
2021-01-03 17:27:21 +01:00
|
|
|
return try_invoke_and_make_composite<
|
|
|
|
std::optional<T>>(get_object<T, Ts...>(), std::forward<Fun>(fun));
|
2021-01-19 20:26:36 +01:00
|
|
|
}
|
2021-01-01 23:52:14 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
private:
|
|
|
|
// tries to invoke the given function (see below), if the function
|
|
|
|
// returns a value which can be used as a conditional, and it returns
|
|
|
|
// false, the function sets an error, and allows the invoke of the
|
|
|
|
// next possible conversion as if the validation of the current one
|
|
|
|
// failed
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename Arg, typename Fun = none>
|
2020-12-26 00:46:42 +01:00
|
|
|
void try_invoke(Arg&& arg, Fun&& fun) {
|
2020-12-26 00:56:39 +01:00
|
|
|
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, none>;
|
2020-12-26 00:46:42 +01:00
|
|
|
if constexpr (!is_none) {
|
|
|
|
using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun)));
|
|
|
|
constexpr bool returns_void = std::is_same_v<Ret, void>;
|
|
|
|
if constexpr (!returns_void) {
|
|
|
|
if (!try_invoke_impl(arg, std::forward<Fun>(fun))) {
|
|
|
|
set_error_failed_check();
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
} else {
|
|
|
|
try_invoke_impl(arg, std::forward<Fun>(fun));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 00:56:39 +01:00
|
|
|
// tries to invoke the function if not none
|
2020-12-26 00:46:42 +01:00
|
|
|
// it first tries to invoke the function without arguments,
|
|
|
|
// than with one argument if the function accepts the whole tuple
|
|
|
|
// as an argument, and finally tries to invoke it with the tuple
|
|
|
|
// laid out as a parameter pack
|
2020-12-26 00:56:39 +01:00
|
|
|
template <typename Arg, typename Fun = none>
|
2020-12-26 00:46:42 +01:00
|
|
|
auto try_invoke_impl(Arg&& arg, Fun&& fun) {
|
2020-12-26 00:56:39 +01:00
|
|
|
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, none>;
|
2020-12-26 00:46:42 +01:00
|
|
|
if constexpr (!is_none) {
|
|
|
|
if constexpr (std::is_invocable_v<Fun>) {
|
|
|
|
return fun();
|
|
|
|
} else if constexpr (std::is_invocable_v<Fun, Arg>) {
|
|
|
|
return std::invoke(std::forward<Fun>(fun),
|
|
|
|
std::forward<Arg>(arg));
|
|
|
|
} else {
|
|
|
|
return std::apply(std::forward<Fun>(fun),
|
|
|
|
std::forward<Arg>(arg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-01-03 17:27:21 +01:00
|
|
|
template <typename T, typename Fun = none>
|
|
|
|
composite<T> try_invoke_and_make_composite(T&& value, Fun&& fun) {
|
|
|
|
if (valid()) {
|
|
|
|
try_invoke(*value, std::forward<Fun>(fun));
|
|
|
|
}
|
|
|
|
return {valid() ? std::move(value) : std::nullopt, *this};
|
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
////////////////
|
|
|
|
// line reading
|
|
|
|
////////////////
|
2020-12-11 18:14:06 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
class reader {
|
2020-12-26 00:46:42 +01:00
|
|
|
char* buffer_{nullptr};
|
2021-01-10 23:51:20 +01:00
|
|
|
char* next_line_buffer_{nullptr};
|
2021-01-31 23:08:46 +01:00
|
|
|
char* helper_buffer_{nullptr};
|
2021-01-10 23:51:20 +01:00
|
|
|
|
2021-01-17 21:46:36 +01:00
|
|
|
converter<Matchers...> converter_;
|
|
|
|
converter<Matchers...> next_line_converter_;
|
2021-01-10 23:51:20 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
size_t size_{0};
|
2021-01-31 23:08:46 +01:00
|
|
|
size_t helper_size_{0};
|
2021-01-10 23:51:20 +01:00
|
|
|
const std::string& delim_;
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
bool crlf;
|
|
|
|
|
|
|
|
bool escaped_eol(size_t size) {
|
|
|
|
if constexpr (setup<Matchers...>::escape::enabled) {
|
|
|
|
const char* curr;
|
|
|
|
for (curr = next_line_buffer_ + size - 1;
|
|
|
|
curr >= next_line_buffer_ &&
|
|
|
|
setup<Matchers...>::escape::match(*curr);
|
|
|
|
--curr) {
|
|
|
|
}
|
|
|
|
return (next_line_buffer_ - curr + size) % 2 == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unterminated_quote() {
|
|
|
|
if constexpr (ss::setup<Matchers...>::quote::enabled) {
|
|
|
|
if (next_line_converter_.unterminated_quote()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void undo_remove_eol(size_t& string_end) {
|
|
|
|
if (crlf) {
|
|
|
|
memcpy(next_line_buffer_ + string_end, "\r\n\0", 3);
|
|
|
|
string_end += 2;
|
|
|
|
} else {
|
|
|
|
memcpy(next_line_buffer_ + string_end, "\n\0", 2);
|
|
|
|
string_end += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t remove_eol(char*& buffer, size_t size) {
|
|
|
|
size_t new_size = size - 1;
|
|
|
|
if (size >= 2 && buffer[size - 2] == '\r') {
|
|
|
|
crlf = true;
|
|
|
|
new_size--;
|
|
|
|
} else {
|
|
|
|
crlf = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[new_size] = '\0';
|
|
|
|
return new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void realloc_concat(char*& first, size_t& first_size,
|
|
|
|
const char* const second, size_t second_size) {
|
|
|
|
first = static_cast<char*>(realloc(static_cast<void*>(first),
|
|
|
|
first_size + second_size + 2));
|
|
|
|
|
|
|
|
memcpy(first + first_size, second, second_size + 1);
|
|
|
|
first_size += second_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool append_line(FILE* file, char*& dst_buffer, size_t& dst_size) {
|
|
|
|
undo_remove_eol(dst_size);
|
|
|
|
|
|
|
|
ssize_t ssize = getline(&helper_buffer_, &helper_size_, file);
|
|
|
|
if (ssize == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size = remove_eol(helper_buffer_, ssize);
|
|
|
|
realloc_concat(dst_buffer, dst_size, helper_buffer_, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
public:
|
2021-01-31 23:08:46 +01:00
|
|
|
reader(const std::string& delimiter) : delim_{delimiter} {
|
2021-01-10 23:51:20 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
~reader() {
|
2020-12-26 00:46:42 +01:00
|
|
|
free(buffer_);
|
2021-01-10 23:51:20 +01:00
|
|
|
free(next_line_buffer_);
|
2021-01-31 23:08:46 +01:00
|
|
|
free(helper_buffer_);
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-11 18:14:06 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
bool read(FILE* file) {
|
2021-01-31 23:08:46 +01:00
|
|
|
ssize_t ssize = getline(&next_line_buffer_, &size_, file);
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
if (ssize == -1) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-12 23:32:06 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
size_t size = remove_eol(next_line_buffer_, ssize);
|
|
|
|
|
|
|
|
while (escaped_eol(size)) {
|
|
|
|
if (!append_line(file, next_line_buffer_, size)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2021-01-10 23:51:20 +01:00
|
|
|
next_line_converter_.split(next_line_buffer_, delim_);
|
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
while (unterminated_quote()) {
|
|
|
|
if (!append_line(file, next_line_buffer_, size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
next_line_converter_.resplit(next_line_buffer_, size);
|
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
return true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 23:51:20 +01:00
|
|
|
void set_error_mode(error_mode mode) {
|
|
|
|
converter_.set_error_mode(mode);
|
|
|
|
next_line_converter_.set_error_mode(mode);
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:46:36 +01:00
|
|
|
converter<Matchers...>& get_converter() {
|
2021-01-10 23:51:20 +01:00
|
|
|
return converter_;
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
const char* get_buffer() const {
|
2020-12-26 00:46:42 +01:00
|
|
|
return buffer_;
|
2020-12-12 23:32:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
void update() {
|
2021-01-10 23:51:20 +01:00
|
|
|
std::swap(buffer_, next_line_buffer_);
|
|
|
|
std::swap(converter_, next_line_converter_);
|
2020-12-15 22:32:34 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void read_line() {
|
2021-01-31 23:08:46 +01:00
|
|
|
eof_ = !reader_.read(file_);
|
2020-12-26 00:46:42 +01:00
|
|
|
++line_number_;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// error
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
void clear_error() {
|
|
|
|
string_error_.clear();
|
|
|
|
bool_error_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_failed_check() {
|
2021-01-01 21:57:14 +01:00
|
|
|
if (error_mode_ == error_mode::error_string) {
|
2020-12-26 00:46:42 +01:00
|
|
|
string_error_.append(file_name_).append(" failed check.");
|
|
|
|
} else {
|
|
|
|
bool_error_ = true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
void set_error_file_not_open() {
|
2021-01-02 02:31:45 +01:00
|
|
|
string_error_.append(file_name_).append(" could not be opened.");
|
2020-12-31 01:01:18 +01:00
|
|
|
bool_error_ = true;
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
void set_error_eof_reached() {
|
2021-01-01 21:57:14 +01:00
|
|
|
if (error_mode_ == error_mode::error_string) {
|
2020-12-26 00:46:42 +01:00
|
|
|
string_error_.append(file_name_).append(" reached end of file.");
|
|
|
|
} else {
|
|
|
|
bool_error_ = true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_invalid_conversion() {
|
2021-01-01 21:57:14 +01:00
|
|
|
if (error_mode_ == error_mode::error_string) {
|
2020-12-26 00:46:42 +01:00
|
|
|
string_error_.append(file_name_)
|
|
|
|
.append(" ")
|
|
|
|
.append(std::to_string(line_number_))
|
|
|
|
.append(": ")
|
2021-01-31 23:08:46 +01:00
|
|
|
.append(reader_.get_converter().error_msg())
|
2020-12-26 00:46:42 +01:00
|
|
|
.append(": \"")
|
2021-01-31 23:08:46 +01:00
|
|
|
.append(reader_.get_buffer())
|
2020-12-26 00:46:42 +01:00
|
|
|
.append("\"");
|
|
|
|
} else {
|
|
|
|
bool_error_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// members
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
const std::string file_name_;
|
|
|
|
const std::string delim_;
|
|
|
|
std::string string_error_;
|
2021-01-01 23:52:14 +01:00
|
|
|
bool bool_error_{false};
|
2021-01-01 21:57:14 +01:00
|
|
|
error_mode error_mode_{error_mode::error_bool};
|
2020-12-26 00:46:42 +01:00
|
|
|
FILE* file_{nullptr};
|
2021-01-31 23:08:46 +01:00
|
|
|
reader reader_{delim_};
|
2020-12-26 00:46:42 +01:00
|
|
|
size_t line_number_{0};
|
|
|
|
bool eof_{false};
|
2020-12-10 19:26:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* ss */
|