xref: /titanic_50/usr/src/lib/libcurses/screen/mbtranslate.c (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<stdlib.h>
34 #include	<sys/types.h>
35 #include	"curses_inc.h"
36 
37 
38 /*
39  *	Translate process code to byte-equivalent
40  *	Return the length of the byte-equivalent string
41  */
42 
43 /*
44  *	use _curs_wctomb() instead of _code2byte(code, bytes)
45  */
46 
47 
48 /*
49  *	Translate a set of byte to a single process code
50  */
51 
52 /*
53  *	use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
54  */
55 
56 
57 /*
58  *	Translate a string of wchar_t to a byte string.
59  *	code: the input code string
60  *	byte: if not NULL, space to store the output string
61  *	n: maximum number of codes to be translated.
62  */
63 char
64 *_strcode2byte(wchar_t *code, char *byte, int n)
65 {
66 	char		*bufp;
67 	wchar_t		*endcode;
68 	static char	*buf;
69 	static int	bufsize;
70 
71 	/* compute the length of the code string */
72 	if (n < 0)
73 		for (n = 0; code[n] != 0; ++n)
74 			;
75 
76 	/* get space to store the translated string */
77 	if (!byte && (n*CSMAX+1) > bufsize) {
78 		if (buf)
79 			free(buf);
80 		bufsize = n * CSMAX + 1;
81 		if ((buf = malloc(bufsize * sizeof (char))) == NULL)
82 			bufsize = 0;
83 		}
84 
85 	/* no space to do it */
86 	if (!byte && !buf)
87 		return (NULL);
88 
89 	/* start the translation */
90 	bufp = byte ? byte : buf;
91 	endcode = code+n;
92 	while (code < endcode && *code) {
93 		bufp += _curs_wctomb(bufp, *code & TRIM);
94 		++code;
95 	}
96 	*bufp = '\0';
97 
98 	return (byte ? byte : buf);
99 }
100 
101 
102 
103 /*
104  *	Translate a byte-string to a wchar_t string.
105  */
106 wchar_t
107 *_strbyte2code(char *byte, wchar_t *code, int n)
108 {
109 	char		*endbyte;
110 	wchar_t		*bufp;
111 	static wchar_t	*buf;
112 	static int	bufsize;
113 
114 	if (n < 0)
115 		for (n = 0; byte[n] != '\0'; ++n)
116 			;
117 
118 	if (!code && (n + 1) > bufsize) {
119 		if (buf)
120 			free((char *)buf);
121 		bufsize = n + 1;
122 		if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
123 		    NULL)
124 			bufsize = 0;
125 	}
126 
127 	if (!code && !buf)
128 		return (NULL);
129 
130 	bufp = code ? code : buf;
131 	endbyte = byte + n;
132 
133 	while (byte < endbyte && *byte) {
134 		int		type, width;
135 		wchar_t		wchar;
136 
137 		type = TYPE(*byte & 0377);
138 		width = cswidth[type];
139 		if (type == 1 || type == 2)
140 			width++;
141 
142 		if (byte + width <= endbyte) {
143 			(void) _curs_mbtowc(&wchar, byte, width);
144 			*bufp++ = wchar;
145 		}
146 
147 		byte += width;
148 	}
149 	*bufp = 0;
150 
151 	return (code ? code : buf);
152 }
153