From 13d5de4e7b8a57ab2bc82f0984ae15dc61ddaf44 Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Fri, 19 Sep 2025 17:51:01 -0300 Subject: [PATCH] ROM file check optimizations and cleanups --- src/include/86box/rom.h | 2 +- src/mem/rom.c | 66 +++++++++++++++++------------------------ src/qt/qt_platform.cpp | 9 +++--- src/unix/unix.c | 14 ++++----- 4 files changed, 39 insertions(+), 52 deletions(-) diff --git a/src/include/86box/rom.h b/src/include/86box/rom.h index 331d78526..807649956 100644 --- a/src/include/86box/rom.h +++ b/src/include/86box/rom.h @@ -59,7 +59,7 @@ extern void rom_writel(uint32_t addr, uint32_t val, void *priv); extern void rom_get_full_path(char *dest, const char *fn); extern FILE *rom_fopen(const char *fn, char *mode); -extern int rom_getfile(char *fn, char *s, int size); +extern int rom_getfile(const char *fn, char *s, int size); extern int rom_present(const char *fn); extern int rom_load_linear_oddeven(const char *fn, uint32_t addr, int sz, diff --git a/src/mem/rom.c b/src/mem/rom.c index c7db01d72..ebebc6fc6 100644 --- a/src/mem/rom.c +++ b/src/mem/rom.c @@ -92,8 +92,9 @@ rom_check(const char *fn) { FILE *fp = NULL; int ret = 0; + char last = fn[strlen(fn) - 1]; - if ((fn[strlen(fn) - 1] == '/') || (fn[strlen(fn) - 1] == '\\')) + if ((last == '/') || (last == '\\')) ret = plat_dir_check((char *) fn); else { fp = fopen(fn, "rb"); @@ -112,7 +113,7 @@ rom_get_full_path(char *dest, const char *fn) dest[0] = 0x00; - if (strstr(fn, "roms/") == fn) { + if (!strncmp(fn, "roms/", 5)) { /* Relative path */ for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { path_append_filename(temp, rom_path->path, fn + 5); @@ -130,34 +131,6 @@ rom_get_full_path(char *dest, const char *fn) } } -bool -rom_fcheck(const char *fn) -{ - char temp[1024]; - bool exists = false; - - if (fn == NULL) - return false; - - if (strstr(fn, "roms/") == fn) { - /* Relative path */ - for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { - path_append_filename(temp, rom_path->path, fn + 5); - - exists = plat_file_check(temp); - - if (exists) { - return exists; - } - } - - return exists; - } else { - /* Absolute path */ - return plat_file_check(fn); - } -} - FILE * rom_fopen(const char *fn, char *mode) { @@ -167,14 +140,13 @@ rom_fopen(const char *fn, char *mode) if ((fn == NULL) || (mode == NULL)) return NULL; - if (strstr(fn, "roms/") == fn) { + if (!strncmp(fn, "roms/", 5)) { /* Relative path */ for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { path_append_filename(temp, rom_path->path, fn + 5); - if ((fp = plat_fopen(temp, mode)) != NULL) { + if ((fp = plat_fopen(temp, mode)) != NULL) return fp; - } } return fp; @@ -185,16 +157,16 @@ rom_fopen(const char *fn, char *mode) } int -rom_getfile(char *fn, char *s, int size) +rom_getfile(const char *fn, char *s, int size) { char temp[1024]; - if (strstr(fn, "roms/") == fn) { + if (!strncmp(fn, "roms/", 5)) { /* Relative path */ for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { path_append_filename(temp, rom_path->path, fn + 5); - if (rom_present(temp)) { + if (plat_file_check(temp)) { strncpy(s, temp, size); return 1; } @@ -203,7 +175,7 @@ rom_getfile(char *fn, char *s, int size) return 0; } else { /* Absolute path */ - if (rom_present(fn)) { + if (plat_file_check(fn)) { strncpy(s, fn, size); return 1; } @@ -215,7 +187,25 @@ rom_getfile(char *fn, char *s, int size) int rom_present(const char *fn) { - return rom_fcheck(fn); + char temp[1024]; + + if (fn == NULL) + return 0; + + if (!strncmp(fn, "roms/", 5)) { + /* Relative path */ + for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) { + path_append_filename(temp, rom_path->path, fn + 5); + + if (plat_file_check(temp)) + return 1; + } + + return 0; + } else { + /* Absolute path */ + return plat_file_check(fn); + } } uint8_t diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index f36856e5f..ad49e5e7c 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -258,13 +258,12 @@ plat_file_check(const char *path) #ifdef _WIN32 auto data = QString::fromUtf8(path).toStdWString(); auto res = GetFileAttributesW(data.c_str()); - return (res != INVALID_FILE_ATTRIBUTES && !(res & FILE_ATTRIBUTE_DIRECTORY)); + return (res != INVALID_FILE_ATTRIBUTES) && !(res & FILE_ATTRIBUTE_DIRECTORY); #else - struct stat dummy; - if (stat(path, &dummy) < 0) { + struct stat stats; + if (stat(path, &stats) < 0) return 0; - } - return !S_ISDIR(dummy.st_mode); + return !S_ISDIR(stats.st_mode); #endif } diff --git a/src/unix/unix.c b/src/unix/unix.c index 20727928c..ee13284fd 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -405,21 +405,19 @@ path_append_filename(char *dest, const char *s1, const char *s2) int plat_dir_check(char *path) { - struct stat dummy; - if (stat(path, &dummy) < 0) { + struct stat stats; + if (stat(path, &stats) < 0) return 0; - } - return S_ISDIR(dummy.st_mode); + return S_ISDIR(stats.st_mode); } int plat_file_check(const char *path) { - struct stat dummy; - if (stat(path, &dummy) < 0) { + struct stat stats; + if (stat(path, &stats) < 0) return 0; - } - return !S_ISDIR(dummy.st_mode); + return !S_ISDIR(stats.st_mode); } int