Commit: 2dba2e72b76ba2933d6ca7d3e79ad669c430e3a5
Author: Campbell Barton
Date:   Wed Jan 8 17:39:12 2014 +1100
https://developer.blender.org/rB2dba2e72b76ba2933d6ca7d3e79ad669c430e3a5

Code Cleanup: de-duplicate text pasting which only used the first line

===================================================================

M       source/blender/editors/curve/editfont.c
M       source/blender/editors/interface/interface_handlers.c
M       source/blender/editors/space_console/console_ops.c
M       source/blender/editors/space_text/text_ops.c
M       source/blender/editors/util/numinput.c
M       source/blender/makesrna/intern/rna_wm.c
M       source/blender/python/mathutils/mathutils.h
M       source/blender/windowmanager/WM_api.h
M       source/blender/windowmanager/intern/wm_window.c
M       source/blenderplayer/bad_level_call_stubs/stubs.c

===================================================================

diff --git a/source/blender/editors/curve/editfont.c 
b/source/blender/editors/curve/editfont.c
index 56b8c96..b76eaaf 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -515,14 +515,12 @@ static int paste_from_clipboard(bContext *C, ReportList 
*reports)
        int filelen;
        int retval;
 
-       strp = WM_clipboard_text_get(false);
+       strp = WM_clipboard_text_get(false, &filelen);
        if (strp == NULL) {
                BKE_report(reports, RPT_ERROR, "Clipboard empty");
                return OPERATOR_CANCELLED;
        }
 
