Updated string to integer conversions

This commit is contained in:
Uwe Seimet 2021-09-01 10:27:30 +02:00
parent bee8fa0d34
commit ee98e68d1e

View File

@ -253,11 +253,20 @@ void Reset()
bus->Reset();
}
//---------------------------------------------------------------------------
//
// Get the list of attached devices
//
//---------------------------------------------------------------------------
bool GetAsInt(const string& value, int& result)
{
try {
result = std::stoul(value);
}
catch(const invalid_argument& e) {
return false;
}
catch(const out_of_range& e) {
return false;
}
return true;
}
void GetImageFile(PbImageFile *image_file, const string& filename)
{
@ -1013,15 +1022,12 @@ bool ProcessCmd(const int fd, const PbCommand& command)
else if (command.operation() == RESERVE) {
set<int> reserved;
for (int i = 0; i < command.params_size(); i++) {
try {
reserved.insert(std::stoi(command.params(i)));
}
catch(const invalid_argument& e) {
return ReturnStatus(fd, false, "Invalid ID " + command.params(i));
}
catch(const out_of_range& e) {
int id;
if (!GetAsInt(command.params(i), id)) {
return ReturnStatus(fd, false, "Invalid ID " + command.params(i));
}
reserved.insert(id);
}
reserved_ids = reserved;
@ -1098,7 +1104,9 @@ bool ParseArgument(int argc, char* argv[], int& port)
continue;
case 'b': {
block_size = atoi(optarg);
if (!GetAsInt(optarg, block_size)) {
cerr << "Invalid block size " << optarg << endl;
}
continue;
}
@ -1124,8 +1132,7 @@ bool ParseArgument(int argc, char* argv[], int& port)
continue;
case 'p':
port = atoi(optarg);
if (port <= 0 || port > 65535) {
if (!GetAsInt(optarg, port) || port <= 0 || port > 65535) {
cerr << "Invalid port " << optarg << ", port must be between 1 and 65535" << endl;
return false;
}