From da146ac7df93a02508c8c42286aa3533d5fda570 Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Sat, 20 Dec 2025 16:06:28 -0300 Subject: [PATCH] Unify ROM and asset path appending into a single function with duplicate checking --- src/mem/rom.c | 90 +++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 50 deletions(-) diff --git a/src/mem/rom.c b/src/mem/rom.c index 5798b4252..c2959e1b3 100644 --- a/src/mem/rom.c +++ b/src/mem/rom.c @@ -55,64 +55,54 @@ rom_log(const char *fmt, ...) # define rom_log(fmt, ...) #endif +static void +add_path(rom_path_t *list, const char *path) +{ + rom_path_t *rom_path = calloc(1, sizeof(rom_path_t)); + + /* Save the path, turning it into absolute if needed. */ + if (!path_abs((char *) path)) { + plat_getcwd(rom_path->path, sizeof(rom_path->path)); + path_append_filename(rom_path->path, rom_path->path, path); + } else { + strncpy(rom_path->path, path, sizeof(rom_path->path) - 1); + } + + /* Ensure the path ends with a separator. */ + path_slash(rom_path->path); + + /* Iterate to the end of the list. */ + if (list->path[0] != '\0') { + while (1) { + /* Check for duplicates. */ + if (!strcmp(list->path, rom_path->path)) { + free(rom_path); + return; + } + if (list->next == NULL) + break; + list = list->next; + } + + /* Add the new entry. */ + list->next = rom_path; + } else { + /* Set path on the first entry. */ + memcpy(list, rom_path, sizeof(rom_path_t)); + free(rom_path); + } +} + void rom_add_path(const char *path) { - char cwd[1024] = { 0 }; - - rom_path_t *rom_path = &rom_paths; - - if (rom_paths.path[0] != '\0') { - // Iterate to the end of the list. - 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 (!path_abs((char *) path)) { - plat_getcwd(cwd, sizeof(cwd)); - path_slash(cwd); - snprintf(rom_path->path, sizeof(rom_path->path), "%s%s", cwd, path); - } else { - snprintf(rom_path->path, sizeof(rom_path->path), "%s", path); - } - - // Ensure the path ends with a separator. - path_slash(rom_path->path); + add_path(&rom_paths, path); } void asset_add_path(const char *path) { - char cwd[1024] = { 0 }; - - rom_path_t *asset_path = &asset_paths; - - if (asset_paths.path[0] != '\0') { - // Iterate to the end of the list. - while (asset_path->next != NULL) { - asset_path = asset_path->next; - } - - // Allocate the new entry. - asset_path = asset_path->next = calloc(1, sizeof(rom_path_t)); - } - - // Save the path, turning it into absolute if needed. - if (!path_abs((char *) path)) { - plat_getcwd(cwd, sizeof(cwd)); - path_slash(cwd); - snprintf(asset_path->path, sizeof(asset_path->path), "%s%s", cwd, path); - } else { - snprintf(asset_path->path, sizeof(asset_path->path), "%s", path); - } - - // Ensure the path ends with a separator. - path_slash(asset_path->path); + add_path(&asset_paths, path); } static int