mirror of
https://github.com/akuker/RASCSI.git
synced 2025-01-23 12:31:10 +00:00
Updated string to integer conversions
This commit is contained in:
parent
bee8fa0d34
commit
ee98e68d1e
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user