xref: /illumos-gate/usr/src/lib/libcurses/screen/mbtranslate.c (revision d227ea68051b5404410ee1880d0bba1ebb33a4cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*  Copyright (c) 1988 AT&T */
23 /*    All Rights Reserved   */
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 #include	<stdlib.h>
32 #include	<sys/types.h>
33 #include	"curses_inc.h"
34 
35 
36 /*
37  *	Translate process code to byte-equivalent
38  *	Return the length of the byte-equivalent string
39  */
40 
41 /*
42  *	use _curs_wctomb() instead of _code2byte(code, bytes)
43  */
44 
45 
46 /*
47  *	Translate a set of byte to a single process code
48  */
49 
50 /*
51  *	use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
52  */
53 
54 
55 /*
56  *	Translate a string of wchar_t to a byte string.
57  *	code: the input code string
58  *	byte: if not NULL, space to store the output string
59  *	n: maximum number of codes to be translated.
60  */
61 char
62 *_strcode2byte(wchar_t *code, char *byte, int n)
63 {
64 	char		*bufp;
65 	wchar_t		*endcode;
66 	static char	*buf;
67 	static int	bufsize;
68 
69 	/* compute the length of the code string */
70 	if (n < 0)
71 		for (n = 0; code[n] != 0; ++n)
72 			;
73 
74 	/* get space to store the translated string */
75 	if (!byte && (n*CSMAX+1) > bufsize) {
76 		if (buf)
77 			free(buf);
78 		bufsize = n * CSMAX + 1;
79 		if ((buf = malloc(bufsize * sizeof (char))) == NULL)
80 			bufsize = 0;
81 		}
82 
83 	/* no space to do it */
84 	if (!byte && !buf)
85 		return (NULL);
86 
87 	/* start the translation */
88 	bufp = byte ? byte : buf;
89 	endcode = code+n;
90 	while (code < endcode && *code) {
91 		bufp += _curs_wctomb(bufp, *code & TRIM);
92 		++code;
93 	}
94 	*bufp = '\0';
95 
96 	return (byte ? byte : buf);
97 }
98 
99 
100 
101 /*
102  *	Translate a byte-string to a wchar_t string.
103  */
104 wchar_t
105 *_strbyte2code(char *byte, wchar_t *code, int n)
106 {
107 	char		*endbyte;
108 	wchar_t		*bufp;
109 	static wchar_t	*buf;
110 	static int	bufsize;
111 
112 	if (n < 0)
113 		for (n = 0; byte[n] != '\0'; ++n)
114 			;
115 
116 	if (!code && (n + 1) > bufsize) {
117 		if (buf)
118 			free((char *)buf);
119 		bufsize = n + 1;
120 		if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
121 		    NULL)
122 			bufsize = 0;
123 	}
124 
125 	if (!code && !buf)
126 		return (NULL);
127 
128 	bufp = code ? code : buf;
129 	endbyte = byte + n;
130 
131 	while (byte < endbyte && *byte) {
132 		int		type, width;
133 		wchar_t		wchar;
134 
135 		type = TYPE(*byte & 0377);
136 		width = cswidth[type];
137 		if (type == 1 || type == 2)
138 			width++;
139 
140 		if (byte + width <= endbyte) {
141 			(void) _curs_mbtowc(&wchar, byte, width);
142 			*bufp++ = wchar;
143 		}
144 
145 		byte += width;
146 	}
147 	*bufp = 0;
148 
149 	return (code ? code : buf);
150 }
151