Browse Source

Util: Add simple validation to ArgParser number conversions

master
Riyyi 3 years ago
parent
commit
ca95ef1cf1
  1. 52
      src/util/argparser.cpp

52
src/util/argparser.cpp

@ -399,33 +399,41 @@ AcceptFunction ArgParser::getAcceptFunction(bool& value)
AcceptFunction ArgParser::getAcceptFunction(const char*& value) AcceptFunction ArgParser::getAcceptFunction(const char*& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
value = v; value = input;
return true; return true;
}; };
} }
AcceptFunction ArgParser::getAcceptFunction(std::string& value) AcceptFunction ArgParser::getAcceptFunction(std::string& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
value = v; value = input;
return true; return true;
}; };
} }
AcceptFunction ArgParser::getAcceptFunction(std::string_view& value) AcceptFunction ArgParser::getAcceptFunction(std::string_view& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
value = v; value = input;
return true; return true;
}; };
} }
AcceptFunction ArgParser::getAcceptFunction(int& value) AcceptFunction ArgParser::getAcceptFunction(int& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
const char* validate = input;
for (; *validate != '\0'; ++validate) {
// - [0-9]
if (*validate != 45 && (*validate < 48 || *validate > 57)) {
return false;
}
}
try { try {
value = std::stoi(v); value = std::stoi(input);
return true; return true;
} }
catch (...) { catch (...) {
@ -436,10 +444,18 @@ AcceptFunction ArgParser::getAcceptFunction(int& value)
AcceptFunction ArgParser::getAcceptFunction(unsigned int& value) AcceptFunction ArgParser::getAcceptFunction(unsigned int& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
const char* validate = input;
for (; *validate != '\0'; ++validate) {
// [0-9]
if (*validate < 48 || *validate > 57) {
return false;
}
}
unsigned long convert = 0; unsigned long convert = 0;
try { try {
convert = std::stoul(v); convert = std::stoul(input);
} }
catch (...) { catch (...) {
return false; return false;
@ -456,9 +472,17 @@ AcceptFunction ArgParser::getAcceptFunction(unsigned int& value)
AcceptFunction ArgParser::getAcceptFunction(double& value) AcceptFunction ArgParser::getAcceptFunction(double& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
const char* validate = input;
for (; *validate != '\0'; ++validate) {
// . [0-9]
if (*validate != 46 && (*validate < 48 || *validate > 57)) {
return false;
}
}
try { try {
value = std::stod(v); value = std::stod(input);
return true; return true;
} }
catch (...) { catch (...) {
@ -469,8 +493,8 @@ AcceptFunction ArgParser::getAcceptFunction(double& value)
AcceptFunction ArgParser::getAcceptFunction(std::vector<std::string>& value) AcceptFunction ArgParser::getAcceptFunction(std::vector<std::string>& value)
{ {
return [&value](const char* v) -> bool { return [&value](const char* input) -> bool {
value.push_back(v); value.push_back(input);
return true; return true;
}; };
} }

Loading…
Cancel
Save