Better command-line handling (issue 266) contributed by Pasi Kallinen

git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1227 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Yves Rizoud 2009-11-29 18:25:14 +00:00
parent 224902e18e
commit 0a7bd889d1
2 changed files with 224 additions and 160 deletions

View File

@ -343,6 +343,9 @@ static const T_Help_table helptable_credits[] =
HELP_TEXT (" Petter Lindquist") HELP_TEXT (" Petter Lindquist")
HELP_TEXT (" C64 file and image formats") HELP_TEXT (" C64 file and image formats")
HELP_TEXT ("") HELP_TEXT ("")
HELP_TEXT (" Pasi Kallinen")
HELP_TEXT (" Better command-line handling")
HELP_TEXT ("")
HELP_TEXT ("") HELP_TEXT ("")
HELP_TITLE(" ART") HELP_TITLE(" ART")
HELP_TEXT ("") HELP_TEXT ("")
@ -414,10 +417,10 @@ static const T_Help_table helptable_credits[] =
HELP_TEXT (" HoraK-FDF iLKke Iw2evk ") HELP_TEXT (" HoraK-FDF iLKke Iw2evk ")
HELP_TEXT (" Jamon keito kusma ") HELP_TEXT (" Jamon keito kusma ")
HELP_TEXT (" Lord Graga MagerValp mind ") HELP_TEXT (" Lord Graga MagerValp mind ")
HELP_TEXT (" MooZ the Peach petter ") HELP_TEXT (" MooZ Pasi Kallinen the Peach ")
HELP_TEXT (" richienyhus tape.wyrm TeeEmCee ") HELP_TEXT (" petter richienyhus tape.wyrm ")
HELP_TEXT (" tempest Timo Kurrpa titus^Rab ") HELP_TEXT (" TeeEmCee tempest Timo Kurrpa ")
HELP_TEXT (" Tobé 00ai99") HELP_TEXT (" titus^Rab Tobé 00ai99")
HELP_TEXT ("") HELP_TEXT ("")
HELP_TEXT ("") HELP_TEXT ("")
HELP_TEXT (" ... posted the annoying bug reports.") HELP_TEXT (" ... posted the annoying bug reports.")

165
main.c
View File

