Code Cleanup: de-duplicate text pasting which only used the first line
authorCampbell Barton <ideasman42@gmail.com>
Wed, 8 Jan 2014 06:39:12 +0000 (17:39 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 8 Jan 2014 06:39:12 +0000 (17:39 +1100)
source/blender/editors/curve/editfont.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/space_console/console_ops.c
source/blender/editors/space_text/text_ops.c
source/blender/editors/util/numinput.c
source/blender/makesrna/intern/rna_wm.c
source/blender/python/mathutils/mathutils.h
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_window.c
source/blenderplayer/bad_level_call_stubs/stubs.c

index 56b8c96c60ac1bb693585e2f0b5b0b7cf06db99d..b76eaaf47b7323932a8501ad35da635bb1b8b6d4 100644 (file)
@@ -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;
index 470c042e7c7d5c4714377db8b4a2793e6e0217b2..79ae8536acc75fcd9f2862134054eca9d405bfca 100644 (file)
@@ -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) {
index f24a204912e868687fd6ef270af6043319bc53db..0ddaa3444ccf8f29e5df4d44d89f5d494d4f10ac 100644 (file)
@@ -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)
index 2849db7330a60be1af6f09ae1f1c30b3fafc6f4c..cd4c214e0d6e4139e8ba8007cd34ccfec00e0965 100644 (file)
@@ -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;
index 78499dac55bf9a2e83e44e9d60a9b2fb3509b681..2828973e91d43f7beda0cbc3df518e4063dd1036 100644 (file)
@@ -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) {
index 15cb06b313ef0ca1d722d931f80ea7b14cbee45e..0eda2662e4328e30f1c4371503efe601a0ae7020 100644 (file)
@@ -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)
index 4c057a144615814cd0a260e77057c2a7d8e4b3b3..eb25d9bff073943c35debf89404295672445a958 100644 (file)
@@ -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 *);
 
index d8f558bf2c9ac8f1bf32321ec81b0fa45d463bc7..8c4c41be84b395c6cb773499f4d744b93f1b1e00 100644 (file)
@@ -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 */
index d3936ff32e299b9abc2657e3f450b4f7b73aa28a..19b87766ffd46937eec47c3a139224eff4cecee0 100644 (file)
@@ -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) {
index a5900f2620c8aeec0cbf7f98166ab929273da9d0..107b03ecf26addfe850cd0d66c11e6e1407c8533 100644 (file)
@@ -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);}