mirror of
https://github.com/red0124/ssp.git
synced 2025-12-14 21:59:55 +01:00
make indent with 4 caracters instead of 8
This commit is contained in:
@@ -16,344 +16,328 @@ template <typename...>
|
||||
class composite;
|
||||
|
||||
class parser {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
~parser() {
|
||||
fclose(file_);
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
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 {
|
||||
return eof_;
|
||||
}
|
||||
|
||||
bool ignore_next() {
|
||||
return buff_.read(file_);
|
||||
}
|
||||
|
||||
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() {
|
||||
buff_.update();
|
||||
clear_error();
|
||||
if (eof_) {
|
||||
set_error_eof_reached();
|
||||
return {};
|
||||
}
|
||||
|
||||
split_input_ = converter_.split(buff_.get(), delim_);
|
||||
auto value = converter_.convert<T, Ts...>(split_input_);
|
||||
|
||||
if (!converter_.valid()) {
|
||||
set_error_invalid_conversion();
|
||||
}
|
||||
|
||||
read_line();
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// composite conversion
|
||||
////////////////
|
||||
template <typename... Ts>
|
||||
class composite {
|
||||
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;
|
||||
}
|
||||
composite(std::tuple<Ts...>&& values, parser& parser)
|
||||
: values_{std::move(values)}, parser_{parser} {
|
||||
}
|
||||
|
||||
~parser() {
|
||||
fclose(file_);
|
||||
}
|
||||
|
||||
bool valid() const {
|
||||
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 {
|
||||
return eof_;
|
||||
}
|
||||
|
||||
bool ignore_next() {
|
||||
return buff_.read(file_);
|
||||
}
|
||||
|
||||
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() {
|
||||
buff_.update();
|
||||
clear_error();
|
||||
if (eof_) {
|
||||
set_error_eof_reached();
|
||||
return {};
|
||||
}
|
||||
|
||||
split_input_ = converter_.split(buff_.get(), delim_);
|
||||
auto value = converter_.convert<T, Ts...>(split_input_);
|
||||
|
||||
if (!converter_.valid()) {
|
||||
set_error_invalid_conversion();
|
||||
}
|
||||
|
||||
read_line();
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// composite conversion
|
||||
////////////////
|
||||
template <typename... Ts>
|
||||
class composite {
|
||||
public:
|
||||
composite(std::tuple<Ts...>&& values, parser& parser)
|
||||
: values_{std::move(values)}, parser_{parser} {
|
||||
}
|
||||
|
||||
// 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
|
||||
template <typename... Us, typename Fun = None>
|
||||
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>>
|
||||
or_else(Fun&& fun = None{}) {
|
||||
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));
|
||||
}
|
||||
|
||||
// same as or_else, but saves the result into a 'U' object
|
||||
// instead of a tuple
|
||||
template <typename U, typename... Us, typename Fun = None>
|
||||
composite<Ts..., std::optional<U>> or_else_object(
|
||||
Fun&& fun = None{}) {
|
||||
std::optional<U> value;
|
||||
try_convert_and_invoke<U, Us...>(value, fun);
|
||||
return composite_with(std::move(value));
|
||||
}
|
||||
|
||||
std::tuple<Ts...> values() {
|
||||
return values_;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
composite<Ts..., T> composite_with(T&& new_value) {
|
||||
auto merged_values =
|
||||
std::tuple_cat(std::move(values_),
|
||||
std::tuple{
|
||||
std::forward<T>(new_value)});
|
||||
return {std::move(merged_values), parser_};
|
||||
}
|
||||
|
||||
template <typename U, typename... Us, typename Fun = None>
|
||||
void try_convert_and_invoke(std::optional<U>& value,
|
||||
Fun&& fun) {
|
||||
if (!parser_.valid()) {
|
||||
std::optional<U> new_value;
|
||||
auto tuple_output = try_same<Us...>();
|
||||
if constexpr (!std::is_same_v<
|
||||
U, decltype(tuple_output)>) {
|
||||
new_value = to_object<U>(
|
||||
std::move(tuple_output));
|
||||
} else {
|
||||
new_value = std::move(tuple_output);
|
||||
}
|
||||
if (parser_.valid()) {
|
||||
value = std::move(new_value);
|
||||
parser_.try_invoke(*value,
|
||||
std::forward<Fun>(
|
||||
fun));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
// tries to convert a line and returns a composite which is
|
||||
// able to try additional conversions in case of failure
|
||||
template <typename... Ts, typename Fun = None>
|
||||
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
|
||||
// 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
|
||||
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<Ts...>> value;
|
||||
auto new_value = get_next<Ts...>();
|
||||
if (valid()) {
|
||||
value = std::move(new_value);
|
||||
try_invoke(*value, std::forward<Fun>(fun));
|
||||
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));
|
||||
}
|
||||
|
||||
// same as or_else, but saves the result into a 'U' object
|
||||
// instead of a tuple
|
||||
template <typename U, typename... Us, typename Fun = None>
|
||||
composite<Ts..., std::optional<U>> or_else_object(Fun&& fun = None{}) {
|
||||
std::optional<U> value;
|
||||
try_convert_and_invoke<U, Us...>(value, fun);
|
||||
return composite_with(std::move(value));
|
||||
}
|
||||
|
||||
std::tuple<Ts...> values() {
|
||||
return values_;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
return {std::move(value), *this};
|
||||
};
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename...>
|
||||
friend class composite;
|
||||
|
||||
// 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
|
||||
template <typename Arg, typename Fun = None>
|
||||
void try_invoke(Arg&& arg, Fun&& fun) {
|
||||
constexpr bool is_none =
|
||||
std::is_same_v<std::decay_t<Fun>, None>;
|
||||
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();
|
||||
}
|
||||
} else {
|
||||
try_invoke_impl(arg, std::forward<Fun>(fun));
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
composite<Ts..., T> composite_with(T&& new_value) {
|
||||
auto merged_values =
|
||||
std::tuple_cat(std::move(values_),
|
||||
std::tuple{std::forward<T>(new_value)});
|
||||
return {std::move(merged_values), parser_};
|
||||
}
|
||||
|
||||
// tries to invoke the function if not None
|
||||
// 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
|
||||
template <typename Arg, typename Fun = None>
|
||||
auto try_invoke_impl(Arg&& arg, Fun&& fun) {
|
||||
constexpr bool is_none =
|
||||
std::is_same_v<std::decay_t<Fun>, None>;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////
|
||||
// line reading
|
||||
////////////////
|
||||
|
||||
class buffer {
|
||||
char* buffer_{nullptr};
|
||||
char* new_buffer_{nullptr};
|
||||
size_t size_{0};
|
||||
|
||||
public:
|
||||
~buffer() {
|
||||
free(buffer_);
|
||||
free(new_buffer_);
|
||||
}
|
||||
|
||||
bool read(FILE* file) {
|
||||
ssize_t size = getline(&new_buffer_, &size_, file);
|
||||
size_t string_end = size - 1;
|
||||
|
||||
if (size == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (size >= 2 && new_buffer_[size - 2] == '\r') {
|
||||
string_end--;
|
||||
}
|
||||
|
||||
new_buffer_[string_end] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* get() const {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
void update() {
|
||||
std::swap(buffer_, new_buffer_);
|
||||
}
|
||||
};
|
||||
|
||||
void read_line() {
|
||||
eof_ = !buff_.read(file_);
|
||||
++line_number_;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// error
|
||||
////////////////
|
||||
|
||||
void clear_error() {
|
||||
string_error_.clear();
|
||||
bool_error_ = false;
|
||||
}
|
||||
|
||||
void set_error_failed_check() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_)
|
||||
.append(" failed check.");
|
||||
template <typename U, typename... Us, typename Fun = None>
|
||||
void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
|
||||
if (!parser_.valid()) {
|
||||
std::optional<U> new_value;
|
||||
auto tuple_output = try_same<Us...>();
|
||||
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
|
||||
new_value = to_object<U>(std::move(tuple_output));
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
new_value = std::move(tuple_output);
|
||||
}
|
||||
if (parser_.valid()) {
|
||||
value = std::move(new_value);
|
||||
parser_.try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void set_error_file_not_open() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_)
|
||||
.append(" could not be not open.");
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
void set_error_eof_reached() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_)
|
||||
.append(" reached end of file.");
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
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
|
||||
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 = std::move(new_value);
|
||||
try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
return {std::move(value), *this};
|
||||
};
|
||||
|
||||
private:
|
||||
template <typename...>
|
||||
friend class composite;
|
||||
|
||||
// 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
|
||||
template <typename Arg, typename Fun = None>
|
||||
void try_invoke(Arg&& arg, Fun&& fun) {
|
||||
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
|
||||
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();
|
||||
}
|
||||
} else {
|
||||
try_invoke_impl(arg, std::forward<Fun>(fun));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// tries to invoke the function if not None
|
||||
// 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
|
||||
template <typename Arg, typename Fun = None>
|
||||
auto try_invoke_impl(Arg&& arg, Fun&& fun) {
|
||||
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////
|
||||
// line reading
|
||||
////////////////
|
||||
|
||||
class buffer {
|
||||
char* buffer_{nullptr};
|
||||
char* new_buffer_{nullptr};
|
||||
size_t size_{0};
|
||||
|
||||
public:
|
||||
~buffer() {
|
||||
free(buffer_);
|
||||
free(new_buffer_);
|
||||
}
|
||||
|
||||
void set_error_invalid_conversion() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_)
|
||||
.append(" ")
|
||||
.append(std::to_string(line_number_))
|
||||
.append(": ")
|
||||
.append(converter_.error_msg())
|
||||
.append(": \"")
|
||||
.append(buff_.get())
|
||||
.append("\"");
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
}
|
||||
bool read(FILE* file) {
|
||||
ssize_t size = getline(&new_buffer_, &size_, file);
|
||||
size_t string_end = size - 1;
|
||||
|
||||
if (size == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (size >= 2 && new_buffer_[size - 2] == '\r') {
|
||||
string_end--;
|
||||
}
|
||||
|
||||
new_buffer_[string_end] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// members
|
||||
////////////////
|
||||
const char* get() const {
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
const std::string file_name_;
|
||||
const std::string delim_;
|
||||
std::string string_error_;
|
||||
bool bool_error_;
|
||||
error_mode error_mode_{error_mode::String};
|
||||
converter converter_;
|
||||
converter::split_input split_input_;
|
||||
FILE* file_{nullptr};
|
||||
buffer buff_;
|
||||
size_t line_number_{0};
|
||||
bool eof_{false};
|
||||
void update() {
|
||||
std::swap(buffer_, new_buffer_);
|
||||
}
|
||||
};
|
||||
|
||||
void read_line() {
|
||||
eof_ = !buff_.read(file_);
|
||||
++line_number_;
|
||||
}
|
||||
|
||||
////////////////
|
||||
// error
|
||||
////////////////
|
||||
|
||||
void clear_error() {
|
||||
string_error_.clear();
|
||||
bool_error_ = false;
|
||||
}
|
||||
|
||||
void set_error_failed_check() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_).append(" failed check.");
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void set_error_file_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() {
|
||||
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() {
|
||||
if (error_mode_ == error_mode::String) {
|
||||
string_error_.append(file_name_)
|
||||
.append(" ")
|
||||
.append(std::to_string(line_number_))
|
||||
.append(": ")
|
||||
.append(converter_.error_msg())
|
||||
.append(": \"")
|
||||
.append(buff_.get())
|
||||
.append("\"");
|
||||
} else {
|
||||
bool_error_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////
|
||||
// members
|
||||
////////////////
|
||||
|
||||
const std::string file_name_;
|
||||
const std::string delim_;
|
||||
std::string string_error_;
|
||||
bool bool_error_;
|
||||
error_mode error_mode_{error_mode::String};
|
||||
converter converter_;
|
||||
converter::split_input split_input_;
|
||||
FILE* file_{nullptr};
|
||||
buffer buff_;
|
||||
size_t line_number_{0};
|
||||
bool eof_{false};
|
||||
};
|
||||
|
||||
} /* ss */
|
||||
|
||||
Reference in New Issue
Block a user