@ -2,6 +2,7 @@
*/ */
/* Grafx2 - The Ultimate 256-color bitmap paint program /* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2009 Pasi Kallinen
Copyright 2008 Peter Gordon Copyright 2008 Peter Gordon
Copyright 2008 Franck Charlet Copyright 2008 Franck Charlet
Copyright 2007 Adrien Destugues Copyright 2007 Adrien Destugues
@ -81,19 +82,21 @@
void Display_syntax(void) void Display_syntax(void)
{ {
int mode_index; int mode_index;
printf("Syntax: grafx2 [<arguments>] [<picture>]\n\n"); printf("Syntax: grafx2 [<arguments>] [<picture1>] [<picture2>]\n\n");
printf("<arguments> can be:]\n"); printf("<arguments> can be:]\n");
printf("\t/? /h /help for this help screen\n"); printf("\t-? -h -H -help for this help screen\n");
printf("\t/wide to emulate a video mode with wide pixels (2x1)\n"); printf("\t-wide to emulate a video mode with wide pixels (2x1)\n");
printf("\t/tall to emulate a video mode with tall pixels (1x2)\n"); printf("\t-tall to emulate a video mode with tall pixels (1x2)\n");
printf("\t/double to emulate a video mode with double pixels (2x2)\n"); printf("\t-double to emulate a video mode with double pixels (2x2)\n");
printf("\t/wide2 to emulate a video mode with double wide pixels (4x2)\n"); printf("\t-wide2 to emulate a video mode with double wide pixels (4x2)\n");
printf("\t/tall2 to emulate a video mode with double tall pixels (2x4)\n"); printf("\t-tall2 to emulate a video mode with double tall pixels (2x4)\n");
printf("\t/triple to emulate a video mode with triple pixels (3x3)\n"); printf("\t-triple to emulate a video mode with triple pixels (3x3)\n");
printf("\t/quadruple to emulate a video mode with quadruple pixels (4x4)\n"); printf("\t-quadruple to emulate a video mode with quadruple pixels (4x4)\n");
printf("\t/rgb n to reduce RGB precision from 256 to n levels\n"); printf("\t-rgb n to reduce RGB precision from 256 to n levels\n");
printf("\t/skin <filename> to use an alternate file with the menu graphics\n"); printf("\t-skin <filename> to use an alternate file with the menu graphics\n");
printf("\t/mode <videomode> to set a video mode\n\n"); printf("\t-mode <videomode> to set a video mode\n");
printf("Arguments can be prefixed either by / - or --\n");
printf("They can also be abbreviated.\n\n");
printf("Available video modes:\n\n"); printf("Available video modes:\n\n");
for (mode_index = 0; mode_index < Nb_video_modes; mode_index += 12) for (mode_index = 0; mode_index < Nb_video_modes; mode_index += 12)
{ {
@ -171,13 +174,49 @@ void Error_function(int error_code, const char *filename, int line_number, const
} }
} }
enum CMD_PARAMS
{
CMDPARAM_HELP,
CMDPARAM_MODE,
CMDPARAM_PIXELRATIO_TALL,
CMDPARAM_PIXELRATIO_WIDE,
CMDPARAM_PIXELRATIO_DOUBLE,
CMDPARAM_PIXELRATIO_TRIPLE,
CMDPARAM_PIXELRATIO_QUAD,
CMDPARAM_PIXELRATIO_TALL2,
CMDPARAM_PIXELRATIO_WIDE2,
CMDPARAM_RGB,
CMDPARAM_SKIN
};
struct {
const char *param;
int id;
} cmdparams[] = {
{"?", CMDPARAM_HELP},
{"h", CMDPARAM_HELP},
{"H", CMDPARAM_HELP},
{"help", CMDPARAM_HELP},
{"mode", CMDPARAM_MODE},
{"tall", CMDPARAM_PIXELRATIO_TALL},
{"wide", CMDPARAM_PIXELRATIO_WIDE},
{"double", CMDPARAM_PIXELRATIO_DOUBLE},
{"triple", CMDPARAM_PIXELRATIO_TRIPLE},
{"quadruple", CMDPARAM_PIXELRATIO_QUAD},
{"tall2", CMDPARAM_PIXELRATIO_TALL2},
{"wide2", CMDPARAM_PIXELRATIO_WIDE2},
{"rgb", CMDPARAM_RGB},
{"skin", CMDPARAM_SKIN}
};
#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof(x[0]))
// --------------------- Analyse de la ligne de commande --------------------- // --------------------- Analyse de la ligne de commande ---------------------
void Analyze_command_line(int argc, char * argv[]) void Analyze_command_line(int argc, char * argv[])
{ {
char *buffer ; char *buffer ;
int index; int index;
File_in_command_line = 0; File_in_command_line = 0;
Resolution_in_command_line = 0; Resolution_in_command_line = 0;
@ -185,17 +224,46 @@ void Analyze_command_line(int argc, char * argv[])
for (index = 1; index<argc; index++) for (index = 1; index<argc; index++)
{ {
if ( !strcmp(argv[index],"/?") || char *s = argv[index];
!strcmp(argv[index],"/h") || int is_switch = ((strchr(s,'/') == s) || (strchr(s,'-') == s) || (strstr(s,"--") == s));
!strcmp(argv[index],"/H") ) int tmpi;
int paramtype = -1;
if (is_switch)
{ {
// help int param_matches = 0;
int param_match = -1;
if (*s == '-')
{
s++;
if (*s == '-')
s++;
}
else
s++;
for (tmpi = 0; tmpi < ARRAY_SIZE(cmdparams); tmpi++)
{
if (!strcmp(s, cmdparams[tmpi].param))
{
paramtype = cmdparams[tmpi].id;
break;
}
else if (strstr(cmdparams[tmpi].param, s))
{
param_matches++;
param_match = cmdparams[tmpi].id;
}
}
if (paramtype == -1 && param_matches == 1)
paramtype = param_match;
}
switch (paramtype)
{
case CMDPARAM_HELP:
Display_syntax(); Display_syntax();
exit(0); exit(0);
} case CMDPARAM_MODE:
else if ( !strcmp(argv[index],"/mode") )
{
// mode
index++; index++;
if (index<argc) if (index<argc)
{ {
@ -219,37 +287,29 @@ void Analyze_command_line(int argc, char * argv[])
Display_syntax(); Display_syntax();
exit(0); exit(0);
} }
} break;
else if ( !strcmp(argv[index],"/tall") ) case CMDPARAM_PIXELRATIO_TALL:
{
Pixel_ratio = PIXEL_TALL; Pixel_ratio = PIXEL_TALL;
} break;
else if ( !strcmp(argv[index],"/wide") ) case CMDPARAM_PIXELRATIO_WIDE:
{
Pixel_ratio = PIXEL_WIDE; Pixel_ratio = PIXEL_WIDE;
} break;
else if ( !strcmp(argv[index],"/double") ) case CMDPARAM_PIXELRATIO_DOUBLE:
{
Pixel_ratio = PIXEL_DOUBLE; Pixel_ratio = PIXEL_DOUBLE;
} break;
else if ( !strcmp(argv[index],"/triple") ) case CMDPARAM_PIXELRATIO_TRIPLE:
{
Pixel_ratio = PIXEL_TRIPLE; Pixel_ratio = PIXEL_TRIPLE;
} break;
else if ( !strcmp(argv[index],"/quadruple") ) case CMDPARAM_PIXELRATIO_QUAD:
{
Pixel_ratio = PIXEL_QUAD; Pixel_ratio = PIXEL_QUAD;
} break;
else if ( !strcmp(argv[index],"/tall2") ) case CMDPARAM_PIXELRATIO_TALL2:
{
Pixel_ratio = PIXEL_TALL2; Pixel_ratio = PIXEL_TALL2;
} break;
else if ( !strcmp(argv[index],"/wide2") ) case CMDPARAM_PIXELRATIO_WIDE2:
{
Pixel_ratio = PIXEL_WIDE2; Pixel_ratio = PIXEL_WIDE2;
} break;
else if ( !strcmp(argv[index],"/rgb") ) case CMDPARAM_RGB:
{
// echelle des composants RGB // echelle des composants RGB
index++; index++;
if (index<argc) if (index<argc)
@ -270,9 +330,8 @@ void Analyze_command_line(int argc, char * argv[])
Display_syntax(); Display_syntax();
exit(0); exit(0);
} }
} break;
else if ( !strcmp(argv[index],"/skin") ) case CMDPARAM_SKIN:
{
// GUI skin file // GUI skin file
index++; index++;
if (index<argc) if (index<argc)
@ -285,9 +344,8 @@ void Analyze_command_line(int argc, char * argv[])
Display_syntax(); Display_syntax();
exit(0); exit(0);
} }
} break;
else default:
{
// Si ce n'est pas un paramètre, c'est le nom du fichier à ouvrir // Si ce n'est pas un paramètre, c'est le nom du fichier à ouvrir
if (File_in_command_line > 1) if (File_in_command_line > 1)
{ {
@ -307,7 +365,9 @@ void Analyze_command_line(int argc, char * argv[])
Extract_path(Main_file_directory, buffer); Extract_path(Main_file_directory, buffer);
Extract_filename(Main_filename, buffer); Extract_filename(Main_filename, buffer);
free(buffer); free(buffer);
} else { }
else
{
Extract_path(Spare_file_directory, buffer); Extract_path(Spare_file_directory, buffer);
Extract_filename(Spare_filename, buffer); Extract_filename(Spare_filename, buffer);
free(buffer); free(buffer);
@ -319,6 +379,7 @@ void Analyze_command_line(int argc, char * argv[])
Display_syntax(); Display_syntax();
exit(0); exit(0);
} }
break;
} }
} }
} }