-       filelen = strlen(strp);
-
        if ((filelen <= MAXTEXT) && font_paste_utf8(C, strp, filelen)) {
                text_update_edited(C, scene, obedit, 1, FO_EDIT);
                retval = OPERATOR_FINISHED;
diff --git a/source/blender/editors/interface/interface_handlers.c 
b/source/blender/editors/interface/interface_handlers.c
index 470c042..79ae853 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -1402,15 +1402,10 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, 
uiHandleButtonData *data,
 
        if (mode == 'v') {
                /* extract first line from clipboard in case of multi-line 
copies */
-               char *p, *pbuf = WM_clipboard_text_get(0);
-               p = pbuf;
-               if (p) {
-                       int i = 0;
-                       while (*p && *p != '\r' && *p != '\n' && i < 
UI_MAX_DRAW_STR) {
-                               buf[i++] = *p;
-                               p++;
-                       }
-                       buf[i] = 0;
+               int pbuf_len;
+               char *pbuf = WM_clipboard_text_get_firstline(false, &pbuf_len);
+               if (pbuf) {
+                       BLI_strncpy(buf, pbuf, sizeof(buf));
                        MEM_freeN(pbuf);
                }
        }
@@ -1997,7 +1992,7 @@ enum {
 
 static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const 
int mode)
 {
-       char *str, *p, *pbuf;
+       char *str, *pbuf;
        int x;
        bool changed = false;
        int str_len, buf_len;
@@ -2009,17 +2004,13 @@ static bool ui_textedit_copypaste(uiBut *but, 
uiHandleButtonData *data, const in
        if (mode == UI_TEXTEDIT_PASTE) {
                /* TODO, ensure UTF8 ui_is_but_utf8() - campbell */
                /* extract the first line from the clipboard */
-               p = pbuf = WM_clipboard_text_get(0);
+               pbuf = WM_clipboard_text_get_firstline(false, &buf_len);
 
-               if (p && p[0]) {
+               if (pbuf) {
                        char buf[UI_MAX_DRAW_STR] = {0};
                        unsigned int y;
-                       buf_len = 0;
-                       while (*p && *p != '\r' && *p != '\n' && buf_len < 
UI_MAX_DRAW_STR - 1) {
-                               buf[buf_len++] = *p;
-                               p++;
-                       }
-                       buf[buf_len] = 0;
+
+                       buf_len = BLI_strncpy_rlen(buf, pbuf, sizeof(buf));
 
                        /* paste over the current selection */
                        if ((but->selend - but->selsta) > 0) {
diff --git a/source/blender/editors/space_console/console_ops.c 
b/source/blender/editors/space_console/console_ops.c
index f24a204..0ddaa34 100644
--- a/source/blender/editors/space_console/console_ops.c
+++ b/source/blender/editors/space_console/console_ops.c
@@ -970,8 +970,9 @@ static int console_paste_exec(bContext *C, wmOperator 
*UNUSED(op))
        SpaceConsole *sc = CTX_wm_space_console(C);
        ARegion *ar = CTX_wm_region(C);
        ConsoleLine *ci = console_history_verify(C);
+       int buf_len;
 
-       char *buf_str = WM_clipboard_text_get(0);
+       char *buf_str = WM_clipboard_text_get(false, &buf_len);
        char *buf_step, *buf_next;
 
        if (buf_str == NULL)
diff --git a/source/blender/editors/space_text/text_ops.c 
b/source/blender/editors/space_text/text_ops.c
index 2849db7..cd4c214 100644
--- a/source/blender/editors/space_text/text_ops.c
+++ b/source/blender/editors/space_text/text_ops.c
@@ -797,11 +797,12 @@ static char *txt_copy_selected(Text *text)
 
 static int text_paste_exec(bContext *C, wmOperator *op)
 {
+       const bool selection = RNA_boolean_get(op->ptr, "selection");
        Text *text = CTX_data_edit_text(C);
        char *buf;
-       int selection = RNA_boolean_get(op->ptr, "selection");
+       int buf_len;
 
-       buf = WM_clipboard_text_get(selection);
+       buf = WM_clipboard_text_get(selection, &buf_len);
 
        if (!buf)
                return OPERATOR_CANCELLED;
diff --git a/source/blender/editors/util/numinput.c 
b/source/blender/editors/util/numinput.c
index 78499da..2828973 100644
--- a/source/blender/editors/util/numinput.c
+++ b/source/blender/editors/util/numinput.c
@@ -341,17 +341,13 @@ bool handleNumInput(bContext *C, NumInput *n, const 
wmEvent *event)
                case VKEY:
                        if (event->ctrl) {
                                /* extract the first line from the clipboard */
-                               char *pbuf = WM_clipboard_text_get(0);
+                               int pbuf_len;
+                               char *pbuf = 
WM_clipboard_text_get_firstline(false, &pbuf_len);
 
                                if (pbuf) {
                                        bool success;
-                                       /* Only copy string until first of this 
char. */
-                                       char *cr = strchr(pbuf, '\r');
-                                       char *cn = strchr(pbuf, '\n');
-                                       if (cn && cn < cr) cr = cn;
-                                       if (cr) *cr = '\0';
 
-                                       success = editstr_insert_at_cursor(n, 
pbuf, strlen(pbuf));
+                                       success = editstr_insert_at_cursor(n, 
pbuf, pbuf_len);
 
                                        MEM_freeN(pbuf);
                                        if (!success) {
diff --git a/source/blender/makesrna/intern/rna_wm.c 
b/source/blender/makesrna/intern/rna_wm.c
index 15cb06b..0eda266 100644
--- a/source/blender/makesrna/intern/rna_wm.c
+++ b/source/blender/makesrna/intern/rna_wm.c
@@ -831,10 +831,11 @@ static int rna_KeyMapItem_userdefined_get(PointerRNA *ptr)
 static void rna_wmClipboard_get(PointerRNA *UNUSED(ptr), char *value)
 {
        char *pbuf;
+       int pbuf_len;
 
-       pbuf = WM_clipboard_text_get(FALSE);
+       pbuf = WM_clipboard_text_get(false, &pbuf_len);
        if (pbuf) {
-               strcpy(value, pbuf);
+               memcpy(value, pbuf, pbuf_len + 1);
                MEM_freeN(pbuf);
        }
        else {
@@ -845,19 +846,14 @@ static void rna_wmClipboard_get(PointerRNA *UNUSED(ptr), 
char *value)
 static int rna_wmClipboard_length(PointerRNA *UNUSED(ptr))
 {
        char *pbuf;
-       int length;
+       int pbuf_len;
 
-       pbuf = WM_clipboard_text_get(FALSE);
+       pbuf = WM_clipboard_text_get(false, &pbuf_len);
        if (pbuf) {
-               length = strlen(pbuf);
                MEM_freeN(pbuf);
        }
-       else {
-               length = 0;
-       }
-       
 
-       return length;
+       return pbuf_len;
 }
 
 static void rna_wmClipboard_set(PointerRNA *UNUSED(ptr), const char *value)
diff --git a/source/blender/python/mathutils/mathutils.h 
b/source/blender/python/mathutils/mathutils.h
index 4c057a1..eb25d9b 100644
--- a/source/blender/python/mathutils/mathutils.h
+++ b/source/blender/python/mathutils/mathutils.h
@@ -55,11 +55,6 @@ typedef struct {
 #include "mathutils_Euler.h"
 #include "mathutils_Color.h"
 
-// /* utility submodules */
-//
-//
-//#include "mathutils_kdtree.h"
-
 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *);
 PyObject *BaseMathObject_is_wrapped_get(BaseMathObject *self, void *);
 
diff --git a/source/blender/windowmanager/WM_api.h 
b/source/blender/windowmanager/WM_api.h
index d8f558b..8c4c41b 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -409,7 +409,8 @@ void                WM_job_main_thread_lock_acquire(struct 
wmJob *job);
 void           WM_job_main_thread_lock_release(struct wmJob *job);
 
                        /* clipboard */
-char       *WM_clipboard_text_get(bool selection);
+char       *WM_clipboard_text_get(bool selection, int *r_len);
+char       *WM_clipboard_text_get_firstline(bool selection, int *r_len);
 void        WM_clipboard_text_set(const char *buf, bool selection);
 
                        /* progress */
diff --git a/source/blender/windowmanager/intern/wm_window.c 
b/source/blender/windowmanager/intern/wm_window.c
index d3936ff..19b8776 100644
--- a/source/blender/windowmanager/intern/wm_window.c
+++ b/source/blender/windowmanager/intern/wm_window.c
@@ -1199,31 +1199,71 @@ void WM_event_remove_timer(wmWindowManager *wm, 
wmWindow *UNUSED(win), wmTimer *
 
 /* ******************* clipboard **************** */
 
-char *WM_clipboard_text_get(bool selection)
+static char *wm_clipboard_text_get_ex(bool selection, int *r_len,
+                                      bool firstline)
 {
        char *p, *p2, *buf, *newbuf;
 
-       if (G.background)
+       if (G.background) {
+               *r_len = 0;
                return NULL;
+       }
 
        buf = (char *)GHOST_getClipboard(selection);
-       if (!buf)
+       if (!buf) {
+               *r_len = 0;
                return NULL;
+       }
        
        /* always convert from \r\n to \n */
-       newbuf = MEM_callocN(strlen(buf) + 1, __func__);
+       p2 = newbuf = MEM_mallocN(strlen(buf) + 1, __func__);
 
-       for (p = buf, p2 = newbuf; *p; p++) {
-               if (*p != '\r')
-                       *(p2++) = *p;
+       if (firstline) {
+               /* will return an over-alloc'ed value in the case there are 
newlines */
+               for (p = buf; *p; p++) {
+                       if ((*p != '\n') && (*p != '\r')) {
+                               *(p2++) = *p;
+                       }
+                       else {
+                               break;
+                       }
+               }
+       }
+       else {
+               for (p = buf; *p; p++) {
+                       if (*p != '\r') {
+                               *(p2++) = *p;
+                       }
+               }
        }
+
        *p2 = '\0';
 
        free(buf); /* ghost uses regular malloc */
        
+       *r_len = (p2 - newbuf);
+
        return newbuf;
 }
 
+/**
+ * Return text from the clipboard.
+ *
+ * \note Caller needs to check for valid utf8 if this is a requirement.
+ */
+char *WM_clipboard_text_get(bool selection, int *r_len)
+{
+       return wm_clipboard_text_get_ex(selection, r_len, false);
+}
+
+/**
+ * Convenience function for pasting to areas of Blender which don't support 
newlines.
+ */
+char *WM_clipboard_text_get_firstline(bool selection, int *r_len)
+{
+       return wm_clipboard_text_get_ex(selection, r_len, true);
+}
+
 void WM_clipboard_text_set(const char *buf, bool selection)
 {
        if (!G.background) {
diff --git a/source/blenderplayer/bad_level_call_stubs/stubs.c 
b/source/blenderplayer/bad_level_call_stubs/stubs.c
index a5900f2..107b03e 100644
--- a/source/blenderplayer/bad_level_call_stubs/stubs.c
+++ b/source/blenderplayer/bad_level_call_stubs/stubs.c
@@ -209,7 +209,8 @@ void WM_operator_handlers_clear(struct bContext *C, struct 
wmOperatorType *ot) {
 void WM_autosave_init(struct bContext *C) {STUB_ASSERT(0);}
 void WM_jobs_kill_all_except(struct wmWindowManager *wm) {STUB_ASSERT(0);}
 
-char *WM_clipboard_text_get(int selection) {STUB_ASSERT(0); return (char *)0;}
+char *WM_clipboard_text_get(bool selection, int *r_len) {STUB_ASSERT(0); 
return (char *)0;}
+char *WM_clipboard_text_get_firstline(bool selection, int *r_len) 
{STUB_ASSERT(0); return (char *)0;}
 void WM_clipboard_text_set(char *buf, int selection) {STUB_ASSERT(0);}
 
 void WM_cursor_set(struct wmWindow *win, int curor) {STUB_ASSERT(0);}

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to