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
RevLine 
[2749]1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2002 Chris Schoeneman
[2752]4 * Copyright (C) 2006 Knut St. Osmundsen
[2749]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
[2752]16#include "CPMClipboardAnyTextConverter.h"
[2749]17
18//
[2752]19// CPMClipboardAnyTextConverter
[2749]20//
21
[2752]22CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter()
[2749]23{
24 // do nothing
25}
26
[2752]27CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter()
[2749]28{
29 // do nothing
30}
31
32IClipboard::EFormat
[2752]33CPMClipboardAnyTextConverter::getFormat() const
[2749]34{
35 return IClipboard::kText;
36}
[2752]37ULONG
38CPMClipboardAnyTextConverter::getPMFormatInfo() const
39{
40 return CFI_POINTER;
41}
[2749]42
[2752]43ULONG
44CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const
[2749]45{
46 // convert linefeeds and then convert to desired encoding
[2752]47 CString text = doFromIClipboard(convertLinefeedToPM(data));
48 UInt32 cb = text.size();
[2749]49
50 // copy to memory handle
[2752]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}
[2749]58
[2752]59void
60CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
61{
62 DosFreeMem((PVOID)ulPMData);
[2749]63}
64
65CString
[2752]66CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const
[2749]67{
68 // convert text
[2752]69 const char *psz = (const char *)data;
70 CString text = doToIClipboard(CString(psz));
[2749]71
72 // convert newlines
73 return convertLinefeedToUnix(text);
74}
75
76CString
[2752]77CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
[2749]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
[2752]110CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const
[2749]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.