Skip to content

Commit

Permalink
unify stod/stoi invalid_argument::what parse
Browse files Browse the repository at this point in the history
This is a hack to check whether given invalid_argument message belongs
to stoi/stod. It is usually used to catch non-numeric user input where
a number was expected.
  • Loading branch information
MartinPulec committed Sep 13, 2024
1 parent ab25c2d commit ef0cd71
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/hd-rum-translator/hd-rum-translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,7 @@ int main(int argc, char **argv)
try {
ret = parse_fmt(argc, argv, &params);
} catch (invalid_argument &e) {
if (strcmp(e.what(), "stoi") != 0 &&
strcmp(e.what(), "stod") != 0) {
if (!invalid_arg_is_numeric(e.what())) {
throw;
}
MSG(ERROR, "Non-numeric value passed to option "
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,7 @@ parse_options(int argc, char *argv[], struct ug_options *opt)
try {
return parse_options_internal(argc, argv, opt);
} catch (logic_error &e) {
if (strcmp(e.what(), "stoi") != 0 &&
strcmp(e.what(), "stod") != 0) {
if (!invalid_arg_is_numeric(e.what())) {
throw;
}
if (dynamic_cast<invalid_argument *>(&e) != nullptr) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,16 @@ bool is_arm_mac() {
return false;
#endif
}

/**
* @param what value returned by std::logic_error::what()
* @returns if given std::invalid_argument message belongs to a stoi/stof
* converison
*/

bool invalid_arg_is_numeric(const char *what) {
if (what == nullptr) {
return false;
}
return strcmp(what, "stoi") == 0 || strcmp(what, "stod") == 0;
}
2 changes: 2 additions & 0 deletions src/utils/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct key_val {
};
void print_module_usage(const char *module_name, const struct key_val *options, const struct key_val *options_full, bool print_full_help);

bool invalid_arg_is_numeric(const char *what);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 1 addition & 2 deletions src/video_display/decklink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1416,8 +1416,7 @@ static void *display_decklink_init(struct module *parent, const char *fmt, unsig
try {
succeeded = settings_init(s, fmt, cardId);
} catch (exception &e) {
if (strcmp(e.what(), "stoi") == 0 ||
strcmp(e.what(), "stod") == 0) {
if (invalid_arg_is_numeric(e.what())) {
MSG(ERROR, "Invalid number passed where numeric "
"argument expected!\n");
} else {
Expand Down

0 comments on commit ef0cd71

Please sign in to comment.