1 /*
2  * synergy -- mouse and keyboard sharing utility
3  * Copyright (C) 2012-2016 Symless Ltd.
4  * Copyright (C) 2004 Chris Schoeneman
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 LICENSE 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  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "platform/OSXClipboardAnyTextConverter.h"
20 
21 #include <algorithm>
22 
23 //
24 // OSXClipboardAnyTextConverter
25 //
26 
OSXClipboardAnyTextConverter()27 OSXClipboardAnyTextConverter::OSXClipboardAnyTextConverter()
28 {
29     // do nothing
30 }
31 
~OSXClipboardAnyTextConverter()32 OSXClipboardAnyTextConverter::~OSXClipboardAnyTextConverter()
33 {
34     // do nothing
35 }
36 
37 IClipboard::EFormat
getFormat() const38 OSXClipboardAnyTextConverter::getFormat() const
39 {
40     return IClipboard::kText;
41 }
42 
43 String
fromIClipboard(const String & data) const44 OSXClipboardAnyTextConverter::fromIClipboard(const String& data) const
45 {
46     // convert linefeeds and then convert to desired encoding
47     return doFromIClipboard(convertLinefeedToMacOS(data));
48 }
49 
50 String
toIClipboard(const String & data) const51 OSXClipboardAnyTextConverter::toIClipboard(const String& data) const
52 {
53     // convert text then newlines
54     return convertLinefeedToUnix(doToIClipboard(data));
55 }
56 
57 static
58 bool
isLF(char ch)59 isLF(char ch)
60 {
61     return (ch == '\n');
62 }
63 
64 static
65 bool
isCR(char ch)66 isCR(char ch)
67 {
68     return (ch == '\r');
69 }
70 
71 String
convertLinefeedToMacOS(const String & src)72 OSXClipboardAnyTextConverter::convertLinefeedToMacOS(const String& src)
73 {
74     // note -- we assume src is a valid UTF-8 string
75     String copy = src;
76 
77     std::replace_if(copy.begin(), copy.end(), isLF, '\r');
78 
79     return copy;
80 }
81 
82 String
convertLinefeedToUnix(const String & src)83 OSXClipboardAnyTextConverter::convertLinefeedToUnix(const String& src)
84 {
85     String copy = src;
86 
87     std::replace_if(copy.begin(), copy.end(), isCR, '\n');
88 
89     return copy;
90 }
91