Skip to content

Commit

Permalink
* Use fill_pathname_basedir where possible
Browse files Browse the repository at this point in the history
* Move static variable to only function where it's used
* Change signature of file_path.c function
  • Loading branch information
LibretroAdmin committed Jan 15, 2025
1 parent e0f09f6 commit a5c9d95
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 69 deletions.
9 changes: 3 additions & 6 deletions disk_index_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,9 @@ bool disk_index_file_init(
/* > Get disk index file directory */
if (!string_is_empty(dir_savefile))
strlcpy(disk_index_file_dir, dir_savefile, sizeof(disk_index_file_dir));
else
{
/* Use content directory */
strlcpy(disk_index_file_dir, content_path, sizeof(disk_index_file_dir));
path_basedir(disk_index_file_dir);
}
else /* Use content directory */
fill_pathname_basedir(disk_index_file_dir, content_path,
sizeof(disk_index_file_dir));

/* > Create directory, if required */
if ( !path_is_directory(disk_index_file_dir)
Expand Down
35 changes: 17 additions & 18 deletions gfx/video_shader_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,6 @@ struct wildcard_token
char token_name[24];
};

static struct wildcard_token wildcard_tokens[SHADER_NUM_WILDCARDS] = {
{RARCH_WILDCARD_CONTENT_DIR, "$CONTENT-DIR$"},
{RARCH_WILDCARD_CORE, "$CORE$"},
{RARCH_WILDCARD_GAME, "$GAME$"},
{RARCH_WILDCARD_VIDEO_DRIVER, "$VID-DRV$"},
{RARCH_WILDCARD_VIDEO_DRIVER_PRESET_EXT, "$VID-DRV-PRESET-EXT$"},
{RARCH_WILDCARD_VIDEO_DRIVER_SHADER_EXT, "$VID-DRV-SHADER-EXT$"},
{RARCH_WILDCARD_CORE_REQUESTED_ROTATION, "$CORE-REQ-ROT$"},
{RARCH_WILDCARD_VIDEO_ALLOW_CORE_ROTATION, "$VID-ALLOW-CORE-ROT$"},
{RARCH_WILDCARD_VIDEO_USER_ROTATION, "$VID-USER-ROT$"},
{RARCH_WILDCARD_VIDEO_FINAL_ROTATION, "$VID-FINAL-ROT$"},
{RARCH_WILDCARD_SCREEN_ORIENTATION, "$SCREEN-ORIENT$"},
{RARCH_WILDCARD_VIEWPORT_ASPECT_ORIENTATION, "$VIEW-ASPECT-ORIENT$"},
{RARCH_WILDCARD_CORE_ASPECT_ORIENTATION, "$CORE-ASPECT-ORIENT$"},
{RARCH_WILDCARD_PRESET_DIR, "$PRESET-DIR$"},
{RARCH_WILDCARD_PRESET, "$PRESET$"},
};

/* TODO/FIXME - global state - perhaps move outside this file */
static path_change_data_t *file_change_data = NULL;

Expand Down Expand Up @@ -231,6 +213,23 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_
{
int i = 0;
char replaced_path[PATH_MAX_LENGTH];
static struct wildcard_token wildcard_tokens[SHADER_NUM_WILDCARDS] = {
{RARCH_WILDCARD_CONTENT_DIR, "$CONTENT-DIR$"},
{RARCH_WILDCARD_CORE, "$CORE$"},
{RARCH_WILDCARD_GAME, "$GAME$"},
{RARCH_WILDCARD_VIDEO_DRIVER, "$VID-DRV$"},
{RARCH_WILDCARD_VIDEO_DRIVER_PRESET_EXT, "$VID-DRV-PRESET-EXT$"},
{RARCH_WILDCARD_VIDEO_DRIVER_SHADER_EXT, "$VID-DRV-SHADER-EXT$"},
{RARCH_WILDCARD_CORE_REQUESTED_ROTATION, "$CORE-REQ-ROT$"},
{RARCH_WILDCARD_VIDEO_ALLOW_CORE_ROTATION, "$VID-ALLOW-CORE-ROT$"},
{RARCH_WILDCARD_VIDEO_USER_ROTATION, "$VID-USER-ROT$"},
{RARCH_WILDCARD_VIDEO_FINAL_ROTATION, "$VID-FINAL-ROT$"},
{RARCH_WILDCARD_SCREEN_ORIENTATION, "$SCREEN-ORIENT$"},
{RARCH_WILDCARD_VIEWPORT_ASPECT_ORIENTATION, "$VIEW-ASPECT-ORIENT$"},
{RARCH_WILDCARD_CORE_ASPECT_ORIENTATION, "$CORE-ASPECT-ORIENT$"},
{RARCH_WILDCARD_PRESET_DIR, "$PRESET-DIR$"},
{RARCH_WILDCARD_PRESET, "$PRESET$"},
};

if (!strstr(s, RARCH_WILDCARD_DELIMITER))
return;
Expand Down
25 changes: 11 additions & 14 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,18 @@ size_t path_basedir(char *s)
char *last_slash = NULL;
if (!s || s[0] == '\0' || s[1] == '\0')
return (s && s[0] != '\0') ? 1 : 0;
slash = strrchr(s, '/');
backslash = strrchr(s, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
slash = strrchr(s, '/');
backslash = strrchr(s, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
{
last_slash[1] = '\0';
last_slash[1] = '\0';
return last_slash + 1 - s;
}
else
{
s[0] = '.';
s[1] = PATH_DEFAULT_SLASH_C();
s[2] = '\0';
return 2;
}
s[0] = '.';
s[1] = PATH_DEFAULT_SLASH_C();
s[2] = '\0';
return 2;
}

/**
Expand Down Expand Up @@ -1446,13 +1443,13 @@ size_t fill_pathname_application_path(char *s, size_t len)
return 0;
}

void fill_pathname_application_dir(char *s, size_t len)
size_t fill_pathname_application_dir(char *s, size_t len)
{
#ifdef __WINRT__
strlcpy(s, uwp_dir_install, len);
return strlcpy(s, uwp_dir_install, len);
#else
fill_pathname_application_path(s, len);
path_basedir(s);
return path_basedir(s);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion libretro-common/include/file/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ size_t fill_pathname_slash(char *path, size_t size);

#if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL)
size_t fill_pathname_application_path(char *buf, size_t size);
void fill_pathname_application_dir(char *buf, size_t size);
size_t fill_pathname_application_dir(char *buf, size_t size);
size_t fill_pathname_home_dir(char *buf, size_t size);
#endif

Expand Down
8 changes: 5 additions & 3 deletions menu/cbs/menu_cbs_ok.c
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,12 @@ int generic_action_ok_displaylist_push(
{
filebrowser_clear_type();
if (content_get_subsystem_rom_id() > 0)
strlcpy(tmp, content_get_subsystem_rom(content_get_subsystem_rom_id() - 1), sizeof(tmp));
fill_pathname_basedir(tmp,
content_get_subsystem_rom(content_get_subsystem_rom_id() - 1),
sizeof(tmp));
else
strlcpy(tmp, path_get(RARCH_PATH_CONTENT), sizeof(tmp));
path_basedir(tmp);
fill_pathname_basedir(tmp,
path_get(RARCH_PATH_CONTENT), sizeof(tmp));

if (content_get_subsystem() != (int)type - MENU_SETTINGS_SUBSYSTEM_ADD)
content_clear_subsystem();
Expand Down
16 changes: 12 additions & 4 deletions menu/menu_displaylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,9 @@ static int filebrowser_parse(
if (str_list->size <= 0)
{
char dir[DIR_MAX_LENGTH];
fill_pathname_application_dir(dir, sizeof(dir));
size_t _len = fill_pathname_application_dir(dir, sizeof(dir));
if (string_ends_with(full_path, "/") && !string_ends_with(dir, "/"))
strlcat(dir, "/", sizeof(dir));
strlcpy(dir + _len, "/", sizeof(dir) - _len);
if (string_is_equal(dir, full_path))
allow_parent_directory = false;
else
Expand Down Expand Up @@ -5139,13 +5139,21 @@ static unsigned menu_displaylist_parse_content_information(
if ( !string_is_empty(content_label)
&& !string_is_empty(db_name))
{
char *last;
char db_path[PATH_MAX_LENGTH];
fill_pathname_join_special(db_path,
settings->paths.path_content_database,
db_name,
sizeof(db_path));
path_remove_extension(db_path);
strlcat(db_path, ".rdb", sizeof(db_path));
last = (char*)strrchr(path_basename(db_path), '.');
if (*last)
{
last[0] = '.';
last[1] = 'r';
last[2] = 'd';
last[3] = 'b';
last[4] = '\0';
}

if (path_is_valid(db_path))
if (menu_entries_append(info_list,
Expand Down
12 changes: 4 additions & 8 deletions menu/menu_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2985,16 +2985,12 @@ static bool menu_shader_manager_save_preset_internal(
fill_pathname_join(buffer, target_dirs[i],
fullname, sizeof(buffer));

strlcpy(basedir, buffer, sizeof(basedir));
path_basedir(basedir);
fill_pathname_basedir(basedir, buffer, sizeof(basedir));

if (!path_is_directory(basedir))
if (!path_is_directory(basedir) && !(ret = path_mkdir(basedir)))
{
if (!(ret = path_mkdir(basedir)))
{
RARCH_WARN("[Shaders]: Failed to create preset directory \"%s\".\n", basedir);
continue;
}
RARCH_WARN("[Shaders]: Failed to create preset directory \"%s\".\n", basedir);
continue;
}

preset_path = buffer;
Expand Down
15 changes: 7 additions & 8 deletions runloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -7928,10 +7928,10 @@ void runloop_path_set_redirect(settings_t *settings,
if ( string_is_empty(intermediate_savefile_dir)
|| savefiles_in_content_dir)
{
strlcpy(intermediate_savefile_dir,
runloop_st->runtime_content_path_basename,
sizeof(intermediate_savefile_dir));
path_basedir(intermediate_savefile_dir);
fill_pathname_basedir(
intermediate_savefile_dir,
runloop_st->runtime_content_path_basename,
sizeof(intermediate_savefile_dir));

if (string_is_empty(intermediate_savefile_dir))
RARCH_LOG("Cannot resolve save file path.\n");
Expand All @@ -7941,10 +7941,9 @@ void runloop_path_set_redirect(settings_t *settings,
if ( string_is_empty(intermediate_savestate_dir)
|| savestates_in_content_dir)
{
strlcpy(intermediate_savestate_dir,
runloop_st->runtime_content_path_basename,
sizeof(intermediate_savestate_dir));
path_basedir(intermediate_savestate_dir);
fill_pathname_basedir(intermediate_savestate_dir,
runloop_st->runtime_content_path_basename,
sizeof(intermediate_savestate_dir));

if (string_is_empty(intermediate_savestate_dir))
RARCH_LOG("Cannot resolve save state file path.\n");
Expand Down
8 changes: 4 additions & 4 deletions tasks/task_content.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ static bool content_file_list_set_info(
{
char archive_path[PATH_MAX_LENGTH];
size_t _len = 0;

file_info->file_in_archive = true;

/* Extract path of parent archive */
if ((_len = (size_t)(1 + archive_delim - path))
>= PATH_MAX_LENGTH)
Expand All @@ -582,9 +585,6 @@ static bool content_file_list_set_info(
* this is the basename of the archive file without
* extension */
fill_pathname_base(name, archive_path, sizeof(name));
path_remove_extension(name);

file_info->file_in_archive = true;
}
else
{
Expand All @@ -596,8 +596,8 @@ static bool content_file_list_set_info(
* is the basename of the content file, without
* extension */
fill_pathname_base(name, path, sizeof(name));
path_remove_extension(name);
}
path_remove_extension(name);

if (!string_is_empty(dir))
{
Expand Down
4 changes: 1 addition & 3 deletions tasks/task_translation.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ static const char *ai_service_get_str(enum translation_lang id)
bool run_translation_service(settings_t *settings, bool paused)
{
struct video_viewport vp;
uint8_t header[54];
size_t pitch;
unsigned width, height;
const void *data = NULL;
Expand Down Expand Up @@ -947,11 +946,10 @@ bool run_translation_service(settings_t *settings, bool paused)
the BMP header as bytes, and then covert that to a
b64 encoded array for transport in JSON.
*/
form_bmp_header(header, width, height, false);
if (!(bmp_buffer = (uint8_t*)malloc(width * height * 3 + 54)))
goto finish;

memcpy(bmp_buffer, header, 54 * sizeof(uint8_t));
form_bmp_header(bmp_buffer, width, height, false);
memcpy(bmp_buffer + 54,
bit24_image,
width * height * 3 * sizeof(uint8_t));
Expand Down

0 comments on commit a5c9d95

Please sign in to comment.