1 const DataType *ConfigKey::data_type() const
3 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
5 throw Error(SR_ERR_NA);
6 return DataType::get(info->datatype);
9 string ConfigKey::identifier() const
11 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
13 throw Error(SR_ERR_NA);
14 return valid_string(info->id);
17 string ConfigKey::description() const
19 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, id());
21 throw Error(SR_ERR_NA);
22 return valid_string(info->name);
25 const ConfigKey *ConfigKey::get_by_identifier(string identifier)
27 const struct sr_key_info *info = sr_key_info_name_get(SR_KEY_CONFIG, identifier.c_str());
29 throw Error(SR_ERR_ARG);
30 return get(info->key);
35 #ifndef HAVE_STOI_STOD
37 /* Fallback implementation of stoi and stod */
44 static inline int stoi( const std::string& str )
48 const long ret = std::strtol(str.c_str(), &endptr, 10);
49 if (endptr == str.c_str())
50 throw std::invalid_argument("stoi");
51 else if (errno == ERANGE ||
52 ret < std::numeric_limits<int>::min() ||
53 ret > std::numeric_limits<int>::max())
54 throw std::out_of_range("stoi");
59 static inline double stod( const std::string& str )
63 const double ret = std::strtod(str.c_str(), &endptr);
64 if (endptr == str.c_str())
65 throw std::invalid_argument("stod");
66 else if (errno == ERANGE)
67 throw std::out_of_range("stod");
73 Glib::VariantBase ConfigKey::parse_string(string value) const
78 switch (data_type()->id())
81 check(sr_parse_sizestring(value.c_str(), &p));
82 variant = g_variant_new_uint64(p);
85 variant = g_variant_new_string(value.c_str());
88 variant = g_variant_new_boolean(sr_parse_boolstring(value.c_str()));
92 variant = g_variant_new_double(stod(value));
93 } catch (invalid_argument) {
94 throw Error(SR_ERR_ARG);
97 case SR_T_RATIONAL_PERIOD:
98 check(sr_parse_period(value.c_str(), &p, &q));
99 variant = g_variant_new("(tt)", p, q);
101 case SR_T_RATIONAL_VOLT:
102 check(sr_parse_voltage(value.c_str(), &p, &q));
103 variant = g_variant_new("(tt)", p, q);
107 variant = g_variant_new_int32(stoi(value));
108 } catch (invalid_argument) {
109 throw Error(SR_ERR_ARG);
113 throw Error(SR_ERR_BUG);
116 return Glib::VariantBase(variant, false);