| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman
|
|---|
| 4 | * Copyright (C) 2006 Knut St. Osmundsen
|
|---|
| 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 COPYING 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 |
|
|---|
| 16 | #include "CPMClipboardAnyTextConverter.h"
|
|---|
| 17 |
|
|---|
| 18 | //
|
|---|
| 19 | // CPMClipboardAnyTextConverter
|
|---|
| 20 | //
|
|---|
| 21 |
|
|---|
| 22 | CPMClipboardAnyTextConverter::CPMClipboardAnyTextConverter()
|
|---|
| 23 | {
|
|---|
| 24 | // do nothing
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | CPMClipboardAnyTextConverter::~CPMClipboardAnyTextConverter()
|
|---|
| 28 | {
|
|---|
| 29 | // do nothing
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | IClipboard::EFormat
|
|---|
| 33 | CPMClipboardAnyTextConverter::getFormat() const
|
|---|
| 34 | {
|
|---|
| 35 | return IClipboard::kText;
|
|---|
| 36 | }
|
|---|
| 37 | ULONG
|
|---|
| 38 | CPMClipboardAnyTextConverter::getPMFormatInfo() const
|
|---|
| 39 | {
|
|---|
| 40 | return CFI_POINTER;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | ULONG
|
|---|
| 44 | CPMClipboardAnyTextConverter::fromIClipboard(const CString& data) const
|
|---|
| 45 | {
|
|---|
| 46 | // convert linefeeds and then convert to desired encoding
|
|---|
| 47 | CString text = doFromIClipboard(convertLinefeedToPM(data));
|
|---|
| 48 | UInt32 cb = text.size();
|
|---|
| 49 |
|
|---|
| 50 | // copy to memory handle
|
|---|
| 51 | PVOID pv = NULL;
|
|---|
| 52 | APIRET rc = DosAllocSharedMem(&pv, NULL, cb, PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE | OBJ_GIVEABLE);
|
|---|
| 53 | if (rc == NO_ERROR) {
|
|---|
| 54 | return (ULONG)memcpy(pv, text.data(), cb);
|
|---|
| 55 | }
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void
|
|---|
| 60 | CPMClipboardAnyTextConverter::freePMData(ULONG ulPMData) const
|
|---|
| 61 | {
|
|---|
| 62 | DosFreeMem((PVOID)ulPMData);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | CString
|
|---|
| 66 | CPMClipboardAnyTextConverter::toIClipboard(ULONG data) const
|
|---|
| 67 | {
|
|---|
| 68 | // convert text
|
|---|
| 69 | const char *psz = (const char *)data;
|
|---|
| 70 | CString text = doToIClipboard(CString(psz));
|
|---|
| 71 |
|
|---|
| 72 | // convert newlines
|
|---|
| 73 | return convertLinefeedToUnix(text);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | CString
|
|---|
| 77 | CPMClipboardAnyTextConverter::convertLinefeedToPM(const CString& src) const
|
|---|
| 78 | {
|
|---|
| 79 | // note -- we assume src is a valid UTF-8 string
|
|---|
| 80 |
|
|---|
| 81 | // count newlines in string
|
|---|
| 82 | UInt32 numNewlines = 0;
|
|---|
| 83 | UInt32 n = src.size();
|
|---|
| 84 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
|---|
| 85 | if (*scan == '\n') {
|
|---|
| 86 | ++numNewlines;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | if (numNewlines == 0) {
|
|---|
| 90 | return src;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | // allocate new string
|
|---|
| 94 | CString dst;
|
|---|
| 95 | dst.reserve(src.size() + numNewlines);
|
|---|
| 96 |
|
|---|
| 97 | // copy string, converting newlines
|
|---|
| 98 | n = src.size();
|
|---|
| 99 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
|---|
| 100 | if (scan[0] == '\n') {
|
|---|
| 101 | dst += '\r';
|
|---|
| 102 | }
|
|---|
| 103 | dst += scan[0];
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return dst;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | CString
|
|---|
| 110 | CPMClipboardAnyTextConverter::convertLinefeedToUnix(const CString& src) const
|
|---|
| 111 | {
|
|---|
| 112 | // count newlines in string
|
|---|
| 113 | UInt32 numNewlines = 0;
|
|---|
| 114 | UInt32 n = src.size();
|
|---|
| 115 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
|---|
| 116 | if (scan[0] == '\r' && scan[1] == '\n') {
|
|---|
| 117 | ++numNewlines;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | if (numNewlines == 0) {
|
|---|
| 121 | return src;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | // allocate new string
|
|---|
| 125 | CString dst;
|
|---|
| 126 | dst.reserve(src.size());
|
|---|
| 127 |
|
|---|
| 128 | // copy string, converting newlines
|
|---|
| 129 | n = src.size();
|
|---|
| 130 | for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
|---|
| 131 | if (scan[0] != '\r' || scan[1] != '\n') {
|
|---|
| 132 | dst += scan[0];
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return dst;
|
|---|
| 137 | }
|
|---|