diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-11-07 11:49:07 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-11-07 11:49:07 +0300 |
commit | 20b16e4074eb0b015a94bd5738c76dafaca250db (patch) | |
tree | 2b348e906db0a01b2d0b033d0780cfeb05e4f445 /source/blender/blenkernel | |
parent | fbcaa502ca8e71390e3f43ee9cc18b1ccfe840a2 (diff) |
de-duplicate unique naming logic, was used in 7 different places, convert into a function call.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/intern/customdata.c | 36 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/deform.c | 35 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/library.c | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mball.c | 16 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/nla.c | 21 |
5 files changed, 43 insertions, 69 deletions
diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index cd476d8491b..beb6c085d64 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -34,6 +34,7 @@ #include <math.h> #include <string.h> +#include <assert.h> #include "MEM_guardedalloc.h" @@ -41,6 +42,7 @@ #include "DNA_ID.h" #include "BLI_blenlib.h" +#include "BLI_path_util.h" #include "BLI_linklist.h" #include "BLI_math.h" #include "BLI_mempool.h" @@ -2261,32 +2263,26 @@ static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int return 0; } -void CustomData_set_layer_unique_name(CustomData *data, int index) +static int customdata_unique_check(void *arg, const char *name) { + struct {CustomData *data; int type; int index;} *data_arg= arg; + return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index); +} + +void CustomData_set_layer_unique_name(CustomData *data, int index) +{ CustomDataLayer *nlayer= &data->layers[index]; const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type); + struct {CustomData *data; int type; int index;} data_arg; + data_arg.data= data; + data_arg.type= nlayer->type; + data_arg.index= index; + if (!typeInfo->defaultname) return; - - if (nlayer->name[0] == '\0') - BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name)); - - if(cd_layer_find_dupe(data, nlayer->name, nlayer->type, index)) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(nlayer->name)]; - char left[sizeof(nlayer->name)]; - int number; - int len= BLI_split_name_num(left, &number, nlayer->name); - do { /* nested while loop looks bad but likely it wont run most times */ - while(BLI_snprintf(tempname, sizeof(tempname), "%s.%03d", left, number) >= sizeof(tempname)) { - if(len > 0) left[--len]= '\0'; /* word too long */ - else number= 0; /* reset, must be a massive number */ - } - } while(number++, cd_layer_find_dupe(data, tempname, nlayer->type, index)); - - BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name)); - } + + BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name)); } int CustomData_verify_versions(struct CustomData *data, int index) diff --git a/source/blender/blenkernel/intern/deform.c b/source/blender/blenkernel/intern/deform.c index 10fbd247c84..2eecc6d5c1b 100644 --- a/source/blender/blenkernel/intern/deform.c +++ b/source/blender/blenkernel/intern/deform.c @@ -319,32 +319,19 @@ static int defgroup_find_name_dupe(const char *name, bDeformGroup *dg, Object *o return 0; } +static int defgroup_unique_check(void *arg, const char *name) +{ + struct {Object *ob; void *dg;} *data= arg; + return defgroup_find_name_dupe(name, data->dg, data->ob); +} + void defgroup_unique_name (bDeformGroup *dg, Object *ob) -{ - if (!ob) - return; - - /* See if we are given an empty string */ - if (dg->name[0] == '\0') { - /* give it default name first */ - strcpy (dg->name, "Group"); - } - - if(defgroup_find_name_dupe(dg->name, dg, ob)) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(dg->name)]; - char left[sizeof(dg->name)]; - int number; - int len= BLI_split_name_num(left, &number, dg->name); - do { /* nested while loop looks bad but likely it wont run most times */ - while(BLI_snprintf(tempname, sizeof(tempname), "%s.%03d", left, number) >= sizeof(tempname)) { - if(len > 0) left[--len]= '\0'; /* word too long */ - else number= 0; /* reset, must be a massive number */ - } - } while(number++, defgroup_find_name_dupe(tempname, dg, ob)); +{ + struct {Object *ob; void *dg;} data; + data.ob= ob; + data.dg= dg; - BLI_strncpy(dg->name, tempname, sizeof(dg->name)); - } + BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name)); } /* finds the best possible flipped name. For renaming; check for unique names afterwards */ diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index ed9907b6869..34351f9b113 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -1063,7 +1063,7 @@ static int check_for_dupid(ListBase *lb, ID *id, char *name) memset(in_use, 0, sizeof(in_use)); /* get name portion, number portion ("name.number") */ - left_len= BLI_split_name_num(left, &nr, name); + left_len= BLI_split_name_num(left, &nr, name, '.'); /* if new name will be too long, truncate it */ if(nr > 999 && left_len > 16) { @@ -1080,7 +1080,7 @@ static int check_for_dupid(ListBase *lb, ID *id, char *name) (idtest->lib == NULL) && (*name == *(idtest->name+2)) && (strncmp(name, idtest->name+2, left_len)==0) && - (BLI_split_name_num(leftest, &nrtest, idtest->name+2) == left_len) + (BLI_split_name_num(leftest, &nrtest, idtest->name+2, '.') == left_len) ) { if(nrtest < sizeof(in_use)) in_use[nrtest]= 1; /* mark as used */ diff --git a/source/blender/blenkernel/intern/mball.c b/source/blender/blenkernel/intern/mball.c index fd3bc47da9e..dc343e0d6c2 100644 --- a/source/blender/blenkernel/intern/mball.c +++ b/source/blender/blenkernel/intern/mball.c @@ -330,8 +330,8 @@ int is_mball_basis_for(Object *ob1, Object *ob2) int basis1nr, basis2nr; char basis1name[32], basis2name[32]; - BLI_split_name_num(basis1name, &basis1nr, ob1->id.name+2); - BLI_split_name_num(basis2name, &basis2nr, ob2->id.name+2); + BLI_split_name_num(basis1name, &basis1nr, ob1->id.name+2, '.'); + BLI_split_name_num(basis2name, &basis2nr, ob2->id.name+2, '.'); if(!strcmp(basis1name, basis2name)) return is_basis_mball(ob1); else return 0; @@ -352,7 +352,7 @@ void copy_mball_properties(Scene *scene, Object *active_object) int basisnr, obnr; char basisname[32], obname[32]; - BLI_split_name_num(basisname, &basisnr, active_object->id.name+2); + BLI_split_name_num(basisname, &basisnr, active_object->id.name+2, '.'); /* XXX recursion check, see scene.c, just too simple code this next_object() */ if(F_ERROR==next_object(&sce_iter, 0, 0, 0)) @@ -361,7 +361,7 @@ void copy_mball_properties(Scene *scene, Object *active_object) while(next_object(&sce_iter, 1, &base, &ob)) { if (ob->type==OB_MBALL) { if(ob!=active_object){ - BLI_split_name_num(obname, &obnr, ob->id.name+2); + BLI_split_name_num(obname, &obnr, ob->id.name+2, '.'); /* Object ob has to be in same "group" ... it means, that it has to have * same base of its name */ @@ -395,7 +395,7 @@ Object *find_basis_mball(Scene *scene, Object *basis) int basisnr, obnr; char basisname[32], obname[32]; - BLI_split_name_num(basisname, &basisnr, basis->id.name+2); + BLI_split_name_num(basisname, &basisnr, basis->id.name+2, '.'); totelem= 0; /* XXX recursion check, see scene.c, just too simple code this next_object() */ @@ -415,7 +415,7 @@ Object *find_basis_mball(Scene *scene, Object *basis) else ml= mb->elems.first; } else{ - BLI_split_name_num(obname, &obnr, ob->id.name+2); + BLI_split_name_num(obname, &obnr, ob->id.name+2, '.'); /* object ob has to be in same "group" ... it means, that it has to have * same base of its name */ @@ -1572,7 +1572,7 @@ float init_meta(Scene *scene, Object *ob) /* return totsize */ invert_m4_m4(obinv, ob->obmat); a= 0; - BLI_split_name_num(obname, &obnr, ob->id.name+2); + BLI_split_name_num(obname, &obnr, ob->id.name+2, '.'); /* make main array */ next_object(&sce_iter, 0, 0, 0); @@ -1593,7 +1593,7 @@ float init_meta(Scene *scene, Object *ob) /* return totsize */ char name[32]; int nr; - BLI_split_name_num(name, &nr, bob->id.name+2); + BLI_split_name_num(name, &nr, bob->id.name+2, '.'); if( strcmp(obname, name)==0 ) { mb= bob->data; diff --git a/source/blender/blenkernel/intern/nla.c b/source/blender/blenkernel/intern/nla.c index 423ccb00e8e..910de5c6763 100644 --- a/source/blender/blenkernel/intern/nla.c +++ b/source/blender/blenkernel/intern/nla.c @@ -1204,6 +1204,11 @@ void BKE_nlastrip_validate_fcurves (NlaStrip *strip) } } +static int nla_editbone_name_check(void *arg, const char *name) +{ + return BLI_ghash_haskey((GHash *)arg, (void *)name); +} + /* Sanity Validation ------------------------------------ */ /* Find (and set) a unique name for a strip from the whole AnimData block @@ -1259,21 +1264,7 @@ void BKE_nlastrip_validate_name (AnimData *adt, NlaStrip *strip) /* if the hash-table has a match for this name, try other names... * - in an extreme case, it might not be able to find a name, but then everything else in Blender would fail too :) */ - if (BLI_ghash_haskey(gh, strip->name)) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(strip->name)]; - char left[sizeof(strip->name)]; - int number; - int len= BLI_split_name_num(left, &number, strip->name); - do { /* nested while loop looks bad but likely it wont run most times */ - while(BLI_snprintf(tempname, sizeof(tempname), "%s.%03d", left, number) >= sizeof(tempname)) { - if(len > 0) left[--len]= '\0'; /* word too long */ - else number= 0; /* reset, must be a massive number */ - } - } while(number++, BLI_ghash_haskey(gh, tempname)); - - BLI_strncpy(strip->name, tempname, sizeof(strip->name)); - } + BLI_uniquename_cb(nla_editbone_name_check, (void *)gh, "NlaStrip", '.', strip->name, sizeof(strip->name)); /* free the hash... */ BLI_ghash_free(gh, NULL, NULL); |