4 * ROX-Filer, filer for the ROX desktop project
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
21 /* This code is taken from glib2 and has been converted to the GPL, as allowed
22 * under the terms of the LGPL. Removed lots of stuff! When we move fully to
23 * glib2, this file will disappear.
25 * gconvert.c: Convert between character sets using iconv
26 * Copyright Red Hat Inc., 2000
27 * Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com
52 g_utf8_get_charset_internal (const char **a
)
54 const char *charset
= getenv("CHARSET");
56 if (charset
&& *charset
)
60 if (charset
&& strstr (charset
, "UTF-8"))
66 /* Assume this for compatibility at present. */
72 static int utf8_locale_cache
= -1;
73 static const char *utf8_charset_cache
= NULL
;
76 g_get_charset (const char **charset
)
78 if (utf8_locale_cache
!= -1)
81 *charset
= utf8_charset_cache
;
82 return utf8_locale_cache
;
84 utf8_locale_cache
= g_utf8_get_charset_internal (&utf8_charset_cache
);
86 *charset
= utf8_charset_cache
;
87 return utf8_locale_cache
;
94 g_iconv (GIConv converter
,
100 iconv_t cd
= (iconv_t
)converter
;
102 return iconv (cd
, inbuf
, inbytes_left
, outbuf
, outbytes_left
);
106 g_iconv_close (GIConv converter
)
108 iconv_t cd
= (iconv_t
)converter
;
110 return iconv_close (cd
);
114 open_converter (const gchar
*to_codeset
,
115 const gchar
*from_codeset
,
118 GIConv cd
= (GIConv
) iconv_open (to_codeset
, from_codeset
);
120 if (cd
== (iconv_t
) -1)
122 /* Something went wrong. */
124 g_warning("Conversion from character set '%s' to '%s' is not supported",
125 from_codeset
, to_codeset
);
127 g_warning("Could not open converter from '%s' to '%s': %s",
128 from_codeset
, to_codeset
, strerror (errno
));
137 g_convert (const gchar
*str
,
139 const gchar
*to_codeset
,
140 const gchar
*from_codeset
,
142 gsize
*bytes_written
,
149 g_return_val_if_fail (str
!= NULL
, NULL
);
150 g_return_val_if_fail (to_codeset
!= NULL
, NULL
);
151 g_return_val_if_fail (from_codeset
!= NULL
, NULL
);
153 cd
= open_converter (to_codeset
, from_codeset
, error
);
155 if (cd
== (GIConv
) -1)
166 res
= g_convert_with_iconv (str
, len
, cd
,
167 bytes_read
, bytes_written
,
180 g_convert_with_iconv (const gchar
*str
,
184 gsize
*bytes_written
,
190 gsize inbytes_remaining
;
191 gsize outbytes_remaining
;
194 gboolean have_error
= FALSE
;
196 g_return_val_if_fail (str
!= NULL
, NULL
);
197 g_return_val_if_fail (converter
!= (GIConv
) -1, NULL
);
203 inbytes_remaining
= len
;
204 outbuf_size
= len
+ 1; /* + 1 for nul in case len == 1 */
206 outbytes_remaining
= outbuf_size
- 1; /* -1 for nul */
207 outp
= dest
= g_malloc (outbuf_size
);
211 err
= g_iconv (converter
, (char **)&p
, &inbytes_remaining
, &outp
, &outbytes_remaining
);
213 if (err
== (size_t) -1)
218 /* Incomplete text, do not report an error */
222 size_t used
= outp
- dest
;
225 dest
= g_realloc (dest
, outbuf_size
);
228 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for nul */
233 g_warning("Invalid byte sequence in conversion input");
237 g_warning("Error during conversion: %s", strerror (errno
));
246 *bytes_read
= p
- str
;
249 if ((p
- str
) != len
)
253 g_warning("Partial character sequence at end of input");
260 *bytes_written
= outp
- dest
; /* Doesn't include '\0' */
273 g_locale_to_utf8 (const gchar
*opsysstring
,
276 gsize
*bytes_written
,
281 if (g_get_charset (&charset
))
282 return g_strdup (opsysstring
);
284 return g_convert (opsysstring
, len
,
285 "UTF-8", charset
, bytes_read
, bytes_written
, error
);
289 g_locale_from_utf8 (const gchar
*utf8string
,
292 gsize
*bytes_written
,
295 const gchar
*charset
;
297 if (g_get_charset (&charset
))
298 return g_strdup (utf8string
);
300 return g_convert (utf8string
, len
,
301 charset
, "UTF-8", bytes_read
, bytes_written
, error
);