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 | |
parent | fbcaa502ca8e71390e3f43ee9cc18b1ccfe840a2 (diff) |
de-duplicate unique naming logic, was used in 7 different places, convert into a function call.
Diffstat (limited to 'source/blender')
-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 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 86 | ||||
-rw-r--r-- | source/blender/editors/armature/editarmature.c | 54 | ||||
-rw-r--r-- | source/blender/editors/transform/transform_orientations.c | 35 |
9 files changed, 128 insertions, 162 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); diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 69b10661e23..cba78e6c8f7 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -117,10 +117,11 @@ int BLI_testextensie_array(const char *str, const char **ext_array); int BLI_testextensie_glob(const char *str, const char *ext_fnmatch); int BLI_replace_extension(char *path, int maxlen, const char *ext); void BLI_uniquename(struct ListBase *list, void *vlink, const char defname[], char delim, short name_offs, short len); +int BLI_uniquename_cb(int (*unique_check)(void *, const char *), void *arg, const char defname[], char delim, char *name, short name_len); void BLI_newname(char * name, int add); int BLI_stringdec(const char *string, char *head, char *start, unsigned short *numlen); void BLI_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic); -int BLI_split_name_num(char *left, int *nr, const char *name); +int BLI_split_name_num(char *left, int *nr, const char *name, const char delim); void BLI_splitdirstring(char *di,char *fi); /* make sure path separators conform to system one */ diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 466012eb9b8..fffc74e822a 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -32,6 +32,7 @@ #include <ctype.h> #include <string.h> #include <stdlib.h> +#include <assert.h> #include "MEM_guardedalloc.h" @@ -76,6 +77,7 @@ #endif /* WIN32 */ /* local */ +#define UNIQUE_NAME_MAX 128 static int add_win32_extension(char *name); static char *blender_version_decimal(void); @@ -138,7 +140,7 @@ void BLI_stringenc(char *string, const char *head, const char *tail, unsigned sh /* Foo.001 -> "Foo", 1 * Returns the length of "Foo" */ -int BLI_split_name_num(char *left, int *nr, const char *name) +int BLI_split_name_num(char *left, int *nr, const char *name, const char delim) { int a; @@ -146,10 +148,10 @@ int BLI_split_name_num(char *left, int *nr, const char *name) a= strlen(name); memcpy(left, name, (a + 1) * sizeof(char)); - if(a>1 && name[a-1]=='.') return a; + if(a>1 && name[a-1]==delim) return a; while(a--) { - if( name[a]=='.' ) { + if( name[a]==delim ) { left[a]= 0; *nr= atol(name+a+1); /* casting down to an int, can overflow for large numbers */ @@ -170,7 +172,7 @@ int BLI_split_name_num(char *left, int *nr, const char *name) void BLI_newname(char *name, int add) { - char head[128], tail[128]; + char head[UNIQUE_NAME_MAX], tail[UNIQUE_NAME_MAX]; int pic; unsigned short digits; @@ -190,13 +192,41 @@ void BLI_newname(char *name, int add) BLI_stringenc(name, head, tail, digits, pic); } + + +int BLI_uniquename_cb(int (*unique_check)(void *, const char *), void *arg, const char defname[], char delim, char *name, short name_len) +{ + if(name == '\0') { + BLI_strncpy(name, defname, name_len); + } + + if(unique_check(arg, name)) { + char tempname[UNIQUE_NAME_MAX]; + char left[UNIQUE_NAME_MAX]; + int number; + int len= BLI_split_name_num(left, &number, name, delim); + do { /* nested while loop looks bad but likely it wont run most times */ + while(BLI_snprintf(tempname, name_len, "%s%c%03d", left, delim, number) >= name_len) { + if(len > 0) left[--len]= '\0'; /* word too long */ + else number= 0; /* reset, must be a massive number */ + } + } while(number++, unique_check(arg, tempname)); + + BLI_strncpy(name, tempname, name_len); + + return 1; + } + + return 0; +} + /* little helper macro for BLI_uniquename */ #ifndef GIVE_STRADDR #define GIVE_STRADDR(data, offset) ( ((char *)data) + offset ) #endif /* Generic function to set a unique name. It is only designed to be used in situations - * where the name is part of the struct, and also that the name is at most 128 chars long. + * where the name is part of the struct, and also that the name is at most UNIQUE_NAME_MAX chars long. * * For places where this is used, see constraint.c for example... * @@ -220,42 +250,30 @@ static int uniquename_find_dupe(ListBase *list, void *vlink, const char *name, s return 0; } +static int uniquename_unique_check(void *arg, const char *name) +{ + struct {ListBase *lb; void *vlink; short name_offs;} *data= arg; + return uniquename_find_dupe(data->lb, data->vlink, name, data->name_offs); +} + void BLI_uniquename(ListBase *list, void *vlink, const char defname[], char delim, short name_offs, short name_len) -{ - /* Make sure length can be handled */ - if ((name_len < 0) || (name_len > 128)) - return; - +{ + struct {ListBase *lb; void *vlink; short name_offs;} data; + data.lb= list; + data.vlink= vlink; + data.name_offs= name_offs; + + assert((name_len > 1) && (name_len <= UNIQUE_NAME_MAX)); + /* See if we are given an empty string */ if (ELEM(NULL, vlink, defname)) return; - - if (GIVE_STRADDR(vlink, name_offs) == '\0') { - /* give it default name first */ - BLI_strncpy(GIVE_STRADDR(vlink, name_offs), defname, name_len); - } - - /* See if we even need to do this */ - if (list == NULL) - return; - if(uniquename_find_dupe(list,vlink, GIVE_STRADDR(vlink, name_offs), name_offs)) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[128]; - char left[128]; - int number; - int len= BLI_split_name_num(left, &number, GIVE_STRADDR(vlink, name_offs)); - do { /* nested while loop looks bad but likely it wont run most times */ - while(BLI_snprintf(tempname, name_len, "%s%c%03d", left, delim, number) >= name_len) { - if(len > 0) left[--len]= '\0'; /* word too long */ - else number= 0; /* reset, must be a massive number */ - } - } while(number++, uniquename_find_dupe(list, vlink, tempname, name_offs)); - - BLI_strncpy(GIVE_STRADDR(vlink, name_offs), tempname, name_len); - } + BLI_uniquename_cb(uniquename_unique_check, &data, defname, delim, GIVE_STRADDR(vlink, name_offs), name_len); } + + /* ******************** string encoding ***************** */ /* This is quite an ugly function... its purpose is to diff --git a/source/blender/editors/armature/editarmature.c b/source/blender/editors/armature/editarmature.c index dcfc2762735..9dd86a96dae 100644 --- a/source/blender/editors/armature/editarmature.c +++ b/source/blender/editors/armature/editarmature.c @@ -506,27 +506,20 @@ static EditBone *editbone_name_exists (ListBase *edbo, const char *name) } /* note: there's a unique_bone_name() too! */ -void unique_editbone_name (ListBase *edbo, char *name, EditBone *bone) +static int editbone_unique_check(void *arg, const char *name) { - EditBone *dupli; + struct {ListBase *lb;void *bone;} *data= arg; + EditBone *dupli= editbone_name_exists(data->lb, name); + return dupli && dupli != data->bone; +} - dupli = editbone_name_exists(edbo, name); - - if (dupli && bone != dupli) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(bone->name)]; - char left[sizeof(bone->name)]; - int number; - int len= BLI_split_name_num(left, &number, 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++, ((dupli= editbone_name_exists(edbo, tempname)) && bone != dupli)); +void unique_editbone_name (ListBase *edbo, char *name, EditBone *bone) +{ + struct {ListBase *lb; void *bone;} data; + data.lb= edbo; + data.bone= bone; - BLI_strncpy(name, tempname, sizeof(bone->name)); - } + BLI_uniquename_cb(editbone_unique_check, &data, "Bone", '.', name, sizeof(bone->name)); } /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */ @@ -5398,26 +5391,17 @@ void POSE_OT_reveal(wmOperatorType *ot) /* ************* RENAMING DISASTERS ************ */ -/* note: there's a unique_editbone_name() too! */ -static void unique_bone_name (bArmature *arm, char *name) -{ - if (get_named_bone(arm, name)) { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(((Bone *)NULL)->name)]; - char left[sizeof(((Bone *)NULL)->name)]; - int number; - int len= BLI_split_name_num(left, &number, 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++, get_named_bone(arm, tempname)); +static int bone_unique_check(void *arg, const char *name) +{ + return get_named_bone((bArmature *)arg, name) != NULL; +} - BLI_strncpy(name, tempname, sizeof(tempname)); - } +void unique_bone_name(bArmature *arm, char *name) +{ + BLI_uniquename_cb(bone_unique_check, (void *)arm, "Bone", '.', name, sizeof(((Bone *)NULL)->name)); } + #define MAXBONENAME 32 /* helper call for armature_bone_rename */ static void constraint_bone_name_fix(Object *ob, ListBase *conlist, char *oldname, char *newname) diff --git a/source/blender/editors/transform/transform_orientations.c b/source/blender/editors/transform/transform_orientations.c index 085b784b6f0..dcfad455575 100644 --- a/source/blender/editors/transform/transform_orientations.c +++ b/source/blender/editors/transform/transform_orientations.c @@ -72,13 +72,12 @@ void BIF_clearTransformOrientation(bContext *C) } } -TransformOrientation* findOrientationName(bContext *C, char *name) +static TransformOrientation* findOrientationName(ListBase *lb, const char *name) { - ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces; TransformOrientation *ts= NULL; - for (ts = transform_spaces->first; ts; ts = ts->next) { - if (strncmp(ts->name, name, 35) == 0) { + for (ts= lb->first; ts; ts = ts->next) { + if (strncmp(ts->name, name, sizeof(ts->name)-1) == 0) { return ts; } } @@ -86,24 +85,14 @@ TransformOrientation* findOrientationName(bContext *C, char *name) return NULL; } -static void uniqueOrientationName(bContext *C, char *name) +static int uniqueOrientationNameCheck(void *arg, const char *name) { - if (findOrientationName(C, name) != NULL) - { - /* note: this block is used in other places, when changing logic apply to all others, search this message */ - char tempname[sizeof(((TransformOrientation *)NULL)->name)]; - char left[sizeof(((TransformOrientation *)NULL)->name)]; - int number; - int len= BLI_split_name_num(left, &number, 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++, findOrientationName(C, tempname)); - - BLI_strncpy(name, tempname, sizeof(tempname)); - } + return findOrientationName((ListBase *)arg, name) != NULL; +} + +static void uniqueOrientationName(ListBase *lb, char *name) +{ + BLI_uniquename_cb(uniqueOrientationNameCheck, lb, "Space", '.', name, sizeof(((TransformOrientation *)NULL)->name)); } void BIF_createTransformOrientation(bContext *C, ReportList *reports, char *name, int use, int overwrite) @@ -277,11 +266,11 @@ TransformOrientation* addMatrixSpace(bContext *C, float mat[3][3], char name[], if (overwrite) { - ts = findOrientationName(C, name); + ts = findOrientationName(transform_spaces, name); } else { - uniqueOrientationName(C, name); + uniqueOrientationName(transform_spaces, name); } /* if not, create a new one */ |