From 7199311132404ce721b7c76fdb8f08dff507d42a Mon Sep 17 00:00:00 2001 From: cancel Date: Mon, 6 Jan 2020 03:46:49 +0900 Subject: [PATCH] Clean up some annoying uses of atoi Just use crummy old sscanf for now. --- tui_main.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tui_main.c b/tui_main.c index ae4068b..ff1f80f 100644 --- a/tui_main.c +++ b/tui_main.c @@ -2218,6 +2218,15 @@ void push_portmidi_output_device_menu(Midi_mode const* midi_mode) { // Misc utils // +bool read_int(char const* str, int* out) { + int a; + int res = sscanf(str, "%d", &a); + if (res != 1) + return false; + *out = a; + return true; +} + // Reads something like '5x3' or '5'. Writes the same value to both outputs if // only one is specified. Returns false on error. bool read_nxn_or_n(char const* str, int* out_a, int* out_b) { @@ -2422,9 +2431,7 @@ int main(int argc, char** argv) { } } break; case Argopt_undo_limit: { - undo_history_limit = atoi(optarg); - if (undo_history_limit < 0 || - (undo_history_limit == 0 && strcmp(optarg, "0"))) { + if (!read_int(optarg, &undo_history_limit) || undo_history_limit < 0) { fprintf(stderr, "Bad undo-limit argument %s.\n" "Must be 0 or positive integer.\n", @@ -2443,8 +2450,7 @@ int main(int argc, char** argv) { } } break; case Argopt_seed: { - init_seed = atoi(optarg); - if (init_seed < 0 || (init_seed == 0 && strcmp(optarg, "0"))) { + if (!read_int(optarg, &init_seed) || init_seed < 0) { fprintf(stderr, "Bad seed argument %s.\n" "Must be 0 or positive integer.\n",