1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun *Permission is hereby granted, free of charge, to any person obtaining
5*4882a593Smuzhiyun * a copy of this software and associated documentation files (the
6*4882a593Smuzhiyun *"Software"), to deal in the Software without restriction, including
7*4882a593Smuzhiyun *without limitation the rights to use, copy, modify, merge, publish,
8*4882a593Smuzhiyun *distribute, sublicense, and/or sell copies of the Software, and to
9*4882a593Smuzhiyun *permit persons to whom the Software is furnished to do so, subject to
10*4882a593Smuzhiyun *the following conditions:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun *The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun *included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19*4882a593Smuzhiyun *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20*4882a593Smuzhiyun *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21*4882a593Smuzhiyun *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun *Except as contained in this notice, the name of Harold L Hunt II
24*4882a593Smuzhiyun *shall not be used in advertising or otherwise to promote the sale, use
25*4882a593Smuzhiyun *or other dealings in this Software without prior written authorization
26*4882a593Smuzhiyun *from Harold L Hunt II.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Authors: Harold L Hunt II
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
32*4882a593Smuzhiyun #include <xwin-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * Including any server header might define the macro _XSERVER64 on 64 bit machines.
37*4882a593Smuzhiyun * That macro must _NOT_ be defined for Xlib client code, otherwise bad things happen.
38*4882a593Smuzhiyun * So let's undef that macro if necessary.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun #ifdef _XSERVER64
41*4882a593Smuzhiyun #undef _XSERVER64
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #include <stdlib.h>
45*4882a593Smuzhiyun #include "internal.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Convert \r\n to \n
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * NOTE: This was heavily inspired by, Cygwin's
51*4882a593Smuzhiyun * winsup/cygwin/fhandler.cc/fhandler_base::read ()
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun void
winClipboardDOStoUNIX(char * pszSrc,int iLength)55*4882a593Smuzhiyun winClipboardDOStoUNIX(char *pszSrc, int iLength)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun char *pszDest = pszSrc;
58*4882a593Smuzhiyun char *pszEnd = pszSrc + iLength;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Loop until the last character */
61*4882a593Smuzhiyun while (pszSrc < pszEnd) {
62*4882a593Smuzhiyun /* Copy the current source character to current destination character */
63*4882a593Smuzhiyun *pszDest = *pszSrc;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* Advance to the next source character */
66*4882a593Smuzhiyun pszSrc++;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* Don't advance the destination character if we need to drop an \r */
69*4882a593Smuzhiyun if (*pszDest != '\r' || *pszSrc != '\n')
70*4882a593Smuzhiyun pszDest++;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Move the terminating null */
74*4882a593Smuzhiyun *pszDest = '\0';
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * Convert \n to \r\n
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun void
winClipboardUNIXtoDOS(char ** ppszData,int iLength)82*4882a593Smuzhiyun winClipboardUNIXtoDOS(char **ppszData, int iLength)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun int iNewlineCount = 0;
85*4882a593Smuzhiyun char *pszSrc = *ppszData;
86*4882a593Smuzhiyun char *pszEnd = pszSrc + iLength;
87*4882a593Smuzhiyun char *pszDest = NULL, *pszDestBegin = NULL;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun winDebug("UNIXtoDOS () - Original data:'%s'\n", *ppszData);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Count \n characters without leading \r */
92*4882a593Smuzhiyun while (pszSrc < pszEnd) {
93*4882a593Smuzhiyun /* Skip ahead two character if found set of \r\n */
94*4882a593Smuzhiyun if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
95*4882a593Smuzhiyun pszSrc += 2;
96*4882a593Smuzhiyun continue;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* Increment the count if found naked \n */
100*4882a593Smuzhiyun if (*pszSrc == '\n') {
101*4882a593Smuzhiyun iNewlineCount++;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun pszSrc++;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* Return if no naked \n's */
108*4882a593Smuzhiyun if (iNewlineCount == 0)
109*4882a593Smuzhiyun return;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Allocate a new string */
112*4882a593Smuzhiyun pszDestBegin = pszDest = malloc(iLength + iNewlineCount + 1);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Set source pointer to beginning of data string */
115*4882a593Smuzhiyun pszSrc = *ppszData;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Loop through all characters in source string */
118*4882a593Smuzhiyun while (pszSrc < pszEnd) {
119*4882a593Smuzhiyun /* Copy line endings that are already valid */
120*4882a593Smuzhiyun if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
121*4882a593Smuzhiyun *pszDest = *pszSrc;
122*4882a593Smuzhiyun *(pszDest + 1) = *(pszSrc + 1);
123*4882a593Smuzhiyun pszDest += 2;
124*4882a593Smuzhiyun pszSrc += 2;
125*4882a593Smuzhiyun continue;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* Add \r to naked \n's */
129*4882a593Smuzhiyun if (*pszSrc == '\n') {
130*4882a593Smuzhiyun *pszDest = '\r';
131*4882a593Smuzhiyun *(pszDest + 1) = *pszSrc;
132*4882a593Smuzhiyun pszDest += 2;
133*4882a593Smuzhiyun pszSrc += 1;
134*4882a593Smuzhiyun continue;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* Copy normal characters */
138*4882a593Smuzhiyun *pszDest = *pszSrc;
139*4882a593Smuzhiyun pszSrc++;
140*4882a593Smuzhiyun pszDest++;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* Put terminating null at end of new string */
144*4882a593Smuzhiyun *pszDest = '\0';
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Swap string pointers */
147*4882a593Smuzhiyun free(*ppszData);
148*4882a593Smuzhiyun *ppszData = pszDestBegin;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun winDebug("UNIXtoDOS () - Final string:'%s'\n", pszDestBegin);
151*4882a593Smuzhiyun }
152