add composite, add bool error handling

This commit is contained in:
ado 2020-12-12 23:32:06 +01:00
parent 0c25efd353
commit a06121ff92
2 changed files with 177 additions and 37 deletions

View File

@ -99,16 +99,20 @@ struct tied_class {
template <typename... Ts> template <typename... Ts>
constexpr bool tied_class_v = tied_class<Ts...>::value; constexpr bool tied_class_v = tied_class<Ts...>::value;
// the error can be set inside a string, or a bool
enum class error_mode { String, Bool };
//////////////// ////////////////
// converter // converter
//////////////// ////////////////
class converter { class converter {
using string_range = std::pair<const char*, const char*>; using string_range = std::pair<const char*, const char*>;
using split_input = std::vector<string_range>;
constexpr static auto default_delimiter = ','; constexpr static auto default_delimiter = ',';
public: public:
using split_input = std::vector<string_range>;
// parses line with given delimiter, returns a 'T' object created with // parses line with given delimiter, returns a 'T' object created with
// extracted values of type 'Ts' // extracted values of type 'Ts'
template <typename T, typename... Ts> template <typename T, typename... Ts>
@ -154,11 +158,17 @@ class converter {
} }
bool valid() const { bool valid() const {
return error_.empty(); return (error_mode_ == error_mode::String)
? string_error_.empty()
: bool_error_ == false;
} }
const std::string& error() const { const std::string& error_msg() const {
return error_; return string_error_;
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
} }
// 'splits' string by given delimiter, returns vector of pairs which // 'splits' string by given delimiter, returns vector of pairs which
@ -185,6 +195,11 @@ class converter {
// error // error
//////////////// ////////////////
void clear_error() {
string_error_.clear();
bool_error_ = false;
}
std::string error_sufix(const string_range msg, size_t pos) const { std::string error_sufix(const string_range msg, size_t pos) const {
std::string error; std::string error;
error.reserve(32); error.reserve(32);
@ -197,22 +212,38 @@ class converter {
} }
void set_error_invalid_conversion(const string_range msg, size_t pos) { void set_error_invalid_conversion(const string_range msg, size_t pos) {
error_.clear(); if (error_mode_ == error_mode::String) {
error_.append("invalid conversion for parameter ") string_error_.clear();
string_error_
.append("invalid conversion for parameter ")
.append(error_sufix(msg, pos)); .append(error_sufix(msg, pos));
} else {
bool_error_ = true;
}
} }
void set_error_validate(const char* const error, const string_range msg, void set_error_validate(const char* const error, const string_range msg,
size_t pos) { size_t pos) {
error_.clear(); if (error_mode_ == error_mode::String) {
error_.append(error).append(" ").append(error_sufix(msg, pos)); string_error_.clear();
string_error_.append(error).append(" ").append(
error_sufix(msg, pos));
} else {
bool_error_ = true;
}
} }
void set_error_number_of_colums(size_t expected_pos, size_t pos) { void set_error_number_of_colums(size_t expected_pos, size_t pos) {
error_.append("invalid number of columns, expected: ") if (error_mode_ == error_mode::String) {
string_error_.clear();
string_error_
.append("invalid number of columns, expected: ")
.append(std::to_string(expected_pos)) .append(std::to_string(expected_pos))
.append(", got: ") .append(", got: ")
.append(std::to_string(pos)); .append(std::to_string(pos));
} else {
bool_error_ = true;
}
} }
//////////////// ////////////////
@ -221,7 +252,7 @@ class converter {
template <typename... Ts> template <typename... Ts>
no_void_validator_tup_t<Ts...> convert_impl(const split_input& elems) { no_void_validator_tup_t<Ts...> convert_impl(const split_input& elems) {
error_.clear(); clear_error();
no_void_validator_tup_t<Ts...> ret{}; no_void_validator_tup_t<Ts...> ret{};
if (sizeof...(Ts) != elems.size()) { if (sizeof...(Ts) != elems.size()) {
set_error_number_of_colums(sizeof...(Ts), elems.size()); set_error_number_of_colums(sizeof...(Ts), elems.size());
@ -339,7 +370,9 @@ class converter {
//////////////// ////////////////
std::vector<string_range> input_; std::vector<string_range> input_;
std::string error_; std::string string_error_;
bool bool_error_;
enum error_mode error_mode_ { error_mode::String };
}; };
template <> template <>

View File

@ -4,12 +4,17 @@
#include "extract.hpp" #include "extract.hpp"
#include "restrictions.hpp" #include "restrictions.hpp"
#include <cstring> #include <cstring>
#include <optional>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
#include <vector> #include <vector>
namespace ss { namespace ss {
struct None {};
template <typename...>
class composite;
class parser { class parser {
public: public:
parser(const std::string& file_name, const std::string& delimiter) parser(const std::string& file_name, const std::string& delimiter)
@ -28,7 +33,18 @@ class parser {
} }
bool valid() const { bool valid() const {
return error_.empty(); return (error_mode_ == error_mode::String)
? string_error_.empty()
: bool_error_ == false;
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
converter_.set_error_mode(mode);
}
const std::string& error_msg() const {
return string_error_;
} }
bool eof() const { bool eof() const {
@ -39,10 +55,6 @@ class parser {
return buff_.read(file_); return buff_.read(file_);
} }
const std::string& error() const {
return error_;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
T get_struct() { T get_struct() {
return to_struct<T>(get_next<Ts...>()); return to_struct<T>(get_next<Ts...>());
@ -50,13 +62,15 @@ class parser {
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
error_.clear(); buff_.update();
clear_error();
if (eof_) { if (eof_) {
set_error_eof_reached(); set_error_eof_reached();
return {}; return {};
} }
auto value = converter_.convert<T, Ts...>(buff_.get(), delim_); split_input_ = converter_.split(buff_.get(), delim_);
auto value = converter_.convert<T, Ts...>(split_input_);
if (!converter_.valid()) { if (!converter_.valid()) {
set_error_invalid_conversion(); set_error_invalid_conversion();
@ -66,39 +80,110 @@ class parser {
return value; return value;
} }
////////////////
// composite conversion
////////////////
template <typename... Ts>
class composite {
public:
composite(std::tuple<Ts...> values, parser& parser)
: values_{values}, parser_{parser} {
}
template <typename... Us, typename Fun = None>
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>>
or_else(Fun&& fun = None{}) {
std::optional<no_void_validator_tup_t<Us...>> value;
if (!parser_.valid()) {
auto new_value = try_same<Us...>();
if (parser_.valid()) {
value = new_value;
if constexpr (!std::is_same_v<Fun,
None>) {
fun(*value);
}
}
}
auto values =
std::tuple_cat(std::move(values_),
std::tuple{std::move(value)});
return {std::move(values), parser_};
}
private: private:
template <typename U, typename... Us>
no_void_validator_tup_t<U, Us...> try_same() {
parser_.clear_error();
auto value = parser_.converter_.convert<U, Us...>(
parser_.split_input_);
if (!parser_.converter_.valid()) {
parser_.set_error_invalid_conversion();
}
return value;
}
std::tuple<Ts...> values_;
parser& parser_;
};
template <typename... Ts, typename Fun = None>
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
Fun&& fun = None{}) {
std::optional<no_void_validator_tup_t<Ts...>> value;
auto new_value = get_next<Ts...>();
if (valid()) {
value = new_value;
if constexpr (!std::is_same_v<Fun, None>) {
fun(*value);
}
}
return {value, *this};
};
private:
template <typename...>
friend class composite;
//////////////// ////////////////
// line reading // line reading
//////////////// ////////////////
class buffer { class buffer {
char* buffer_{nullptr}; char* buffer_{nullptr};
char* new_buffer_{nullptr};
size_t size_{0}; size_t size_{0};
public: public:
~buffer() { ~buffer() {
free(buffer_); free(buffer_);
free(new_buffer_);
} }
bool read(FILE* file) { bool read(FILE* file) {
ssize_t size = getline(&buffer_, &size_, file); ssize_t size = getline(&new_buffer_, &size_, file);
size_t string_end = size - 1; size_t string_end = size - 1;
if (size == -1) { if (size == -1) {
return false; return false;
} }
if (size >= 2 && buffer_[size - 2] == '\r') { if (size >= 2 && new_buffer_[size - 2] == '\r') {
string_end--; string_end--;
} }
buffer_[string_end] = '\0'; new_buffer_[string_end] = '\0';
return true; return true;
} }
const char* get() const { const char* get() const {
return buffer_; return buffer_;
} }
void update() {
std::swap(buffer_, new_buffer_);
}
}; };
void read_line() { void read_line() {
@ -110,23 +195,42 @@ class parser {
// error // error
//////////////// ////////////////
void clear_error() {
string_error_.clear();
bool_error_ = false;
}
void set_error_file_not_open() { void set_error_file_not_open() {
error_.append(file_name_).append(" could not be not open."); if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" could not be not open.");
} else {
bool_error_ = true;
}
} }
void set_error_eof_reached() { void set_error_eof_reached() {
error_.append(file_name_).append(" reached end of file."); if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" reached end of file.");
} else {
bool_error_ = true;
}
} }
void set_error_invalid_conversion() { void set_error_invalid_conversion() {
error_.append(file_name_) if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" ") .append(" ")
.append(std::to_string(line_number_)) .append(std::to_string(line_number_))
.append(": ") .append(": ")
.append(converter_.error()) .append(converter_.error_msg())
.append(": \"") .append(": \"")
.append(buff_.get()); .append(buff_.get())
error_.append("\""); .append("\"");
} else {
bool_error_ = true;
}
} }
//////////////// ////////////////
@ -135,8 +239,11 @@ class parser {
const std::string file_name_; const std::string file_name_;
const std::string delim_; const std::string delim_;
std::string error_; std::string string_error_;
ss::converter converter_; bool bool_error_;
error_mode error_mode_{error_mode::String};
converter converter_;
converter::split_input split_input_;
FILE* file_{nullptr}; FILE* file_{nullptr};
buffer buff_; buffer buff_;
size_t line_number_{0}; size_t line_number_{0};