First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xwin / winclipboardtextconv.c
blobfd2e696c3c7bb1caa89f6bc55ddbed198cbd83a2
1 /*
2 *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
4 *Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 *"Software"), to deal in the Software without restriction, including
7 *without limitation the rights to use, copy, modify, merge, publish,
8 *distribute, sublicense, and/or sell copies of the Software, and to
9 *permit persons to whom the Software is furnished to do so, subject to
10 *the following conditions:
12 *The above copyright notice and this permission notice shall be
13 *included in all copies or substantial portions of the Software.
15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *Except as contained in this notice, the name of Harold L Hunt II
24 *shall not be used in advertising or otherwise to promote the sale, use
25 *or other dealings in this Software without prior written authorization
26 *from Harold L Hunt II.
28 * Authors: Harold L Hunt II
31 #ifdef HAVE_XWIN_CONFIG_H
32 #include <xwin-config.h>
33 #endif
34 #include "win.h"
35 #include <stdio.h>
36 #include <stdlib.h>
38 void
39 winClipboardDOStoUNIX (char *pszSrc, int iLength);
40 void
41 winClipboardUNIXtoDOS (unsigned char **ppszData, int iLength);
44 * Convert \r\n to \n
46 * NOTE: This was heavily inspired by, Cygwin's
47 * winsup/cygwin/fhandler.cc/fhandler_base::read ()
50 void
51 winClipboardDOStoUNIX (char *pszSrc, int iLength)
53 char *pszDest = pszSrc;
54 char *pszEnd = pszSrc + iLength;
56 /* Loop until the last character */
57 while (pszSrc < pszEnd)
59 /* Copy the current source character to current destination character */
60 *pszDest = *pszSrc;
62 /* Advance to the next source character */
63 pszSrc++;
65 /* Don't advance the destination character if we need to drop an \r */
66 if (*pszDest != '\r' || *pszSrc != '\n')
67 pszDest++;
70 /* Move the terminating null */
71 *pszDest = '\0';
76 * Convert \n to \r\n
79 void
80 winClipboardUNIXtoDOS (unsigned char **ppszData, int iLength)
82 int iNewlineCount = 0;
83 unsigned char *pszSrc = *ppszData;
84 unsigned char *pszEnd = pszSrc + iLength;
85 unsigned char *pszDest = NULL, *pszDestBegin = NULL;
87 #if 0
88 ErrorF ("UNIXtoDOS () - Original data:\n%s\n", *ppszData);
89 #endif
91 /* Count \n characters without leading \r */
92 while (pszSrc < pszEnd)
94 /* Skip ahead two character if found set of \r\n */
95 if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n')
97 pszSrc += 2;
98 continue;
101 /* Increment the count if found naked \n */
102 if (*pszSrc == '\n')
104 iNewlineCount++;
107 pszSrc++;
110 /* Return if no naked \n's */
111 if (iNewlineCount == 0)
112 return;
114 /* Allocate a new string */
115 pszDestBegin = pszDest = malloc (iLength + iNewlineCount + 1);
117 /* Set source pointer to beginning of data string */
118 pszSrc = *ppszData;
120 /* Loop through all characters in source string */
121 while (pszSrc < pszEnd)
123 /* Copy line endings that are already valid */
124 if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n')
126 *pszDest = *pszSrc;
127 *(pszDest + 1) = *(pszSrc + 1);
128 pszDest += 2;
129 pszSrc += 2;
130 continue;
133 /* Add \r to naked \n's */
134 if (*pszSrc == '\n')
136 *pszDest = '\r';
137 *(pszDest + 1) = *pszSrc;
138 pszDest += 2;
139 pszSrc += 1;
140 continue;
143 /* Copy normal characters */
144 *pszDest = *pszSrc;
145 pszSrc++;
146 pszDest++;
149 /* Put terminating null at end of new string */
150 *pszDest = '\0';
152 /* Swap string pointers */
153 free (*ppszData);
154 *ppszData = pszDestBegin;
156 #if 0
157 ErrorF ("UNIXtoDOS () - Final string:\n%s\n", pszDestBegin);
158 #endif