From 5be5b35e82358f5682af1b5b3d31d5c01f1fdd23 Mon Sep 17 00:00:00 2001 From: Sorgelig Date: Sat, 25 Feb 2023 20:34:37 +0800 Subject: [PATCH] Support for named INI files. --- cfg.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++------ cfg.h | 1 + menu.cpp | 8 +++---- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cfg.cpp b/cfg.cpp index aa9a0c2..9f0e37d 100644 --- a/cfg.cpp +++ b/cfg.cpp @@ -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)); diff --git a/cfg.h b/cfg.h index 33f7030..53a163e 100644 --- a/cfg.h +++ b/cfg.h @@ -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, ...); diff --git a/menu.cpp b/menu.cpp index e8b1b90..ae6557a 100644 --- a/menu.cpp +++ b/menu.cpp @@ -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)); } @@ -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))) @@ -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); @@ -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, " ");