-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipTabToHtml.cpp
481 lines (426 loc) · 12 KB
/
ClipTabToHtml.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// ClipTabToHtml.cpp : Defines the class behaviors for the application.
//
#include "framework.h"
#include "ClipTabToHtml.h"
//#include "ClipTabToHtmlDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int BuildHtmlTable(const TCHAR* source, TCHAR* target);
void CopyHTML(char* html);
// CClipTabToHtmlApp
BEGIN_MESSAGE_MAP(CClipTabToHtmlApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CClipTabToHtmlApp construction
CClipTabToHtmlApp::CClipTabToHtmlApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CClipTabToHtmlApp object
CClipTabToHtmlApp theApp;
// CClipTabToHtmlApp initialization
BOOL CClipTabToHtmlApp::InitInstance()
{
/*
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
*/
CWinApp::InitInstance();
ConvertClipBoard();
/*
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Activate "Windows Native" visual manager for enabling themes in MFC controls
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("ClipTabToHtml"));
CClipTabToHtmlDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
ConvertClipBoard();
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
// Delete the shell manager created above.
if (pShellManager != nullptr)
{
delete pShellManager;
}
#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
ControlBarCleanUp();
#endif
*/
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
// CopyHtml() - Copies given HTML to the clipboard.
// The HTML/BODY blanket is provided, so you only need to
// call it like CallHtml("<b>This is a test</b>");
void CopyHTML(char* html)
{
// Create temporary buffer for HTML header...
char* buf = new char[400 + strlen(html)];
if (!buf) return;
// Get clipboard id for HTML format...
static int cfid = 0;
if (!cfid) cfid = RegisterClipboardFormat(TEXT("HTML Format"));
// Create a template string for the HTML header...
strcpy(buf,
"Version:0.9\r\n"
"StartHTML:00000000\r\n"
"EndHTML:00000000\r\n"
"StartFragment:00000000\r\n"
"EndFragment:00000000\r\n"
"<html><body>\r\n"
"<!--StartFragment -->\r\n");
// Append the HTML...
strcat(buf, html);
strcat(buf, "\r\n");
// Finish up the HTML format...
strcat(buf,
"<!--EndFragment-->\r\n"
"</body>\r\n"
"</html>");
// Now go back, calculate all the lengths, and write out the
// necessary header information. Note, wsprintf() truncates the
// string when you overwrite it so you follow up with code to replace
// the 0 appended at the end with a '\r'...
char* ptr = strstr(buf, "StartHTML");
// TODO: Do not use. Consider using one of the following functions instead:
// StringCbPrintf, StringCbPrintfEx, StringCchPrintf, or StringCchPrintfEx.
// See Security Considerations.
wsprintfA(ptr + 10, "%08u", strstr(buf, "<html>") - buf);
*(ptr + 10 + 8) = '\r';
ptr = strstr(buf, "EndHTML");
wsprintfA(ptr + 8, "%08u", strlen(buf));
*(ptr + 8 + 8) = '\r';
ptr = strstr(buf, "StartFragment");
wsprintfA(ptr + 14, "%08u", strstr(buf, "<!--StartFrag") - buf);
*(ptr + 14 + 8) = '\r';
ptr = strstr(buf, "EndFragment");
wsprintfA(ptr + 12, "%08u", strstr(buf, "<!--EndFrag") - buf);
*(ptr + 12 + 8) = '\r';
// Now you have everything in place ready to put on the clipboard.
// Open the clipboard...
if (OpenClipboard(0))
{
// Empty what's in there...
//EmptyClipboard();
// Allocate global memory for transfer...
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, strlen(buf) + 4);
// Put your string in the global memory...
char* ptr = (char*)GlobalLock(hText);
strcpy(ptr, buf);
GlobalUnlock(hText);
::SetClipboardData(cfid, hText);
CloseClipboard();
// Free memory...
GlobalFree(hText);
}
// Clean up...
delete[] buf;
}
int BuildHtmlTable(const TCHAR* source, TCHAR* target)
{
int tsize = 0;
// html table
// having style tag inside of body is not W3C-conform, but works.
static const TCHAR table_open[] = TEXT("<style> th, td { border: thin solid; padding-right: 4px; padding-left: 4px; } </style><table style=\"border-collapse: collapse;\">");
static const TCHAR table_close[] = TEXT("</table>");
BOOL bTable = FALSE;
// table header
static const TCHAR thead_open[] = TEXT("<thead>");
static const TCHAR thead_close[] = TEXT("</thead>");
BOOL bTableHead = FALSE;
// table body
static const TCHAR tbody_open[] = TEXT("<tbody>");
static const TCHAR tbody_close[] = TEXT("</tbody>");
BOOL bTableBody = FALSE;
// table row
static const TCHAR tr_open[] = TEXT("<tr>");
static const TCHAR tr_close[] = TEXT("</tr>");
BOOL bTableRow = FALSE;
// header and data item
static const TCHAR th_open[] = TEXT("<th>");
static const TCHAR th_close[] = TEXT("</th>");
static const TCHAR td_open[] = TEXT("<td>");
static const TCHAR td_close[] = TEXT("</td>");
BOOL bTableItem = FALSE;
TCHAR c, d;
int i = 0;
// skip empty lines
while ((c = source[i]) != '\0')
{
if (c != '\n' && c != '\r')
{
if (target)
lstrcpy(target + tsize, table_open);
tsize += lstrlen(table_open);
bTable = TRUE;
if (target)
lstrcpy(target + tsize, tr_open);
tsize += lstrlen(tr_open);
bTableRow = TRUE;
if (target)
lstrcpy(target + tsize, th_open);
tsize += lstrlen(th_open);
bTableItem = TRUE;
break;
}
i++;
}
// header row
while ((c = source[i++]) != '\0')
{
if (c == '\n' || c == '\r')
{
if (c == '\r' && (d = source[i]) == '\n')
c = source[i++];
else if (c == '\n' && (d = source[i]) == '\r')
c = source[i++];
if (target)
lstrcpy(target + tsize, th_close);
tsize += lstrlen(th_close);
bTableItem = FALSE;
if (target)
lstrcpy(target + tsize, tr_close);
tsize += lstrlen(tr_close);
bTableRow = FALSE;
break;
}
switch (c)
{
case '\t':
if (target)
lstrcpy(target + tsize, th_close);
tsize += lstrlen(th_close);
if (target)
lstrcpy(target + tsize, th_open);
tsize += lstrlen(th_open);
bTableItem = TRUE;
break;
case '<':
if (target)
lstrcpy(target + tsize, TEXT("<"));
tsize += lstrlen(TEXT("<"));
break;
case '>':
if (target)
lstrcpy(target + tsize, TEXT(">"));
tsize += lstrlen(TEXT(">"));
break;
case '&':
if (target)
lstrcpy(target + tsize, TEXT("&"));
tsize += lstrlen(TEXT("&"));
break;
default:
if (target)
target[tsize] = c;
tsize++;
}
}
if ((c = source[i]) == '\0') // only header, no data rows
{
// close everything and return
if (bTableItem)
{
if (target)
lstrcpy(target + tsize, th_close);
tsize += lstrlen(th_close);
bTableItem = FALSE;
}
if (bTableRow)
{
if (target)
lstrcpy(target + tsize, tr_close);
tsize += lstrlen(tr_close);
bTableRow = FALSE;
}
if (bTable)
{
if (target)
lstrcpy(target + tsize, table_close);
tsize += lstrlen(table_close);
bTable = FALSE;
}
if (target)
target[tsize] = '\0';
tsize++;
return tsize;
}
else
{
// data row follows: open first row, first item
if (target)
lstrcpy(target + tsize, tr_open);
tsize += lstrlen(tr_open);
bTableRow = TRUE;
if (target)
lstrcpy(target + tsize, td_open);
tsize += lstrlen(td_open);
bTableItem = TRUE;
}
// data rows
while ((c = source[i++]) != '\0')
{
if (c == '\n' || c == '\r')
{
if (c == '\r' && (d = source[i]) == '\n')
i++;
else if (c == '\n' && (d = source[i]) == '\r')
i++;
// close the current oitem and row
if (bTableItem)
{
if (target)
lstrcpy(target + tsize, td_close);
tsize += lstrlen(td_close);
bTableItem = FALSE;
}
if (bTableRow)
{
if (target)
lstrcpy(target + tsize, tr_close);
tsize += lstrlen(tr_close);
bTableRow = FALSE;
}
continue;
}
if (!bTableRow)
{
if (target)
lstrcpy(target + tsize, tr_open);
tsize += lstrlen(tr_open);
bTableRow = TRUE;
}
if (!bTableItem)
{
if (target)
lstrcpy(target + tsize, td_open);
tsize += lstrlen(td_open);
bTableItem = TRUE;
}
switch (c)
{
case '\t':
if (target)
lstrcpy(target + tsize, td_close);
tsize += lstrlen(td_close);
if (target)
lstrcpy(target + tsize, td_open);
tsize += lstrlen(td_open);
break;
case '<':
if (target)
lstrcpy(target + tsize, TEXT("<"));
tsize += lstrlen(TEXT("<"));
break;
case '>':
if (target)
lstrcpy(target + tsize, TEXT(">"));
tsize += lstrlen(TEXT(">"));
break;
case '&':
if (target)
lstrcpy(target + tsize, TEXT("&"));
tsize += lstrlen(TEXT("&"));
break;
default:
if (target)
target[tsize] = c;
tsize++;
}
}
if (bTableItem)
{
if (target)
lstrcpy(target + tsize, td_close);
tsize += lstrlen(td_close);
bTableItem = FALSE;
}
if (bTableRow)
{
if (target)
lstrcpy(target + tsize, tr_close);
tsize += lstrlen(tr_close);
bTableRow = FALSE;
}
if (bTable)
{
if (target)
lstrcpy(target + tsize, table_close);
tsize += lstrlen(table_close);
bTable = FALSE;
}
if (target)
target[tsize] = '\0';
tsize++;
return tsize;
}
void CClipTabToHtmlApp::ConvertClipBoard()
{
//open the clipboard
#ifdef _UNICODE
if (::IsClipboardFormatAvailable(CF_UNICODETEXT) && ::OpenClipboard(NULL))
{
HANDLE hData = ::GetClipboardData(CF_UNICODETEXT);
WCHAR* buffer = (WCHAR*)::GlobalLock(hData);
int tablesize = BuildHtmlTable(buffer, nullptr); // find out how much memory is needed
WCHAR* wtable = new WCHAR[tablesize]; // allocate that memory
BuildHtmlTable(buffer, wtable); // wtable is widechar
int len = (int)wcslen(wtable) + 1; // include trailing '\0'
// each WCHAR may expand to maximal 4 byte in UTF-8
char* utf8table = new char[len * 4];
int cnt = ::WideCharToMultiByte(CP_UTF8, 0, wtable, len, utf8table, len * 4, NULL, FALSE);
ASSERT(cnt > 0);
delete[] wtable;
ASSERT(cnt >= 0);
::GlobalUnlock(hData);
::CloseClipboard();
if (cnt > 0 && cnt <= len * 4)
CopyHTML(utf8table); // create as clipboard "HTML Format"
delete[] utf8table;
}
// else
#endif
/*
if (IsClipboardFormatAvailable(CF_TEXT) && ::OpenClipboard(NULL))
{
HANDLE hData = GetClipboardData(CF_TEXT);
char* buffer = (char*)GlobalLock(hData);
sClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}
*/
}