Files
openmine/conf.c
2026-01-26 19:10:01 -05:00

251 lines
4.7 KiB
C

#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#define PACKAGE_VERSION "0.2b"
#define PACKAGE_NAME "openmine"
#endif
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include "gamedefs.h"
#include "structs.h"
#include "conf.h"
extern char *progname;
static int
getuint (const char *s, unsigned *myuint)
{
long int l;
char *error;
if (s == NULL)
return -1;
l = strtol (s, &error, 0);
if (error && *error)
return -1;
if (l < 0 || l > UINT_MAX)
return -1;
if (myuint)
*myuint = l;
return 0;
}
static void
help (void)
{
printf (PACKAGE_NAME " v" PACKAGE_VERSION "\n"
"\n"
"[OPCIONES]:\n"
"\t-h, --help\t\tMostrar este mensaje.\n"
"\t-f, --fullscreen\tCargar el juego a pantalla completa.\n"
"\t-l[num] [cant]\t\tEspecificar [cant] de cubos en el lado [num]\n"
"\t\t\t\t(de 1 a 3).\n"
"\t-c [cant]\t\tTodos los lados tendrán la misma [cant]\n"
"\t\t\t\tde cubos.\n"
"\t-m [cant]\t\tEspecificar [cant] de minas.\n"
"\t-t [tema]\t\tCargar el juego con el [tema] especificado\n"
"\t\t\t\t(el tema debe existir).\n" "\t-s [xxx]\t\t????\n");
exit (EXIT_SUCCESS);
}
static int
processFile (struct config *c, const char *rc)
{
FILE *fp;
fp = fopen (rc, "r");
if (fp == NULL)
{
fprintf (stderr, "%s: unable to open rc file `%s'\n", progname, rc);
fprintf (stderr, "%s: %s\n", progname, strerror (errno));
return -1;
}
else
{
/* TODO: make this smarter.
* TODO: at least flush on error
*/
fscanf (fp, "theme=%s\n", c->theme);
fscanf (fp, "lado1=%u\n", c->side + 0);
c->side[0] += 2;
fscanf (fp, "lado2=%u\n", c->side + 1);
c->side[1] += 2;
fscanf (fp, "lado3=%u\n", c->side + 2);
c->side[2] += 2;
fscanf (fp, "cantidad=%u", &(c->n));
}
fclose (fp);
return 0;
}
/*
* Search Path:
* options
* ./mine.conf
* ~/.mine.conf (UNIX)
* default options
*/
int
getOptions (struct config *c, int argc, char **argv)
{
if (c == NULL || argv == NULL || argc < 1)
return 1;
memset (c, 0, sizeof (*c));
/* default */
c->flags = SDL_OPENGL;
strcpy (c->theme, RC_THEME "/");
c->side[0] = c->side[1] = c->side[2] = 7;
c->n = 15;
c->cube = 0.25f;
c->side[0] += 2;
c->side[1] += 2;
c->side[2] += 2;
processFile (c, RC_FILE);
if (processCmdLine (c, argc, argv))
return -1;
return 0;
}
static int
processCmdLine (struct config *c, int argc, char **argv)
{
int nRet;
int fail = 0;
/* TODO: getopt or something similar?
*/
progname = argv[0];
while (!fail && --argc)
{
argv++;
if (!strcmp (*argv, "-t"))
{
argc--;
if (argc == 0)
{
printf ("%s: argument missing\n", progname);
fail = 1;
}
else
{
if (strlen (argv[1]) >= sizeof (c->theme) - 2)
{
printf ("%s: theme name is too long\n", progname);
fail = 1;
}
else
{
strcpy (c->theme, *(++argv));
strcat (c->theme, "/");
}
}
}
else if (!strcmp (*argv, "-l1") ||
!strcmp (*argv, "-l2") || !strcmp (*argv, "-l3"))
{
unsigned d = (*argv)[2] - '1';
argc--;
if (argc == 0)
{
printf ("%s: argument missing\n", progname);
fail = 1;
}
else
{
nRet = getuint (*(++argv), c->side + d);
if (nRet == -1)
{
printf ("%s: `%s' is not an int\n", progname, *(argv - 1));
fail = 1;
}
}
c->side[d] += 2;
}
else if (!strcmp (*argv, "-c"))
{
argc--;
if (argc == 0)
{
printf ("%s: argument missing\n", progname);
fail = 1;
}
else
{
nRet = getuint (*(++argv), c->side + 0);
if (nRet == -1)
{
printf ("%s: `%s' is not an int\n", progname, *(argv - 1));
fail = 1;
}
}
c->side[1] = c->side[2] = c->side[0] += 2;
}
else if (!strcmp (*argv, "-s"))
{
unsigned p;
argc--;
if (argc == 0)
{
printf ("%s: argument missing\n", progname);
fail = 1;
}
else
{
nRet = getuint (*(++argv), &p);
if (nRet == -1)
{
printf ("%s: `%s' is not an int\n", progname, *(argv - 1));
fail = 1;
}
}
c->cube = 1.0f / (2.0f + p);
}
else if (!strcmp (*argv, "-m"))
{
if (argc == 0)
{
printf ("%s: argument missing\n", progname);
fail = 1;
}
else
{
nRet = getuint (*(++argv), &(c->n));
if (nRet == -1)
{
printf ("%s: `%s' is not an int\n", progname, *(argv - 1));
fail = 1;
}
}
argc--;
}
else if (!strcmp (*argv, "--fullscreen") || !strcmp (*argv, "-f"))
c->flags |= SDL_FULLSCREEN;
else if (!strcmp (*argv, "--help") || !strcmp (*argv, "-h"))
help ();
}
return fail;
}