Codechange: use ranged for loop and separate function instead of goto

This commit is contained in:
Rubidium 2024-04-10 21:57:27 +02:00 committed by rubidium42
parent e8a56db21d
commit 5592b4409b
2 changed files with 36 additions and 34 deletions

View File

@ -21,8 +21,6 @@
*/
int GetOptData::GetOpt()
{
OptionSpan::iterator odata;
char *s = this->cont;
if (s == nullptr) {
if (this->numleft == 0) return -1; // No arguments left -> finished.
@ -34,10 +32,10 @@ int GetOptData::GetOpt()
this->numleft--;
/* Is it a long option? */
for (odata = std::begin(this->options); odata != std::end(this->options); odata++) {
if (odata->longname != nullptr && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
for (auto &option : this->options) {
if (option.longname != nullptr && !strcmp(option.longname, s)) { // Long options always use the entire argument.
this->cont = nullptr;
goto set_optval;
return this->GetOpt(option);
}
}
@ -45,39 +43,42 @@ int GetOptData::GetOpt()
}
/* Is it a short option? */
for (odata = std::begin(this->options); odata != std::end(this->options); odata++) {
if (odata->shortname != '\0' && *s == odata->shortname) {
for (auto &option : this->options) {
if (option.shortname != '\0' && *s == option.shortname) {
this->cont = (s[1] != '\0') ? s + 1 : nullptr;
set_optval: // Handle option value of *odata .
this->opt = nullptr;
switch (odata->type) {
case ODF_NO_VALUE:
return odata->id;
case ODF_HAS_VALUE:
case ODF_OPTIONAL_VALUE:
if (this->cont != nullptr) { // Remainder of the argument is the option value.
this->opt = this->cont;
this->cont = nullptr;
return odata->id;
}
/* No more arguments, either return an error or a value-less option. */
if (this->numleft == 0) return (odata->type == ODF_HAS_VALUE) ? -2 : odata->id;
/* Next argument looks like another option, let's not return it as option value. */
if (odata->type == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;
this->opt = this->argv[0]; // Next argument is the option value.
this->argv++;
this->numleft--;
return odata->id;
default: NOT_REACHED();
}
return this->GetOpt(option);
}
}
return -2; // No other ways to interpret the text -> error.
}
int GetOptData::GetOpt(const OptionData &option)
{
this->opt = nullptr;
switch (option.type) {
case ODF_NO_VALUE:
return option.id;
case ODF_HAS_VALUE:
case ODF_OPTIONAL_VALUE:
if (this->cont != nullptr) { // Remainder of the argument is the option value.
this->opt = this->cont;
this->cont = nullptr;
return option.id;
}
/* No more arguments, either return an error or a value-less option. */
if (this->numleft == 0) return (option.type == ODF_HAS_VALUE) ? -2 : option.id;
/* Next argument looks like another option, let's not return it as option value. */
if (option.type == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return option.id;
this->opt = this->argv[0]; // Next argument is the option value.
this->argv++;
this->numleft--;
return option.id;
default: NOT_REACHED();
}
}

View File

@ -50,6 +50,7 @@ struct GetOptData {
}
int GetOpt();
int GetOpt(const OptionData &option);
};
#endif /* GETOPTDATA_H */