From ef0cd7130df5ed088f05edf4c3bfdbbfd752bc7d Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Fri, 13 Sep 2024 12:38:23 +0200 Subject: [PATCH] unify stod/stoi invalid_argument::what parse 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. --- src/hd-rum-translator/hd-rum-translator.cpp | 3 +-- src/main.cpp | 3 +-- src/utils/misc.cpp | 13 +++++++++++++ src/utils/misc.h | 2 ++ src/video_display/decklink.cpp | 3 +-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/hd-rum-translator/hd-rum-translator.cpp b/src/hd-rum-translator/hd-rum-translator.cpp index 47c8069f32..ed44f0425f 100644 --- a/src/hd-rum-translator/hd-rum-translator.cpp +++ b/src/hd-rum-translator/hd-rum-translator.cpp @@ -1026,8 +1026,7 @@ int main(int argc, char **argv) try { ret = parse_fmt(argc, argv, ¶ms); } 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 " diff --git a/src/main.cpp b/src/main.cpp index aca7c3d43c..3def1f9cb5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(&e) != nullptr) { diff --git a/src/utils/misc.cpp b/src/utils/misc.cpp index 53854e7476..1d5c8b7d11 100644 --- a/src/utils/misc.cpp +++ b/src/utils/misc.cpp @@ -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; +} diff --git a/src/utils/misc.h b/src/utils/misc.h index 90acd938f7..06f5c7ee4d 100644 --- a/src/utils/misc.h +++ b/src/utils/misc.h @@ -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 diff --git a/src/video_display/decklink.cpp b/src/video_display/decklink.cpp index 9d30478e91..1d038f9873 100644 --- a/src/video_display/decklink.cpp +++ b/src/video_display/decklink.cpp @@ -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 {