-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyClipboard.c
119 lines (118 loc) · 3.43 KB
/
CopyClipboard.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__WINDOWS__)||defined(_WIN32)||defined(__CYGWIN__)
#include <Windows.h>
#include <winuser.h>
//Copy this I guess?
/*
here is a
multiline example
to test
*/
char * copyFromClipboard()
{
int length=0;
OpenClipboard(0);
HGLOBAL hglb;
hglb =GetClipboardData(CF_TEXT);
if(hglb!=NULL)
{
CloseClipboard();
return (char*)hglb;
}
else
{
printf("The clipboard was empty\n");
CloseClipboard();
return 0;
}
}
void copyToClipboard(const char *str)
{
const size_t len = strlen(str) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), str, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
}
//we can't run this outside of windows...
#elif defined(__APPLE__)
char * copyFromClipboard(char * output)
{
//This uses a temporary file to get the value of the clipboard.
//Once a better solution is found it will be implemented.
char c;
int inputSize=0;
FILE *fpoint = NULL;
freopen("tempfile.txt","w+",stdout);
const char issued_cmd[] ="pbpaste";
char cmd[strlen(issued_cmd)-1];
sprintf(cmd,issued_cmd);
system(cmd);
freopen("/dev/tty","w",stdout);
fpoint=fopen("tempfile.txt","r");
fseek(fpoint,0,SEEK_END);
inputSize = ftell(fpoint);
rewind(fpoint);
fread(output,sizeof(char),inputSize,fpoint);
fclose(fpoint);
remove("tempfile.txt");
printf("%s\n",output);
return output;
}
void copyToClipboard(const char *str)
{
const char issued_cmd[] ="echo '%s' | pbcopy";
char cmd[strlen(str)+strlen(issued_cmd)-1];
sprintf(cmd,issued_cmd,str);
system(cmd);
}
//we can't run this outside of MACOS
#else //catchall for linux devices
char * copyFromClipboard(char * output)
{
//This uses a temporary file to get the value of the clipboard.
//Once a better solution is found it will be implemented.
char c;
int inputSize=0;
FILE *fpoint = NULL;
const char issued_cmd[] ="xclip -selection clipboard -o > tempfile.txt";
char cmd[strlen(issued_cmd)-1];
sprintf(cmd,issued_cmd);
system(cmd);
fpoint=fopen("tempfile.txt","r");
fseek(fpoint,0,SEEK_END);
inputSize = ftell(fpoint);
char buffer[inputSize];
printf("got char length from file\n");
rewind(fpoint);
fread(buffer,(size_t)sizeof(char),(size_t)inputSize,fpoint);
puts("read into output");
fclose(fpoint);
remove("tempfile.txt");
output = buffer;
return output;
}
void copyToClipboard(const char *str)
{
const char issued_cmd[] ="echo '%s' | xclip -selection clipboard";
char cmd[strlen(str)+strlen(issued_cmd)-1];
sprintf(cmd,issued_cmd,str);
system(cmd);
}
#endif
/*
int main()
{
char * output;
output = copyFromClipboard(output);
printf("output from main \n%s\n",output);
copyToClipboard("This is only a test,\ndo not adjust your television set");
//copyFromClipboard("Worked ");
return 0;
}
*/