[Bf-blender-cvs] [62f2b75] master: UI: refactor button string get/set into functions.
Campbell Barton
noreply at git.blender.org
Tue Jan 6 01:06:36 CET 2015
Commit: 62f2b751f8c2c94bd783ce3d9bef6fc5c7d783c7
Author: Campbell Barton
Date: Tue Jan 6 11:05:08 2015 +1100
Branches: master
https://developer.blender.org/rB62f2b751f8c2c94bd783ce3d9bef6fc5c7d783c7
UI: refactor button string get/set into functions.
===================================================================
M source/blender/editors/interface/interface.c
===================================================================
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 69ff1e7..0b1d1c8 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -2164,6 +2164,32 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double
return ok;
}
+/* just the assignment/free part */
+static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len)
+{
+ BLI_assert(str_len == strlen(str));
+ BLI_assert(but->str == NULL);
+ str_len += 1;
+
+ if (str_len > UI_MAX_NAME_STR) {
+ but->str = MEM_mallocN(str_len, "ui_def_but str");
+ }
+ else {
+ but->str = but->strdata;
+ }
+ memcpy(but->str, str, str_len);
+}
+
+static void ui_but_string_free_internal(uiBut *but)
+{
+ if (but->str) {
+ if (but->str != but->strdata) {
+ MEM_freeN(but->str);
+ }
+ /* must call 'ui_but_string_set_internal' after */
+ but->str = NULL;
+ }
+}
bool ui_but_string_set(bContext *C, uiBut *but, const char *str)
{
@@ -2647,18 +2673,9 @@ void ui_but_update(uiBut *but)
if (RNA_property_enum_name_gettexted(but->block->evil_C,
&but->rnapoin, but->rnaprop, value, &buf))
{
- if (but->str == but->strdata) {
- if (strlen(buf) < sizeof(but->strdata)) {
- BLI_strncpy(but->str, buf, sizeof(but->strdata));
- }
- else {
- but->str = BLI_strdup(buf);
- }
- }
- else {
- MEM_SAFE_FREE(but->str);
- but->str = BLI_strdup(buf);
- }
+ size_t slen = strlen(buf);
+ ui_but_string_free_internal(but);
+ ui_but_string_set_internal(but, buf, slen);
}
}
}
@@ -3066,13 +3083,7 @@ static uiBut *ui_def_but(uiBlock *block, int type, int retval, const char *str,
but->retval = retval;
slen = strlen(str);
- if (slen >= UI_MAX_NAME_STR) {
- but->str = MEM_mallocN(slen + 1, "ui_def_but str");
- }
- else {
- but->str = but->strdata;
- }
- memcpy(but->str, str, slen + 1);
+ ui_but_string_set_internal(but, str, slen);
but->rect.xmin = x;
but->rect.ymin = y;
More information about the Bf-blender-cvs
mailing list