Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xwin / winclipboardtextconv.c
CommitLineData
a09e091a
JB
1/*
2 *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3 *
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:
11 *
12 *The above copyright notice and this permission notice shall be
13 *included in all copies or substantial portions of the Software.
14 *
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.
22 *
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.
27 *
28 * Authors: Harold L Hunt II
29 */
30
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>
37
38void
39 winClipboardDOStoUNIX(char *pszSrc, int iLength);
40void
41 winClipboardUNIXtoDOS(char **ppszData, int iLength);
42
43/*
44 * Convert \r\n to \n
45 *
46 * NOTE: This was heavily inspired by, Cygwin's
47 * winsup/cygwin/fhandler.cc/fhandler_base::read ()
48 */
49
50void
51winClipboardDOStoUNIX(char *pszSrc, int iLength)
52{
53 char *pszDest = pszSrc;
54 char *pszEnd = pszSrc + iLength;
55
56 /* Loop until the last character */
57 while (pszSrc < pszEnd) {
58 /* Copy the current source character to current destination character */
59 *pszDest = *pszSrc;
60
61 /* Advance to the next source character */
62 pszSrc++;
63
64 /* Don't advance the destination character if we need to drop an \r */
65 if (*pszDest != '\r' || *pszSrc != '\n')
66 pszDest++;
67 }
68
69 /* Move the terminating null */
70 *pszDest = '\0';
71}
72
73/*
74 * Convert \n to \r\n
75 */
76
77void
78winClipboardUNIXtoDOS(char **ppszData, int iLength)
79{
80 int iNewlineCount = 0;
81 char *pszSrc = *ppszData;
82 char *pszEnd = pszSrc + iLength;
83 char *pszDest = NULL, *pszDestBegin = NULL;
84
85 winDebug("UNIXtoDOS () - Original data:'%s'\n", *ppszData);
86
87 /* Count \n characters without leading \r */
88 while (pszSrc < pszEnd) {
89 /* Skip ahead two character if found set of \r\n */
90 if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
91 pszSrc += 2;
92 continue;
93 }
94
95 /* Increment the count if found naked \n */
96 if (*pszSrc == '\n') {
97 iNewlineCount++;
98 }
99
100 pszSrc++;
101 }
102
103 /* Return if no naked \n's */
104 if (iNewlineCount == 0)
105 return;
106
107 /* Allocate a new string */
108 pszDestBegin = pszDest = malloc(iLength + iNewlineCount + 1);
109
110 /* Set source pointer to beginning of data string */
111 pszSrc = *ppszData;
112
113 /* Loop through all characters in source string */
114 while (pszSrc < pszEnd) {
115 /* Copy line endings that are already valid */
116 if (*pszSrc == '\r' && pszSrc + 1 < pszEnd && *(pszSrc + 1) == '\n') {
117 *pszDest = *pszSrc;
118 *(pszDest + 1) = *(pszSrc + 1);
119 pszDest += 2;
120 pszSrc += 2;
121 continue;
122 }
123
124 /* Add \r to naked \n's */
125 if (*pszSrc == '\n') {
126 *pszDest = '\r';
127 *(pszDest + 1) = *pszSrc;
128 pszDest += 2;
129 pszSrc += 1;
130 continue;
131 }
132
133 /* Copy normal characters */
134 *pszDest = *pszSrc;
135 pszSrc++;
136 pszDest++;
137 }
138
139 /* Put terminating null at end of new string */
140 *pszDest = '\0';
141
142 /* Swap string pointers */
143 free(*ppszData);
144 *ppszData = pszDestBegin;
145
146 winDebug("UNIXtoDOS () - Final string:'%s'\n", pszDestBegin);
147}