From 2735f6fda66076c34f853afaccfc4dd78f66ca1f Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 18 Aug 2015 13:18:50 +0200 Subject: Replace 'BLO_is_a_library' by 'BLO_library_path_explode'. This new func will be fully used by upcomming code (it mostly adds the extraction of library item name as well as library file and ID group). --- source/blender/blenloader/BLO_readfile.h | 9 ++-- source/blender/blenloader/intern/readfile.c | 83 +++++++++++++++++------------ 2 files changed, 54 insertions(+), 38 deletions(-) (limited to 'source/blender/blenloader') diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h index 6fdcf7065c3..5f881c0855c 100644 --- a/source/blender/blenloader/BLO_readfile.h +++ b/source/blender/blenloader/BLO_readfile.h @@ -195,10 +195,13 @@ BLO_blendhandle_close(BlendHandle *bh); bool BLO_has_bfile_extension(const char *str); /** - * return ok when a blenderfile, in dir is the filename, - * in group the type of libdata + * \param path the full path to explode. + * \param r_dir the string that'll contain path up to blend file itself ('library' path). + * \param r_group the string that'll contain 'group' part of the path, if any. May be NULL. + * \param r_name the string that'll contain data's name part of the path, if any. May be NULL. + * \return true if path contains a blend file. */ -bool BLO_is_a_library(const char *path, char *dir, char *group); +bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name); /** diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index 32b736bc687..6581fb3f3b7 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -1234,47 +1234,60 @@ bool BLO_has_bfile_extension(const char *str) return BLI_testextensie_array(str, ext_test); } -bool BLO_is_a_library(const char *path, char *dir, char *group) +bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name) { - /* return ok when a blenderfile, in dir is the filename, - * in group the type of libdata - */ - int len; - char *fd; - - /* if path leads to a directory we can be sure we're not in a library */ - if (BLI_is_dir(path)) return 0; + /* We might get some data names with slashes, so we have to go up in path until we find blend file itself, + * then we now next path item is group, and everything else is data name. */ + char *slash = NULL, *prev_slash = NULL, c = '\0'; - strcpy(dir, path); - len = strlen(dir); - if (len < 7) return 0; - if ((dir[len - 1] != '/') && (dir[len - 1] != '\\')) return 0; - - group[0] = '\0'; - dir[len - 1] = '\0'; + r_dir[0] = '\0'; + if (r_group) { + *r_group = NULL; + } + if (r_name) { + *r_name = NULL; + } + + /* if path leads to an existing directory, we can be sure we're not (in) a library */ + if (BLI_is_dir(path)) { + return false; + } + + strcpy(r_dir, path); - /* Find the last slash */ - fd = (char *)BLI_last_slash(dir); + while ((slash = (char *)BLI_last_slash(r_dir))) { + char tc = *slash; + *slash = '\0'; + if (BLO_has_bfile_extension(r_dir)) { + break; + } - if (fd == NULL) return 0; - *fd = 0; - if (BLO_has_bfile_extension(fd+1)) { - /* the last part of the dir is a .blend file, no group follows */ - *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */ + if (prev_slash) { + *prev_slash = c; + } + prev_slash = slash; + c = tc; } - else { - const char * const gp = fd + 1; // in case we have a .blend file, gp points to the group - - /* Find the last slash */ - fd = (char *)BLI_last_slash(dir); - if (!fd || !BLO_has_bfile_extension(fd+1)) return 0; - - /* now we know that we are in a blend file and it is safe to - * assume that gp actually points to a group */ - if (!STREQ("Screen", gp)) - BLI_strncpy(group, gp, BLO_GROUP_MAX); + + if (!slash) { + return false; } - return 1; + + if (slash[1] != '\0') { + BLI_assert(strlen(slash + 1) < BLO_GROUP_MAX); + if (r_group) { + *r_group = slash + 1; + } + } + + if (prev_slash && (prev_slash[1] != '\0')) { + BLI_assert(strlen(prev_slash + 1) < MAX_ID_NAME - 2); + if (r_name) { + *r_name = prev_slash + 1; + } + } + + return true; } /* ************** OLD POINTERS ******************* */ -- cgit v1.2.3