From a6f4d5a97a457101535c935e1901bbe43ec5c055 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Fri, 26 Mar 2021 15:02:10 +0100 Subject: [PATCH] Add remaining arguments and accept signed integers. --- include/options.h | 11 +++++++---- include/strtoint.h | 2 +- src/main.c | 27 +++++++++++++++++++++++++++ src/strtoint.c | 15 ++++++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/include/options.h b/include/options.h index c45a92e..91c3ead 100644 --- a/include/options.h +++ b/include/options.h @@ -5,21 +5,24 @@ #define BUFFER_SIZE 256 struct Options { + /* required arguments */ char tileset_path[BUFFER_SIZE]; char level_path[BUFFER_SIZE]; + /* optionnal arguments, default values in conf.h */ int tile_width; int tile_height; - int tileset_width; - int tileset_height; int editor_width; int editor_height; int editor_target_fps; int editor_draw_offset_x; int editor_draw_offset_y; - int picker_window_width; - int picker_window_height; int picker_target_fps; int picker_padding; + /* determined after previous options */ + int tileset_width; + int tileset_height; + int picker_window_width; + int picker_window_height; }; struct Options options_defaults(void); diff --git a/include/strtoint.h b/include/strtoint.h index b5b9d13..2383d64 100644 --- a/include/strtoint.h +++ b/include/strtoint.h @@ -2,5 +2,5 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -/* Attempt to convert a string to positive integer. Return -1 on error. */ +/* Attempt to convert a string to integer. */ int strtoint(char *string); diff --git a/src/main.c b/src/main.c index efde1ac..3de4dba 100644 --- a/src/main.c +++ b/src/main.c @@ -94,6 +94,11 @@ static void process_arguments(int argc, char **argv, {"tile-height", required_argument, 0, 'h'}, {"editor-width", required_argument, 0, 'W'}, {"editor-height", required_argument, 0, 'H'}, + {"editor-fps", required_argument, 0, 'f'}, + {"editor-off-x", required_argument, 0, 'o'}, + {"editor-off-y", required_argument, 0, 'O'}, + {"picker-fps", required_argument, 0, 'F'}, + {"picker-padding", required_argument, 0, 'p'}, {0, 0, 0, 0}, }; /* getopt_long stores the option index here */ @@ -134,10 +139,32 @@ static void process_arguments(int argc, char **argv, if (options->editor_height <= 0) exit(EXIT_FAILURE); break; + case 'f': /* editor target FPS */ + options->editor_target_fps = strtoint(optarg); + if (options->editor_height <= 0) + exit(EXIT_FAILURE); + break; + case 'o': /* editor draw offset x */ + options->editor_draw_offset_x = strtoint(optarg); + break; + case 'O': /* editor draw offset y */ + 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); + break; + case 'p': /* picker padding */ + options->picker_padding = strtoint(optarg); + if (options->picker_padding < 0) + exit(EXIT_FAILURE); + break; case '?': /* getopt_long already printed an error message */ exit(EXIT_FAILURE); + break; default: fprintf(stderr, "ERROR: option -%c non handled " diff --git a/src/strtoint.c b/src/strtoint.c index 87eb414..8d234c6 100644 --- a/src/strtoint.c +++ b/src/strtoint.c @@ -4,22 +4,27 @@ #include "strtoint.h" #include #include +#include -/* Attempt to convert a string to positive integer. Return -1 on error. */ +/* Attempt to convert a string to integer. */ int strtoint(char *string) { char character; int i; const size_t string_len = strlen(string); int sum = 0; int multiplier = 1; - for (i = string_len - 1; i >= 0; i -= 1) { + int negative = string[0] == '-'; + for (i = string_len - 1; i >= negative; i -= 1) { character = string[i]; if (character < '0' || character > '9') { - fprintf(stderr, "ERROR: flag expected a positive number argument, got \"%s\"\n", string); - return -1; + fprintf(stderr, "ERROR: flag expected a number argument, got \"%s\"\n", string); + exit(EXIT_FAILURE); } sum += multiplier * (character - '0'); multiplier *= 10; } - return sum; + if (negative) + return sum * -1; + else + return sum; }