From c170d1e01b74aab579df81878f87ec92edd5b553 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Fri, 26 Mar 2021 15:54:43 +0100 Subject: [PATCH] Cleaner value checking for integer command line options --- src/editing_area/main.c | 4 ++-- src/main.c | 51 ++++++++++++++++++++++------------------- src/strtoint.c | 10 +++++--- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 6f7b046..98de2b8 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -27,8 +27,8 @@ int editing_area_main(struct Options options, Texture2D tileset; /* initialize raylib */ - InitWindow(options.editor_width, - options.editor_height, "SLE main window"); + InitWindow(options.editor_width, options.editor_height, + "SLE main window"); SetTargetFPS(options.editor_target_fps); init_mouse(options); /* load textures */ diff --git a/src/main.c b/src/main.c index 3de4dba..6836096 100644 --- a/src/main.c +++ b/src/main.c @@ -4,8 +4,8 @@ #include "editing_area/main.h" #include "options.h" #include "shared_data.h" -#include "tile_picker/main.h" #include "strtoint.h" +#include "tile_picker/main.h" #include #include #include @@ -20,6 +20,7 @@ static void *create_shared_memory(size_t size); static void overflow_prevention(void); /* work on optarg */ static void process_arguments(int argc, char **argv, struct Options *options); +static int int_arg_min(int opt, int min); static void set_tileset_dimensions(struct Options *options); /* The editor fork to create two windows. One will be the level editor @@ -120,45 +121,35 @@ static void process_arguments(int argc, char **argv, strcpy(options->tileset_path, optarg); break; case 'w': /* tile width */ - options->tile_width = strtoint(optarg); - if (options->tile_width <= 0) - exit(EXIT_FAILURE); + options->tile_width = int_arg_min(opt, 1); break; case 'h': /* tile height */ - options->tile_height = strtoint(optarg); - if (options->tile_height <= 0) - exit(EXIT_FAILURE); + options->tile_height = int_arg_min(opt, 1); break; case 'W': /* editor width */ - options->editor_width = strtoint(optarg); - if (options->editor_width <= 0) - exit(EXIT_FAILURE); + options->editor_width = int_arg_min(opt, 1); break; case 'H': /* editor height */ - options->editor_height = strtoint(optarg); - if (options->editor_height <= 0) - exit(EXIT_FAILURE); + options->editor_height = int_arg_min(opt, 1); break; case 'f': /* editor target FPS */ - options->editor_target_fps = strtoint(optarg); - if (options->editor_height <= 0) - exit(EXIT_FAILURE); + options->editor_target_fps = + int_arg_min(opt, 1); break; case 'o': /* editor draw offset x */ - options->editor_draw_offset_x = strtoint(optarg); + options->editor_draw_offset_x = + strtoint(optarg); break; case 'O': /* editor draw offset y */ - options->editor_draw_offset_y = strtoint(optarg); + options->editor_draw_offset_y = + strtoint(optarg); break; case 'F': /* picker target FPS */ - options->picker_target_fps = strtoint(optarg); - if (options->picker_target_fps <= 0) - exit(EXIT_FAILURE); + options->picker_target_fps = + int_arg_min(opt, 1); break; case 'p': /* picker padding */ - options->picker_padding = strtoint(optarg); - if (options->picker_padding < 0) - exit(EXIT_FAILURE); + options->picker_padding = int_arg_min(opt, 0); break; case '?': /* getopt_long already printed an error message @@ -192,6 +183,18 @@ static void process_arguments(int argc, char **argv, } } +static int int_arg_min(int opt, int min) +{ + const int value = strtoint(optarg); + if (value < min) { + fprintf(stderr, + "ERROR: minimum value for option -%c is %d, " + "got %d\n", + opt, min, value); + exit(EXIT_FAILURE); + } +} + static void set_tileset_dimensions(struct Options *options) { const Image tileset = LoadImage(options->tileset_path); diff --git a/src/strtoint.c b/src/strtoint.c index 8d234c6..953fab2 100644 --- a/src/strtoint.c +++ b/src/strtoint.c @@ -2,12 +2,13 @@ /* Copyright (C) 2021 KikooDX */ #include "strtoint.h" -#include #include #include +#include /* Attempt to convert a string to integer. */ -int strtoint(char *string) { +int strtoint(char *string) +{ char character; int i; const size_t string_len = strlen(string); @@ -17,7 +18,10 @@ int strtoint(char *string) { for (i = string_len - 1; i >= negative; i -= 1) { character = string[i]; if (character < '0' || character > '9') { - fprintf(stderr, "ERROR: flag expected a number argument, got \"%s\"\n", string); + fprintf(stderr, + "ERROR: flag expected a number " + "argument, got \"%s\"\n", + string); exit(EXIT_FAILURE); } sum += multiplier * (character - '0');