diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2018-08-23 09:37:32 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2018-08-23 09:46:07 +0300 |
commit | faafe6e6bc505c91324e84abf49eadd463a77afe (patch) | |
tree | 1a6b2a009d6f61f0dfd02fa280133d7fad75aa8c /source/blender/blenkernel | |
parent | 922ca68e17f2fe15fbe9872cb7297fecd7dafb1a (diff) |
Add helper to BKE_library to generate unique string key for an ID.
Basically just concatenates ID's name (including its IDtype code) and
that library's name, if any. This must give unique string in a given
Main database, suitable for GHash keys e.g.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_library.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/library.c | 19 |
2 files changed, 21 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h index 16e05cd136a..50447e02fe5 100644 --- a/source/blender/blenkernel/BKE_library.h +++ b/source/blender/blenkernel/BKE_library.h @@ -201,6 +201,8 @@ void BKE_main_lib_objects_recalc_all(struct Main *bmain); /* (MAX_ID_NAME - 2) + 3 */ void BKE_id_ui_prefix(char name[66 + 1], const struct ID *id); +char *BKE_id_to_unique_string_key(const struct ID *id); + void BKE_library_free(struct Library *lib); void BKE_library_make_local( diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index 08d68f4a480..06207eabe31 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -2535,6 +2535,25 @@ void BKE_id_ui_prefix(char name[MAX_ID_NAME + 1], const ID *id) strcpy(name + 3, id->name + 2); } +/** + * Returns an allocated string concatenating ID name (including two-chars type code) and its lib name if any, + * which is expected to be unique in a given Main database.. + */ +char *BKE_id_to_unique_string_key(const struct ID *id) +{ + const size_t key_len_base = strlen(id->name) + 1; + const size_t key_len_ext = ((id->lib != NULL) ? strlen(id->lib->name) : 0) + 1; + const size_t key_len = key_len_base + key_len_ext - 1; + char *key = MEM_mallocN(key_len, __func__); + + BLI_strncpy(key, id->name, key_len_base); + if (id->lib != NULL) { + BLI_strncpy(key + key_len_base - 1, id->lib->name, key_len_ext); + } + + return key; +} + void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath) { /* in some cases this is used to update the absolute path from the |