135c4bbdfSmrg/*
235c4bbdfSmrg *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
335c4bbdfSmrg *
435c4bbdfSmrg *Permission is hereby granted, free of charge, to any person obtaining
535c4bbdfSmrg * a copy of this software and associated documentation files (the
635c4bbdfSmrg *"Software"), to deal in the Software without restriction, including
735c4bbdfSmrg *without limitation the rights to use, copy, modify, merge, publish,
835c4bbdfSmrg *distribute, sublicense, and/or sell copies of the Software, and to
935c4bbdfSmrg *permit persons to whom the Software is furnished to do so, subject to
1035c4bbdfSmrg *the following conditions:
1135c4bbdfSmrg *
1235c4bbdfSmrg *The above copyright notice and this permission notice shall be
1335c4bbdfSmrg *included in all copies or substantial portions of the Software.
1435c4bbdfSmrg *
1535c4bbdfSmrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1635c4bbdfSmrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1735c4bbdfSmrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1835c4bbdfSmrg *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
1935c4bbdfSmrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
2035c4bbdfSmrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2135c4bbdfSmrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2235c4bbdfSmrg *
2335c4bbdfSmrg *Except as contained in this notice, the name of Harold L Hunt II
2435c4bbdfSmrg *shall not be used in advertising or otherwise to promote the sale, use
2535c4bbdfSmrg *or other dealings in this Software without prior written authorization
2635c4bbdfSmrg *from Harold L Hunt II.
2735c4bbdfSmrg *
2835c4bbdfSmrg * Authors:	Harold L Hunt II
2935c4bbdfSmrg */
3035c4bbdfSmrg
3135c4bbdfSmrg#ifdef HAVE_XWIN_CONFIG_H
3235c4bbdfSmrg#include <xwin-config.h>
3335c4bbdfSmrg#endif
3435c4bbdfSmrg
3535c4bbdfSmrg#include <stdlib.h>
3635c4bbdfSmrg#include "internal.h"
3735c4bbdfSmrg
3835c4bbdfSmrg/*
3935c4bbdfSmrg * Convert \r\n to \n
4035c4bbdfSmrg *
4135c4bbdfSmrg * NOTE: This was heavily inspired by, Cygwin's
4235c4bbdfSmrg * winsup/cygwin/fhandler.cc/fhandler_base::read ()
4335c4bbdfSmrg */
4435c4bbdfSmrg
4535c4bbdfSmrgvoid
4635c4bbdfSmrgwinClipboardDOStoUNIX(char *pszSrc, int iLength)
4735c4bbdfSmrg{
4835c4bbdfSmrg    char *pszDest = pszSrc;
4935c4bbdfSmrg    char *pszEnd = pszSrc + iLength;
5035c4bbdfSmrg
5135c4bbdfSmrg    /* Loop until the last character */
5235c4bbdfSmrg    while (pszSrc < pszEnd) {
5335c4bbdfSmrg        /* Copy the current source character to current destination character */
5435c4bbdfSmrg        *pszDest = *pszSrc;
5535c4bbdfSmrg
5635c4bbdfSmrg        /* Advance to the next source character */
5735c4bbdfSmrg        pszSrc++;
5835c4bbdfSmrg
5935c4bbdfSmrg        /* Don't advance the destination character if we need to drop an \r */
6035c4bbdfSmrg        if (*pszDest != '\r' || *pszSrc != '\n')
6135c4bbdfSmrg            pszDest++;
6235c4bbdfSmrg    }
6335c4bbdfSmrg
6435c4bbdfSmrg    /* Move the terminating null */
6535c4bbdfSmrg    *pszDest = '\0';
6635c4bbdfSmrg}
6735c4bbdfSmrg
6835c4bbdfSmrg/*
6935c4bbdfSmrg * Convert \n to \r\n
7035c4bbdfSmrg */
7135c4bbdfSmrg
7235c4bbdfSmrgvoid
7335c4bbdfSmrgwinClipboardUNIXtoDOS(char **ppszData, int iLength)
7435c4bbdfSmrg{
7535c4bbdfSmrg    int iNewlineCount = 0;
7635c4bbdfSmrg    char *pszSrc = *ppszData;
7735c4bbdfSmrg    char *pszEnd = pszSrc + iLength;
7835c4bbdfSmrg    char *pszDest = NULL, *pszDestBegin = NULL;
7935c4bbdfSmrg
8035c4bbdfSmrg    winDebug("UNIXtoDOS () - Original data:'%s'\n", *ppszData);
8135c4bbdfSmrg
8235c4bbdfSmrg    /* Count \n characters without leading \r */
8335c4bbdfSmrg    while (pszSrc < pszEnd) {
8435c4bbdfSmrg        /* Skip ahead two character if found set of \r\n */
8535c4bbdfSmrg        if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
8635c4bbdfSmrg            pszSrc += 2;
8735c4bbdfSmrg            continue;
8835c4bbdfSmrg        }
8935c4bbdfSmrg
9035c4bbdfSmrg        /* Increment the count if found naked \n */
9135c4bbdfSmrg        if (*pszSrc == '\n') {
9235c4bbdfSmrg            iNewlineCount++;
9335c4bbdfSmrg        }
9435c4bbdfSmrg
9535c4bbdfSmrg        pszSrc++;
9635c4bbdfSmrg    }
9735c4bbdfSmrg
9835c4bbdfSmrg    /* Return if no naked \n's */
9935c4bbdfSmrg    if (iNewlineCount == 0)
10035c4bbdfSmrg        return;
10135c4bbdfSmrg
10235c4bbdfSmrg    /* Allocate a new string */
10335c4bbdfSmrg    pszDestBegin = pszDest = malloc(iLength + iNewlineCount + 1);
10435c4bbdfSmrg
10535c4bbdfSmrg    /* Set source pointer to beginning of data string */
10635c4bbdfSmrg    pszSrc = *ppszData;
10735c4bbdfSmrg
10835c4bbdfSmrg    /* Loop through all characters in source string */
10935c4bbdfSmrg    while (pszSrc < pszEnd) {
11035c4bbdfSmrg        /* Copy line endings that are already valid */
11135c4bbdfSmrg        if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
11235c4bbdfSmrg            *pszDest = *pszSrc;
11335c4bbdfSmrg            *(pszDest + 1) = *(pszSrc + 1);
11435c4bbdfSmrg            pszDest += 2;
11535c4bbdfSmrg            pszSrc += 2;
11635c4bbdfSmrg            continue;
11735c4bbdfSmrg        }
11835c4bbdfSmrg
11935c4bbdfSmrg        /* Add \r to naked \n's */
12035c4bbdfSmrg        if (*pszSrc == '\n') {
12135c4bbdfSmrg            *pszDest = '\r';
12235c4bbdfSmrg            *(pszDest + 1) = *pszSrc;
12335c4bbdfSmrg            pszDest += 2;
12435c4bbdfSmrg            pszSrc += 1;
12535c4bbdfSmrg            continue;
12635c4bbdfSmrg        }
12735c4bbdfSmrg
12835c4bbdfSmrg        /* Copy normal characters */
12935c4bbdfSmrg        *pszDest = *pszSrc;
13035c4bbdfSmrg        pszSrc++;
13135c4bbdfSmrg        pszDest++;
13235c4bbdfSmrg    }
13335c4bbdfSmrg
13435c4bbdfSmrg    /* Put terminating null at end of new string */
13535c4bbdfSmrg    *pszDest = '\0';
13635c4bbdfSmrg
13735c4bbdfSmrg    /* Swap string pointers */
13835c4bbdfSmrg    free(*ppszData);
13935c4bbdfSmrg    *ppszData = pszDestBegin;
14035c4bbdfSmrg
14135c4bbdfSmrg    winDebug("UNIXtoDOS () - Final string:'%s'\n", pszDestBegin);
14235c4bbdfSmrg}
143