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
48 g_utf8_get_charset_internal (const char **a
)
50 const char *charset
= getenv("CHARSET");
52 if (charset
&& *charset
)
56 if (charset
&& strstr (charset
, "UTF-8"))
62 /* Assume this for compatibility at present. */
68 static int utf8_locale_cache
= -1;
69 static const char *utf8_charset_cache
= NULL
;
72 g_get_charset (const char **charset
)
74 if (utf8_locale_cache
!= -1)
77 *charset
= utf8_charset_cache
;
78 return utf8_locale_cache
;
80 utf8_locale_cache
= g_utf8_get_charset_internal (&utf8_charset_cache
);
82 *charset
= utf8_charset_cache
;
83 return utf8_locale_cache
;
89 #if defined(USE_LIBICONV) && !defined (_LIBICONV_H)
90 #error libiconv in use but included iconv.h not from libiconv
92 #if !defined(USE_LIBICONV) && defined (_LIBICONV_H)
93 #error libiconv not in use but included iconv.h is from libiconv
97 g_iconv (GIConv converter
,
101 gsize
*outbytes_left
)
103 iconv_t cd
= (iconv_t
)converter
;
105 return iconv (cd
, inbuf
, inbytes_left
, outbuf
, outbytes_left
);
109 g_iconv_close (GIConv converter
)
111 iconv_t cd
= (iconv_t
)converter
;
113 return iconv_close (cd
);
117 open_converter (const gchar
*to_codeset
,
118 const gchar
*from_codeset
,
121 GIConv cd
= (GIConv
) iconv_open (to_codeset
, from_codeset
);
123 if (cd
== (iconv_t
) -1)
125 /* Something went wrong. */
127 g_warning("Conversion from character set '%s' to '%s' is not supported",
128 from_codeset
, to_codeset
);
130 g_warning("Could not open converter from '%s' to '%s': %s",
131 from_codeset
, to_codeset
, strerror (errno
));
139 g_convert (const gchar
*str
,
141 const gchar
*to_codeset
,
142 const gchar
*from_codeset
,
144 gsize
*bytes_written
,
150 g_return_val_if_fail (str
!= NULL
, NULL
);
151 g_return_val_if_fail (to_codeset
!= NULL
, NULL
);
152 g_return_val_if_fail (from_codeset
!= NULL
, NULL
);
154 cd
= open_converter (to_codeset
, from_codeset
, error
);
156 if (cd
== (GIConv
) -1)
167 res
= g_convert_with_iconv (str
, len
, cd
,
168 bytes_read
, bytes_written
,
177 g_convert_with_iconv (const gchar
*str
,
181 gsize
*bytes_written
,
187 gsize inbytes_remaining
;
188 gsize outbytes_remaining
;
191 gboolean have_error
= FALSE
;
193 g_return_val_if_fail (str
!= NULL
, NULL
);
194 g_return_val_if_fail (converter
!= (GIConv
) -1, NULL
);
200 inbytes_remaining
= len
;
201 outbuf_size
= len
+ 1; /* + 1 for nul in case len == 1 */
203 outbytes_remaining
= outbuf_size
- 1; /* -1 for nul */
204 outp
= dest
= g_malloc (outbuf_size
);
208 err
= g_iconv (converter
, (char **)&p
, &inbytes_remaining
, &outp
, &outbytes_remaining
);
210 if (err
== (size_t) -1)
215 /* Incomplete text, do not report an error */
219 size_t used
= outp
- dest
;
222 dest
= g_realloc (dest
, outbuf_size
);
225 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for nul */
230 g_warning("Invalid byte sequence in conversion input");
234 g_warning("Error during conversion: %s", strerror (errno
));
243 *bytes_read
= p
- str
;
246 if ((p
- str
) != len
)
250 g_warning("Partial character sequence at end of input");
257 *bytes_written
= outp
- dest
; /* Doesn't include '\0' */
269 g_locale_to_utf8 (const gchar
*opsysstring
,
272 gsize
*bytes_written
,
277 if (g_get_charset (&charset
))
278 return g_strdup (opsysstring
);
280 return g_convert (opsysstring
, len
,
281 "UTF-8", charset
, bytes_read
, bytes_written
, error
);
285 g_locale_from_utf8 (const gchar
*utf8string
,
288 gsize
*bytes_written
,
291 const gchar
*charset
;
293 if (g_get_charset (&charset
))
294 return g_strdup (utf8string
);
296 return g_convert (utf8string
, len
,
297 charset
, "UTF-8", bytes_read
, bytes_written
, error
);