Split off command-line options parsing from the main function
This commit is contained in:
parent
7cd9faefe0
commit
4dd410a2af
5 changed files with 196 additions and 130 deletions
142
connect4.c
142
connect4.c
|
|
@ -20,33 +20,13 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#ifndef NO_OPTIONS
|
||||
#include <getopt.h>
|
||||
#endif /* ! NO_OPTIONS */
|
||||
#include <string.h>
|
||||
#include "macros.h"
|
||||
#include "config.h"
|
||||
#include "logic.h"
|
||||
#include "types.h"
|
||||
#include "ui.h"
|
||||
|
||||
#define VERSION 0.3.1
|
||||
|
||||
#ifndef GITHASH
|
||||
#define FULLVERSION VERSION
|
||||
#else
|
||||
#define FULLVERSION VERSION~git.GITHASH
|
||||
#endif
|
||||
|
||||
#define VERSIONSTRING \
|
||||
"AnnaConnect version "XSTR(FULLVERSION)", Copyright (C) Anna Snoeijs\n"
|
||||
|
||||
#ifndef NO_OPTIONS
|
||||
// The LISENCE file
|
||||
// Not null-terminated so need pointers for both start and end
|
||||
extern char _binary_LICENSE_start[];
|
||||
extern char _binary_LICENSE_end[];
|
||||
#endif /* ! NO_OPTIONS */
|
||||
#include "commandline.h"
|
||||
|
||||
int main(
|
||||
#ifndef NO_OPTIONS // [[maybe_unused]] is unsupported in C99, so this will do
|
||||
|
|
@ -66,113 +46,21 @@ int main(
|
|||
FILE *outputfile = NULL;
|
||||
char *filename = NULL;
|
||||
bool randomMoves = 0;
|
||||
// Parse options
|
||||
for(;;){
|
||||
// Allowed options
|
||||
static struct option long_options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "license", no_argument, NULL, 'l' },
|
||||
{ "version", no_argument, NULL, '\0' },
|
||||
{ "columns", required_argument, NULL, 'c' },
|
||||
{ "rows", required_argument, NULL, 'r' },
|
||||
{ "output", required_argument, NULL, 'o' },
|
||||
{ "slow-calcWins", no_argument, NULL, '\0' },
|
||||
{ "random-moves", no_argument, NULL, '\0' },
|
||||
{ 0 }
|
||||
};
|
||||
// Index of current option
|
||||
int option_index = 0;
|
||||
// Get next option
|
||||
char c = (char)getopt_long( argc, argv, "hlc:r:o:",
|
||||
long_options, &option_index
|
||||
);
|
||||
if( c == -1 ) break;
|
||||
// Handle option
|
||||
switch( c ){
|
||||
case '\0': // Any option without shorthand
|
||||
switch( option_index ){
|
||||
case 2: // --version
|
||||
printf( VERSIONSTRING );
|
||||
return 0;
|
||||
case 6: // --slow-calcWins
|
||||
free( wins );
|
||||
wins = NULL;
|
||||
break;
|
||||
case 7: // --random-moves
|
||||
randomMoves = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(
|
||||
stderr,
|
||||
"%s: unhandled option \'--%s\' \n",
|
||||
argv[0],
|
||||
long_options[ option_index ].name
|
||||
);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'h': // --help
|
||||
printf( "Usage: %s [OPTIONS]\n\n", argv[0] );
|
||||
printf(
|
||||
" -h --help Print this help text\n"
|
||||
" -l --license Print the LICENSE file\n"
|
||||
" --version Print the version string\n"
|
||||
"\n"
|
||||
" -c n --columns n Set the amount of columns\n"
|
||||
" -r n --rows n Set the amount of rows\n"
|
||||
"\n"
|
||||
" -o file --output file Ouput moves taken in game to file\n"
|
||||
"\n"
|
||||
" --slow-calcwins Use the reference implementation of calcWins()\n"
|
||||
" --random-moves Play random moves instead of asking for input\n"
|
||||
);
|
||||
return 0;
|
||||
case 'l': // --license
|
||||
for(
|
||||
char *p = _binary_LICENSE_start;
|
||||
p != _binary_LICENSE_end;
|
||||
p++
|
||||
){
|
||||
putchar( *p );
|
||||
}
|
||||
return 0;
|
||||
case 'c': // --columns
|
||||
playboard.columns = (columnsint_t)atoi(optarg);
|
||||
break;
|
||||
case 'r': // --rows
|
||||
playboard.rows = (rowsint_t)atoi(optarg);
|
||||
break;
|
||||
case 'o':
|
||||
// filename = strdup(optarg);
|
||||
filename = calloc( strlen( optarg ) + 1, sizeof(char) );
|
||||
if( filename != NULL ){
|
||||
strcpy( filename, optarg );
|
||||
outputfile = fopen( filename, "w" );
|
||||
}else{
|
||||
fprintf( stderr, "ERR: COULD NOT ALLOCATE FILENAME\n" );
|
||||
}
|
||||
break;
|
||||
}
|
||||
switch(
|
||||
parseArgs(
|
||||
argc,
|
||||
argv,
|
||||
&playboard,
|
||||
&wins,
|
||||
&outputfile,
|
||||
&filename,
|
||||
&randomMoves
|
||||
)
|
||||
){
|
||||
case 0: break;
|
||||
case 1: return 0;
|
||||
default: return -1;
|
||||
}
|
||||
// Check for unhandled options
|
||||
if( optind < argc ) {
|
||||
fprintf( stderr, "non-option ARGV-elements: " );
|
||||
while( optind < argc )
|
||||
printf( "%s ", argv[optind++] );
|
||||
putchar( '\n' );
|
||||
}
|
||||
if( playboard.rows < 1 ){
|
||||
fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE AT LEAST 1\n" );
|
||||
return -1;
|
||||
}
|
||||
#define ROWOVERFLOW (rowsint_t)( sizeof(column_t) * CHAR_BIT - 1 )
|
||||
if( playboard.rows >= ROWOVERFLOW ){
|
||||
fprintf( stderr,
|
||||
"ERR: AMOUT OF ROWS MUST BE LESS THAN %d\n", ROWOVERFLOW
|
||||
);
|
||||
return -1;
|
||||
}
|
||||
#undef ROWOVERFLOW
|
||||
#endif /* ! NO_OPTIONS */
|
||||
// Start the actual program
|
||||
printf(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue