Skip to content

Commit

Permalink
Support for named INI files.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorgelig committed Feb 25, 2023
1 parent 0c74d29 commit 5be5b35
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
66 changes: 60 additions & 6 deletions cfg.cpp
Expand Up @@ -426,20 +426,74 @@ static int cfg_error_count = 0;

const char* cfg_get_name(uint8_t alt)
{
static int done = 0;
static char names[3][64] = {};
static char name[64];
strcpy(name, "MiSTer.ini");

if (alt == 1)
if (!done)
{
strcpy(name, "MiSTer_alt_1.ini");
if (FileExists(name)) return name;
return "MiSTer_alt.ini";
done = 1;
DIR *d = opendir(getRootDir());
if (!d)
{
printf("Couldn't open dir: %s\n", getRootDir());
}
else
{
struct dirent *de;
int i = 0;
while ((de = readdir(d)) && i < 3)
{
int len = strlen(de->d_name);
if (!strncasecmp(de->d_name, "MiSTer_", 7) && !strcasecmp(de->d_name + len - 4, ".ini"))
{
snprintf(names[i], sizeof(names[0]), "%s", de->d_name);
i++;
}
}
closedir(d);
}

for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 3; j++)
{
if ((!names[j - 1][0] && names[j][0]) || (names[j - 1][0] && names[j][0] && strcasecmp(names[j - 1], names[j]) > 0))
{
strcpy(name, names[j - 1]);
strcpy(names[j - 1], names[j]);
strcpy(names[j], name);
}
}
}
}

if (alt && alt < 4) sprintf(name, "MiSTer_alt_%d.ini", alt);
strcpy(name, "MiSTer.ini");
if (alt && alt < 4) strcpy(name, names[alt-1]);
return name;
}

const char* cfg_get_label(uint8_t alt)
{
if (!alt) return "Main";

const char *name = cfg_get_name(alt);
if (!name[0]) return " -- ";

static char label[6];
snprintf(label, sizeof(label), "%s", name + 7);
char *p = strrchr(label, '.');
if (p) *p = 0;
if (!strcasecmp(label, "alt")) return "Alt1";
if (!strcasecmp(label, "alt_1")) return "Alt1";
if (!strcasecmp(label, "alt_2")) return "Alt2";
if (!strcasecmp(label, "alt_3")) return "Alt3";

for (int i = 0; i < 4; i++) if (!label[i]) label[i] = ' ';
label[4] = 0;
return label;
}

void cfg_parse()
{
memset(&cfg, 0, sizeof(cfg));
Expand Down
1 change: 1 addition & 0 deletions cfg.h
Expand Up @@ -99,6 +99,7 @@ extern cfg_t cfg;
void cfg_parse();
void cfg_print();
const char* cfg_get_name(uint8_t alt);
const char* cfg_get_label(uint8_t alt);
bool cfg_has_video_sections();

void cfg_error(const char *fmt, ...);
Expand Down
8 changes: 4 additions & 4 deletions menu.cpp
Expand Up @@ -1701,7 +1701,7 @@ void HandleUI(void)
static char str[1024];
sprintf(str, "%s.f%c", user_io_get_core_name(), p[idx]);
if (FileLoadConfig(str, str, sizeof(str)) && str[0])
{
{
strcat(s, " ");
strcat(s, GetNameFromPath(str));
}
Expand Down Expand Up @@ -2077,7 +2077,7 @@ void HandleUI(void)
strcpy(fs_pFileExt, ext);

memcpy(Selected_tmp, Selected_S[(int)ioctl_index], sizeof(Selected_tmp));
if (is_x86() || is_pcxt()) strcpy(Selected_tmp, x86_get_image_path(ioctl_index));
if (is_x86() || is_pcxt()) strcpy(Selected_tmp, x86_get_image_path(ioctl_index));
if (is_psx() && (ioctl_index == 2 || ioctl_index == 3)) fs_Options |= SCANO_SAVES;

if (is_pce() || is_megacd() || is_x86() || (is_psx() && !(fs_Options & SCANO_SAVES)))
Expand Down Expand Up @@ -2196,7 +2196,7 @@ void HandleUI(void)

if (is_pce() && !bit) pcecd_reset();
if (is_saturn() && !bit) saturn_reset();

user_io_status_set(opt, 1, ex);
user_io_status_set(opt, 0, ex);

Expand Down Expand Up @@ -3455,7 +3455,7 @@ void HandleUI(void)
if (m) strcat(s, "\xc ");
m = (i == (flag >> 4) && en);
if (!en) strcat(s, "\xb");
strcat(s, (!i) ? "Main" : (i == 1) ? "Alt1" : (i == 2) ? "Alt2" : "Alt3");
strcat(s, cfg_get_label(i));
if (!en) strcat(s, "\xb");
}
strcat(s, " ");
Expand Down

0 comments on commit 5be5b35

Please sign in to comment.