source: trunk/synergy/lib/platform/CPMClipboardAnyTextConverter.cpp@ 2752

Last change on this file since 2752 was 2752, checked in by bird, 19 years ago

Two classes (CPMScreen and CPMKeyState) + the hook dll left (and debugging of course).

File size: 3.0 KB
Line 
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
18//
19// CPMClipboardAnyTextConverter
20//
21
22CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter()
23{
24 // do nothing
25}
26
27CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter()
28{
29 // do nothing
30}
31
32IClipboard::EFormat
33CPMClipboardAnyTextConverter::getFormat() const
34{
35 return IClipboard::kText;
36}
37ULONG
38CPMClipboardAnyTextConverter::getPMFormatInfo() const
39{
40 return CFI_POINTER;
41}
42
43ULONG
44CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const
45{
46 // convert linefeeds and then convert to desired encoding
47 CString text = doFromIClipboard(convertLinefeedToPM(data));
48 UInt32 cb = text.size();
49
50 // copy to memory handle
51 PVOID pv = NULL;
52 APIRET rc = DosAllocSharedMem(&pv, NULL, cb, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
53 if (rc == NO_ERROR) {
54 return (ULONG)memcpy(pv, text.data(), cb);
55 }
56 return 0;
57}
58
59void
60CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
61{
62 DosFreeMem((PVOID)ulPMData);
63}
64
65CString
66CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const
67{
68 // convert text
69 const char *psz = (const char *)data;
70 CString text = doToIClipboard(CString(psz));
71
72 // convert newlines
73 return convertLinefeedToUnix(text);
74}
75
76CString
77CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
78{
79 // note -- we assume src is a valid UTF-8 string
80
81 // count newlines in string
82 UInt32 numNewlines = 0;
83 UInt32 n = src.size();
84 for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
85 if (*scan == '\n') {
86 ++numNewlines;
87 }
88 }
89 if (numNewlines == 0) {
90 return src;
91 }
92
93 // allocate new string
94 CString dst;
95 dst.reserve(src.size() + numNewlines);
96
97 // copy string, converting newlines
98 n = src.size();
99 for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
100 if (scan[0] == '\n') {
101 dst += '\r';
102 }
103 dst += scan[0];
104 }
105
106 return dst;
107}
108
109CString
110CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const
111{
112 // count newlines in string
113 UInt32 numNewlines = 0;
114 UInt32 n = src.size();
115 for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
116 if (scan[0] == '\r' && scan[1] == '\n') {
117 ++numNewlines;
118 }
119 }
120 if (numNewlines == 0) {
121 return src;
122 }
123
124 // allocate new string
125 CString dst;
126 dst.reserve(src.size());
127
128 // copy string, converting newlines
129 n = src.size();
130 for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
131 if (scan[0] != '\r' || scan[1] != '\n') {
132 dst += scan[0];
133 }
134 }
135
136 return dst;
137}
Note: See TracBrowser for help on using the repository browser.