Cleanup: Move blend file path utilities out of blenloader module
These are not really about reading or writing .blend files, they are general utilities for file-names/paths. Having to link to the blendloader library just for these utilities is annoying. Moved them to `BKE_blendfile.h` now, in agreement with Bastien. Pull Request: #105825
This commit is contained in:
@@ -19,6 +19,27 @@ struct ReportList;
|
||||
struct UserDef;
|
||||
struct bContext;
|
||||
|
||||
/**
|
||||
* Check whether given path ends with a blend file compatible extension
|
||||
* (`.blend`, `.ble` or `.blend.gz`).
|
||||
*
|
||||
* \param str: The path to check.
|
||||
* \return true is this path ends with a blender file extension.
|
||||
*/
|
||||
bool BKE_has_bfile_extension(const char *str);
|
||||
/**
|
||||
* Try to explode given path into its 'library components'
|
||||
* (i.e. a .blend file, id type/group, and data-block itself).
|
||||
*
|
||||
* \param path: the full path to explode.
|
||||
* \param r_dir: the string that'll contain path up to blend file itself ('library' path).
|
||||
* WARNING! Must be #FILE_MAX_LIBEXTRA long (it also stores group and name strings)!
|
||||
* \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 BKE_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name);
|
||||
|
||||
/**
|
||||
* Shared setup function that makes the data from `bfd` into the current blend file,
|
||||
* replacing the contents of #G.main.
|
||||
|
||||
@@ -62,6 +62,78 @@
|
||||
# include "BPY_extern.h"
|
||||
#endif
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Blend/Library Paths
|
||||
* \{ */
|
||||
|
||||
bool BKE_has_bfile_extension(const char *str)
|
||||
{
|
||||
const char *ext_test[4] = {".blend", ".ble", ".blend.gz", nullptr};
|
||||
return BLI_path_extension_check_array(str, ext_test);
|
||||
}
|
||||
|
||||
bool BKE_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
|
||||
{
|
||||
/* We might get some data names with slashes,
|
||||
* so we have to go up in path until we find blend file itself,
|
||||
* then we know next path item is group, and everything else is data name. */
|
||||
char *slash = nullptr, *prev_slash = nullptr, c = '\0';
|
||||
|
||||
r_dir[0] = '\0';
|
||||
if (r_group) {
|
||||
*r_group = nullptr;
|
||||
}
|
||||
if (r_name) {
|
||||
*r_name = nullptr;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
while ((slash = (char *)BLI_path_slash_rfind(r_dir))) {
|
||||
char tc = *slash;
|
||||
*slash = '\0';
|
||||
if (BKE_has_bfile_extension(r_dir) && BLI_is_file(r_dir)) {
|
||||
break;
|
||||
}
|
||||
if (STREQ(r_dir, BLO_EMBEDDED_STARTUP_BLEND)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (prev_slash) {
|
||||
*prev_slash = c;
|
||||
}
|
||||
prev_slash = slash;
|
||||
c = tc;
|
||||
}
|
||||
|
||||
if (!slash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Blend File IO (High Level)
|
||||
* \{ */
|
||||
|
||||
Reference in New Issue
Block a user