MCPcopy Create free account
hub / github.com/NetHack/NetHack / port_insert_pastebuf

Function port_insert_pastebuf

sys/windows/windsys.c:362–432  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

360#ifdef RUNTIME_PASTEBUF_SUPPORT
361
362void port_insert_pastebuf(char *buf)
363{
364 /* This implementation will utilize the windows clipboard
365 * to accomplish this.
366 */
367
368 HGLOBAL hglbCopy;
369 WCHAR *w, w2[2];
370 /* int cc; */
371 int rc, abytes;
372 LPWSTR lpwstrCopy;
373 HANDLE hresult;
374
375 if (!buf)
376 return;
377
378 /* cc = strlen(buf); */
379 /* last arg=0 means "tell me the size of the buffer that I need" */
380 rc = MultiByteToWideChar(GetConsoleOutputCP(), 0, buf, -1, w2, 0);
381 if (!rc) return;
382
383 abytes = rc * sizeof(WCHAR);
384 w = (WCHAR *)alloc(abytes);
385 /* Housekeeping need: +free(w) */
386
387 rc = MultiByteToWideChar(GetConsoleOutputCP(), 0, buf, -1, w, rc);
388 if (!rc) {
389 free(w);
390 return;
391 }
392 if (!OpenClipboard(NULL)) {
393 free(w);
394 return;
395 }
396 /* Housekeeping need: +CloseClipboard(), free(w) */
397
398 EmptyClipboard();
399
400 /* allocate global mem obj to hold the text */
401
402 hglbCopy = GlobalAlloc(GMEM_MOVEABLE, abytes);
403 if (hglbCopy == NULL) {
404 CloseClipboard();
405 free(w);
406 return;
407 }
408 /* Housekeeping need: +GlobalFree(hglbCopy), CloseClipboard(), free(w) */
409
410 lpwstrCopy = (LPWSTR)GlobalLock(hglbCopy);
411 /* Housekeeping need: +GlobalUnlock(hglbCopy), GlobalFree(hglbCopy),
412 CloseClipboard(), free(w) */
413
414 if (lpwstrCopy) {
415 memcpy(lpwstrCopy, w, abytes);
416 GlobalUnlock(hglbCopy);
417 /* Housekeeping need: GlobalFree(hglbCopy), CloseClipboard(), free(w)
418 */
419

Callers

nothing calls this directly

Calls 2

allocFunction · 0.50
raw_printfFunction · 0.50

Tested by

no test coverage detected