Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

CMSWindowsClipboardAnyTextConverter.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "CMSWindowsClipboardAnyTextConverter.h"
00016 
00017 //
00018 // CMSWindowsClipboardAnyTextConverter
00019 //
00020 
00021 CMSWindowsClipboardAnyTextConverter::CMSWindowsClipboardAnyTextConverter()
00022 {
00023     // do nothing
00024 }
00025 
00026 CMSWindowsClipboardAnyTextConverter::~CMSWindowsClipboardAnyTextConverter()
00027 {
00028     // do nothing
00029 }
00030 
00031 IClipboard::EFormat
00032 CMSWindowsClipboardAnyTextConverter::getFormat() const
00033 {
00034     return IClipboard::kText;
00035 }
00036 
00037 HANDLE
00038 CMSWindowsClipboardAnyTextConverter::fromIClipboard(const CString& data) const
00039 {
00040     // convert linefeeds and then convert to desired encoding
00041     CString text = doFromIClipboard(convertLinefeedToWin32(data));
00042     UInt32 size  = text.size();
00043 
00044     // copy to memory handle
00045     HGLOBAL gData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
00046     if (gData != NULL) {
00047         // get a pointer to the allocated memory
00048         char* dst = (char*)GlobalLock(gData);
00049         if (dst != NULL) {
00050             memcpy(dst, text.data(), size);
00051             GlobalUnlock(gData);
00052         }
00053         else {
00054             GlobalFree(gData);
00055             gData = NULL;
00056         }
00057     }
00058 
00059     return gData;
00060 }
00061 
00062 CString
00063 CMSWindowsClipboardAnyTextConverter::toIClipboard(HANDLE data) const
00064 {
00065     // get datator
00066     const char* src = (const char*)GlobalLock(data);
00067     UInt32 srcSize = (UInt32)GlobalSize(data);
00068     if (src == NULL || srcSize <= 1) {
00069         return CString();
00070     }
00071 
00072     // convert text
00073     CString text = doToIClipboard(CString(src, srcSize));
00074 
00075     // release handle
00076     GlobalUnlock(data);
00077 
00078     // convert newlines
00079     return convertLinefeedToUnix(text);
00080 }
00081 
00082 CString
00083 CMSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(
00084                 const CString& src) const
00085 {
00086     // note -- we assume src is a valid UTF-8 string
00087 
00088     // count newlines in string
00089     UInt32 numNewlines = 0;
00090     UInt32 n           = src.size();
00091     for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
00092         if (*scan == '\n') {
00093             ++numNewlines;
00094         }
00095     }
00096     if (numNewlines == 0) {
00097         return src;
00098     }
00099 
00100     // allocate new string
00101     CString dst;
00102     dst.reserve(src.size() + numNewlines);
00103 
00104     // copy string, converting newlines
00105     n = src.size();
00106     for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
00107         if (scan[0] == '\n') {
00108             dst += '\r';
00109         }
00110         dst += scan[0];
00111     }
00112 
00113     return dst;
00114 }
00115 
00116 CString
00117 CMSWindowsClipboardAnyTextConverter::convertLinefeedToUnix(
00118                 const CString& src) const
00119 {
00120     // count newlines in string
00121     UInt32 numNewlines = 0;
00122     UInt32 n           = src.size();
00123     for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
00124         if (scan[0] == '\r' && scan[1] == '\n') {
00125             ++numNewlines;
00126         }
00127     }
00128     if (numNewlines == 0) {
00129         return src;
00130     }
00131 
00132     // allocate new string
00133     CString dst;
00134     dst.reserve(src.size());
00135 
00136     // copy string, converting newlines
00137     n = src.size();
00138     for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
00139         if (scan[0] != '\r' || scan[1] != '\n') {
00140             dst += scan[0];
00141         }
00142     }
00143 
00144     return dst;
00145 }

Generated on Fri Nov 6 00:21:14 2009 for synergy-plus by  doxygen 1.3.9.1