Move ROM search path code to rom.c, rewrite rom_fopen

This commit is contained in:
David Hrdlička
2022-04-08 00:46:30 +02:00
parent a780d9a241
commit 3449a173ae
6 changed files with 71 additions and 129 deletions

View File

@@ -384,48 +384,6 @@ pc_log(const char *fmt, ...)
#define pc_log(fmt, ...) #define pc_log(fmt, ...)
#endif #endif
void
add_rom_path(const char* path)
{
static char cwd[1024];
memset(cwd, 0x00, sizeof(cwd));
rom_path_t* cur_rom_path = &rom_paths;
while (cur_rom_path->next != NULL) {
cur_rom_path = cur_rom_path->next;
}
if (!plat_path_abs((char*)path)) {
/*
* This looks like a relative path.
*
* Add it to the current working directory
* to convert it (back) to an absolute path.
*/
plat_getcwd(cwd, 1024);
plat_path_slash(cwd);
snprintf(cur_rom_path->rom_path, 1024, "%s%s%c", cwd, path, 0);
}
else {
/*
* The user-provided path seems like an
* absolute path, so just use that.
*/
strncpy(cur_rom_path->rom_path, path, 1024);
}
plat_path_slash(cur_rom_path->rom_path);
cur_rom_path->next = calloc(1, sizeof(rom_path_t));
}
// Copied over from Unix code, which in turn is lifted from musl. Needed for parsing XDG_DATA_DIRS.
static char *local_strsep(char **str, const char *sep)
{
char *s = *str, *end;
if (!s) return NULL;
end = s + strcspn(s, sep);
if (*end) *end++ = 0;
else end = 0;
*str = end;
return s;
}
/* /*
* Perform initial startup of the PC. * Perform initial startup of the PC.
@@ -539,7 +497,7 @@ usage:
if ((c+1) == argc) goto usage; if ((c+1) == argc) goto usage;
strcpy(path2, argv[++c]); strcpy(path2, argv[++c]);
add_rom_path(path2); rom_add_path(path2);
} else if (!strcasecmp(argv[c], "--config") || } else if (!strcasecmp(argv[c], "--config") ||
!strcasecmp(argv[c], "-C")) { !strcasecmp(argv[c], "-C")) {
if ((c+1) == argc) goto usage; if ((c+1) == argc) goto usage;
@@ -641,7 +599,7 @@ usage:
plat_path_slash(vmrppath); plat_path_slash(vmrppath);
strcat(vmrppath, "roms"); strcat(vmrppath, "roms");
plat_path_slash(vmrppath); plat_path_slash(vmrppath);
add_rom_path(vmrppath); rom_add_path(vmrppath);
if (path2[0] == '\0') { if (path2[0] == '\0') {
strcpy(path2, vmrppath); strcpy(path2, vmrppath);
} }
@@ -664,7 +622,7 @@ usage:
snprintf(default_rom_path, 1024, "%s%s%c", default_rom_path, "roms", 0); snprintf(default_rom_path, 1024, "%s%s%c", default_rom_path, "roms", 0);
plat_path_slash(default_rom_path); plat_path_slash(default_rom_path);
} }
add_rom_path(default_rom_path); rom_add_path(default_rom_path);
if (path2[0] == '\0') { if (path2[0] == '\0') {
strcpy(path2, default_rom_path); strcpy(path2, default_rom_path);
} }
@@ -771,7 +729,7 @@ usage:
if (rom_paths.next) { if (rom_paths.next) {
rom_path_t* cur_rom_path = &rom_paths; rom_path_t* cur_rom_path = &rom_paths;
while (cur_rom_path->next) { while (cur_rom_path->next) {
pclog("# ROM path: %s\n", cur_rom_path->rom_path); pclog("# ROM path: %s\n", cur_rom_path->path);
cur_rom_path = cur_rom_path->next; cur_rom_path = cur_rom_path->next;
} }
} }

View File

@@ -42,13 +42,13 @@ typedef struct {
typedef struct rom_path_t { typedef struct rom_path_t {
char rom_path[1024]; char path[1024];
struct rom_path_t* next; struct rom_path_t* next;
} rom_path_t; } rom_path_t;
extern rom_path_t rom_paths; extern rom_path_t rom_paths;
extern void add_rom_path(const char* path); extern void rom_add_path(const char* path);
extern uint8_t rom_read(uint32_t addr, void *p); extern uint8_t rom_read(uint32_t addr, void *p);
extern uint16_t rom_readw(uint32_t addr, void *p); extern uint16_t rom_readw(uint32_t addr, void *p);

View File

@@ -57,47 +57,55 @@ rom_log(const char *fmt, ...)
#define rom_log(fmt, ...) #define rom_log(fmt, ...)
#endif #endif
void
rom_add_path(const char* path)
{
char cwd[1024] = { 0 };
// Iterate to the end of the list.
rom_path_t* rom_path = &rom_paths;
while (rom_path->next != NULL) {
rom_path = rom_path->next;
}
// Allocate the new entry.
rom_path = rom_path->next = calloc(1, sizeof(rom_path_t));
// Save the path, turning it into absolute if needed.
if (!plat_path_abs((char*) path)) {
plat_getcwd(cwd, 1024);
plat_path_slash(cwd);
snprintf(rom_path->path, 1024, "%s%s%c", cwd, path, 0);
} else {
strncpy(rom_path->path, path, 1024);
}
// Ensure the path ends with a separator.
plat_path_slash(rom_path->path);
}
FILE * FILE *
rom_fopen(char *fn, char *mode) rom_fopen(char *fn, char *mode)
{ {
char temp[1024]; char temp[1024];
char *fn2; rom_path_t *rom_path = &rom_paths;
FILE *fp;
if ((strstr(fn, "roms/") == fn) || (strstr(fn, "roms\\") == fn)) { if (strstr(fn, "roms/") == fn) {
/* Relative path */ /* Relative path */
fn2 = (char *) malloc(strlen(fn) + 1); do {
memcpy(fn2, fn, strlen(fn) + 1); plat_append_filename(temp, rom_path->path, fn + 5);
if (rom_paths.next) { if (fp = plat_fopen(temp, mode)) {
rom_path_t* cur_rom_path = &rom_paths; return fp;
memset(fn2, 0x00, strlen(fn) + 1);
memcpy(fn2, &(fn[5]), strlen(fn) - 4);
while (cur_rom_path->next) {
memset(temp, 0, sizeof(temp));
plat_append_filename(temp, cur_rom_path->rom_path, fn2);
if (rom_present(temp)) {
break;
} }
cur_rom_path = cur_rom_path->next; } while(rom_path = rom_path->next);
}
} else {
/* Make sure to make it a backslash, just in case there's malformed
code calling us that assumes Windows. */
if (fn2[4] == '\\')
fn2[4] = '/';
plat_append_filename(temp, exe_path, fn2); return fp;
}
free(fn2);
fn2 = NULL;
return(plat_fopen(temp, mode));
} else { } else {
/* Absolute path */ /* Absolute path */
return(plat_fopen(fn, mode)); return plat_fopen(fn, mode);
} }
} }
@@ -105,54 +113,30 @@ rom_fopen(char *fn, char *mode)
int int
rom_getfile(char *fn, char *s, int size) rom_getfile(char *fn, char *s, int size)
{ {
char temp[1024] = {'\0'}; char temp[1024];
char *fn2; rom_path_t *rom_path = &rom_paths;
int retval = 0;
if ((strstr(fn, "roms/") == fn) || (strstr(fn, "roms\\") == fn)) { if (strstr(fn, "roms/") == fn) {
/* Relative path */ /* Relative path */
fn2 = (char *) malloc(strlen(fn) + 1); do {
memcpy(fn2, fn, strlen(fn) + 1); plat_append_filename(temp, rom_path->path, fn + 5);
if (rom_paths.next) {
rom_path_t* cur_rom_path = &rom_paths;
memset(fn2, 0x00, strlen(fn) + 1);
memcpy(fn2, &(fn[5]), strlen(fn) - 4);
while (cur_rom_path->next) {
memset(temp, 0, sizeof(temp));
plat_append_filename(temp, cur_rom_path->rom_path, fn2);
if (rom_present(temp)) { if (rom_present(temp)) {
strncpy(s, temp, size); strncpy(s, temp, size);
retval = 1; return 1;
break;
} }
cur_rom_path = cur_rom_path->next; } while(rom_path = rom_path->next);
}
} else {
/* Make sure to make it a backslash, just in case there's malformed
code calling us that assumes Windows. */
if (fn2[4] == '\\')
fn2[4] = '/';
plat_append_filename(temp, exe_path, fn2); return 0;
if (rom_present(temp)) {
strncpy(s, temp, size);
retval = 1;
}
}
free(fn2);
fn2 = NULL;
} else { } else {
/* Absolute path */ /* Absolute path */
if (rom_present(fn)) { if (rom_present(fn)) {
strncpy(s, fn, size); strncpy(s, fn, size);
retval = 1; return 1;
}
} }
return(retval); return 0;
}
} }

