-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaste_utf8.cpp
More file actions
406 lines (333 loc) · 7.19 KB
/
paste_utf8.cpp
File metadata and controls
406 lines (333 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <iostream>
#include <vector>
#include <windows.h>
#include <stringapiset.h>
#include <fcntl.h>
#include <io.h>
const int MAX_DATA_SIZE = 32768;
class LockedMem {
public:
LockedMem(HANDLE hnd);
void Release() { is_released = true; }
~LockedMem();
LPVOID Lock();
void Unlock();
protected:
bool is_locked;
bool is_released;
HANDLE h;
};
void LockedMem::Unlock()
{
if (is_locked)
{
GlobalUnlock(h);
is_locked = false;
}
}
LockedMem::LockedMem(HANDLE hnd)
{
is_locked = false;
is_released = false;
h = hnd;
}
LPVOID LockedMem::Lock()
{
if (!is_locked)
{
auto res = GlobalLock(h);
is_locked = (res != NULL);
return res;
}
else
{
return NULL;
}
}
LockedMem::~LockedMem()
{
Unlock();
if (!is_released)
{
GlobalFree(h);
}
}
class Clipboard {
public:
Clipboard();
bool open();
bool get_clipboard_utf8(std::string& res);
bool set_clipboard_utf8(std::string& txt);
void close();
static bool to_utf8(std::string& res);
static bool from_utf8(std::string& data_in);
~Clipboard();
protected:
bool is_open;
};
bool Clipboard::to_utf8(std::string& res)
{
Clipboard clip;
if (!clip.open())
{
return false;
}
if (!clip.get_clipboard_utf8(res))
{
return false;
}
return true;
}
bool Clipboard::from_utf8(std::string& data_in)
{
Clipboard clip;
if (!clip.open())
{
return false;
}
if (!clip.set_clipboard_utf8(data_in))
{
return false;
}
return true;
}
void Clipboard::close()
{
if (is_open)
{
CloseClipboard();
is_open = false;
}
}
Clipboard::~Clipboard()
{
close();
}
Clipboard::Clipboard()
{
is_open = false;
}
// True on success
bool Clipboard::open()
{
if (!is_open)
{
auto res = OpenClipboard(NULL);
is_open = res != 0;
}
return is_open;
}
bool Clipboard::set_clipboard_utf8(std::string& txt)
{
if (!is_open)
{
return false;
}
// determine output buffer size in UTF-16 chars
auto num_chars_needed = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)txt.c_str(), -1, NULL, 0);
if (num_chars_needed == 0)
{
return false;
}
// calculate output buffer size in bytes
int num_bytes_needed = 2 * num_chars_needed;
std::string utf16_string(num_bytes_needed, 32);
// Convert to UTF-16
auto conv_res = MultiByteToWideChar(CP_UTF8, 0, (LPCCH)txt.c_str(), -1, (LPWSTR)utf16_string.c_str(), num_chars_needed);
if (conv_res == 0)
{
return false;
}
auto mem_handle = GlobalAlloc(GMEM_MOVEABLE, num_bytes_needed);
if (mem_handle == NULL)
{
return false;
}
LockedMem mem(mem_handle);
auto ptr = mem.Lock();
if (ptr == NULL)
{
// GlobalFree is called by the destructor of mem
return false;
}
std::memcpy(ptr, utf16_string.c_str(), num_bytes_needed);
mem.Unlock();
auto h = SetClipboardData(CF_UNICODETEXT, mem_handle);
if (h == NULL)
{
// GlobalFree is called by the destructor of mem
return false;
}
// The memory handle is now owned by the system. We do therefore not call
// GlobalFree()
mem.Release();
return true;
}
bool Clipboard::get_clipboard_utf8(std::string& res) {
if (!is_open)
{
return false;
}
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclipboarddata says:
// The clipboard controls the handle that the GetClipboardData function returns, not the application. The
// application should copy the data immediately. The application **must not free the handle** nor leave it locked.
auto h = GetClipboardData(CF_UNICODETEXT);
if (h == NULL)
{
return false;
}
auto mem = LockedMem(h);
// We are not responsible for freeing the memory handle
mem.Release();
auto utf16_data = mem.Lock();
if (utf16_data == NULL)
{
return false;
}
// determine output buffer size
auto num_bytes_needed = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)utf16_data, -1, NULL, 0, NULL, NULL);
if (num_bytes_needed == 0)
{
return false;
}
// convert to UTF-8
std::vector<char> buf(num_bytes_needed);
auto conv_res = WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)utf16_data, -1, buf.data(), num_bytes_needed, NULL, NULL);
if (conv_res == 0)
{
return false;
}
auto temp = std::string(buf.data());
res = temp;
// GlobalUnlock(h) is called by destructor of mem
// see above: Not calling GlobalFree(h);
return true;
}
void help() {
std::cout << "usage: paste_utf8.exe [-b] | [-t] | [-h] | [-c]" << std::endl;
std::cout << " -b read/write in binary mode (default)" << std::endl;
std::cout << " -t read/write in text mode" << std::endl;
std::cout << " -c copy stdin to clipbpoard" << std::endl;
std::cout << " -h print help message" << std::endl;
std::cout << std::endl;
std::cout << "This program prints the contents of the clipboard as an UTF-8 encoded string to stdout." << std::endl;
std::cout << "If the -c option is specified paste_utf8.exe copies data from stdin to the clipboard." << std::endl;
}
bool parse_opts(int argc, char* argv[], bool& do_stop, bool& do_copy) {
bool binary = true;
do_copy = false;
do_stop = false;
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
std::string opt = std::string(argv[i]);
if (opt == "-h")
{
help();
do_stop = true;
}
else
{
if (opt == "-b")
{
binary = true;
}
if (opt == "-t")
{
binary = false;
}
if (opt == "-c")
{
do_copy = true;
}
}
}
}
return binary;
}
int paste_clipboard(bool binary)
{
auto clip_contents = std::string();
if (!Clipboard::to_utf8(clip_contents))
{
return 42;
}
if (binary)
{
(void)_setmode(_fileno(stdout), O_BINARY);
}
std::cout << clip_contents << std::endl;
std::cout.flush();
if (binary)
{
(void)_setmode(_fileno(stdout), O_TEXT);
}
return 0;
}
int read_stdin(bool binary, std::vector<char>& data)
{
int res = 0;
// yes I know, this can also be controlled from std::iostream
if (binary)
{
(void)_setmode(_fileno(stdin), O_BINARY);
}
try
{
std::cin.read(data.data(), data.size());
// if eof is not set we either have data which is too long or
// reading from cin failed. Here both cases are considered an
// error.
if (!std::cin.eof())
{
// Goto end ;-)
throw 42;
}
res = static_cast<int>(std::cin.gcount());
}
catch(...)
{
res = -1;
}
if (binary) {
(void)_setmode(_fileno(stdin), O_TEXT);
}
return res;
}
int set_clipboard(bool binary, std::vector<char>& data)
{
Clipboard clip;
auto data_len = read_stdin(binary, data);
if (data_len < 0)
{
return 42;
}
std::string utf8_string(data.data(), data_len);
if (!Clipboard::from_utf8(utf8_string))
{
return 42;
}
return 0;
}
int main(int argc, char *argv[])
{
bool do_copy;
bool do_stop = false;
bool binary = parse_opts(argc, argv, do_stop, do_copy);
int res;
std::vector<char> data(MAX_DATA_SIZE, 0);
if (do_stop)
{
return 42;
}
if (!do_copy)
{
res = paste_clipboard(binary);
}
else
{
res = set_clipboard(binary, data);
}
return res;
}