mirror of https://git.sr.ht/~kikoodx/sle
Cleaner value checking for integer command line options
This commit is contained in:
parent
a6f4d5a97a
commit
c170d1e01b
|
@ -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 */
|
||||
|
|
51
src/main.c
51
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 <getopt.h>
|
||||
#include <raylib.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -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);
|
||||
|
|
|
@ -2,12 +2,13 @@
|
|||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#include "strtoint.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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');
|
||||
|
|
Loading…
Reference in New Issue