• Main Page
  • Classes
  • Files
  • File List

CMSWindowsClipboardHTMLConverter.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CMSWindowsClipboardHTMLConverter.h"
00020 #include "CStringUtil.h"
00021 
00022 //
00023 // CMSWindowsClipboardHTMLConverter
00024 //
00025 
00026 CMSWindowsClipboardHTMLConverter::CMSWindowsClipboardHTMLConverter()
00027 {
00028     m_format = RegisterClipboardFormat("HTML Format");
00029 }
00030 
00031 CMSWindowsClipboardHTMLConverter::~CMSWindowsClipboardHTMLConverter()
00032 {
00033     // do nothing
00034 }
00035 
00036 IClipboard::EFormat
00037 CMSWindowsClipboardHTMLConverter::getFormat() const
00038 {
00039     return IClipboard::kHTML;
00040 }
00041 
00042 UINT
00043 CMSWindowsClipboardHTMLConverter::getWin32Format() const
00044 {
00045     return m_format;
00046 }
00047 
00048 CString
00049 CMSWindowsClipboardHTMLConverter::doFromIClipboard(const CString& data) const
00050 {
00051     // prepare to CF_HTML format prefix and suffix
00052     CString prefix("Version:0.9\r\nStartHTML:0000000105\r\n"
00053                     "EndHTML:ZZZZZZZZZZ\r\n"
00054                     "StartFragment:XXXXXXXXXX\r\nEndFragment:YYYYYYYYYY\r\n"
00055                     "<!DOCTYPE><HTML><BODY><!--StartFragment-->");
00056     CString suffix("<!--EndFragment--></BODY></HTML>\r\n");
00057 
00058     // Get byte offsets for header
00059     UInt32 StartFragment = (UInt32)prefix.size();
00060     UInt32 EndFragment   = StartFragment + (UInt32)data.size();
00061     // StartHTML is constant by the design of the prefix
00062     UInt32 EndHTML = EndFragment + (UInt32)suffix.size();
00063 
00064     prefix.replace(prefix.find("XXXXXXXXXX"), 10,
00065                             CStringUtil::print("%010u", StartFragment));
00066     prefix.replace(prefix.find("YYYYYYYYYY"), 10,
00067                             CStringUtil::print("%010u", EndFragment));
00068     prefix.replace(prefix.find("ZZZZZZZZZZ"), 10,
00069                             CStringUtil::print("%010u", EndHTML));
00070 
00071     // concatenate
00072     prefix += data;
00073     prefix += suffix;
00074     return prefix;
00075 }
00076 
00077 CString
00078 CMSWindowsClipboardHTMLConverter::doToIClipboard(const CString& data) const
00079 {
00080     // get fragment start/end args
00081     CString startArg = findArg(data, "StartFragment");
00082     CString endArg   = findArg(data, "EndFragment");
00083     if (startArg.empty() || endArg.empty()) {
00084         return CString();
00085     }
00086 
00087     // convert args to integers
00088     SInt32 start = (SInt32)atoi(startArg.c_str());
00089     SInt32 end   = (SInt32)atoi(endArg.c_str());
00090     if (start <= 0 || end <= 0 || start >= end) {
00091         return CString();
00092     }
00093 
00094     // extract the fragment
00095     return data.substr(start, end - start);
00096 }
00097 
00098 CString
00099 CMSWindowsClipboardHTMLConverter::findArg(
00100                 const CString& data, const CString& name) const
00101 {
00102     CString::size_type i = data.find(name);
00103     if (i == CString::npos) {
00104         return CString();
00105     }
00106     i = data.find_first_of(":\r\n", i);
00107     if (i == CString::npos || data[i] != ':') {
00108         return CString();
00109     }
00110     i = data.find_first_of("0123456789\r\n", i + 1);
00111     if (i == CString::npos || data[i] == '\r' || data[i] == '\n') {
00112         return CString();
00113     }
00114     CString::size_type j = data.find_first_not_of("0123456789", i);
00115     if (j == CString::npos) {
00116         j = data.size();
00117     }
00118     return data.substr(i, j - i);
00119 }

Generated on Sat May 25 2013 00:00:04 for Synergy by  doxygen 1.7.1