Add remaining arguments and accept signed integers.

This commit is contained in:
KikooDX 2021-03-26 15:02:10 +01:00
parent baff9fe66d
commit a6f4d5a97a
4 changed files with 45 additions and 10 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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 "

View File

@ -4,22 +4,27 @@
#include "strtoint.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/* 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;
}