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/blenlib | |
parent | fbcaa502ca8e71390e3f43ee9cc18b1ccfe840a2 (diff) |
de-duplicate unique naming logic, was used in 7 different places, convert into a function call.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 86 |
2 files changed, 54 insertions, 35 deletions
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 |