/* * synergy -- mouse and keyboard sharing utility * Copyright (C) 2002 Chris Schoeneman * Copyright (C) 2006 Knut St. Osmundsen * * This package is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * found in the file COPYING that should have accompanied this file. * * This package is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include "CPMClipboardAnyTextConverter.h" // // CPMClipboardAnyTextConverter // CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter() { // do nothing } CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter() { // do nothing } IClipboard::EFormat CPMClipboardAnyTextConverter::getFormat() const { return IClipboard::kText; } ULONG CPMClipboardAnyTextConverter::getPMFormatInfo() const { return CFI_POINTER; } ULONG CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const { // convert linefeeds and then convert to desired encoding CString text = doFromIClipboard(convertLinefeedToPM(data)); UInt32 cb = text.size(); // copy to memory handle PVOID pv = NULL; APIRET rc = DosAllocSharedMem(&pv, NULL, cb, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE); if (rc == NO_ERROR) { return (ULONG)memcpy(pv, text.data(), cb); } return 0; } void CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const { DosFreeMem((PVOID)ulPMData); } CString CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const { // convert text const char *psz = (const char *)data; CString text = doToIClipboard(CString(psz)); // convert newlines return convertLinefeedToUnix(text); } CString CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const { // note -- we assume src is a valid UTF-8 string // count newlines in string UInt32 numNewlines = 0; UInt32 n = src.size(); for (const char* scan = src.c_str(); n > 0; ++scan, --n) { if (*scan == '\n') { ++numNewlines; } } if (numNewlines == 0) { return src; } // allocate new string CString dst; dst.reserve(src.size() + numNewlines); // copy string, converting newlines n = src.size(); for (const char* scan = src.c_str(); n > 0; ++scan, --n) { if (scan[0] == '\n') { dst += '\r'; } dst += scan[0]; } return dst; } CString CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const { // count newlines in string UInt32 numNewlines = 0; UInt32 n = src.size(); for (const char* scan = src.c_str(); n > 0; ++scan, --n) { if (scan[0] == '\r' && scan[1] == '\n') { ++numNewlines; } } if (numNewlines == 0) { return src; } // allocate new string CString dst; dst.reserve(src.size()); // copy string, converting newlines n = src.size(); for (const char* scan = src.c_str(); n > 0; ++scan, --n) { if (scan[0] != '\r' || scan[1] != '\n') { dst += scan[0]; } } return dst; }