refactor handling of partition arguments so it does not modify the string passed

This commit is contained in:
Axel Kohlmeyer
2020-11-19 13:48:28 -05:00
parent 3d7fd453c3
commit 949274a2a4

View File

@ -158,7 +158,6 @@ void Universe::reorder(char *style, char *arg)
void Universe::add_world(char *str) void Universe::add_world(char *str)
{ {
int n,nper; int n,nper;
char *ptr;
n = 1; n = 1;
nper = 0; nper = 0;
@ -171,28 +170,23 @@ void Universe::add_world(char *str)
// str may not be empty and may only consist of digits or 'x' // str may not be empty and may only consist of digits or 'x'
size_t len = strlen(str); std::string part(str);
if (len < 1) valid = false; if (part.size() == 0) valid = false;
for (size_t i=0; i < len; ++i) if (part.find_first_not_of("0123456789x") != std::string::npos) valid = false;
if (isdigit(str[i]) || str[i] == 'x') continue;
else valid = false;
if (valid) { if (valid) {
if ((ptr = strchr(str,'x')) != nullptr) { std::size_t found = part.find_first_of("x");
// 'x' may not be the first or last character // 'x' may not be the first or last character
if (ptr == str) { if ((found == 0) || (found == (part.size() - 1))) {
valid = false;
} else if (strlen(str) == len-1) {
valid = false; valid = false;
} else if (found == std::string::npos) {
nper = atoi(part.c_str());
} else { } else {
*ptr = '\0'; n = atoi(part.substr(0,found).c_str());
n = atoi(str); nper = atoi(part.substr(found-1).c_str());\
nper = atoi(ptr+1);
*ptr = 'x';
} }
} else nper = atoi(str);
} }
// require minimum of 1 partition with 1 processor // require minimum of 1 partition with 1 processor