98 lines
2.6 KiB
C++
98 lines
2.6 KiB
C++
|
|
#include "fast_double_parser.h"
|
|
|
|
#include "settings.h"
|
|
#include "exception.h"
|
|
#include "ml_date.h"
|
|
|
|
namespace usql {
|
|
|
|
std::vector<std::pair<std::string, std::string>> Settings::m_settings =
|
|
{ std::make_pair("DATE_FORMAT", "%Y-%m-%d %H:%M:%S"),
|
|
std::make_pair("BOOL_TRUE_LITERAL", "Y"),
|
|
std::make_pair("BOOL_FALSE_LITERAL", "N"),
|
|
std::make_pair("DOUBLE_FORMAT", "%.2f"),
|
|
std::make_pair("USE_INDEXSCAN", "Y"),
|
|
std::make_pair("MAX_PARALLELISM", "1") }; // values "AUTO" or number of workers; when number negative means std::thread::hardware_concurrency() - number
|
|
|
|
|
|
|
|
long Settings::string_to_long(const std::string &intstr) {
|
|
try {
|
|
return std::stol(intstr);
|
|
} catch (const std::invalid_argument &e) {
|
|
throw Exception("error parsing as integer: " + intstr);
|
|
}
|
|
}
|
|
|
|
std::string Settings::long_to_string(long intval) {
|
|
return std::to_string(intval);
|
|
}
|
|
|
|
|
|
double Settings::string_to_double(const std::string &doublestr) {
|
|
double result;
|
|
const char * endptr = fast_double_parser::parse_number(doublestr.c_str(), &result);
|
|
if (endptr == nullptr) {
|
|
throw Exception("error parsing as double: " + doublestr);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string Settings::double_to_string(double d) {
|
|
char buffer[32];
|
|
int r, buf_size = 32;
|
|
|
|
r = snprintf(buffer, buf_size, get_setting("DOUBLE_FORMAT").c_str(), d);
|
|
if (r > 0 && r < buf_size) return std::string(buffer);
|
|
|
|
// ?? return std::to_string(d);
|
|
return "ERROR";
|
|
}
|
|
|
|
|
|
long Settings::string_to_date(const std::string &datestr) {
|
|
return ::string_to_date(datestr, get_setting("DATE_FORMAT"));
|
|
}
|
|
|
|
std::string Settings::date_to_string(long date) {
|
|
return ::date_to_string(date, get_setting("DATE_FORMAT"));
|
|
}
|
|
|
|
|
|
bool Settings::string_to_bool(const std::string &value) {
|
|
if (value == "true" || value == get_setting("BOOL_TRUE_LITERAL"))
|
|
return true;
|
|
|
|
if (value == "false" || value == get_setting("BOOL_FALSE_LITERAL"))
|
|
return false;
|
|
|
|
throw Exception("string_to_bool, unrecognized value: " + value);
|
|
}
|
|
|
|
std::string Settings::bool_to_string(bool value) {
|
|
return value ? "true" : "false";
|
|
}
|
|
|
|
std::string Settings::get_setting(const std::string &name) {
|
|
for(const auto& pair : m_settings) {
|
|
if (pair.first == name) return pair.second;
|
|
}
|
|
throw Exception("unsupported setting name: " + name);
|
|
}
|
|
|
|
bool Settings::get_bool_setting(const std::string &name) {
|
|
return string_to_bool(get_setting(name));
|
|
}
|
|
|
|
void Settings::set_setting(const std::string &name, const std::string &value) {
|
|
for (auto it = begin(m_settings); it != end(m_settings); ++it) {
|
|
if (it->first == name) {
|
|
*it = std::make_pair(name, value);
|
|
return;
|
|
}
|
|
}
|
|
throw Exception("unsupported setting name: " + name);
|
|
}
|
|
|
|
} // namespace
|