4 * Part of gwm, the Gratuitous Window Manager,
5 * by Gary Wong, <gtw@gnu.org>.
7 * Copyright (C) 2009 Gary Wong
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of version 3 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 extern char *to_utf8( enum gwm_encoding encoding
, const char *in
,
51 outlen
= len
<< ( encoding
== ENCODING_COMPOUND
? 2 : 1 );
56 outp
= out
= xmalloc( outlen
+ 1 );
59 if( encoding
== ENCODING_COMPOUND
) {
60 if( !tried_iso2022
) {
61 iso2022
= iconv_open( "UTF-8", "ISO-2022-JP-2" );
65 if( iso2022
!= (iconv_t
) -1 ) {
66 static const char resetseq
[ 3 ] = "\x1B\x2D\x41";
70 /* Reset the decoder to the Compound Text initial state
71 (G1 = ASCII, G3 = ISO 8859-1). */
73 iconv( iso2022
, NULL
, NULL
, NULL
, NULL
);
74 /* Bah. Several old implementations of iconv() declared
75 the inbuf parameter as (const char **), but SUS says
76 it's simply (char **). We cast the thing to (void *),
77 which will keep them both happy. */
78 iconv( iso2022
, (void *) &inp
, &resetlen
, &outp
, &outlen
);
80 iconv( iso2022
, (void *) &in
, &len
, &outp
, &outlen
);
84 return xrealloc( out
, outp
- out
);
91 *outp
++ = 0xC0 | ( *in
>> 6 );
92 *outp
++ = 0x80 | ( *in
& 0x3F );
98 return xrealloc( out
, outp
- out
);
101 extern void cleanup_utf8( void ) {
104 if( tried_iso2022
&& iso2022
!= (iconv_t
) -1 )
105 iconv_close( iso2022
);