| 138 | } |
| 139 | |
| 140 | int Option::parse_value( |
| 141 | const std::string& raw_val, |
| 142 | value_t *out, |
| 143 | std::string *error_message, |
| 144 | std::string *normalized_value) const |
| 145 | { |
| 146 | std::string val = raw_val; |
| 147 | |
| 148 | int r = pre_validate(&val, error_message); |
| 149 | if (r != 0) { |
| 150 | return r; |
| 151 | } |
| 152 | |
| 153 | if (type == Option::TYPE_INT) { |
| 154 | int64_t f = strict_si_cast<int64_t>(val, error_message); |
| 155 | if (!error_message->empty()) { |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | *out = f; |
| 159 | } else if (type == Option::TYPE_UINT) { |
| 160 | uint64_t f = strict_si_cast<uint64_t>(val, error_message); |
| 161 | if (!error_message->empty()) { |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | *out = f; |
| 165 | } else if (type == Option::TYPE_STR) { |
| 166 | *out = val; |
| 167 | } else if (type == Option::TYPE_FLOAT) { |
| 168 | double f = strict_strtod(val.c_str(), error_message); |
| 169 | if (!error_message->empty()) { |
| 170 | return -EINVAL; |
| 171 | } else { |
| 172 | *out = f; |
| 173 | } |
| 174 | } else if (type == Option::TYPE_BOOL) { |
| 175 | bool b = strict_strtob(val.c_str(), error_message); |
| 176 | if (!error_message->empty()) { |
| 177 | return -EINVAL; |
| 178 | } else { |
| 179 | *out = b; |
| 180 | } |
| 181 | } else if (type == Option::TYPE_ADDR) { |
| 182 | entity_addr_t addr; |
| 183 | if (!addr.parse(val)){ |
| 184 | return -EINVAL; |
| 185 | } |
| 186 | *out = addr; |
| 187 | } else if (type == Option::TYPE_ADDRVEC) { |
| 188 | entity_addrvec_t addr; |
| 189 | if (!addr.parse(val.c_str())){ |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | *out = addr; |
| 193 | } else if (type == Option::TYPE_UUID) { |
| 194 | uuid_d uuid; |
| 195 | if (!uuid.parse(val.c_str())) { |
| 196 | return -EINVAL; |
| 197 | } |
no test coverage detected