diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-01-08 10:39:12 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-01-08 10:39:12 +0400 |
commit | 2dba2e72b76ba2933d6ca7d3e79ad669c430e3a5 (patch) | |
tree | 19e2d2af6a5923952d29151d5ae43a69b11508e4 /source/blender/windowmanager/intern/wm_window.c | |
parent | 3fbd63c52e25344f115b5cd67a218436bc4007cf (diff) |
Code Cleanup: de-duplicate text pasting which only used the first line
Diffstat (limited to 'source/blender/windowmanager/intern/wm_window.c')
-rw-r--r-- | source/blender/windowmanager/intern/wm_window.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/source/blender/windowmanager/intern/wm_window.c b/source/blender/windowmanager/intern/wm_window.c index d3936ff32e2..19b87766ffd 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) { |