flycast/core/cfg/cl.cpp

167 lines
3.0 KiB
C++
Raw Normal View History

2013-12-19 17:10:14 +00:00
/*
Command line parsing
~yay~
Nothing too interesting here, really
*/
2019-09-07 12:37:39 +00:00
#include <cstdio>
#include <cctype>
#include <cstring>
2013-12-19 17:10:14 +00:00
#include "cfg/cfg.h"
2020-01-31 22:51:12 +00:00
char* trim_ws(char* str)
2013-12-19 17:10:14 +00:00
{
if (str==0 || strlen(str)==0)
return 0;
while(*str)
{
if (!isspace(*str))
break;
str++;
}
size_t l=strlen(str);
if (l==0)
return 0;
while(l>0)
{
if (!isspace(str[l-1]))
break;
str[l-1]=0;
l--;
}
if (l==0)
return 0;
return str;
}
2020-01-31 22:51:12 +00:00
int setconfig(char** arg,int cl)
2013-12-19 17:10:14 +00:00
{
int rv=0;
for(;;)
{
if (cl<1)
{
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "-config : invalid number of parameters, format is section:key=value");
2013-12-19 17:10:14 +00:00
return rv;
}
2020-01-31 22:51:12 +00:00
char* sep=strstr(arg[1],":");
2013-12-19 17:10:14 +00:00
if (sep==0)
{
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "-config : invalid parameter %s, format is section:key=value", arg[1]);
2013-12-19 17:10:14 +00:00
return rv;
}
2020-01-31 22:51:12 +00:00
char* value=strstr(sep+1,"=");
2013-12-19 17:10:14 +00:00
if (value==0)
{
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "-config : invalid parameter %s, format is section:key=value", arg[1]);
2013-12-19 17:10:14 +00:00
return rv;
}
*sep++=0;
*value++=0;
2020-01-31 22:51:12 +00:00
char* sect=trim_ws(arg[1]);
char* key=trim_ws(sep);
2013-12-19 17:10:14 +00:00
value=trim_ws(value);
if (sect==0 || key==0)
{
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "-config : invalid parameter, format is section:key=value");
2013-12-19 17:10:14 +00:00
return rv;
}
2020-01-31 22:51:12 +00:00
const char* constval = value;
2015-07-29 01:23:08 +00:00
if (constval==0)
constval="";
2019-07-01 16:23:10 +00:00
INFO_LOG(COMMON, "Virtual cfg %s:%s=%s", sect, key, value);
2013-12-19 17:10:14 +00:00
2015-07-17 21:56:51 +00:00
cfgSetVirtual(sect,key,value);
2013-12-19 17:10:14 +00:00
rv++;
if (cl>=3 && stricmp(arg[2],",")==0)
{
cl-=2;
arg+=2;
rv++;
continue;
}
else
break;
}
return rv;
}
2020-03-27 14:25:47 +00:00
static int showhelp()
2013-12-19 17:10:14 +00:00
{
2020-03-27 14:25:47 +00:00
printf("Usage: flycast [OPTION]... [CONTENT]\n\n");
printf("Options:\n");
printf("-config section:key=value add a virtual config value;\n");
printf(" virtual config values won't be saved to the .cfg file\n");
printf(" unless a different value is written to them\n");
printf("-help display this help\n");
exit(0);
return 0;
}
2013-12-19 17:10:14 +00:00
2020-01-31 22:51:12 +00:00
bool ParseCommandLine(int argc,char* argv[])
{
settings.imgread.ImagePath[0] = '\0';
2013-12-19 17:10:14 +00:00
int cl=argc-2;
2020-01-31 22:51:12 +00:00
char** arg=argv+1;
2013-12-19 17:10:14 +00:00
while(cl>=0)
{
if (stricmp(*arg,"-help")==0 || stricmp(*arg,"--help")==0)
2013-12-19 17:10:14 +00:00
{
2020-03-27 14:25:47 +00:00
showhelp();
}
else if (stricmp(*arg,"-config")==0 || stricmp(*arg,"--config")==0)
2013-12-19 17:10:14 +00:00
{
int as=setconfig(arg,cl);
cl-=as;
arg+=as;
}
2020-04-26 08:03:57 +00:00
#if defined(__APPLE__)
else if (!strncmp(*arg, "-NSDocumentRevisions", 20))
{
arg++;
cl--;
}
#endif
2013-12-19 17:10:14 +00:00
else
{
char* extension = strrchr(*arg, '.');
if (extension
&& (stricmp(extension, ".cdi") == 0 || stricmp(extension, ".chd") == 0
|| stricmp(extension, ".gdi") == 0 || stricmp(extension, ".cue") == 0))
{
2019-07-01 16:23:10 +00:00
INFO_LOG(COMMON, "Using '%s' as cd image", *arg);
strcpy(settings.imgread.ImagePath, *arg);
}
else if (extension && stricmp(extension, ".elf") == 0)
{
2019-07-01 16:23:10 +00:00
INFO_LOG(COMMON, "Using '%s' as reios elf file", *arg);
2019-07-30 17:04:51 +00:00
cfgSetVirtual("config", "bios.UseReios", "yes");
strcpy(settings.imgread.ImagePath, *arg);
}
else
{
2019-07-01 16:23:10 +00:00
INFO_LOG(COMMON, "Using '%s' as rom", *arg);
strcpy(settings.imgread.ImagePath, *arg);
}
2013-12-19 17:10:14 +00:00
}
arg++;
cl--;
}
return false;
2015-07-17 21:56:51 +00:00
}