shithub: puzzles

Download patch

ref: cf7988afb39eacb608eeacc73eb08a42db91b3d4
parent: 82b8e2faf5962031fd9da4c7fa057afecf81cafa
author: Simon Tatham <anakin@pobox.com>
date: Mon May 2 12:37:20 EDT 2005

Fix line endings when pasting on Windows.

[originally from svn r5736]

--- a/windows.c
+++ b/windows.c
@@ -330,9 +330,30 @@
 void write_clip(HWND hwnd, char *data)
 {
     HGLOBAL clipdata;
-    int len = strlen(data);
+    int len, i, j;
+    char *data2;
     void *lock;
 
+    /*
+     * Windows expects CRLF in the clipboard, so we must convert
+     * any \n that has come out of the puzzle backend.
+     */
+    len = 0;
+    for (i = 0; data[i]; i++) {
+	if (data[i] == '\n')
+	    len++;
+	len++;
+    }
+    data2 = snewn(len+1, char);
+    j = 0;
+    for (i = 0; data[i]; i++) {
+	if (data[i] == '\n')
+	    data2[j++] = '\r';
+	data2[j++] = data[i];
+    }
+    assert(j == len);
+    data2[j] = '\0';
+
     clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
     if (!clipdata)
 	return;
@@ -339,7 +360,7 @@
     lock = GlobalLock(clipdata);
     if (!lock)
 	return;
-    memcpy(lock, data, len);
+    memcpy(lock, data2, len);
     ((unsigned char *) lock)[len] = 0;
     GlobalUnlock(clipdata);
 
@@ -349,6 +370,8 @@
 	CloseClipboard();
     } else
 	GlobalFree(clipdata);
+
+    sfree(data2);
 }
 
 /*
@@ -933,6 +956,7 @@
 		    write_clip(hwnd, text);
 		else
 		    MessageBeep(MB_ICONWARNING);
+		sfree(text);
 	    }
 	    break;
 	  case IDM_SOLVE: