-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathclipfunc.cpp
219 lines (185 loc) · 5.54 KB
/
clipfunc.cpp
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
/* clipfunc.cpp: functions for getting/saving data to/from the clipboard
Copyright (C) 2012, Project Pluto
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include <stdio.h>
#include "watdefs.h"
int copy_buffer_to_clipboard( const char *contents, const long length);
int copy_file_to_clipboard( const char *filename); /* clipfunc.cpp */
int clipboard_to_file( const char *filename, const int append,
const bool use_selection); /* clipfunc.cpp */
#ifdef _WIN32
#include <windows.h>
/* Following is copied shamelessly from pdcclip.c from PDCurses. */
/* With one strange addition: "solo" line feeds are expanded to */
/* CR/LFs, resulting in the output being 'n_added' bytes larger. */
int copy_buffer_to_clipboard( const char *contents, const long length)
{
HGLOBAL ptr1;
LPTSTR ptr2;
int i, n_added;
if (!OpenClipboard(NULL))
return -3;
for( i = n_added = 0; i < length; i++)
if( contents[i] != 13 && contents[i + 1] == 10)
n_added++;
ptr1 = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
(length + 1 + n_added) * sizeof(TCHAR));
if (!ptr1)
return -4;
ptr2 = (LPTSTR)GlobalLock(ptr1);
if( !ptr2)
{
GlobalFree(ptr1);
return -6;
}
for( i = n_added = 0; i < length; i++)
{
ptr2[i + n_added] = contents[i];
if( contents[i] != 13 && contents[i + 1] == 10)
{
n_added++;
ptr2[i + n_added] = 13;
}
}
ptr2[length + n_added] = 0;
// memcpy((char *)ptr2, contents, length + 1);
GlobalUnlock(ptr1);
EmptyClipboard();
if (!SetClipboardData( CF_TEXT, ptr1))
{
GlobalFree(ptr1);
return -5;
}
CloseClipboard();
return 0;
}
int copy_file_to_clipboard( const char *filename)
{
FILE *ifile = fopen( filename, "rb");
int rval;
if( ifile)
{
size_t filesize;
char *buff;
fseek( ifile, 0L, SEEK_END);
filesize = ftell( ifile);
fseek( ifile, 0L, SEEK_SET);
buff = (char *)malloc( filesize);
if( buff)
{
fread( buff, 1, filesize, ifile);
rval = copy_buffer_to_clipboard( buff, (long)filesize);
free( buff);
}
else
rval = -2;
fclose( ifile);
}
else
rval = -1;
return( rval);
}
int clipboard_to_file( const char *filename, const int append,
const bool use_selection)
{
int rval;
/* sadly, we can't get at the selected text in MSWin */
INTENTIONALLY_UNUSED_PARAMETER( use_selection);
if( !OpenClipboard(NULL))
rval = -1;
else
{
void *text;
FILE *ofile;
const char *permits = (append ? "ab" : "wb");
HANDLE handle = GetClipboardData( CF_OEMTEXT);
if( !handle)
rval = -2;
else if( (text = GlobalLock( handle)) == NULL)
rval = -4;
else if( (ofile = fopen( filename, permits)) != NULL)
{
rval = 0;
fwrite( text, 1, strlen( (char *)text), ofile);
fclose( ofile);
}
else
rval = -3;
CloseClipboard();
}
return rval;
}
#elif defined __PDCURSES__ && !defined VT
#include <stdlib.h>
#include "curses.h"
int clipboard_to_file( const char *filename, const int append,
const bool use_selection)
{
long size = -99;
char *contents;
int err_code;
INTENTIONALLY_UNUSED_PARAMETER( use_selection);
err_code = PDC_getclipboard( &contents, &size);
if( err_code == PDC_CLIP_SUCCESS)
{
FILE *ofile = fopen( filename, "wb");
if( ofile)
{
fwrite( contents, size, 1, ofile);
fclose( ofile);
}
PDC_freeclipboard( contents);
}
return( err_code);
}
int copy_file_to_clipboard( const char *filename)
{
FILE *ifile = fopen( filename, "rb");
int err_code = -1;
if( ifile)
{
size_t length, bytes_read;
char *buff;
fseek( ifile, 0L, SEEK_END);
length = (size_t)ftell( ifile);
fseek( ifile, 0L, SEEK_SET);
buff = (char *)malloc( length + 1);
assert( buff);
buff[length] = '\0';
bytes_read = fread( buff, 1, length, ifile);
assert( bytes_read == length);
fclose( ifile);
err_code = PDC_setclipboard( buff, length);
free( buff);
}
return( err_code);
}
#else /* non-PDCurses, non-Windows: use xclip */
#include <stdlib.h>
int clipboard_to_file( const char *filename, const int append,
const bool use_selection)
{
char cmd[80];
snprintf( cmd, sizeof( cmd), "xclip -o %s >%c %s",
(use_selection ? "" : "-se c"),
(append ? '>' : ' '), filename);
return( system( cmd));
}
int copy_file_to_clipboard( const char *filename)
{
char cmd[80];
snprintf( cmd, sizeof( cmd), "xclip -i %s", filename);
return( system( cmd));
}
#endif