View File

@@ -602,6 +602,6 @@ plat_init_rom_paths()
#endif #endif
for (auto& path : paths) { for (auto& path : paths) {
add_rom_path(QDir(path).filePath("86Box/roms").toUtf8().constData()); rom_add_path(QDir(path).filePath("86Box/roms").toUtf8().constData());
} }
} }

View File

@@ -766,7 +766,7 @@ plat_init_rom_paths()
if (!plat_dir_check(xdg_rom_path)) if (!plat_dir_check(xdg_rom_path))
plat_dir_create(xdg_rom_path); plat_dir_create(xdg_rom_path);
add_rom_path(xdg_rom_path); rom_add_path(xdg_rom_path);
} else { } else {
char home_rom_path[1024] = { 0 }; char home_rom_path[1024] = { 0 };
snprintf(home_rom_path, 1024, "%s/.local/share/86Box/", getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir); snprintf(home_rom_path, 1024, "%s/.local/share/86Box/", getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir);
@@ -777,7 +777,7 @@ plat_init_rom_paths()
if (!plat_dir_check(home_rom_path)) if (!plat_dir_check(home_rom_path))
plat_dir_create(home_rom_path); plat_dir_create(home_rom_path);
add_rom_path(home_rom_path); rom_add_path(home_rom_path);
} }
if (getenv("XDG_DATA_DIRS")) { if (getenv("XDG_DATA_DIRS")) {
char* xdg_rom_paths = strdup(getenv("XDG_DATA_DIRS")); char* xdg_rom_paths = strdup(getenv("XDG_DATA_DIRS"));
@@ -792,18 +792,18 @@ plat_init_rom_paths()
strcat(real_xdg_rom_path, cur_xdg_rom_path); strcat(real_xdg_rom_path, cur_xdg_rom_path);
plat_path_slash(real_xdg_rom_path); plat_path_slash(real_xdg_rom_path);
strcat(real_xdg_rom_path, "86Box/roms/"); strcat(real_xdg_rom_path, "86Box/roms/");
add_rom_path(real_xdg_rom_path); rom_add_path(real_xdg_rom_path);
} }
} }
free(xdg_rom_paths_orig); free(xdg_rom_paths_orig);
} else { } else {
add_rom_path("/usr/local/share/86Box/roms/"); rom_add_path("/usr/local/share/86Box/roms/");
add_rom_path("/usr/share/86Box/roms/"); rom_add_path("/usr/share/86Box/roms/");
} }
#else #else
char default_rom_path[1024] = { '\0 '}; char default_rom_path[1024] = { '\0 '};
getDefaultROMPath(default_rom_path); getDefaultROMPath(default_rom_path);
add_rom_path(default_rom_path); rom_path_add(default_rom_path);
#endif #endif
} }

View File

@@ -932,7 +932,7 @@ plat_init_rom_paths()
CreateDirectoryW(appdata_dir, NULL); CreateDirectoryW(appdata_dir, NULL);
wcscat(appdata_dir, "\\"); wcscat(appdata_dir, "\\");
c16stombs(appdata_dir_a, appdata_dir, 1024); c16stombs(appdata_dir_a, appdata_dir, 1024);
add_rom_path(appdata_dir_a); rom_add_path(appdata_dir_a);
} }
} }