1 /*
2  * barrier -- 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 
fromIClipboard(const std::string & data) const43 std::string OSXClipboardAnyTextConverter::fromIClipboard(const std::string& data) const
44 {
45     // convert linefeeds and then convert to desired encoding
46     return doFromIClipboard(convertLinefeedToMacOS(data));
47 }
48 
toIClipboard(const std::string & data) const49 std::string OSXClipboardAnyTextConverter::toIClipboard(const std::string& data) const
50 {
51     // convert text then newlines
52     return convertLinefeedToUnix(doToIClipboard(data));
53 }
54 
55 static
56 bool
isLF(char ch)57 isLF(char ch)
58 {
59     return (ch == '\n');
60 }
61 
62 static
63 bool
isCR(char ch)64 isCR(char ch)
65 {
66     return (ch == '\r');
67 }
68 
convertLinefeedToMacOS(const std::string & src)69 std::string OSXClipboardAnyTextConverter::convertLinefeedToMacOS(const std::string& src)
70 {
71     // note -- we assume src is a valid UTF-8 string
72     std::string copy = src;
73 
74     std::replace_if(copy.begin(), copy.end(), isLF, '\r');
75 
76     return copy;
77 }
78 
convertLinefeedToUnix(const std::string & src)79 std::string OSXClipboardAnyTextConverter::convertLinefeedToUnix(const std::string& src)
80 {
81     std::string copy = src;
82 
83     std::replace_if(copy.begin(), copy.end(), isCR, '\n');
84 
85     return copy;
86 }
87