Skip to content

Commit

Permalink
Cleanups/less string copies and indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Dec 24, 2024
1 parent 2cfdccc commit 8074a83
Show file tree
Hide file tree
Showing 25 changed files with 226 additions and 285 deletions.
14 changes: 6 additions & 8 deletions disk_index_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,13 @@ bool disk_index_file_init(
}

/* > Create directory, if required */
if (!path_is_directory(disk_index_file_dir))
if ( !path_is_directory(disk_index_file_dir)
&& !path_mkdir(disk_index_file_dir))
{
if (!path_mkdir(disk_index_file_dir))
{
RARCH_ERR(
"[disk index file] failed to create directory for disk index file: %s\n",
disk_index_file_dir);
goto error;
}
RARCH_ERR(
"[disk index file] failed to create directory for disk index file: %s\n",
disk_index_file_dir);
goto error;
}

/* > Generate final path */
Expand Down
7 changes: 1 addition & 6 deletions gfx/gfx_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,6 @@ bool gfx_display_reset_icon_texture(
uintptr_t *item, enum texture_filter_type filter_type,
unsigned *width, unsigned *height)
{
char texpath[PATH_MAX_LENGTH];
struct texture_image ti;

ti.width = 0;
Expand All @@ -1142,15 +1141,11 @@ bool gfx_display_reset_icon_texture(

if (string_is_empty(texture_path))
return false;

strlcpy(texpath, texture_path, sizeof(texpath));

if (!image_texture_load(&ti, texpath))
if (!image_texture_load(&ti, texture_path))
return false;

if (width)
*width = ti.width;

if (height)
*height = ti.height;

Expand Down
22 changes: 14 additions & 8 deletions gfx/video_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ bool video_driver_translate_coord_viewport(
* This should make it possible to hit the edges on all *
* screen resolutions, even when pointer cannot be *
* moved offscreen. */

if (mouse_x > 0 && mouse_x < norm_full_vp_width)
scaled_screen_x = ((mouse_x * 0xffff)
/ (norm_full_vp_width - 1)) - 0x8000;
Expand Down Expand Up @@ -3245,10 +3245,10 @@ float video_driver_get_refresh_rate(void)
return 0.0f;
}

void video_driver_set_gpu_api_version_string(const char *str)
size_t video_driver_set_gpu_api_version_string(const char *str)
{
video_driver_state_t *video_st = &video_driver_st;
strlcpy(video_st->gpu_api_version_string, str,
return strlcpy(video_st->gpu_api_version_string, str,
sizeof(video_st->gpu_api_version_string));
}

Expand Down Expand Up @@ -3857,7 +3857,10 @@ void video_driver_frame(const void *data, unsigned width,
status_text[++buf_pos] = ' ';
status_text[++buf_pos] = '\0';
buf_pos += snprintf(
status_text + buf_pos, sizeof(status_text) - buf_pos, "%.2f/%.2f", last_used_memory / (1024.0f * 1024.0f),
status_text + buf_pos,
sizeof(status_text) - buf_pos,
"%.2f/%.2f",
last_used_memory / (1024.0f * 1024.0f),
last_total_memory / (1024.0f * 1024.0f));
status_text[buf_pos ] = 'M';
status_text[++buf_pos] = 'B';
Expand Down Expand Up @@ -3964,7 +3967,8 @@ void video_driver_frame(const void *data, unsigned width,
if (
(
#ifdef HAVE_VIDEO_FILTER
!video_st->state_filter ||
!video_st->state_filter
||
#endif
!video_info.post_filter_record
|| !data
Expand Down Expand Up @@ -4125,7 +4129,7 @@ void video_driver_frame(const void *data, unsigned width,
" - Reserve: %5.2f ms\n"
" Frame Delay: %2u.00 ms\n"
" - Target: %2u.00 ms\n",
runloop_st->core_run_time / 1000.0f,
runloop_st->core_run_time / 1000.0f,
(1000.0f / video_info.refresh_rate) - video_st->frame_delay_effective - (runloop_st->core_run_time / 1000.0f),
video_st->frame_time_reserve / 1000.0f,
video_st->frame_delay_effective,
Expand Down Expand Up @@ -4224,10 +4228,12 @@ void video_driver_frame(const void *data, unsigned width,
video_st->frame_count, (unsigned)pitch,
#if HAVE_MENU
((video_info.menu_st_flags & MENU_ST_FLAG_SCREENSAVER_ACTIVE) > 0)
|| video_info.notifications_hidden ? "" : video_driver_msg,
|| video_info.notifications_hidden
#else
video_info.notifications_hidden ? "" : video_driver_msg,
video_info.notifications_hidden
#endif
? ""
: video_driver_msg,
&video_info))
video_st->flags |= VIDEO_FLAG_ACTIVE;
else
Expand Down
2 changes: 1 addition & 1 deletion gfx/video_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ bool video_driver_test_all_flags(enum display_flags testflag);

gfx_ctx_flags_t video_driver_get_flags_wrapper(void);

void video_driver_set_gpu_api_version_string(const char *str);
size_t video_driver_set_gpu_api_version_string(const char *str);

const char* video_driver_get_gpu_api_version_string(void);

Expand Down
2 changes: 0 additions & 2 deletions gfx/video_shader_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,6 @@ static bool video_shader_parse_textures(config_file_t *conf,
bool smooth = false;
struct config_entry_list *entry = NULL;

idx[0] = '\0';

if ( !(entry = config_get_entry(conf, id))
|| string_is_empty(entry->value))
{
Expand Down
12 changes: 5 additions & 7 deletions libretro-common/dynamic/dylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ static char last_dyn_error[512];
static void set_dl_error(void)
{
DWORD err = GetLastError();

if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
if (FormatMessage(
FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
last_dyn_error,
sizeof(last_dyn_error) - 1,
last_dyn_error, sizeof(last_dyn_error) - 1,
NULL) == 0)
snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
"unknown error %lu", err);
Expand Down
6 changes: 3 additions & 3 deletions libretro-common/file/archive_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ bool file_archive_extract_file(
const char *archive_path,
const char *valid_exts,
const char *extraction_directory,
char *out_path, size_t len)
char *s, size_t len)
{
struct archive_extract_userdata userdata;
bool ret = true;
Expand Down Expand Up @@ -377,7 +377,7 @@ bool file_archive_extract_file(
}

if (!string_is_empty(userdata.first_extracted_file_path))
strlcpy(out_path, userdata.first_extracted_file_path, len);
strlcpy(s, userdata.first_extracted_file_path, len);

end:
if (userdata.first_extracted_file_path)
Expand Down Expand Up @@ -455,8 +455,8 @@ bool file_archive_perform_mode(const char *path, const char *valid_exts,
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, struct archive_extract_userdata *userdata)
{
file_archive_file_handle_t handle;
int ret;
file_archive_file_handle_t handle;

if (!userdata->transfer || !userdata->transfer->backend)
return false;
Expand Down
30 changes: 16 additions & 14 deletions libretro-common/file/archive_file_7z.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,16 @@ static int sevenzip_parse_file_init(file_archive_transfer_t *state,
}

static int sevenzip_parse_file_iterate_step_internal(
struct sevenzip_context_t *sevenzip_context, char *filename,
const uint8_t **cdata, unsigned *cmode,
uint32_t *size, uint32_t *csize, uint32_t *checksum,
unsigned *payback, struct archive_extract_userdata *userdata)
struct sevenzip_context_t *sevenzip_context,
char *s,
size_t len,
const uint8_t **cdata,
unsigned *cmode,
uint32_t *size,
uint32_t *csize,
uint32_t *checksum,
unsigned *payback,
struct archive_extract_userdata *userdata)
{
if (sevenzip_context->parse_index < sevenzip_context->db.NumFiles)
{
Expand All @@ -431,33 +437,28 @@ static int sevenzip_parse_file_iterate_step_internal(
sevenzip_context->packIndex++;
}

if (len < PATH_MAX_LENGTH &&
!SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index))
if ( (len < PATH_MAX_LENGTH)
&& !SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index))
{
char infile[PATH_MAX_LENGTH];
SRes res = SZ_ERROR_FAIL;
uint16_t *temp = (uint16_t*)malloc(len * sizeof(uint16_t));
SRes res = SZ_ERROR_FAIL;
uint16_t *temp = (uint16_t*)malloc(len * sizeof(uint16_t));

if (!temp)
return -1;

infile[0] = '\0';

SzArEx_GetFileNameUtf16(&sevenzip_context->db, sevenzip_context->parse_index,
temp);

if (temp)
{
res = utf16_to_char_string(temp, infile, sizeof(infile))
res = utf16_to_char_string(temp, s, len)
? SZ_OK : SZ_ERROR_FAIL;
free(temp);
}

if (res != SZ_OK)
return -1;

strlcpy(filename, infile, PATH_MAX_LENGTH);

*cmode = 0; /* unused for 7zip */
*checksum = sevenzip_context->db.CRCs.Vals[sevenzip_context->parse_index];
*size = (uint32_t)SzArEx_GetFileSize(&sevenzip_context->db, sevenzip_context->parse_index);
Expand Down Expand Up @@ -491,6 +492,7 @@ static int sevenzip_parse_file_iterate_step(void *context,

ret = sevenzip_parse_file_iterate_step_internal(sevenzip_context,
userdata->current_file_path,
sizeof(userdata->current_file_path),
&cdata, &cmode, &size, &csize,
&checksum, &payload, userdata);

Expand Down
8 changes: 3 additions & 5 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ size_t path_relative_to(char *s,
if (path[i] == PATH_DEFAULT_SLASH_C())
j = i + 1;

trimmed_path = path+j;
trimmed_base = base+i;
trimmed_path = path + j;
trimmed_base = base + i;

/* Each segment of base turns into ".." */
s[0] = '\0';
Expand Down Expand Up @@ -935,9 +935,7 @@ void fill_pathname_resolve_relative(char *s,
return;
}

if (s != in_refpath)
strlcpy(s, in_refpath, len);
path_basedir(s);
fill_pathname_basedir(s, in_refpath, len);
strlcat(s, in_path, len);
path_resolve_realpath(s, len, false);
}
Expand Down
15 changes: 9 additions & 6 deletions libretro-common/formats/json/rjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ static enum rjson_type _rjson_error_char(rjson_t *json,
char buf[16];
if (json->stack_top->type == RJSON_ERROR)
return RJSON_ERROR;
snprintf(buf, sizeof(buf),
(chr == _rJSON_EOF ? "end of stream" :
(chr >= ' ' && chr <= '~' ? "'%c'" : "byte 0x%02X")), chr);
if (chr == _rJSON_EOF)
strlcpy(buf, "end of stream", sizeof(buf));
else if (chr >= ' ' && chr <= '~')
snprintf(buf, sizeof(buf), "'%c'", chr);
else
snprintf(buf, sizeof(buf), "byte 0x%02X", chr);
return _rjson_error(json, fmt, buf);
}

Expand Down Expand Up @@ -455,7 +458,7 @@ static enum rjson_type _rjson_read_string(rjson_t *json)
case 'n':
esc = '\n';
goto escape_pushchar;
case 'r':
case 'r':
if (!(json->option_flags & RJSON_OPTION_IGNORE_STRING_CARRIAGE_RETURN))
{
esc = '\r';
Expand Down Expand Up @@ -573,7 +576,7 @@ static enum rjson_type _rjson_read_number(rjson_t *json)
goto invalid_number;
if (*p < '0' || *p > '9')
goto invalid_number;
do
do
{
if (++p == end)
return RJSON_NUMBER;
Expand Down Expand Up @@ -988,7 +991,7 @@ void rjson_set_max_depth(rjson_t *json, unsigned int max_depth)

const char *rjson_get_string(rjson_t *json, size_t *length)
{
char* str = (json->string_pass_through
char* str = (json->string_pass_through
? json->string_pass_through : json->string);
if (length)
*length = json->string_len;
Expand Down
13 changes: 2 additions & 11 deletions libretro-common/formats/m3u/m3u_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,6 @@ bool m3u_file_save(
m3u_file_t *m3u_file, enum m3u_file_label_type label_type)
{
size_t i;
const char *slash;
const char *backslash;
char base_dir[DIR_MAX_LENGTH];
char *last_slash = NULL;
RFILE *file = NULL;
Expand All @@ -490,16 +488,9 @@ bool m3u_file_save(
if (string_is_empty(m3u_file->path))
return false;

slash = strrchr(m3u_file->path, '/');
backslash = strrchr(m3u_file->path, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;

/* Get M3U file base directory */
if (last_slash)
{
strlcpy(base_dir, m3u_file->path, sizeof(base_dir));
path_basedir(base_dir);
}
if ((last_slash = find_last_slash(m3u_file->path)))
fill_pathname_basedir(base_dir, m3u_file->path, sizeof(base_dir));
else
base_dir[0] = '\0';

Expand Down
Loading

0 comments on commit 8074a83

Please sign in to comment.