Support RBF display names from names.txt

This commit is contained in:
sorgelig
2020-03-10 09:19:23 +08:00
parent 691093ebf3
commit b25b97a540
4 changed files with 122 additions and 98 deletions

View File

@@ -1075,27 +1075,26 @@ struct DirentComp
if ((de1.de.d_type == DT_DIR) && !strcmp(de1.altname, "..")) return true;
if ((de2.de.d_type == DT_DIR) && !strcmp(de2.altname, "..")) return false;
if ((de1.de.d_type == DT_DIR) && (de2.de.d_type == DT_REG)) return true;
if ((de1.de.d_type == DT_REG) && (de2.de.d_type == DT_DIR)) return false;
if ((de1.de.d_type == DT_DIR) && (de2.de.d_type != DT_DIR)) return true;
if ((de1.de.d_type != DT_DIR) && (de2.de.d_type == DT_DIR)) return false;
if (de1.de.d_type == de2.de.d_type)
int len1 = strlen(de1.altname);
int len2 = strlen(de2.altname);
if ((len1 > 4) && (de1.altname[len1 - 4] == '.')) len1 -= 4;
if ((len2 > 4) && (de2.altname[len2 - 4] == '.')) len2 -= 4;
int len = (len1 < len2) ? len1 : len2;
int ret = strncasecmp(de1.altname, de2.altname, len);
if (!ret)
{
int len1 = strlen(de1.altname);
int len2 = strlen(de2.altname);
if ((len1 > 4) && (de1.altname[len1 - 4] == '.')) len1 -= 4;
if ((len2 > 4) && (de2.altname[len2 - 4] == '.')) len2 -= 4;
int len = (len1 < len2) ? len1 : len2;
int ret = strncasecmp(de1.altname, de2.altname, len);
ret = strcasecmp(de1.datecode, de2.datecode);
if (!ret)
{
return len1 < len2;
}
return ret < 0;
}
return strcasecmp(de1.altname, de2.altname) < 0;
return ret < 0;
}
size_t iterations = 0;
@@ -1130,6 +1129,91 @@ static bool IsInSameFolder(const char *folder, const char *path)
return false;
}
static int names_loaded = 0;
static void get_display_name(direntext_t *dext, const char *ext)
{
static char *names = 0;
snprintf(dext->altname, sizeof(dext->altname), dext->de.d_name);
int len = strlen(dext->altname);
int rbf = (len > 4 && !strcasecmp(dext->altname + len - 4, ".rbf"));
if (rbf)
{
dext->altname[len - 4] = 0;
char *p = strstr(dext->altname, "_20");
if (p) if (strlen(p + 3) < 6) p = 0;
if (p)
{
*p = 0;
strncpy(dext->datecode, p + 3, 15);
dext->datecode[15] = 0;
}
else
{
strcpy(dext->datecode, "------");
}
if (!names_loaded)
{
if (names)
{
free(names);
names = 0;
}
int size = FileLoad("names.txt", 0, 0);
if (size)
{
names = (char*)malloc(size + 1);
if (names)
{
names[0] = 0;
FileLoad("names.txt", names, 0);
names[size] = 0;
}
}
names_loaded = 1;
}
if (names)
{
strcat(dext->altname, ":");
len = strlen(dext->altname);
char *transl = strstr(names, dext->altname);
if (transl)
{
int copy = 0;
transl += len;
len = 0;
while (*transl && len < (int)sizeof(dext->altname) - 1)
{
if (!copy && *transl <= 32)
{
transl++;
continue;
}
if (copy && *transl < 32) break;
copy = 1;
dext->altname[len++] = *transl++;
}
len++;
}
dext->altname[len - 1] = 0;
}
return;
}
//do not remove ext if core supplies more than 1 extension and it's not list of cores
if (strlen(ext) > 3 && strcasecmp(ext, "RBFMRA")) return;
/* find the extension on the end of the name*/
char *fext = strrchr(dext->altname, '.');
if (fext) *fext = 0;
}
int ScanDirectory(char* path, int mode, const char *extension, int options, const char *prefix)
{
static char file_name[1024];
@@ -1291,7 +1375,7 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons
if (!strncasecmp(de->d_name, ".", 1)) continue;
}
direntext_t dext = { *de, 0, "" };
direntext_t dext = { *de, 0, "", "" };
memcpy(dext.altname, de->d_name, sizeof(dext.altname));
if (!strcasecmp(dext.altname + strlen(dext.altname) - 4, ".zip")) dext.altname[strlen(dext.altname) - 4] = 0;
@@ -1393,8 +1477,8 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons
}
{
direntext_t dext = { *de, 0, "" };
memcpy(dext.altname, de->d_name, sizeof(dext.altname));
direntext_t dext = { *de, 0, "", "" };
get_display_name(&dext, extension);
DirItem.push_back(dext);
}
}
@@ -1407,8 +1491,8 @@ int ScanDirectory(char* path, int mode, const char *extension, int options, cons
dirent up;
up.d_type = DT_DIR;
strcpy(up.d_name, "..");
direntext_t dext = { up, 0, "" };
memcpy(dext.altname, up.d_name, sizeof(dext.altname));
direntext_t dext = { up, 0, "", "" };
get_display_name(&dext, extension);
DirItem.push_back(dext);
mz_zip_reader_end(z);

View File

@@ -28,6 +28,7 @@ struct direntext_t
{
dirent de;
int cookie;
char datecode[16];
char altname[256];
};

View File

@@ -4908,16 +4908,28 @@ void HandleUI(void)
OsdWrite(13, str, 0, 0);
strcpy(straux, cfg.bootcore);
sprintf(str, " %s", get_rbf_name_bootcore(straux));
PrintFileName(str, 14, (32 * btimeout) / cfg.bootcore_timeout);
char s[40];
memset(s, ' ', 32); // clear line buffer
s[32] = 0; // set temporary string length to OSD line length
int len = strlen(str);
if (len > 28)
{
len = 27; // trim display length if longer than 30 characters
s[28] = 22;
}
strncpy(s + 1, str, len); // display only name
OsdWrite(14, s, 1, 0, 0, (32 * btimeout) / cfg.bootcore_timeout);
sprintf(str, " Press any key to cancel");
OsdWrite(15, str, 0, 0);
btimeout--;
if (btimeout < 10)
{
OsdWrite(13, "", 0, 0);
strcpy(straux, cfg.bootcore);
sprintf(str, " %s", get_rbf_name_bootcore(straux));
PrintFileName(str, 14, 0);
OsdWrite(14, s, 1, 0, 0, 0);
sprintf(str, " Loading...");
OsdWrite(15, str, 1, 0);
isMraName(cfg.bootcore) ? arcade_load(getFullPath(cfg.bootcore)) : fpga_load_rbf(cfg.bootcore);
@@ -4984,36 +4996,6 @@ void open_joystick_setup()
joymap_first = 1;
}
static int calc_name_length(const char *name, const char *ext, const char **datecode)
{
*datecode = 0;
int len = strlen(name);
int rbf = (len > 4 && !strcasecmp(name + len - 4, ".rbf"));
if (rbf)
{
len -= 4;
const char *p = strstr(name, "_20");
if (p)
{
len = p - name;
p += 3;
if (strlen(p) < 6) p = 0;
}
*datecode = (p) ? p : "------";
return len;
}
//do not remove ext if core supplies more than 1 extension and it's not list of cores
if (strlen(ext) > 3 && strcasecmp(ext, "RBFMRA")) return len;
/* find the extension on the end of the name*/
const char *fext = strrchr(name, '.');
return fext ? fext - name : len;
}
void ScrollLongName(void)
{
// this function is called periodically when file selection window is displayed
@@ -5032,9 +5014,7 @@ void ScrollLongName(void)
if (flist_SelectedItem()->de.d_type != DT_DIR) // if a file
{
const char *datecode;
len = calc_name_length(flist_SelectedItem()->altname, fs_pFileExt, &datecode);
if (!cfg.rbf_hide_datecode && datecode)
if (!cfg.rbf_hide_datecode && flist_SelectedItem()->datecode[0])
{
max_len = 20; // __.__.__ remove that from the end
}
@@ -5043,40 +5023,6 @@ void ScrollLongName(void)
ScrollText(flist_iSelectedEntry()-flist_iFirstEntry(), flist_SelectedItem()->altname, 0, len, max_len, 1);
}
void PrintFileName(char *name, int row, int maxinv)
{
char s[40];
memset(s, ' ', 32); // clear line buffer
s[32] = 0; // set temporary string length to OSD line length
const char *datecode;
int len = calc_name_length(name, "", &datecode);
if (len > 28)
{
len = 27; // trim display length if longer than 30 characters
s[28] = 22;
}
strncpy(s + 1, name, len); // display only name
if (!cfg.rbf_hide_datecode && datecode)
{
int n = 19;
s[n++] = ' ';
s[n++] = datecode[0];
s[n++] = datecode[1];
s[n++] = '.';
s[n++] = datecode[2];
s[n++] = datecode[3];
s[n++] = '.';
s[n++] = datecode[4];
s[n++] = datecode[5];
}
OsdWrite(row, s, 1, 0, 0, maxinv);
}
// print directory contents
void PrintDirectory(void)
{
@@ -5096,13 +5042,6 @@ void PrintDirectory(void)
{
k = flist_iFirstEntry() + i;
len = strlen(flist_DirItem(k)->altname); // get name length
const char *datecode = 0;
if (flist_DirItem(k)->de.d_type != DT_DIR) // if a file
{
len = calc_name_length(flist_DirItem(k)->altname, fs_pFileExt, &datecode);
}
if (len > 28)
{
len = 27; // trim display length if longer than 30 characters
@@ -5118,6 +5057,7 @@ void PrintDirectory(void)
strncpy(s + 1, flist_DirItem(k)->altname, len); // display only name
}
char *datecode = flist_DirItem(k)->datecode;
if (flist_DirItem(k)->de.d_type == DT_DIR) // mark directory with suffix
{
if (!strcmp(flist_DirItem(k)->altname, ".."))
@@ -5129,7 +5069,7 @@ void PrintDirectory(void)
strcpy(&s[22], " <DIR>");
}
}
else if (!cfg.rbf_hide_datecode && datecode)
else if (!cfg.rbf_hide_datecode && datecode[0])
{
int n = 19;
s[n++] = ' ';

1
menu.h
View File

@@ -5,7 +5,6 @@
void HandleUI(void);
void menu_key_set(unsigned int c);
void PrintFileName(char *name, int row, int maxinv);
void PrintDirectory(void);
void ScrollLongName(void);