Merge pull request #10672 from jdgleaver/file-path-optimisations

Various file path handling optimisations
This commit is contained in:
Autechre 2020-05-20 18:21:59 +02:00 committed by GitHub
commit 4713bf5cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 34 deletions

View File

@ -44,7 +44,6 @@
#include <compat/strl.h> #include <compat/strl.h>
#include <compat/posix_string.h> #include <compat/posix_string.h>
#endif #endif
#include <compat/strcasestr.h>
#include <retro_miscellaneous.h> #include <retro_miscellaneous.h>
#include <encodings/utf.h> #include <encodings/utf.h>
@ -124,26 +123,48 @@
*/ */
const char *path_get_archive_delim(const char *path) const char *path_get_archive_delim(const char *path)
{ {
const char *last = find_last_slash(path); const char *last_slash = find_last_slash(path);
const char *delim = NULL; const char *delim = NULL;
char buf[5];
if (!last) buf[0] = '\0';
if (!last_slash)
return NULL; return NULL;
/* Test if it's .zip */ /* Find delimiter position */
delim = strcasestr(last, ".zip#"); delim = strrchr(last_slash, '#');
if (!delim) /* If it's not a .zip, test if it's .apk */ if (!delim)
delim = strcasestr(last, ".apk#"); return NULL;
if (delim) /* Check whether this is a known archive type
return delim + 4; * > Note: The code duplication here is
* deliberate, to maximise performance */
if (delim - last_slash > 4)
{
strlcpy(buf, delim - 4, sizeof(buf));
buf[4] = '\0';
/* If it's not a .zip or .apk file, test if it's .7z */ string_to_lower(buf);
delim = strcasestr(last, ".7z#");
if (delim) /* Check if this is a '.zip', '.apk' or '.7z' file */
return delim + 3; if (string_is_equal(buf, ".zip") ||
string_is_equal(buf, ".apk") ||
string_is_equal(buf + 1, ".7z"))
return delim;
}
else if (delim - last_slash > 3)
{
strlcpy(buf, delim - 3, sizeof(buf));
buf[3] = '\0';
string_to_lower(buf);
/* Check if this is a '.7z' file */
if (string_is_equal(buf, ".7z"))
return delim;
}
return NULL; return NULL;
} }
@ -202,9 +223,12 @@ bool path_is_compressed_file(const char* path)
{ {
const char *ext = path_get_extension(path); const char *ext = path_get_extension(path);
if ( strcasestr(ext, "zip") if (string_is_empty(ext))
|| strcasestr(ext, "apk") return false;
|| strcasestr(ext, "7z"))
if (string_is_equal_noncase(ext, "zip") ||
string_is_equal_noncase(ext, "apk") ||
string_is_equal_noncase(ext, "7z"))
return true; return true;
return false; return false;
@ -1029,10 +1053,11 @@ void fill_pathname_abbreviate_special(char *out_path,
unsigned i; unsigned i;
const char *candidates[3]; const char *candidates[3];
const char *notations[3]; const char *notations[3];
char *application_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); char application_dir[PATH_MAX_LENGTH];
char *home_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); char home_dir[PATH_MAX_LENGTH];
application_dir[0] = '\0'; application_dir[0] = '\0';
home_dir[0] = '\0';
/* application_dir could be zero-string. Safeguard against this. /* application_dir could be zero-string. Safeguard against this.
* *
@ -1049,15 +1074,13 @@ void fill_pathname_abbreviate_special(char *out_path,
notations [1] = "~"; notations [1] = "~";
notations [2] = NULL; notations [2] = NULL;
fill_pathname_application_dir(application_dir, fill_pathname_application_dir(application_dir, sizeof(application_dir));
PATH_MAX_LENGTH * sizeof(char)); fill_pathname_home_dir(home_dir, sizeof(home_dir));
fill_pathname_home_dir(home_dir,
PATH_MAX_LENGTH * sizeof(char));
for (i = 0; candidates[i]; i++) for (i = 0; candidates[i]; i++)
{ {
if (!string_is_empty(candidates[i]) && if (!string_is_empty(candidates[i]) &&
strstr(in_path, candidates[i]) == in_path) string_starts_with(in_path, candidates[i]))
{ {
size_t src_size = strlcpy(out_path, notations[i], size); size_t src_size = strlcpy(out_path, notations[i], size);
@ -1079,8 +1102,6 @@ void fill_pathname_abbreviate_special(char *out_path,
} }
} }
free(application_dir);
free(home_dir);
#endif #endif
retro_assert(strlcpy(out_path, in_path, size) < size); retro_assert(strlcpy(out_path, in_path, size) < size);

View File

@ -45,6 +45,11 @@ static INLINE bool string_is_equal(const char *a, const char *b)
return (a && b) ? !strcmp(a, b) : false; return (a && b) ? !strcmp(a, b) : false;
} }
static INLINE bool string_starts_with(const char *str, const char *prefix)
{
return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false;
}
#define STRLEN_CONST(x) ((sizeof((x))-1)) #define STRLEN_CONST(x) ((sizeof((x))-1))
#define string_is_not_equal(a, b) !string_is_equal((a), (b)) #define string_is_not_equal(a, b) !string_is_equal((a), (b))

View File

@ -3335,14 +3335,12 @@ enum rarch_content_type path_is_media_type(const char *path)
string_to_lower(ext_lower); string_to_lower(ext_lower);
/* hack, to detect livestreams so the ffmpeg core can be started */ /* hack, to detect livestreams so the ffmpeg core can be started */
if ( if (string_starts_with(path, "udp://") ||
strstr(path, "udp://") || string_starts_with(path, "http://") ||
strstr(path, "http://") || string_starts_with(path, "https://") ||
strstr(path, "https://")|| string_starts_with(path, "tcp://") ||
strstr(path, "tcp://") || string_starts_with(path, "rtmp://") ||
strstr(path, "rtmp://") || string_starts_with(path, "rtp://"))
strstr(path, "rtp://")
)
return RARCH_CONTENT_MOVIE; return RARCH_CONTENT_MOVIE;
switch (msg_hash_to_file_type(msg_hash_calculate(ext_lower))) switch (msg_hash_to_file_type(msg_hash_calculate(ext_lower)))