Cleanup/refactor: get rid of fixed name limit in BKE_deform_flip_side_name & co.
authorBastien Montagne <montagne29@wanadoo.fr>
Mon, 16 Jan 2017 19:08:46 +0000 (20:08 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Mon, 16 Jan 2017 19:36:23 +0000 (20:36 +0100)
Those were forcing to use vgroup name define in bones area, or even
mixing with maxbonename... ugly, and totally avoidable.

13 files changed:
source/blender/blenkernel/BKE_deform.h
source/blender/blenkernel/intern/action.c
source/blender/blenkernel/intern/deform.c
source/blender/blenkernel/intern/object_deform.c
source/blender/editors/animation/keyframes_general.c
source/blender/editors/armature/armature_add.c
source/blender/editors/armature/armature_naming.c
source/blender/editors/armature/armature_select.c
source/blender/editors/armature/armature_skinning.c
source/blender/editors/armature/armature_utils.c
source/blender/editors/armature/pose_transform.c
source/blender/editors/object/object_select.c
source/blender/editors/sculpt_paint/paint_vertex.c

index 8756f73df7236fde72405a708fb5e6843e92dfac..a6d1139648d30d0527508540cb74438422e95649 100644 (file)
@@ -119,10 +119,9 @@ void BKE_defvert_extract_vgroup_to_polyweights(
 /* utility function, note that MAX_VGROUP_NAME chars is the maximum string length since its only
  * used with defgroups currently */
 
-void BKE_deform_split_suffix(const char string[MAX_VGROUP_NAME], char base[MAX_VGROUP_NAME], char ext[MAX_VGROUP_NAME]);
-void BKE_deform_split_prefix(const char string[MAX_VGROUP_NAME], char base[MAX_VGROUP_NAME], char ext[MAX_VGROUP_NAME]);
+void BKE_deform_split_suffix(const char *string, char *r_body, char *r_suf, const size_t str_len);
+void BKE_deform_split_prefix(const char *string, char *r_pre, char *r_body, const size_t str_len);
 
-void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME],
-                               const bool strip_number);
+void BKE_deform_flip_side_name(char *r_name, const char *from_name, const bool strip_number, const size_t name_len);
 
 #endif  /* __BKE_DEFORM_H__ */
