diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-01-30 20:09:01 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-01-30 20:30:05 +0400 |
commit | 3c86a1932f848f694ba3a88bb1af35325f3f89a2 (patch) | |
tree | 2b2fa7c9ea70436f5e650673c12300290dfd0f34 /source/blender/blenlib | |
parent | 0cb49286ce45f1b2ce16db0e174170ef2a671dba (diff) |
Code cleanup: add BLI_testextensie_n, replacing multuple calls to BLI_testextensie
also use attributes for BLI path functions
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 39 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 41 |
2 files changed, 59 insertions, 21 deletions
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 0cffa286014..2b06b8b9bb4 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -82,8 +82,8 @@ const char *BLI_get_folder_version(const int id, const int ver, const bool do_ch #define ALTSEP '\\' #endif -void BLI_setenv(const char *env, const char *val); -void BLI_setenv_if_new(const char *env, const char *val); +void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1); +void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1); void BLI_make_file_string(const char *relabase, char *string, const char *dir, const char *file); void BLI_make_exist(char *dir); @@ -95,7 +95,7 @@ void BLI_path_append(char *__restrict dst, const size_t maxlen, const char *__restrict file) ATTR_NONNULL(); void BLI_join_dirfile(char *__restrict string, const size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL(); -const char *BLI_path_basename(const char *path); +const char *BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; typedef enum bli_rebase_state { BLI_REBASE_NO_SRCDIR = 0, @@ -105,18 +105,19 @@ typedef enum bli_rebase_state { int BLI_rebase_path(char *abs, size_t abs_len, char *rel, size_t rel_len, const char *base_dir, const char *src_dir, const char *dest_dir); -const char *BLI_last_slash(const char *string); -int BLI_add_slash(char *string); -void BLI_del_slash(char *string); -const char *BLI_first_slash(const char *string); +const char *BLI_last_slash(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; +int BLI_add_slash(char *string) ATTR_NONNULL(); +void BLI_del_slash(char *string) ATTR_NONNULL(); +const char *BLI_first_slash(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; void BLI_getlastdir(const char *dir, char *last, const size_t maxlen); -bool BLI_testextensie(const char *str, const char *ext); -bool BLI_testextensie_array(const char *str, const char **ext_array); -bool BLI_testextensie_glob(const char *str, const char *ext_fnmatch); -bool BLI_replace_extension(char *path, size_t maxlen, const char *ext); -bool BLI_ensure_extension(char *path, size_t maxlen, const char *ext); -bool BLI_ensure_filename(char *filepath, size_t maxlen, const char *filename); +bool BLI_testextensie(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; +bool BLI_testextensie_n(const char *str, ...) ATTR_NONNULL(1) ATTR_SENTINEL(0); +bool BLI_testextensie_array(const char *str, const char **ext_array) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; +bool BLI_testextensie_glob(const char *str, const char *ext_fnmatch) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; +bool BLI_replace_extension(char *path, size_t maxlen, const char *ext) ATTR_NONNULL(); +bool BLI_ensure_extension(char *path, size_t maxlen, const char *ext) ATTR_NONNULL(); +bool BLI_ensure_filename(char *filepath, size_t maxlen, const char *filename) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; void BLI_uniquename(struct ListBase *list, void *vlink, const char *defname, char delim, int name_offs, int len); bool BLI_uniquename_cb(bool (*unique_check)(void *arg, const char *name), void *arg, const char *defname, char delim, char *name, int name_len); @@ -133,12 +134,16 @@ void BLI_clean(char *path) ATTR_NONNULL(); * converts it to a regular full path. * Also removes garbage from directory paths, like /../ or double slashes etc */ -void BLI_cleanup_file(const char *relabase, char *path); /* removes trailing slash */ -void BLI_cleanup_dir(const char *relabase, char *dir); /* same as above but adds a trailing slash */ -void BLI_cleanup_path(const char *relabase, char *path); /* doesn't touch trailing slash */ + +/* removes trailing slash */ +void BLI_cleanup_file(const char *relabase, char *path) ATTR_NONNULL(2); +/* same as above but adds a trailing slash */ +void BLI_cleanup_dir(const char *relabase, char *dir) ATTR_NONNULL(2); +/* doesn't touch trailing slash */ +void BLI_cleanup_path(const char *relabase, char *path) ATTR_NONNULL(2); /* go back one directory */ -bool BLI_parent_dir(char *path); +bool BLI_parent_dir(char *path) ATTR_NONNULL(); /* return whether directory is root and thus has no parent dir */ bool BLI_has_parent(char *path); diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 422440d8b09..58ff67aa612 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1543,20 +1543,53 @@ void BLI_make_file_string(const char *relabase, char *string, const char *dir, c BLI_clean(string); } +static bool testextensie_ex(const char *str, const size_t str_len, + const char *ext, const size_t ext_len) +{ + BLI_assert(strlen(str) == str_len); + BLI_assert(strlen(ext) == ext_len); + + return (((str_len == 0 || ext_len == 0 || ext_len >= str_len) == 0) && + (BLI_strcasecmp(ext, str + str_len - ext_len) == 0)); +} + /* does str end with ext. */ bool BLI_testextensie(const char *str, const char *ext) { - const size_t a = strlen(str); - const size_t b = strlen(ext); - return !(a == 0 || b == 0 || b >= a) && (BLI_strcasecmp(ext, str + a - b) == 0); + return testextensie_ex(str, strlen(str), ext, strlen(ext)); +} + +bool BLI_testextensie_n(const char *str, ...) +{ + const size_t str_len = strlen(str); + + va_list args; + const char *ext; + bool ret = false; + + va_start(args, str); + + while ((ext = (const char *) va_arg(args, void *))) { + if (testextensie_ex(str, str_len, ext, strlen(ext))) { + ret = true; + goto finally; + } + } + +finally: + va_end(args); + + return ret; } /* does str end with any of the suffixes in *ext_array. */ bool BLI_testextensie_array(const char *str, const char **ext_array) { + const size_t str_len = strlen(str); int i = 0; + while (ext_array[i]) { - if (BLI_testextensie(str, ext_array[i])) { + if (testextensie_ex(str, str_len, ext_array[i], strlen(ext_array[i]))) { return true; } |