1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | * Copyright (C) 2006 Knut St. Osmundsen
|
---|
5 | *
|
---|
6 | * This package is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU General Public License
|
---|
8 | * found in the file COPYING that should have accompanied this file.
|
---|
9 | *
|
---|
10 | * This package is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include "CPMClipboardAnyTextConverter.h"
|
---|
17 | #include "CLog.h"
|
---|
18 |
|
---|
19 | //
|
---|
20 | // CPMClipboardAnyTextConverter
|
---|
21 | //
|
---|
22 |
|
---|
23 | CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter()
|
---|
24 | {
|
---|
25 | // do nothing
|
---|
26 | }
|
---|
27 |
|
---|
28 | CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter()
|
---|
29 | {
|
---|
30 | // do nothing
|
---|
31 | }
|
---|
32 |
|
---|
33 | IClipboard::EFormat
|
---|
34 | CPMClipboardAnyTextConverter::getFormat() const
|
---|
35 | {
|
---|
36 | return IClipboard::kText;
|
---|
37 | }
|
---|
38 | ULONG
|
---|
39 | CPMClipboardAnyTextConverter::getPMFormatInfo() const
|
---|
40 | {
|
---|
41 | return CFI_POINTER;
|
---|
42 | }
|
---|
43 |
|
---|
44 | ULONG
|
---|
45 | CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const
|
---|
46 | {
|
---|
47 | // convert linefeeds and then convert to desired encoding
|
---|
48 | CString text = doFromIClipboard(convertLinefeedToPM(data));
|
---|
49 | UInt32 cb = text.size();
|
---|
50 |
|
---|
51 | // copy to memory handle
|
---|
52 | PVOID pv = NULL;
|
---|
53 | APIRET rc = DosAllocSharedMem(&pv, NULL, cb + 1, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
|
---|
54 | if (rc == NO_ERROR) {
|
---|
55 | const char *psz = text.c_str();
|
---|
56 | LOG((CLOG_DEBUG "fromIClipboard: psz='%s' cb=%d strlen=%d\n", psz, cb, strlen(psz)));
|
---|
57 | return (ULONG)memcpy(pv, psz, cb);
|
---|
58 | }
|
---|
59 | return 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | void
|
---|
63 | CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
|
---|
64 | {
|
---|
65 | DosFreeMem((PVOID)ulPMData);
|
---|
66 | }
|
---|
67 |
|
---|
68 | CString
|
---|
69 | CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const
|
---|
70 | {
|
---|
71 | // convert text
|
---|
72 | const char *psz = (const char *)data;
|
---|
73 | LOG((CLOG_DEBUG "toIClipboard: psz='%s' strlen=%d\n", psz, strlen(psz)));
|
---|
74 | CString raw = CString(psz);
|
---|
75 | LOG((CLOG_DEBUG "toIClipboard: raw='%s'\n", raw.c_str()));
|
---|
76 | CString text = doToIClipboard(raw);
|
---|
77 | LOG((CLOG_DEBUG "toIClipboard: text='%s'\n", text.c_str()));
|
---|
78 |
|
---|
79 | // convert newlines
|
---|
80 | return convertLinefeedToUnix(text);
|
---|
81 | }
|
---|
82 |
|
---|
83 | CString
|
---|
84 | CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
|
---|
85 | {
|
---|
86 | // note -- we assume src is a valid UTF-8 string L
|
---|
87 |
|
---|
88 | // count newlines in string
|
---|
89 | UInt32 numNewlines = 0;
|
---|
90 | UInt32 n = src.size();
|
---|
91 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
---|
92 | if (*scan == '\n') {
|
---|
93 | ++numNewlines;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | if (numNewlines == 0) {
|
---|
97 | return src;
|
---|
98 | }
|
---|
99 |
|
---|
100 | // allocate new string
|
---|
101 | CString dst;
|
---|
102 | dst.reserve(src.size() + numNewlines);
|
---|
103 |
|
---|
104 | // copy string, converting newlines
|
---|
105 | n = src.size();
|
---|
106 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
---|
107 | if (scan[0] == '\n') {
|
---|
108 | dst += '\r';
|
---|
109 | }
|
---|
110 | dst += scan[0];
|
---|
111 | }
|
---|
112 |
|
---|
113 | return dst;
|
---|
114 | }
|
---|
115 |
|
---|
116 | CString
|
---|
117 | CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const
|
---|
118 | {
|
---|
119 | // count newlines in string
|
---|
120 | UInt32 numNewlines = 0;
|
---|
121 | UInt32 n = src.size();
|
---|
122 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
---|
123 | if (scan[0] == '\r' && scan[1] == '\n') {
|
---|
124 | ++numNewlines;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | if (numNewlines == 0) {
|
---|
128 | return src;
|
---|
129 | }
|
---|
130 |
|
---|
131 | // allocate new string
|
---|
132 | CString dst;
|
---|
133 | dst.reserve(src.size());
|
---|
134 |
|
---|
135 | // copy string, converting newlines
|
---|
136 | n = src.size();
|
---|
137 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
---|
138 | if (scan[0] != '\r' || scan[1] != '\n') {
|
---|
139 | dst += scan[0];
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | return dst;
|
---|
144 | }
|
---|