* 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; enum shuffle_mode mode;
unsigned int seed; unsigned int seed;
void (*shuffler) (void **a, size_t len); void (*shuffler) (void **a, size_t len);
char strval[INTSTR_LENGTH]; char strval[INTSTR_LENGTH + 1];
} config = { sm_none, 0, NULL, "" }; } config = { sm_none, 0, NULL, "" };
/* Return string value of --shuffle= option passed. /* Return string value of --shuffle= option passed.
@ -55,55 +55,47 @@ static struct
const char * const char *
shuffle_get_mode () shuffle_get_mode ()
{ {
return config.strval[0] ? config.strval : NULL; return config.strval[0] == '\0' ? NULL : config.strval;
} }
void void
shuffle_set_mode (const char *cmdarg) shuffle_set_mode (const char *cmdarg)
{ {
/* Parse supported '--shuffle' mode. */ /* 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; 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.shuffler = reverse_shuffle_array; config.shuffler = reverse_shuffle_array;
strcpy (config.strval, "reverse"); strcpy (config.strval, "reverse");
break; }
case sm_identity: else if (strcasecmp (cmdarg, "identity") == 0)
{
config.mode = sm_identity;
config.shuffler = identity_shuffle_array; config.shuffler = identity_shuffle_array;
strcpy (config.strval, "identity"); 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'; 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);
} }
} }