| Top |
GtkSourceIconv is a small wrapper for the g_iconv() family of functions, to
use iconv more comfortably. GtkSourceIconv returns GError's and enum values
for the different situations.
Call the functions in this order:
gtk_source_iconv_new()
gtk_source_iconv_open()
gtk_source_iconv_feed() in a loop.
gtk_source_iconv_feed() with inbuf
and inbytes_left
set to NULL (in a
loop too if the output buffer is full).
gtk_source_iconv_close()
gtk_source_iconv_free()
Note that GLib provides the g_convert() family of functions. As the
g_convert() documentation explains, it is necessary to use g_iconv() (or
GtkSourceIconv) for streaming conversions. One other use-case is to have
more control over invalid characters: in the case of the GtkSourceView
library, it is desirable to apply a GtkTextTag to them.
However, if you have the choice, don't use iconv! It's a crappy API and has several design flaws:
When returns iconv()(size_t)-1 (e.g., when the output buffer is full),
we don't know if it has performed lossy conversions. In a normal situation,
the number of lossy conversions performed is returned by as its
return value.iconv()
To fix this flaw, an function could return the number of lossy
conversions performed as an output parameter. And also an
iconv2()iconv_open2 () function could take a flags parameter to configure
whether lossy conversions are allowed.
The EILSEQ error returned by can mean two different things:
there is either an invalid character in the input buffer (so invalid wrt.
the *origin* codeset), or there is a valid character in the input buffer
that cannot be represented in the *target* codeset.iconv()
To fix this flaw, an function could simply return two different
error codes.iconv2()
When the EILSEQ error is returned by , “iconv()*inbuf is left
pointing to the beginning of the invalid multibyte sequence”, however we
don't know the length of the multibyte sequence! So in practice we assume
only one invalid byte, and call again with iconv()inbuf pointing to
the next byte, which may fail again, and so on.
An function would need a more elaborate API to return this kind
of information. For example by returning/filling a struct as an optional
output parameter.iconv2()
Additionally, has different implementations (so working code on
Linux can behave differently on a BSD or Solaris).iconv()
gboolean gtk_source_iconv_open (GtkSourceIconv *conv,const gchar *to_codeset,const gchar *from_codeset,GError **error);
Similar to g_iconv_open(). The difference is that it returns a GError
instead of errno.
[skip]
Since: 299.6
GtkSourceIconvResult gtk_source_iconv_feed (GtkSourceIconv *conv,gchar **inbuf,gsize *inbytes_left,gchar **outbuf,gsize *outbytes_left,GError **error);
Similar to g_iconv(). This function reads the errno value and converts it
either to a GError, or to a GtkSourceIconvResult enumeration value.
error
is set only when GTK_SOURCE_ICONV_RESULT_ERROR is returned.
[skip]
conv |
||
inbuf |
bytes to convert. |
[nullable] |
inbytes_left |
bytes remaining to convert in |
[nullable][inout] |
outbuf |
converted output bytes. |
[not nullable] |
outbytes_left |
bytes available to fill in |
[not nullable][inout] |
error |
Since: 299.6
GtkSourceIconvResult gtk_source_iconv_feed_discard_output (GtkSourceIconv *conv,gchar **inbuf,gsize *inbytes_left,GError **error);
Similar to gtk_source_iconv_feed() but without the output buffer parameters.
[skip]
conv |
||
inbuf |
bytes to convert. |
[nullable] |
inbytes_left |
bytes remaining to convert in |
[nullable][inout] |
error |
a GtkSourceIconvResult enumeration value.
GTK_SOURCE_ICONV_RESULT_OUTPUT_BUFFER_FULL is never returned by this
function.
Since: 299.6
gboolean gtk_source_iconv_close (GtkSourceIconv *conv,GError **error);
Similar to g_iconv_close(). The difference is that it returns a GError
instead of errno.
[skip]
Since: 299.6
void
gtk_source_iconv_free (GtkSourceIconv *conv);
Frees conv
.
[skip]
Since: 299.6
Used as the result value of gtk_source_iconv_feed().
|
Everything OK. |
||
|
An error occurred. |
||
|
Stopped at an invalid character in
the |
||
|
The input byte sequence ends with
an incomplete multi-byte character. |
||
|
The output buffer has no more room for the next converted character. |
||
|
A number of nonreversible conversions have been performed. |
Since: 299.6