Re: UCS-2 in gunicode.h
- From: Havoc Pennington <hp redhat com>
- To: Derek Simkowiak <dereks kd-dev com>
- Cc: gtk-devel-list gnome org
- Subject: Re: UCS-2 in gunicode.h
- Date: 07 Jul 2000 16:01:05 -0400
Hi,
I think we're just going to add a function like this to glib:
gchar*
g_convert (const gchar *str,
gint len,
const gchar *to_codeset,
const gchar *from_codeset,
gint *bytes_converted)
{
gchar *dest;
gchar *outp;
const gchar *p;
size_t inbytes_remaining;
size_t outbytes_remaining;
size_t err;
iconv_t cd;
size_t outbuf_size;
g_return_val_if_fail (str != NULL, NULL);
g_return_val_if_fail (to_codeset != NULL, NULL);
g_return_val_if_fail (from_codeset != NULL, NULL);
cd = iconv_open (to_codeset, from_codeset);
if (cd == (iconv_t) -1)
{
/* Something went wrong. */
if (errno == EINVAL)
g_warning ("Conversion from character set `%s' to `%s' is not supported",
from_codeset, to_codeset);
else
g_warning ("Failed to convert character set `%s' to `%s': %s",
from_codeset, to_codeset, strerror (errno));
if (bytes_converted)
*bytes_converted = 0;
return NULL;
}
if (len < 0)
len = strlen (str);
p = str;
inbytes_remaining = len;
outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
outbytes_remaining = outbuf_size - 1; /* -1 for nul */
outp = dest = g_malloc (outbuf_size);
again:
err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
if (err == (size_t) -1)
{
if (errno == E2BIG)
{
size_t used = outp - dest;
outbuf_size *= 2;
dest = g_realloc (dest, outbuf_size);
outp = dest + used;
outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
goto again;
}
else
g_warning ("iconv() failed: %s", strerror (errno));
}
*outp = '\0';
if (iconv_close (cd) != 0)
g_warning ("Failed to close iconv() conversion descriptor: %s",
strerror (errno));
if (bytes_converted)
*bytes_converted = p - str;
if (p == str)
{
g_free (dest);
return NULL;
}
else
return dest;
}
Havoc
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]