index 2ade4cf08d56bcacc5cb12af13952101adca8b8b..d1b0306c27c6ccfaed97cb71f50e267c37631af1 100644 (file)
@@ -495,7 +495,7 @@ bPoseChannel *BKE_pose_channel_get_mirrored(const bPose *pose, const char *name)
 {
        char name_flip[MAXBONENAME];
 
-       BKE_deform_flip_side_name(name_flip, name, false);
+       BKE_deform_flip_side_name(name_flip, name, false, sizeof(name_flip));
 
        if (!STREQ(name_flip, name)) {
                return BKE_pose_channel_find_name(pose, name_flip);
index abc14c42d4041243616d68ca6c6228ca73c315ab..783455ef6f8ac1f284e7c974504cb3727acd5b41 100644 (file)
@@ -509,7 +509,7 @@ int *defgroup_flip_map(Object *ob, int *flip_map_len, const bool use_default)
                                if (use_default)
                                        map[i] = i;
 
-                               BKE_deform_flip_side_name(name_flip, dg->name, false);
+                               BKE_deform_flip_side_name(name_flip, dg->name, false, sizeof(name_flip));
 
                                if (!STREQ(name_flip, dg->name)) {
                                        flip_num = defgroup_name_index(ob, name_flip);
@@ -545,7 +545,7 @@ int *defgroup_flip_map_single(Object *ob, int *flip_map_len, const bool use_defa
 
                dg = BLI_findlink(&ob->defbase, defgroup);
 
-               BKE_deform_flip_side_name(name_flip, dg->name, false);
+               BKE_deform_flip_side_name(name_flip, dg->name, false, sizeof(name_flip));
                if (!STREQ(name_flip, dg->name)) {
                        flip_num = defgroup_name_index(ob, name_flip);
 
@@ -566,7 +566,7 @@ int defgroup_flip_index(Object *ob, int index, const bool use_default)
 
        if (dg) {
                char name_flip[sizeof(dg->name)];
-               BKE_deform_flip_side_name(name_flip, dg->name, false);
+               BKE_deform_flip_side_name(name_flip, dg->name, false, sizeof(name_flip));
 
                if (!STREQ(name_flip, dg->name)) {
                        flip_index = defgroup_name_index(ob, name_flip);
@@ -615,91 +615,92 @@ static bool is_char_sep(const char c)
  * based on `BLI_split_dirfile()` / `os.path.splitext()`,
  * `"a.b.c"` -> (`"a.b"`, `".c"`).
  */
-void BKE_deform_split_suffix(const char string[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME], char suf[MAX_VGROUP_NAME])
+void BKE_deform_split_suffix(const char *string, char *r_body, char *r_suf, const size_t str_len)
 {
-       size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
+       size_t len = BLI_strnlen(string, str_len);
        size_t i;
 
-       body[0] = suf[0] = '\0';
+       r_body[0] = r_suf[0] = '\0';
 
        for (i = len; i > 0; i--) {
                if (is_char_sep(string[i])) {
-                       BLI_strncpy(body, string, i + 1);
-                       BLI_strncpy(suf, string + i,  (len + 1) - i);
+                       BLI_strncpy(r_body, string, i + 1);
+                       BLI_strncpy(r_suf, string + i,  (len + 1) - i);
                        return;
                }
        }
 
-       memcpy(body, string, len + 1);
+       memcpy(r_body, string, len + 1);
 }
 
 /**
  * `"a.b.c"` -> (`"a."`, `"b.c"`)
  */
-void BKE_deform_split_prefix(const char string[MAX_VGROUP_NAME], char pre[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME])
+void BKE_deform_split_prefix(const char *string, char *r_pre, char *r_body, const size_t str_len)
 {
-       size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
+       size_t len = BLI_strnlen(string, str_len);
        size_t i;
 
-       body[0] = pre[0] = '\0';
+       r_body[0] = r_pre[0] = '\0';
 
        for (i = 1; i < len; i++) {
                if (is_char_sep(string[i])) {
                        i++;
-                       BLI_strncpy(pre, string, i + 1);
-                       BLI_strncpy(body, string + i, (len + 1) - i);
+                       BLI_strncpy(r_pre, string, i + 1);
+                       BLI_strncpy(r_body, string + i, (len + 1) - i);
                        return;
                }
        }
 
-       BLI_strncpy(body, string, len);
+       BLI_strncpy(r_body, string, len);
 }
 
 /**
- * Finds the best possible flipped name. For renaming; check for unique names afterwards.
+ * Finds the best possible flipped (left/right) name. For renaming; check for unique names afterwards.
  *
- * if strip_number: removes number extensions
- *
- * \note don't use sizeof() for 'name' or 'from_name'.
+ * \param r_name flipped name, assumed to be a pointer to a string of at least \a name_len size.
+ * \param from_name original name, assumed to be a pointer to a string of at least \a name_len size.
+ * \param strip_number If set, remove number extensions.
  */
-void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME],
-                               const bool strip_number)
+void BKE_deform_flip_side_name(char *r_name, const char *from_name, const bool strip_number, const size_t name_len)
 {
-       int     len;
-       char    prefix[MAX_VGROUP_NAME]  = "";   /* The part before the facing */
-       char    suffix[MAX_VGROUP_NAME]  = "";   /* The part after the facing */
-       char    replace[MAX_VGROUP_NAME] = "";   /* The replacement string */
-       char    number[MAX_VGROUP_NAME]  = "";   /* The number extension string */
-       char    *index = NULL;
+       size_t len;
+       char *prefix  = alloca(name_len);   /* The part before the facing */
+       char *suffix  = alloca(name_len);   /* The part after the facing */
+       char *replace = alloca(name_len);   /* The replacement string */
+       char *number  = alloca(name_len);   /* The number extension string */
+       char *index = NULL;
        bool is_set = false;
 
+       *prefix = *suffix = *replace = *number = '\0';
+
        /* always copy the name, since this can be called with an uninitialized string */
-       BLI_strncpy(name, from_name, MAX_VGROUP_NAME);
+       BLI_strncpy(r_name, from_name, name_len);
 
-       len = BLI_strnlen(from_name, MAX_VGROUP_NAME);
+       len = BLI_strnlen(from_name, name_len);
        if (len < 3) {
                /* we don't do names like .R or .L */
                return;
        }
 
        /* We first check the case with a .### extension, let's find the last period */
-       if (isdigit(name[len - 1])) {
-               index = strrchr(name, '.'); // last occurrence
+       if (isdigit(r_name[len - 1])) {
+               index = strrchr(r_name, '.'); // last occurrence
                if (index && isdigit(index[1])) { // doesnt handle case bone.1abc2 correct..., whatever!
                        if (strip_number == false) {
-                               BLI_strncpy(number, index, sizeof(number));
+                               BLI_strncpy(number, index, name_len);
                        }
                        *index = 0;
-                       len = BLI_strnlen(name, MAX_VGROUP_NAME);
+                       len = BLI_strnlen(r_name, name_len);
                }
        }
 
-       BLI_strncpy(prefix, name, sizeof(prefix));
+       BLI_strncpy(prefix, r_name, name_len);
 
        /* first case; separator . - _ with extensions r R l L  */
-       if ((len > 1) && is_char_sep(name[len - 2])) {
+       if ((len > 1) && is_char_sep(r_name[len - 2])) {
                is_set = true;
-               switch (name[len - 1]) {
+               switch (r_name[len - 1]) {
                        case 'l':
                                prefix[len - 1] = 0;
                                strcpy(replace, "r");
@@ -722,27 +723,27 @@ void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[
        }
 
        /* case; beginning with r R l L, with separator after it */
-       if (!is_set && is_char_sep(name[1])) {
+       if (!is_set && is_char_sep(r_name[1])) {
                is_set = true;
-               switch (name[0]) {
+               switch (r_name[0]) {
                        case 'l':
                                strcpy(replace, "r");
-                               BLI_strncpy(suffix, name + 1, sizeof(suffix));
+                               BLI_strncpy(suffix, r_name + 1, name_len);
                                prefix[0] = 0;
                                break;
                        case 'r':
                                strcpy(replace, "l");
-                               BLI_strncpy(suffix, name + 1, sizeof(suffix));
+                               BLI_strncpy(suffix, r_name + 1, name_len);
                                prefix[0] = 0;
                                break;
                        case 'L':
                                strcpy(replace, "R");
-                               BLI_strncpy(suffix, name + 1, sizeof(suffix));
+                               BLI_strncpy(suffix, r_name + 1, name_len);
                                prefix[0] = 0;
                                break;
                        case 'R':
                                strcpy(replace, "L");
-                               BLI_strncpy(suffix, name + 1, sizeof(suffix));
+                               BLI_strncpy(suffix, r_name + 1, name_len);
                                prefix[0] = 0;
                                break;
                        default:
@@ -763,7 +764,7 @@ void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[
                                strcpy(replace, (index[1] == 'I') ? "LEFT" : "Left");
                        }
                        *index = 0;
-                       BLI_strncpy(suffix, index + 5, sizeof(suffix));
+                       BLI_strncpy(suffix, index + 5, name_len);
                }
                else if (((index = BLI_strcasestr(prefix, "left")) == prefix) ||
                         (index == prefix + len - 4))
@@ -776,13 +777,11 @@ void BKE_deform_flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[
                                strcpy(replace, (index[1] == 'E') ? "RIGHT" : "Right");
                        }
                        *index = 0;
-                       BLI_strncpy(suffix, index + 4, sizeof(suffix));
+                       BLI_strncpy(suffix, index + 4, name_len);
                }
        }
 
-       (void)is_set;  /* quiet warning */
-
-       BLI_snprintf(name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
+       BLI_snprintf(r_name, name_len, "%s%s%s%s", prefix, replace, suffix, number);
 }
 
 float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
index b5e1ded35bbefe6fe13efac275f02790b69ee3c1..ce8c7919a22b9b844eacd4f153cd6d98ec5491bf 100644 (file)
@@ -623,7 +623,7 @@ void BKE_object_defgroup_mirror_selection(
                if (dg_selection[i]) {
                        char name_flip[MAXBONENAME];
 
-                       BKE_deform_flip_side_name(name_flip, defgroup->name, false);
+                       BKE_deform_flip_side_name(name_flip, defgroup->name, false, sizeof(name_flip));
                        i_mirr = STREQ(name_flip, defgroup->name) ? i : defgroup_name_index(ob, name_flip);
 
                        if ((i_mirr >= 0 && i_mirr < defbase_tot) && (dg_flags_sel[i_mirr] == false)) {
index 1703210f0b6b2d19546b4efe8578f69d896b9219..ae1ca1c4d1e3c4352b1ba44b7d802e05706c2f92 100644 (file)
@@ -668,7 +668,7 @@ static void flip_names(tAnimCopybufItem *aci, char **name)
 
                        /* more ninja stuff, temporary substitute with NULL terminator */
                        str_start[length] = 0;
-                       BKE_deform_flip_side_name(bname_new, str_start, false);
+                       BKE_deform_flip_side_name(bname_new, str_start, false, sizeof(bname_new));
                        str_start[length] = '\"';
 
                        str_iter = *name = MEM_mallocN(sizeof(char) * (prefix_l + postfix_l + length + 1), "flipped_path");
index 6228874343b394cb171d5cc9cf996178549d7d3a..23c2739543ceb34ac8b8cfe6823db013bd8073f1 100644 (file)
@@ -619,9 +619,9 @@ static int armature_symmetrize_exec(bContext *C, wmOperator *op)
                if (EBONE_VISIBLE(arm, ebone_iter) &&
                    (ebone_iter->flag & BONE_SELECTED))
                {
-                       char name_flip[MAX_VGROUP_NAME];
+                       char name_flip[MAXBONENAME];
 
-                       BKE_deform_flip_side_name(name_flip, ebone_iter->name, false);
+                       BKE_deform_flip_side_name(name_flip, ebone_iter->name, false, sizeof(name_flip));
 
                        if (STREQ(name_flip, ebone_iter->name)) {
                                /* if the name matches, we don't have the potential to be mirrored, just skip */
@@ -679,9 +679,9 @@ static int armature_symmetrize_exec(bContext *C, wmOperator *op)
                    /* will be set if the mirror bone already exists (no need to make a new one) */
                    (ebone_iter->temp.ebone == NULL))
                {
-                       char name_flip[MAX_VGROUP_NAME];
+                       char name_flip[MAXBONENAME];
 
-                       BKE_deform_flip_side_name(name_flip, ebone_iter->name, false);
+                       BKE_deform_flip_side_name(name_flip, ebone_iter->name, false, sizeof(name_flip));
 
                        /* bones must have a side-suffix */
                        if (!STREQ(name_flip, ebone_iter->name)) {
index c5cef632ffe26820451e48c04b64f8bfb5b30912..8d949fb8c3dfe6730ae25cec5db0a4b3111dc6ee 100644 (file)
@@ -328,7 +328,7 @@ void ED_armature_bones_flip_names(bArmature *arm, ListBase *bones_names)
 
                /* Do not strip numbers, otherwise we'll end up with completely mismatched names in cases like
                 * Bone.R, Bone.R.001, Bone.R.002, etc. */
-               BKE_deform_flip_side_name(name_flip, name, false);
+               BKE_deform_flip_side_name(name_flip, name, false, sizeof(name_flip));
 
                ED_armature_bone_rename(arm, name, name_flip);
 
index 5a70a45fad4e741e550f0bed562a5525186e884f..a34615e4402e6dc2a0beed18b9188350d33ca401 100644 (file)
@@ -817,10 +817,10 @@ static void select_similar_prefix(bArmature *arm, EditBone *ebone_act)
 {
        EditBone *ebone;
 
-       char body_tmp[MAX_VGROUP_NAME];
-       char prefix_act[MAX_VGROUP_NAME];
+       char body_tmp[MAXBONENAME];
+       char prefix_act[MAXBONENAME];
 
-       BKE_deform_split_prefix(ebone_act->name, prefix_act, body_tmp);
+       BKE_deform_split_prefix(ebone_act->name, prefix_act, body_tmp, sizeof(ebone_act->name));
 
        if (prefix_act[0] == '\0')
                return;
@@ -828,8 +828,8 @@ static void select_similar_prefix(bArmature *arm, EditBone *ebone_act)
        /* Find matches */
        for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
                if (EBONE_SELECTABLE(arm, ebone)) {
-                       char prefix_other[MAX_VGROUP_NAME];
-                       BKE_deform_split_prefix(ebone->name, prefix_other, body_tmp);
+                       char prefix_other[MAXBONENAME];
+                       BKE_deform_split_prefix(ebone->name, prefix_other, body_tmp, sizeof(ebone->name));
                        if (STREQ(prefix_act, prefix_other)) {
                                ED_armature_ebone_select_set(ebone, true);
                        }
@@ -841,10 +841,10 @@ static void select_similar_suffix(bArmature *arm, EditBone *ebone_act)
 {
        EditBone *ebone;
 
-       char body_tmp[MAX_VGROUP_NAME];
-       char suffix_act[MAX_VGROUP_NAME];
+       char body_tmp[MAXBONENAME];
+       char suffix_act[MAXBONENAME];
 
-       BKE_deform_split_suffix(ebone_act->name, body_tmp, suffix_act);
+       BKE_deform_split_suffix(ebone_act->name, body_tmp, suffix_act, sizeof(ebone_act->name));
 
        if (suffix_act[0] == '\0')
                return;
@@ -852,8 +852,8 @@ static void select_similar_suffix(bArmature *arm, EditBone *ebone_act)
        /* Find matches */
        for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
                if (EBONE_SELECTABLE(arm, ebone)) {
-                       char suffix_other[MAX_VGROUP_NAME];
-                       BKE_deform_split_suffix(ebone->name, body_tmp, suffix_other);
+                       char suffix_other[MAXBONENAME];
+                       BKE_deform_split_suffix(ebone->name, body_tmp, suffix_other, sizeof(ebone->name));
                        if (STREQ(suffix_act, suffix_other)) {
                                ED_armature_ebone_select_set(ebone, true);
                        }
index 28fddbab796ed9a656a73741abd7b527388a58b9..24146715f9692f09bf900e9e0f0141ad7cb4736b 100644 (file)
@@ -360,7 +360,7 @@ static void add_verts_to_dgroups(ReportList *reports, Scene *scene, Object *ob,
                if (dgroup && mirror) {
                        char name_flip[MAXBONENAME];
 
-                       BKE_deform_flip_side_name(name_flip, dgroup->name, false);
+                       BKE_deform_flip_side_name(name_flip, dgroup->name, false, sizeof(name_flip));
                        dgroupflip[j] = defgroup_find_name(ob, name_flip);
                }
        }
index 6979a324b696fe471d936f15b50f874d3fecd62f..0f783dbad6941935c7a19cd6e7dac07a0fdad60c 100644 (file)
@@ -262,7 +262,7 @@ EditBone *ED_armature_bone_get_mirrored(const ListBase *edbo, EditBone *ebo)
        if (ebo == NULL)
                return NULL;
        
-       BKE_deform_flip_side_name(name_flip, ebo->name, false);
+       BKE_deform_flip_side_name(name_flip, ebo->name, false, sizeof(name_flip));
        
        if (!STREQ(name_flip, ebo->name)) {
                return ED_armature_bone_find_name(edbo, name_flip);
index b645f1fb2f33abba0b4643b181a4f347dc89875d..a871a0e09665bcbf5f4034fb947b072369688460 100644 (file)
@@ -286,7 +286,7 @@ static bPoseChannel *pose_bone_do_paste(Object *ob, bPoseChannel *chan, const bo
        
        /* get the name - if flipping, we must flip this first */
        if (flip)
-               BKE_deform_flip_side_name(name, chan->name, false);
+               BKE_deform_flip_side_name(name, chan->name, false, sizeof(name));
        else
                BLI_strncpy(name, chan->name, sizeof(name));
        
index f1b7186f8a1cc1276dfdb8f949bce2194b27bbd6..a81b6ef20fc63f0ff0b47475f77cc74428828eb4 100644 (file)
@@ -1131,7 +1131,7 @@ static int object_select_mirror_exec(bContext *C, wmOperator *op)
        {
                char name_flip[MAXBONENAME];
 
-               BKE_deform_flip_side_name(name_flip, primbase->object->id.name + 2, true);
+               BKE_deform_flip_side_name(name_flip, primbase->object->id.name + 2, true, sizeof(name_flip));
                
                if (!STREQ(name_flip, primbase->object->id.name + 2)) {
                        Object *ob = (Object *)BKE_libblock_find_name(ID_OB, name_flip);
index 991025a4d5db2ae6a0dcfe0ee4f7637061e9c3da..3c3763fc0136264550d26739c4fc56b051524c22 100644 (file)
@@ -275,7 +275,7 @@ static int wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
                int mirrdef;
                char name_flip[MAXBONENAME];
 
-               BKE_deform_flip_side_name(name_flip, defgroup->name, false);
+               BKE_deform_flip_side_name(name_flip, defgroup->name, false, sizeof(name_flip));
                mirrdef = defgroup_name_index(ob, name_flip);
                if (mirrdef == -1) {
                        if (BKE_defgroup_new(ob, name_flip)) {