2018-02-19 15:42:18 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
const DataType *ConfigKey::data_type() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 12:51:51 +00:00
|
|
|
const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
|
2014-07-17 17:39:49 +00:00
|
|
|
if (!info)
|
|
|
|
throw Error(SR_ERR_NA);
|
|
|
|
return DataType::get(info->datatype);
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
string ConfigKey::identifier() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 12:51:51 +00:00
|
|
|
const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
|
2014-07-17 17:39:49 +00:00
|
|
|
if (!info)
|
|
|
|
throw Error(SR_ERR_NA);
|
|
|
|
return valid_string(info->id);
|
|
|
|
}
|
|
|
|
|
2014-09-11 20:25:52 +00:00
|
|
|
string ConfigKey::description() const
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-10-11 12:51:51 +00:00
|
|
|
const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
|
2014-07-17 17:39:49 +00:00
|
|
|
if (!info)
|
|
|
|
throw Error(SR_ERR_NA);
|
|
|
|
return valid_string(info->name);
|
|
|
|
}
|
|
|
|
|
2014-10-29 14:33:46 +00:00
|
|
|
const ConfigKey *ConfigKey::get_by_identifier(string identifier)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
2015-08-16 23:30:14 +00:00
|
|
|
const struct sr_key_info *info = sr_key_info_name_get(SR_KEY_CONFIG, identifier.c_str());
|
2014-07-17 17:39:49 +00:00
|
|
|
if (!info)
|
|
|
|
throw Error(SR_ERR_ARG);
|
|
|
|
return get(info->key);
|
|
|
|
}
|
|
|
|
|
2014-08-31 08:56:00 +00:00
|
|
|
#ifndef HAVE_STOI_STOD
|
|
|
|
|
|
|
|
/* Fallback implementation of stoi and stod */
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
static inline int stoi( const std::string& str )
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
errno = 0;
|
|
|
|
const long ret = std::strtol(str.c_str(), &endptr, 10);
|
|
|
|
if (endptr == str.c_str())
|
|
|
|
throw std::invalid_argument("stoi");
|
|
|
|
else if (errno == ERANGE ||
|
|
|
|
ret < std::numeric_limits<int>::min() ||
|
|
|
|
ret > std::numeric_limits<int>::max())
|
|
|
|
throw std::out_of_range("stoi");
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double stod( const std::string& str )
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
errno = 0;
|
|
|
|
const double ret = std::strtod(str.c_str(), &endptr);
|
|
|
|
if (endptr == str.c_str())
|
|
|
|
throw std::invalid_argument("stod");
|
|
|
|
else if (errno == ERANGE)
|
|
|
|
throw std::out_of_range("stod");
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-11 07:54:52 +00:00
|
|
|
Glib::VariantBase ConfigKey::parse_string(string value, enum sr_datatype dt)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
GVariant *variant;
|
|
|
|
uint64_t p, q;
|
|
|
|
|
2017-06-11 07:54:52 +00:00
|
|
|
switch (dt)
|
2014-07-17 17:39:49 +00:00
|
|
|
{
|
|
|
|
case SR_T_UINT64:
|
|
|
|
check(sr_parse_sizestring(value.c_str(), &p));
|
|
|
|
variant = g_variant_new_uint64(p);
|
|
|
|
break;
|
|
|
|
case SR_T_STRING:
|
|
|
|
variant = g_variant_new_string(value.c_str());
|
|
|
|
break;
|
|
|
|
case SR_T_BOOL:
|
|
|
|
variant = g_variant_new_boolean(sr_parse_boolstring(value.c_str()));
|
|
|
|
break;
|
|
|
|
case SR_T_FLOAT:
|
2015-10-25 23:30:49 +00:00
|
|
|
try {
|
|
|
|
variant = g_variant_new_double(stod(value));
|
2018-07-22 15:07:26 +00:00
|
|
|
} catch (invalid_argument&) {
|
2015-10-25 23:30:49 +00:00
|
|
|
throw Error(SR_ERR_ARG);
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
break;
|
|
|
|
case SR_T_RATIONAL_PERIOD:
|
|
|
|
check(sr_parse_period(value.c_str(), &p, &q));
|
|
|
|
variant = g_variant_new("(tt)", p, q);
|
|
|
|
break;
|
|
|
|
case SR_T_RATIONAL_VOLT:
|
|
|
|
check(sr_parse_voltage(value.c_str(), &p, &q));
|
|
|
|
variant = g_variant_new("(tt)", p, q);
|
|
|
|
break;
|
|
|
|
case SR_T_INT32:
|
2015-10-25 23:30:49 +00:00
|
|
|
try {
|
|
|
|
variant = g_variant_new_int32(stoi(value));
|
2018-07-22 15:07:26 +00:00
|
|
|
} catch (invalid_argument&) {
|
2015-10-25 23:30:49 +00:00
|
|
|
throw Error(SR_ERR_ARG);
|
|
|
|
}
|
2014-07-17 17:39:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Error(SR_ERR_BUG);
|
|
|
|
}
|
|
|
|
|
2014-07-24 17:26:19 +00:00
|
|
|
return Glib::VariantBase(variant, false);
|
2014-07-17 17:39:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 07:54:52 +00:00
|
|
|
Glib::VariantBase ConfigKey::parse_string(string value) const
|
|
|
|
{
|
|
|
|
enum sr_datatype dt = (enum sr_datatype)(data_type()->id());
|
|
|
|
return parse_string(value, dt);
|
|
|
|
}
|