1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gcontenttypeprivate.h"
28 #include "gcharsetconverter.h"
30 #include "ginitable.h"
43 * SECTION:gcharsetconverter
44 * @short_description: Convert between charsets
47 * #GCharsetConverter is an implementation of #GConverter based on
51 static void g_charset_converter_iface_init (GConverterIface *iface);
52 static void g_charset_converter_initable_iface_init (GInitableIface *iface);
57 * Conversions between character sets.
59 struct _GCharsetConverter
61 GObject parent_instance;
68 G_DEFINE_TYPE_WITH_CODE (GCharsetConverter, g_charset_converter, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
70 g_charset_converter_iface_init);
71 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
72 g_charset_converter_initable_iface_init))
75 g_charset_converter_finalize (GObject *object)
77 GCharsetConverter *conv;
79 conv = G_CHARSET_CONVERTER (object);
84 g_iconv_close (conv->iconv);
86 G_OBJECT_CLASS (g_charset_converter_parent_class)->finalize (object);
90 g_charset_converter_set_property (GObject *object,
95 GCharsetConverter *conv;
97 conv = G_CHARSET_CONVERTER (object);
101 case PROP_TO_CHARSET:
103 conv->to = g_value_dup_string (value);
106 case PROP_FROM_CHARSET:
108 conv->from = g_value_dup_string (value);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 g_charset_converter_get_property (GObject *object,
124 GCharsetConverter *conv;
126 conv = G_CHARSET_CONVERTER (object);
130 case PROP_TO_CHARSET:
131 g_value_set_string (value, conv->to);
134 case PROP_FROM_CHARSET:
135 g_value_set_string (value, conv->from);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 g_charset_converter_class_init (GCharsetConverterClass *klass)
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149 gobject_class->finalize = g_charset_converter_finalize;
150 gobject_class->get_property = g_charset_converter_get_property;
151 gobject_class->set_property = g_charset_converter_set_property;
153 g_object_class_install_property (gobject_class,
155 g_param_spec_string ("to-charset",
157 P_("The character encoding to convert to"),
159 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
160 G_PARAM_STATIC_STRINGS));
161 g_object_class_install_property (gobject_class,
163 g_param_spec_string ("from-charset",
165 P_("The character encoding to convert from"),
167 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
168 G_PARAM_STATIC_STRINGS));
173 g_charset_converter_init (GCharsetConverter *local)
179 * g_charset_converter_new:
180 * @to_charset: destination charset
181 * @from_charset: source charset
182 * @error: #GError for error reporting, or %NULL to ignore.
184 * Creates a new #GCharsetConverter.
186 * Returns: a new #GCharsetConverter or %NULL on error.
191 g_charset_converter_new (const gchar *to_charset,
192 const gchar *from_charset,
195 GCharsetConverter *conv;
197 conv = g_initable_new (G_TYPE_CHARSET_CONVERTER,
199 "to-charset", to_charset,
200 "from-charset", from_charset,
207 g_charset_converter_reset (GConverter *converter)
209 GCharsetConverter *conv = G_CHARSET_CONVERTER (converter);
211 if (conv->iconv == NULL)
213 g_warning ("Invalid object, not initialized");
217 g_iconv (conv->iconv, NULL, NULL, NULL, NULL);
220 static GConverterResult
221 g_charset_converter_convert (GConverter *converter,
226 GConverterFlags flags,
228 gsize *bytes_written,
231 GCharsetConverter *conv;
233 GConverterResult ret;
234 gchar *inbufp, *outbufp;
235 gsize in_left, out_left;
238 conv = G_CHARSET_CONVERTER (converter);
240 if (conv->iconv == NULL)
242 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
243 _("Invalid object, not initialized"));
244 return G_CONVERTER_ERROR;
247 /* Iconv never produces output with no input, so handle this
251 if (flags & G_CONVERTER_INPUT_AT_END)
252 return G_CONVERTER_FINISHED;
254 if (flags & G_CONVERTER_FLUSH)
255 return G_CONVERTER_FLUSHED;
257 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
258 _("Incomplete multibyte sequence in input"));
259 return G_CONVERTER_ERROR;
262 inbufp = (char *)inbuf;
263 outbufp = (char *)outbuf;
264 in_left = inbuf_size;
265 out_left = outbuf_size;
267 res = g_iconv (conv->iconv,
269 &outbufp, &out_left);
271 *bytes_read = inbufp - (char *)inbuf;
272 *bytes_written = outbufp - (char *)outbuf;
274 /* Don't report error if we converted anything */
275 if (res == (gsize) -1 && *bytes_read == 0)
282 /* Incomplete input text */
283 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
284 _("Incomplete multibyte sequence in input"));
288 /* Not enough destination space */
289 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
290 _("Not enough space in destination"));
294 /* Invalid code sequence */
295 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
296 _("Invalid byte sequence in conversion input"));
300 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
301 _("Error during conversion: %s"),
305 ret = G_CONVERTER_ERROR;
309 ret = G_CONVERTER_CONVERTED;
312 (flags & G_CONVERTER_INPUT_AT_END))
313 ret = G_CONVERTER_FINISHED;
314 else if (in_left == 0 &&
315 (flags & G_CONVERTER_FLUSH))
316 ret = G_CONVERTER_FLUSHED;
323 g_charset_converter_iface_init (GConverterIface *iface)
325 iface->convert = g_charset_converter_convert;
326 iface->reset = g_charset_converter_reset;
330 g_charset_converter_initable_init (GInitable *initable,
331 GCancellable *cancellable,
334 GCharsetConverter *conv;
336 g_return_val_if_fail (G_IS_CHARSET_CONVERTER (initable), FALSE);
338 conv = G_CHARSET_CONVERTER (initable);
340 if (cancellable != NULL)
342 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
343 _("Cancellable initialization not supported"));
348 g_iconv_open (conv->to, conv->from);
350 if (conv->iconv == NULL)
353 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
354 _("Conversion from character set '%s' to '%s' is not supported"),
355 conv->from, conv->to);
357 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
358 _("Could not open converter from '%s' to '%s'"),
359 conv->from, conv->to);
367 g_charset_converter_initable_iface_init (GInitableIface *iface)
369 iface->init = g_charset_converter_initable_init;
372 #define __G_CHARSET_CONVERTER_C__
373 #include "gioaliasdef.c"