* src/shuffle.c: Minor coding changes.

This commit is contained in:
Paul Smith 2022-09-11 14:04:58 -04:00
parent 86fc644dcc
commit aac4004444

View File

@ -46,7 +46,7 @@ static struct
enum shuffle_mode mode;
unsigned int seed;
void (*shuffler) (void **a, size_t len);
char strval[INTSTR_LENGTH];
char strval[INTSTR_LENGTH + 1];
} config = { sm_none, 0, NULL, "" };
/* Return string value of --shuffle= option passed.
@ -55,55 +55,47 @@ static struct
const char *
shuffle_get_mode ()
{
return config.strval[0] ? config.strval : NULL;
return config.strval[0] == '\0' ? NULL : config.strval;
}
void
shuffle_set_mode (const char *cmdarg)
{
/* Parse supported '--shuffle' mode. */
if (strcasecmp (cmdarg, "random") == 0)
if (strcasecmp (cmdarg, "reverse") == 0)
{
config.mode = sm_random;
config.seed = (unsigned int) (time (NULL) ^ make_pid ());
}
else if (strcasecmp (cmdarg, "reverse") == 0)
config.mode = sm_reverse;
else if (strcasecmp (cmdarg, "identity") == 0)
config.mode = sm_identity;
else if (strcasecmp (cmdarg, "none") == 0)
config.mode = sm_none;
/* Assume explicit seed if starts from a digit. */
else
{
const char *err;
config.mode = sm_random;
config.seed = make_toui (cmdarg, &err);
if (err)
{
OS (error, NILF, _("invalid shuffle mode: '%s'"), cmdarg);
die (MAKE_FAILURE);
}
}
switch (config.mode)
{
case sm_random:
config.shuffler = random_shuffle_array;
sprintf (config.strval, "%u", config.seed);
break;
case sm_reverse:
config.mode = sm_reverse;
config.shuffler = reverse_shuffle_array;
strcpy (config.strval, "reverse");
break;
case sm_identity:
}
else if (strcasecmp (cmdarg, "identity") == 0)
{
config.mode = sm_identity;
config.shuffler = identity_shuffle_array;
strcpy (config.strval, "identity");
break;
case sm_none:
}
else if (strcasecmp (cmdarg, "none") == 0)
{
config.mode = sm_none;
config.shuffler = NULL;
config.strval[0] = '\0';
break;
}
else
{
if (strcasecmp (cmdarg, "random") == 0)
config.seed = (unsigned int) (time (NULL) ^ make_pid ());
else
{
/* Assume explicit seed. */
const char *err;
config.seed = make_toui (cmdarg, &err);
if (err)
OSS (fatal, NILF, _("invalid shuffle mode: %s: '%s'"), err, cmdarg);
}
config.mode = sm_random;
config.shuffler = random_shuffle_array;
sprintf (config.strval, "%u", config.seed);
}
}