From 3c86a1932f848f694ba3a88bb1af35325f3f89a2 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 31 Jan 2014 03:09:01 +1100 Subject: Code cleanup: add BLI_testextensie_n, replacing multuple calls to BLI_testextensie also use attributes for BLI path functions --- source/blender/blenkernel/BKE_image.h | 1 + source/blender/blenkernel/intern/idcode.c | 7 ++- source/blender/blenkernel/intern/image.c | 90 +++++++++++++-------------- source/blender/blenkernel/intern/sequencer.c | 14 +---- source/blender/blenlib/BLI_path_util.h | 39 +++++++----- source/blender/blenlib/intern/path_util.c | 41 ++++++++++-- source/blender/blenloader/BLO_readfile.h | 7 ++- source/blender/blenloader/intern/readfile.c | 9 ++- source/blender/editors/space_file/filelist.c | 13 +--- source/blender/imbuf/intern/util.c | 13 +--- source/blender/quicktime/apple/qtkit_import.m | 36 ++++++----- 11 files changed, 142 insertions(+), 128 deletions(-) diff --git a/source/blender/blenkernel/BKE_image.h b/source/blender/blenkernel/BKE_image.h index 65540d78154..a44a9e0c7a9 100644 --- a/source/blender/blenkernel/BKE_image.h +++ b/source/blender/blenkernel/BKE_image.h @@ -153,6 +153,7 @@ struct ImBuf *BKE_image_pool_acquire_ibuf(struct Image *ima, struct ImageUser *i void BKE_image_pool_release_ibuf(struct Image *ima, struct ImBuf *ibuf, struct ImagePool *pool); /* set an alpha mode based on file extension */ +char BKE_image_alpha_mode_from_extension_ex(const char *filepath); void BKE_image_alpha_mode_from_extension(struct Image *image); /* returns a new image or NULL if it can't load */ diff --git a/source/blender/blenkernel/intern/idcode.c b/source/blender/blenkernel/intern/idcode.c index c64c261b9b0..c987e0b67cb 100644 --- a/source/blender/blenkernel/intern/idcode.c +++ b/source/blender/blenkernel/intern/idcode.c @@ -129,6 +129,7 @@ bool BKE_idcode_is_valid(int code) bool BKE_idcode_is_linkable(int code) { IDType *idt = idtype_from_code(code); + BLI_assert(idt); return idt ? ((idt->flags & IDTYPE_FLAGS_ISLINKABLE) != 0) : false; } @@ -142,7 +143,7 @@ bool BKE_idcode_is_linkable(int code) const char *BKE_idcode_to_name(int code) { IDType *idt = idtype_from_code(code); - + BLI_assert(idt); return idt ? idt->name : NULL; } @@ -155,7 +156,7 @@ const char *BKE_idcode_to_name(int code) int BKE_idcode_from_name(const char *name) { IDType *idt = idtype_from_name(name); - + BLI_assert(idt); return idt ? idt->code : 0; } @@ -169,7 +170,7 @@ int BKE_idcode_from_name(const char *name) const char *BKE_idcode_to_name_plural(int code) { IDType *idt = idtype_from_code(code); - + BLI_assert(idt); return idt ? idt->plural : NULL; } diff --git a/source/blender/blenkernel/intern/image.c b/source/blender/blenkernel/intern/image.c index 3e93ab0cbcc..322f09f48d5 100644 --- a/source/blender/blenkernel/intern/image.c +++ b/source/blender/blenkernel/intern/image.c @@ -598,20 +598,21 @@ static void image_init_color_management(Image *ima) } } -void BKE_image_alpha_mode_from_extension(Image *image) +char BKE_image_alpha_mode_from_extension_ex(const char *filepath) { - if (BLI_testextensie(image->name, ".exr") || - BLI_testextensie(image->name, ".cin") || - BLI_testextensie(image->name, ".dpx") || - BLI_testextensie(image->name, ".hdr")) - { - image->alpha_mode = IMA_ALPHA_PREMUL; + if (BLI_testextensie_n(filepath, ".exr", ".cin", ".dpx", ".hdr", NULL)) { + return IMA_ALPHA_PREMUL; } else { - image->alpha_mode = IMA_ALPHA_STRAIGHT; + return IMA_ALPHA_STRAIGHT; } } +void BKE_image_alpha_mode_from_extension(Image *image) +{ + image->alpha_mode = BKE_image_alpha_mode_from_extension_ex(image->name); +} + Image *BKE_image_load(Main *bmain, const char *filepath) { Image *ima; @@ -1266,98 +1267,93 @@ char BKE_imtype_from_arg(const char *imtype_arg) static int do_add_image_extension(char *string, const char imtype, const ImageFormatData *im_format) { const char *extension = NULL; + const char *extension_test; (void)im_format; /* may be unused, depends on build options */ if (imtype == R_IMF_IMTYPE_IRIS) { - if (!BLI_testextensie(string, ".rgb")) - extension = ".rgb"; + if (!BLI_testextensie(string, extension_test = ".rgb")) + extension = extension_test; } else if (imtype == R_IMF_IMTYPE_IRIZ) { - if (!BLI_testextensie(string, ".rgb")) - extension = ".rgb"; + if (!BLI_testextensie(string, extension_test = ".rgb")) + extension = extension_test; } #ifdef WITH_HDR else if (imtype == R_IMF_IMTYPE_RADHDR) { - if (!BLI_testextensie(string, ".hdr")) - extension = ".hdr"; + if (!BLI_testextensie(string, extension_test = ".hdr")) + extension = extension_test; } #endif else if (ELEM5(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) { - if (!BLI_testextensie(string, ".png")) - extension = ".png"; + if (!BLI_testextensie(string, extension_test = ".png")) + extension = extension_test; } #ifdef WITH_DDS else if (imtype == R_IMF_IMTYPE_DDS) { - if (!BLI_testextensie(string, ".dds")) - extension = ".dds"; + if (!BLI_testextensie(string, extension_test = ".dds")) + extension = extension_test; } #endif - else if (imtype == R_IMF_IMTYPE_RAWTGA) { - if (!BLI_testextensie(string, ".tga")) - extension = ".tga"; + else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) { + if (!BLI_testextensie(string, extension_test = ".tga")) + extension = extension_test; } else if (imtype == R_IMF_IMTYPE_BMP) { - if (!BLI_testextensie(string, ".bmp")) - extension = ".bmp"; + if (!BLI_testextensie(string, extension_test = ".bmp")) + extension = extension_test; } #ifdef WITH_TIFF else if (imtype == R_IMF_IMTYPE_TIFF) { - if (!BLI_testextensie(string, ".tif") && - !BLI_testextensie(string, ".tiff")) - { - extension = ".tif"; + if (!BLI_testextensie_n(string, extension_test = ".tif", ".tiff", NULL)) { + extension = extension_test; } } #endif #ifdef WITH_OPENIMAGEIO else if (imtype == R_IMF_IMTYPE_PSD) { - if (!BLI_testextensie(string, ".psd")) - extension = ".psd"; + if (!BLI_testextensie(string, extension_test = ".psd")) + extension = extension_test; } #endif #ifdef WITH_OPENEXR else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) { - if (!BLI_testextensie(string, ".exr")) - extension = ".exr"; + if (!BLI_testextensie(string, extension_test = ".exr")) + extension = extension_test; } #endif #ifdef WITH_CINEON else if (imtype == R_IMF_IMTYPE_CINEON) { - if (!BLI_testextensie(string, ".cin")) - extension = ".cin"; + if (!BLI_testextensie(string, extension_test = ".cin")) + extension = extension_test; } else if (imtype == R_IMF_IMTYPE_DPX) { - if (!BLI_testextensie(string, ".dpx")) - extension = ".dpx"; + if (!BLI_testextensie(string, extension_test = ".dpx")) + extension = extension_test; } #endif - else if (imtype == R_IMF_IMTYPE_TARGA) { - if (!BLI_testextensie(string, ".tga")) - extension = ".tga"; - } #ifdef WITH_OPENJPEG else if (imtype == R_IMF_IMTYPE_JP2) { if (im_format) { if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) { - if (!BLI_testextensie(string, ".jp2")) - extension = ".jp2"; + if (!BLI_testextensie(string, extension_test = ".jp2")) + extension = extension_test; } else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) { - if (!BLI_testextensie(string, ".j2c")) - extension = ".j2c"; + if (!BLI_testextensie(string, extension_test = ".j2c")) + extension = extension_test; } else BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec"); } else { - if (!BLI_testextensie(string, ".jp2")) - extension = ".jp2"; + if (!BLI_testextensie(string, extension_test = ".jp2")) + extension = extension_test; } } #endif else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc - if (!(BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg"))) - extension = ".jpg"; + if (!(BLI_testextensie_n(string, extension_test = ".jpg", ".jpeg", NULL))) + extension = extension_test; } if (extension) { diff --git a/source/blender/blenkernel/intern/sequencer.c b/source/blender/blenkernel/intern/sequencer.c index 4c32e3e7654..b261ad5686b 100644 --- a/source/blender/blenkernel/intern/sequencer.c +++ b/source/blender/blenkernel/intern/sequencer.c @@ -4165,18 +4165,8 @@ Sequence *BKE_sequence_alloc(ListBase *lb, int cfra, int machine) void BKE_sequence_alpha_mode_from_extension(Sequence *seq) { if (seq->strip && seq->strip->stripdata) { - char *name = seq->strip->stripdata->name; - - if (BLI_testextensie(name, ".exr") || - BLI_testextensie(name, ".cin") || - BLI_testextensie(name, ".dpx") || - BLI_testextensie(name, ".hdr")) - { - seq->alpha_mode = IMA_ALPHA_PREMUL; - } - else { - seq->alpha_mode = IMA_ALPHA_STRAIGHT; - } + const char *filename = seq->strip->stripdata->name; + seq->alpha_mode = BKE_image_alpha_mode_from_extension_ex(filename); } } 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; } diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h index 58d0cf6d264..b39f247ccd3 100644 --- a/source/blender/blenloader/BLO_readfile.h +++ b/source/blender/blenloader/BLO_readfile.h @@ -194,12 +194,13 @@ BLO_blendhandle_close(BlendHandle *bh); #define GROUP_MAX 32 -int BLO_has_bfile_extension(const char *str); +bool BLO_has_bfile_extension(const char *str); -/* return ok when a blenderfile, in dir is the filename, +/** + * return ok when a blenderfile, in dir is the filename, * in group the type of libdata */ -int BLO_is_a_library(const char *path, char *dir, char *group); +bool BLO_is_a_library(const char *path, char *dir, char *group); /** diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index 29de575e8b8..2a1ca226d31 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -1151,14 +1151,13 @@ void blo_freefiledata(FileData *fd) /* ************ DIV ****************** */ -int BLO_has_bfile_extension(const char *str) +bool BLO_has_bfile_extension(const char *str) { - return (BLI_testextensie(str, ".ble") || - BLI_testextensie(str, ".blend") || - BLI_testextensie(str, ".blend.gz")); + const char *ext_test[4] = {".blend", ".ble", ".blend.gz", NULL}; + return BLI_testextensie_array(str, ext_test); } -int BLO_is_a_library(const char *path, char *dir, char *group) +bool BLO_is_a_library(const char *path, char *dir, char *group) { /* return ok when a blenderfile, in dir is the filename, * in group the type of libdata diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c index 7282b024399..9e674b34d36 100644 --- a/source/blender/editors/space_file/filelist.c +++ b/source/blender/editors/space_file/filelist.c @@ -789,19 +789,10 @@ static int path_extension_type(const char *path) else if (BLI_testextensie(path, ".py")) { return PYSCRIPTFILE; } - else if (BLI_testextensie(path, ".txt") || - BLI_testextensie(path, ".glsl") || - BLI_testextensie(path, ".osl") || - BLI_testextensie(path, ".data")) - { + else if (BLI_testextensie_n(path, ".txt", ".glsl", ".osl", ".data", NULL)) { return TEXTFILE; } - else if (BLI_testextensie(path, ".ttf") || - BLI_testextensie(path, ".ttc") || - BLI_testextensie(path, ".pfb") || - BLI_testextensie(path, ".otf") || - BLI_testextensie(path, ".otc")) - { + else if (BLI_testextensie_n(path, ".ttf", ".ttc", ".pfb", ".otf", ".otc", NULL)) { return FTFONTFILE; } else if (BLI_testextensie(path, ".btx")) { diff --git a/source/blender/imbuf/intern/util.c b/source/blender/imbuf/intern/util.c index 125979c668e..d929304d185 100644 --- a/source/blender/imbuf/intern/util.c +++ b/source/blender/imbuf/intern/util.c @@ -332,16 +332,9 @@ static int isffmpeg(const char *filename) AVCodec *pCodec; AVCodecContext *pCodecCtx; - if (BLI_testextensie(filename, ".swf") || - BLI_testextensie(filename, ".jpg") || - BLI_testextensie(filename, ".png") || - BLI_testextensie(filename, ".dds") || - BLI_testextensie(filename, ".tga") || - BLI_testextensie(filename, ".bmp") || - BLI_testextensie(filename, ".tif") || - BLI_testextensie(filename, ".exr") || - BLI_testextensie(filename, ".cin") || - BLI_testextensie(filename, ".wav")) + if (BLI_testextensie_n( + filename, + ".swf", ".jpg", ".png", ".dds", ".tga", ".bmp", ".tif", ".exr", ".cin", ".wav", NULL)) { return 0; } diff --git a/source/blender/quicktime/apple/qtkit_import.m b/source/blender/quicktime/apple/qtkit_import.m index d0bf7a30e6d..51a9f128f94 100644 --- a/source/blender/quicktime/apple/qtkit_import.m +++ b/source/blender/quicktime/apple/qtkit_import.m @@ -82,22 +82,26 @@ int anim_is_quicktime(const char *name) NSAutoreleasePool *pool; // don't let quicktime movie import handle these - if (BLI_testextensie(name, ".swf") || - BLI_testextensie(name, ".txt") || - BLI_testextensie(name, ".mpg") || - BLI_testextensie(name, ".avi") || // wouldn't be appropriate ;) - BLI_testextensie(name, ".mov") || // disabled, suboptimal decoding speed - BLI_testextensie(name, ".mp4") || // disabled, suboptimal decoding speed - BLI_testextensie(name, ".m4v") || // disabled, suboptimal decoding speed - BLI_testextensie(name, ".tga") || - BLI_testextensie(name, ".png") || - BLI_testextensie(name, ".bmp") || - BLI_testextensie(name, ".jpg") || - BLI_testextensie(name, ".tif") || - BLI_testextensie(name, ".exr") || - BLI_testextensie(name, ".wav") || - BLI_testextensie(name, ".zip") || - BLI_testextensie(name, ".mp3")) + + if (BLI_testextensie_n( + name, + ".swf", + ".txt", + ".mpg", + ".avi", /* wouldn't be appropriate ;) */ + ".mov", /* disabled, suboptimal decoding speed */ + ".mp4", /* disabled, suboptimal decoding speed */ + ".m4v", /* disabled, suboptimal decoding speed */ + ".tga", + ".png", + ".bmp", + ".jpg", + ".tif", + ".exr", + ".wav", + ".zip", + ".mp3", + NULL)) { return 0; } -- cgit v1.2.3