Let's allow custom validators to not duplicate the code.
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
*out = f;
}
} else if (type == Option::TYPE_BOOL) {
- if (strcasecmp(val.c_str(), "false") == 0) {
- *out = false;
- } else if (strcasecmp(val.c_str(), "true") == 0) {
- *out = true;
+ bool b = strict_strtob(val.c_str(), error_message);
+ if (!error_message->empty()) {
+ return -EINVAL;
} else {
- int b = strict_strtol(val.c_str(), 10, error_message);
- if (!error_message->empty()) {
- return -EINVAL;
- }
- *out = (bool)!!b;
+ *out = b;
}
} else if (type == Option::TYPE_ADDR) {
entity_addr_t addr;
#include <limits>
#include <cmath>
#include <sstream>
+#include <strings.h>
#include <string_view>
using std::ostringstream;
+bool strict_strtob(const char* str, std::string *err)
+{
+ if (strcasecmp(str, "false") == 0) {
+ return false;
+ } else if (strcasecmp(str, "true") == 0) {
+ return true;
+ } else {
+ int b = strict_strtol(str, 10, err);
+ return (bool)!!b;
+ }
+}
+
long long strict_strtoll(std::string_view str, int base, std::string *err)
{
char *endptr;
#include <stdint.h>
}
+bool strict_strtob(const char* str, std::string *err);
+
long long strict_strtoll(const char *str, int base, std::string *err);
int strict_strtol(const char *str, int base, std::string *err);