1706f2543Smrg/* 2706f2543Smrg *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved. 3706f2543Smrg * 4706f2543Smrg *Permission is hereby granted, free of charge, to any person obtaining 5706f2543Smrg * a copy of this software and associated documentation files (the 6706f2543Smrg *"Software"), to deal in the Software without restriction, including 7706f2543Smrg *without limitation the rights to use, copy, modify, merge, publish, 8706f2543Smrg *distribute, sublicense, and/or sell copies of the Software, and to 9706f2543Smrg *permit persons to whom the Software is furnished to do so, subject to 10706f2543Smrg *the following conditions: 11706f2543Smrg * 12706f2543Smrg *The above copyright notice and this permission notice shall be 13706f2543Smrg *included in all copies or substantial portions of the Software. 14706f2543Smrg * 15706f2543Smrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16706f2543Smrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17706f2543Smrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18706f2543Smrg *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR 19706f2543Smrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 20706f2543Smrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21706f2543Smrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22706f2543Smrg * 23706f2543Smrg *Except as contained in this notice, the name of Harold L Hunt II 24706f2543Smrg *shall not be used in advertising or otherwise to promote the sale, use 25706f2543Smrg *or other dealings in this Software without prior written authorization 26706f2543Smrg *from Harold L Hunt II. 27706f2543Smrg * 28706f2543Smrg * Authors: Harold L Hunt II 29706f2543Smrg */ 30706f2543Smrg 31706f2543Smrg#ifdef HAVE_XWIN_CONFIG_H 32706f2543Smrg#include <xwin-config.h> 33706f2543Smrg#endif 34706f2543Smrg#include "win.h" 35706f2543Smrg#include <stdio.h> 36706f2543Smrg#include <stdlib.h> 37706f2543Smrg 38706f2543Smrgvoid 39706f2543SmrgwinClipboardDOStoUNIX (char *pszSrc, int iLength); 40706f2543Smrgvoid 41706f2543SmrgwinClipboardUNIXtoDOS (unsigned char **ppszData, int iLength); 42706f2543Smrg 43706f2543Smrg/* 44706f2543Smrg * Convert \r\n to \n 45706f2543Smrg * 46706f2543Smrg * NOTE: This was heavily inspired by, Cygwin's 47706f2543Smrg * winsup/cygwin/fhandler.cc/fhandler_base::read () 48706f2543Smrg */ 49706f2543Smrg 50706f2543Smrgvoid 51706f2543SmrgwinClipboardDOStoUNIX (char *pszSrc, int iLength) 52706f2543Smrg{ 53706f2543Smrg char *pszDest = pszSrc; 54706f2543Smrg char *pszEnd = pszSrc + iLength; 55706f2543Smrg 56706f2543Smrg /* Loop until the last character */ 57706f2543Smrg while (pszSrc < pszEnd) 58706f2543Smrg { 59706f2543Smrg /* Copy the current source character to current destination character */ 60706f2543Smrg *pszDest = *pszSrc; 61706f2543Smrg 62706f2543Smrg /* Advance to the next source character */ 63706f2543Smrg pszSrc++; 64706f2543Smrg 65706f2543Smrg /* Don't advance the destination character if we need to drop an \r */ 66706f2543Smrg if (*pszDest != '\r' || *pszSrc != '\n') 67706f2543Smrg pszDest++; 68706f2543Smrg } 69706f2543Smrg 70706f2543Smrg /* Move the terminating null */ 71706f2543Smrg *pszDest = '\0'; 72706f2543Smrg} 73706f2543Smrg 74706f2543Smrg 75706f2543Smrg/* 76706f2543Smrg * Convert \n to \r\n 77706f2543Smrg */ 78706f2543Smrg 79706f2543Smrgvoid 80706f2543SmrgwinClipboardUNIXtoDOS (unsigned char **ppszData, int iLength) 81706f2543Smrg{ 82706f2543Smrg int iNewlineCount = 0; 83706f2543Smrg unsigned char *pszSrc = *ppszData; 84706f2543Smrg unsigned char *pszEnd = pszSrc + iLength; 85706f2543Smrg unsigned char *pszDest = NULL, *pszDestBegin = NULL; 86706f2543Smrg 87706f2543Smrg winDebug("UNIXtoDOS () - Original data:'%s'\n", *ppszData); 88706f2543Smrg 89706f2543Smrg /* Count \n characters without leading \r */ 90706f2543Smrg while (pszSrc < pszEnd) 91706f2543Smrg { 92706f2543Smrg /* Skip ahead two character if found set of \r\n */ 93706f2543Smrg if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') 94706f2543Smrg { 95706f2543Smrg pszSrc += 2; 96706f2543Smrg continue; 97706f2543Smrg } 98706f2543Smrg 99706f2543Smrg /* Increment the count if found naked \n */ 100706f2543Smrg if (*pszSrc == '\n') 101706f2543Smrg { 102706f2543Smrg iNewlineCount++; 103706f2543Smrg } 104706f2543Smrg 105706f2543Smrg pszSrc++; 106706f2543Smrg } 107706f2543Smrg 108706f2543Smrg /* Return if no naked \n's */ 109706f2543Smrg if (iNewlineCount == 0) 110706f2543Smrg return; 111706f2543Smrg 112706f2543Smrg /* Allocate a new string */ 113706f2543Smrg pszDestBegin = pszDest = malloc (iLength + iNewlineCount + 1); 114706f2543Smrg 115706f2543Smrg /* Set source pointer to beginning of data string */ 116706f2543Smrg pszSrc = *ppszData; 117706f2543Smrg 118706f2543Smrg /* Loop through all characters in source string */ 119706f2543Smrg while (pszSrc < pszEnd) 120706f2543Smrg { 121706f2543Smrg /* Copy line endings that are already valid */ 122706f2543Smrg if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') 123706f2543Smrg { 124706f2543Smrg *pszDest = *pszSrc; 125706f2543Smrg *(pszDest + 1) = *(pszSrc + 1); 126706f2543Smrg pszDest += 2; 127706f2543Smrg pszSrc += 2; 128706f2543Smrg continue; 129706f2543Smrg } 130706f2543Smrg 131706f2543Smrg /* Add \r to naked \n's */ 132706f2543Smrg if (*pszSrc == '\n') 133706f2543Smrg { 134706f2543Smrg *pszDest = '\r'; 135706f2543Smrg *(pszDest + 1) = *pszSrc; 136706f2543Smrg pszDest += 2; 137706f2543Smrg pszSrc += 1; 138706f2543Smrg continue; 139706f2543Smrg } 140706f2543Smrg 141706f2543Smrg /* Copy normal characters */ 142706f2543Smrg *pszDest = *pszSrc; 143706f2543Smrg pszSrc++; 144706f2543Smrg pszDest++; 145706f2543Smrg } 146706f2543Smrg 147706f2543Smrg /* Put terminating null at end of new string */ 148706f2543Smrg *pszDest = '\0'; 149706f2543Smrg 150706f2543Smrg /* Swap string pointers */ 151706f2543Smrg free (*ppszData); 152706f2543Smrg *ppszData = pszDestBegin; 153706f2543Smrg 154706f2543Smrg winDebug("UNIXtoDOS () - Final string:'%s'\n", pszDestBegin); 155706f2543Smrg} 156