public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* /dev/clipboard pasting with small read() buffer
@ 2012-08-14 20:56 Thomas Wolff
  2012-08-16  9:34 ` Corinna Vinschen
  0 siblings, 1 reply; 10+ messages in thread
From: Thomas Wolff @ 2012-08-14 20:56 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: clipboard-small-buffer.patch --]
[-- Type: text/plain, Size: 2511 bytes --]

--- sav/fhandler_clipboard.cc	2012-07-08 02:36:47.000000000 +0200
+++ ./fhandler_clipboard.cc	2012-08-14 18:25:14.903255600 +0200
@@ -222,6 +222,7 @@ fhandler_dev_clipboard::read (void *ptr,
   UINT formatlist[2];
   int format;
   LPVOID cb_data;
+  int rach;
 
   if (!OpenClipboard (NULL))
     {
@@ -243,12 +244,18 @@ fhandler_dev_clipboard::read (void *ptr,
       cygcb_t *clipbuf = (cygcb_t *) cb_data;
 
       if (pos < clipbuf->len)
-      	{
+	{
 	  ret = ((len > (clipbuf->len - pos)) ? (clipbuf->len - pos) : len);
 	  memcpy (ptr, clipbuf->data + pos , ret);
 	  pos += ret;
 	}
     }
+  else if ((rach = get_readahead ()) >= 0)
+    {
+      /* Deliver from read-ahead buffer. */
+      * (char *) ptr = rach;
+      ret = 1;
+    }
   else
     {
       wchar_t *buf = (wchar_t *) cb_data;
@@ -256,25 +263,46 @@ fhandler_dev_clipboard::read (void *ptr,
       size_t glen = GlobalSize (hglb) / sizeof (WCHAR) - 1;
       if (pos < glen)
 	{
+	  /* If caller's buffer is too small to hold at least one 
+	     max-size character, redirect algorithm to local 
+	     read-ahead buffer, finally fill class read-ahead buffer 
+	     with result and feed caller from there. */
+	  char * _ptr = (char *) ptr;
+	  size_t _len = len;
+	  char cprabuf [8 + 1];	/* need this length for surrogates */
+	  if (len < 8)
+	    {
+	      _ptr = cprabuf;
+	      _len = 8;
+	    }
+
 	  /* Comparing apples and oranges here, but the below loop could become
 	     extremly slow otherwise.  We rather return a few bytes less than
 	     possible instead of being even more slow than usual... */
-	  if (glen > pos + len)
-	    glen = pos + len;
+	  if (glen > pos + _len)
+	    glen = pos + _len;
 	  /* This loop is necessary because the number of bytes returned by
 	     sys_wcstombs does not indicate the number of wide chars used for
 	     it, so we could potentially drop wide chars. */
 	  while ((ret = sys_wcstombs (NULL, 0, buf + pos, glen - pos))
 		  != (size_t) -1
-		 && ret > len)
+		 && ret > _len)
 	     --glen;
 	  if (ret == (size_t) -1)
 	    ret = 0;
 	  else
 	    {
-	      ret = sys_wcstombs ((char *) ptr, (size_t) -1,
+	      ret = sys_wcstombs ((char *) _ptr, (size_t) -1,
 				  buf + pos, glen - pos);
 	      pos = glen;
+	      /* If using read-ahead buffer, copy to class read-ahead buffer
+	         and deliver first byte. */
+	      if (_ptr == cprabuf)
+		{
+		  puts_readahead (cprabuf, ret);
+		  * (char *) ptr = get_readahead ();
+		  ret = 1;
+		}
 	    }
 	}
     }

[-- Attachment #3: clipboard-small-buffer.changelog --]
[-- Type: text/plain, Size: 214 bytes --]

2012-08-14  Thomas Wolff  <towo@towo.net>

	* fhandler_clipboard.cc (fhandler_dev_clipboard::read): Use 
	read-ahead buffer for reading Windows clipboard if caller's 
	buffer is too small for complete characters.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-08-17 13:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-14 20:56 /dev/clipboard pasting with small read() buffer Thomas Wolff
2012-08-16  9:34 ` Corinna Vinschen
2012-08-16 12:12   ` Thomas Wolff
2012-08-16 12:31     ` Corinna Vinschen
2012-08-16 14:21       ` Thomas Wolff
2012-08-16 15:24         ` Eric Blake
2012-08-16 16:23           ` Corinna Vinschen
2012-08-17  8:44             ` Thomas Wolff
2012-08-17  9:23               ` Corinna Vinschen
2012-08-17 13:05                 ` Thomas Wolff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).