1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giochannel.c: IO Channel abstraction
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
42 #undef G_DISABLE_DEPRECATED
48 #define G_IO_NICE_BUF_SIZE 1024
50 static GIOError g_io_error_get_from_g_error (GIOStatus status,
52 static void g_io_channel_purge (GIOChannel *channel);
53 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
55 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
57 gsize *terminator_pos,
61 g_io_channel_init (GIOChannel *channel)
63 channel->ref_count = 1;
64 channel->encoding = g_strdup ("UTF-8");
65 channel->line_term = NULL;
66 channel->buf_size = G_IO_NICE_BUF_SIZE;
67 channel->read_cd = (GIConv) -1;
68 channel->write_cd = (GIConv) -1;
69 channel->read_buf = NULL; /* Lazy allocate buffers */
70 channel->encoded_read_buf = NULL;
71 channel->write_buf = NULL;
72 channel->partial_write_buf[0] = '\0';
73 channel->use_buffer = TRUE;
74 channel->do_encode = FALSE;
75 channel->close_on_unref = FALSE;
79 g_io_channel_ref (GIOChannel *channel)
81 g_return_if_fail (channel != NULL);
87 g_io_channel_unref (GIOChannel *channel)
89 g_return_if_fail (channel != NULL);
92 if (channel->ref_count == 0)
94 if (channel->close_on_unref)
95 g_io_channel_close (channel);
97 g_io_channel_purge (channel);
98 g_free (channel->encoding);
99 if (channel->read_cd != (GIConv) -1)
100 g_iconv_close (channel->read_cd);
101 if (channel->write_cd != (GIConv) -1)
102 g_iconv_close (channel->write_cd);
103 g_free (channel->line_term);
104 if (channel->read_buf)
105 g_string_free (channel->read_buf, TRUE);
106 if (channel->write_buf)
107 g_string_free (channel->write_buf, TRUE);
108 if (channel->encoded_read_buf)
109 g_string_free (channel->encoded_read_buf, TRUE);
110 channel->funcs->io_free (channel);
115 g_io_error_get_from_g_error (GIOStatus status,
120 case G_IO_STATUS_NORMAL:
121 case G_IO_STATUS_EOF:
122 return G_IO_ERROR_NONE;
123 case G_IO_STATUS_AGAIN:
124 return G_IO_ERROR_AGAIN;
125 case G_IO_STATUS_ERROR:
126 if (err->domain != G_IO_CHANNEL_ERROR)
127 return G_IO_ERROR_UNKNOWN;
130 case G_IO_CHANNEL_ERROR_INVAL:
131 return G_IO_ERROR_INVAL;
133 return G_IO_ERROR_UNKNOWN;
136 g_assert_not_reached ();
137 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
143 * @channel: a #GIOChannel.
144 * @buf: a buffer to read the data into (which should be at least count bytes long).
145 * @count: the number of bytes to read from the #GIOChannel.
146 * @bytes_read: returns the number of bytes actually read.
148 * Reads data from a #GIOChannel. This function is depricated. New code should
149 * use g_io_channel_read_chars() instead.
151 * Return value: %G_IO_ERROR_NONE if the operation was successful.
154 g_io_channel_read (GIOChannel *channel,
163 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
164 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
166 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
168 error = g_io_error_get_from_g_error (status, err);
177 * g_io_channel_write:
178 * @channel: a #GIOChannel.
179 * @buf: the buffer containing the data to write.
180 * @count: the number of bytes to write.
181 * @bytes_written: the number of bytes actually written.
183 * Writes data to a #GIOChannel. This function is depricated. New code should
184 * use g_io_channel_write_chars() instead.
186 * Return value: %G_IO_ERROR_NONE if the operation was successful.
189 g_io_channel_write (GIOChannel *channel,
192 gsize *bytes_written)
198 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
199 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
201 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
203 error = g_io_error_get_from_g_error (status, err);
213 * @channel: a #GIOChannel.
214 * @offset: an offset, in bytes, which is added to the position specified by @type
215 * @type: the position in the file, which can be %G_SEEK_CUR (the current
216 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
219 * Sets the current position in the #GIOChannel, similar to the standard library
220 * function fseek(). This function is depricated. New code should
221 * use g_io_channel_seek_position() instead.
223 * Return value: %G_IO_ERROR_NONE if the operation was successful.
226 g_io_channel_seek (GIOChannel *channel,
234 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
235 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
244 g_warning ("g_io_channel_seek: unknown seek type");
245 return G_IO_ERROR_UNKNOWN;
248 status = channel->funcs->io_seek (channel, offset, type, &err);
250 error = g_io_error_get_from_g_error (status, err);
258 /* The function g_io_channel_new_file() is prototyped in both
259 * giounix.c and giowin32.c, so we stick its documentation here.
263 * g_io_channel_new_file:
264 * @filename: A string containing the name of a file.
265 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
266 * the same meaning as in fopen().
267 * @error: A location to return an error of type %G_IO_FILE_ERROR.
269 * Open a file @filename as a #GIOChannel using mode @mode. This
270 * channel will be closed when the last reference to it is dropped,
271 * so there is no need to call g_io_channel_close() (though doing
272 * so will not cause problems, as long as no attempt is made to
273 * access the channel after it is closed).
275 * Return value: A #GIOChannel on success, %NULL on failure.
279 * g_io_channel_close:
280 * @channel: A #GIOChannel
282 * Close an IO channel. Any pending data to be written will be
283 * flushed, ignoring errors. The channel will not be freed until the
284 * last reference is dropped using g_io_channel_unref(). This
285 * function is deprecated: you should use g_io_channel_shutdown()
289 g_io_channel_close (GIOChannel *channel)
293 g_return_if_fail (channel != NULL);
295 g_io_channel_purge (channel);
297 channel->funcs->io_close (channel, &err);
300 { /* No way to return the error */
301 g_warning ("Error closing channel: %s", err->message);
305 channel->close_on_unref = FALSE; /* Because we already did */
306 channel->is_readable = FALSE;
307 channel->is_writeable = FALSE;
308 channel->is_seekable = FALSE;
312 * g_io_channel_shutdown:
313 * @channel: a #GIOChannel
314 * @flush: if %TRUE, flush pending
315 * @err: location to store a #GIOChannelError
317 * Close an IO channel. Any pending data to be written will be
318 * flushed. The channel will not be freed until the
319 * last reference is dropped using g_io_channel_unref().
324 g_io_channel_shutdown (GIOChannel *channel,
328 GIOStatus status, result;
329 GError *tmperr = NULL;
331 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
332 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
334 if (flush && channel->write_buf && channel->write_buf->len > 0)
338 /* Set the channel to blocking, to avoid a busy loop
340 flags = g_io_channel_get_flags (channel);
341 /* Ignore any errors here, they're irrelevant */
342 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
344 result = g_io_channel_flush (channel, &tmperr);
346 if (channel->partial_write_buf[0] != '\0')
348 g_warning ("Partial character at end of write buffer not flushed.\n");
349 channel->partial_write_buf[0] = '\0';
353 result = G_IO_STATUS_NORMAL;
355 status = channel->funcs->io_close (channel, err);
357 channel->close_on_unref = FALSE; /* Because we already did */
358 channel->is_readable = FALSE;
359 channel->is_writeable = FALSE;
360 channel->is_seekable = FALSE;
362 if (status != G_IO_STATUS_NORMAL)
364 g_clear_error (&tmperr);
367 else if (result != G_IO_STATUS_NORMAL)
369 g_propagate_error (err, tmperr);
373 return G_IO_STATUS_NORMAL;
376 /* This function is used for the final flush on close or unref */
378 g_io_channel_purge (GIOChannel *channel)
383 g_return_if_fail (channel != NULL);
385 if (channel->write_buf && channel->write_buf->len > 0)
389 /* Set the channel to blocking, to avoid a busy loop
391 flags = g_io_channel_get_flags (channel);
392 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
394 status = g_io_channel_flush (channel, &err);
397 { /* No way to return the error */
398 g_warning ("Error flushing string: %s", err->message);
403 /* Flush these in case anyone tries to close without unrefing */
405 if (channel->read_buf)
406 g_string_truncate (channel->read_buf, 0);
407 if (channel->write_buf)
408 g_string_truncate (channel->write_buf, 0);
409 if (channel->do_encode)
411 if (channel->encoded_read_buf)
412 g_string_truncate (channel->encoded_read_buf, 0);
414 if (channel->partial_write_buf[0] != '\0')
416 g_warning ("Partial character at end of write buffer not flushed.\n");
417 channel->partial_write_buf[0] = '\0';
423 g_io_create_watch (GIOChannel *channel,
424 GIOCondition condition)
426 g_return_val_if_fail (channel != NULL, NULL);
428 return channel->funcs->io_create_watch (channel, condition);
432 g_io_add_watch_full (GIOChannel *channel,
434 GIOCondition condition,
437 GDestroyNotify notify)
442 g_return_val_if_fail (channel != NULL, 0);
444 source = g_io_create_watch (channel, condition);
446 if (priority != G_PRIORITY_DEFAULT)
447 g_source_set_priority (source, priority);
448 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
450 id = g_source_attach (source, NULL);
451 g_source_unref (source);
457 g_io_add_watch (GIOChannel *channel,
458 GIOCondition condition,
462 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
466 * g_io_channel_get_buffer_condition:
467 * @channel: A #GIOChannel
469 * This function returns a #GIOCondition depending on the status of the
470 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
471 * %G_IO_OUT will be set.
473 * Return value: A #GIOCondition
476 g_io_channel_get_buffer_condition (GIOChannel *channel)
478 GIOCondition condition = 0;
480 if (channel->encoding)
482 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
483 condition |= G_IO_IN; /* Only return if we have full characters */
487 if (channel->read_buf && (channel->read_buf->len > 0))
488 condition |= G_IO_IN;
491 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
492 condition |= G_IO_OUT;
498 * g_io_channel_error_from_errno:
499 * @en: An errno error number, e.g. EINVAL
501 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
504 g_io_channel_error_from_errno (gint en)
507 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
510 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
517 g_warning("Invalid file descriptor.\n");
518 return G_IO_CHANNEL_ERROR_FAILED;
523 g_warning("File descriptor outside valid address space.\n");
524 return G_IO_CHANNEL_ERROR_FAILED;
529 return G_IO_CHANNEL_ERROR_FBIG;
534 return G_IO_CHANNEL_ERROR_INVAL;
539 return G_IO_CHANNEL_ERROR_IO;
544 return G_IO_CHANNEL_ERROR_ISDIR;
549 return G_IO_CHANNEL_ERROR_NOSPC;
554 return G_IO_CHANNEL_ERROR_NXIO;
559 return G_IO_CHANNEL_ERROR_OVERFLOW;
564 return G_IO_CHANNEL_ERROR_PIPE;
568 return G_IO_CHANNEL_ERROR_FAILED;
573 * g_io_channel_set_buffer_size:
574 * @channel: a #GIOChannel
575 * @size: the size of the buffer. 0 == pick a good size
577 * Set the buffer size.
580 g_io_channel_set_buffer_size (GIOChannel *channel,
583 g_return_if_fail (channel != NULL);
586 size = G_IO_NICE_BUF_SIZE;
588 if (size < 10) /* Needs to be larger than the widest char in any encoding */
591 channel->buf_size = size;
595 * g_io_channel_get_buffer_size:
596 * @channel: a #GIOChannel
598 * Get the buffer size.
600 * Return value: the size of the buffer.
603 g_io_channel_get_buffer_size (GIOChannel *channel)
605 g_return_val_if_fail (channel != NULL, 0);
607 return channel->buf_size;
611 * g_io_channel_set_line_term:
612 * @channel: a #GIOChannel
613 * @line_term: The line termination string. Use %NULL for auto detect.
615 * This sets the string that #GIOChannel uses to determine
616 * where in the file a line break occurs.
619 g_io_channel_set_line_term (GIOChannel *channel,
620 const gchar *line_term)
622 g_return_if_fail (channel != NULL);
623 g_return_if_fail (!line_term || line_term[0]); /* Disallow "" */
624 g_return_if_fail (!line_term || g_utf8_validate (line_term, -1, NULL));
625 /* Require valid UTF-8 */
627 g_free (channel->line_term);
628 channel->line_term = g_strdup (line_term);
632 * g_io_channel_get_line_term:
633 * @channel: a #GIOChannel
635 * This returns the string that #GIOChannel uses to determine
636 * where in the file a line break occurs. A value of %NULL
637 * indicates auto detection.
639 * Return value: The line termination string. This value
640 * is owned by GLib and must not be freed.
642 G_CONST_RETURN gchar*
643 g_io_channel_get_line_term (GIOChannel *channel)
645 g_return_val_if_fail (channel != NULL, 0);
647 return channel->line_term;
651 * g_io_channel_set_flags:
652 * @channel: a #GIOChannel
653 * @flags: the flags to set on the channel
654 * @error: A location to return an error of type #GIOChannelError
659 g_io_channel_set_flags (GIOChannel *channel,
663 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
664 g_return_val_if_fail ((error == NULL) || (*error == NULL),
667 return (* channel->funcs->io_set_flags)(channel,
668 flags & G_IO_FLAG_SET_MASK,
673 * g_io_channel_get_flags:
674 * @channel: a #GIOChannel
676 * Gets the current flags for a #GIOChannel, including read-only
677 * flags such as %G_IO_FLAG_IS_READABLE.
679 * Return value: the flags which are set on the channel
682 g_io_channel_get_flags (GIOChannel *channel)
686 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
688 flags = (* channel->funcs->io_get_flags) (channel);
690 /* Cross implementation code */
692 if (channel->is_seekable)
693 flags |= G_IO_FLAG_IS_SEEKABLE;
694 if (channel->is_readable)
695 flags |= G_IO_FLAG_IS_READABLE;
696 if (channel->is_writeable)
697 flags |= G_IO_FLAG_IS_WRITEABLE;
703 * g_io_channel_seek_position:
704 * @channel: a #GIOChannel
705 * @offset: The offset in bytes from the position specified by @type
706 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed if
707 * the channel has the default encoding or the
708 * encoding %G_IO_CHANNEL_ENCODE_RAW for raw file access.
709 * @error: A location to return an error of type #GIOChannelError
711 * Replacement for g_io_channel_seek() with the new API.
716 g_io_channel_seek_position (GIOChannel* channel,
723 /* For files, only one of the read and write buffers can contain data.
724 * For sockets, both can contain data.
727 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
728 g_return_val_if_fail ((error == NULL) || (*error == NULL),
730 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
734 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
735 if (channel->use_buffer)
737 if (channel->do_encode && channel->encoded_read_buf
738 && channel->encoded_read_buf->len > 0)
740 g_warning ("Seek type G_SEEK_CUR not allowed for this"
741 " channel's encoding.\n");
742 return G_IO_STATUS_ERROR;
744 if (channel->read_buf)
745 offset -= channel->read_buf->len;
746 if (channel->encoded_read_buf)
748 g_assert (channel->encoded_read_buf->len == 0 && !channel->do_encode);
750 /* If there's anything here, it's because the encoding is UTF-8,
751 * so we can just subtract the buffer length, the same as for
752 * the unencoded data.
755 offset -= channel->encoded_read_buf->len;
763 g_warning ("g_io_channel_seek_position: unknown seek type");
764 return G_IO_STATUS_ERROR;
767 if (channel->use_buffer)
769 status = g_io_channel_flush (channel, error);
770 if (status != G_IO_STATUS_NORMAL)
774 status = channel->funcs->io_seek (channel, offset, type, error);
776 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
778 if (channel->read_buf)
779 g_string_truncate (channel->read_buf, 0);
781 /* Conversion state no longer matches position in file */
782 if (channel->read_cd != (GIConv) -1)
783 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
784 if (channel->write_cd != (GIConv) -1)
785 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
787 if (channel->encoded_read_buf)
789 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
790 g_string_truncate (channel->encoded_read_buf, 0);
793 if (channel->partial_write_buf[0] != '\0')
795 g_warning ("Partial character at end of write buffer not flushed.\n");
796 channel->partial_write_buf[0] = '\0';
804 * g_io_channel_flush:
805 * @channel: a #GIOChannel
806 * @error: location to store an error of type #GIOChannelError
808 * Flush the write buffer for the GIOChannel.
810 * Return value: the status of the operation: One of
811 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
812 * G_IO_CHANNEL_ERROR.
815 g_io_channel_flush (GIOChannel *channel,
819 gsize this_time = 1, bytes_written = 0;
821 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
822 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
824 if (channel->write_buf == NULL || channel->write_buf->len == 0)
825 return G_IO_STATUS_NORMAL;
829 g_assert (this_time > 0);
831 status = channel->funcs->io_write (channel,
832 channel->write_buf->str + bytes_written,
833 channel->write_buf->len - bytes_written,
835 bytes_written += this_time;
837 while ((bytes_written < channel->write_buf->len)
838 && (status == G_IO_STATUS_NORMAL));
840 g_string_erase (channel->write_buf, 0, bytes_written);
846 * g_io_channel_set_buffered:
847 * @channel: a #GIOChannel
848 * @buffered: whether to set the channel buffered or unbuffered
850 * The buffering state can only be set if the channel's encoding
851 * is %NULL. For any other encoding, the channel must be buffered.
853 * The default state of the channel is buffered.
856 g_io_channel_set_buffered (GIOChannel *channel,
859 g_return_if_fail (channel != NULL);
861 if (channel->encoding != NULL)
863 g_warning ("Need to have NULL encoding to set the buffering state of the "
868 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
869 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
871 channel->use_buffer = buffered;
875 * g_io_channel_get_buffered:
876 * @channel: a #GIOChannel
878 * Return Value: the buffering state of the channel
881 g_io_channel_get_buffered (GIOChannel *channel)
883 g_return_val_if_fail (channel != NULL, FALSE);
885 return channel->use_buffer;
889 * g_io_channel_set_encoding:
890 * @channel: a #GIOChannel
891 * @encoding: the encoding type
892 * @error: location to store an error of type #GConvertError.
894 * Set the encoding for the input/output of the channel. The internal
895 * encoding is always UTF-8. The default encoding for the
896 * external file is UTF-8.
898 * The encoding %NULL is safe to use with binary data.
899 * Encodings other than %NULL must use a buffered channel.
900 * Encodings other than %NULL and UTF-8 cannot
901 * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
902 * and cannot mix reading and writing if the channel is
903 * a file without first doing a seek of type %G_SEEK_SET or
906 * The encoding can only be set under three conditions:
908 * 1. The channel was just created, and has not been written to
911 * 2. The channel is a file, and the file pointer was just
912 * repositioned by a call to g_io_channel_seek_position().
913 * (This flushes all the internal buffers.)
915 * 3. The current encoding is %NULL or UTF-8.
917 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
920 g_io_channel_set_encoding (GIOChannel *channel,
921 const gchar *encoding,
924 GIConv read_cd, write_cd;
927 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
928 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
930 /* Make sure the encoded buffers are empty */
932 g_return_val_if_fail (!channel->encoded_read_buf ||
933 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
934 g_return_val_if_fail (channel->partial_write_buf[0] == '\0', G_IO_STATUS_ERROR);
936 if (!channel->use_buffer)
938 g_warning ("Need to set the channel buffered before setting the encoding.\n");
939 g_warning ("Assuming this is what you meant and acting accordingly.\n");
941 channel->use_buffer = TRUE;
944 did_encode = channel->do_encode;
946 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
948 channel->do_encode = FALSE;
949 read_cd = write_cd = (GIConv) -1;
954 const gchar *from_enc = NULL, *to_enc = NULL;
956 if (channel->is_readable)
958 read_cd = g_iconv_open ("UTF-8", encoding);
960 if (read_cd == (GIConv) -1)
968 read_cd = (GIConv) -1;
970 if (channel->is_writeable && err == 0)
972 write_cd = g_iconv_open (encoding, "UTF-8");
974 if (write_cd == (GIConv) -1)
982 write_cd = (GIConv) -1;
990 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
991 _("Conversion from character set `%s' to `%s' is not supported"),
994 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
995 _("Could not open converter from `%s' to `%s': %s"),
996 from_enc, to_enc, strerror (errno));
998 if (read_cd != (GIConv) -1)
999 g_iconv_close (read_cd);
1000 if (write_cd != (GIConv) -1)
1001 g_iconv_close (write_cd);
1003 return G_IO_STATUS_ERROR;
1006 channel->do_encode = TRUE;
1009 /* The encoding is ok, so set the fields in channel */
1011 if (channel->read_cd != (GIConv) -1)
1012 g_iconv_close (channel->read_cd);
1013 if (channel->write_cd != (GIConv) -1)
1014 g_iconv_close (channel->write_cd);
1016 channel->read_cd = read_cd;
1017 channel->write_cd = write_cd;
1019 g_free (channel->encoding);
1020 channel->encoding = g_strdup (encoding);
1022 return G_IO_STATUS_NORMAL;
1026 * g_io_channel_get_encoding:
1027 * @channel: a #GIOChannel
1029 * Get the encoding for the input/output of the channel. The internal
1030 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
1031 * disables encoding and turns off internal buffering. Both
1032 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
1033 * are safe to use with binary data.
1035 * Return value: A string containing the encoding, this string is
1036 * owned by GLib and must not be freed.
1038 G_CONST_RETURN gchar*
1039 g_io_channel_get_encoding (GIOChannel *channel)
1041 g_return_val_if_fail (channel != NULL, NULL);
1043 return channel->encoding;
1047 g_io_channel_fill_buffer (GIOChannel *channel,
1050 gsize read_size, cur_len, oldlen;
1053 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1055 status = g_io_channel_flush (channel, err);
1056 if (status != G_IO_STATUS_NORMAL)
1059 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1061 g_warning ("Partial character at end of write buffer not flushed.\n");
1062 channel->partial_write_buf[0] = '\0';
1065 if (!channel->read_buf)
1066 channel->read_buf = g_string_sized_new (channel->buf_size);
1068 cur_len = channel->read_buf->len;
1070 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1072 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1073 channel->buf_size, &read_size, err);
1075 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1077 g_string_truncate (channel->read_buf, read_size + cur_len);
1079 if ((status != G_IO_STATUS_NORMAL)
1080 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1083 g_assert (channel->read_buf->len > 0);
1085 if (channel->encoded_read_buf)
1086 oldlen = channel->encoded_read_buf->len;
1090 if (channel->do_encode)
1092 size_t errnum, inbytes_left, outbytes_left;
1093 gchar *inbuf, *outbuf;
1095 if (!channel->encoded_read_buf)
1096 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1100 inbytes_left = channel->read_buf->len;
1101 outbytes_left = MIN (channel->buf_size / 4,
1102 channel->encoded_read_buf->allocated_len
1103 - channel->encoded_read_buf->len);
1105 g_string_set_size (channel->encoded_read_buf,
1106 channel->encoded_read_buf->len + outbytes_left);
1108 inbuf = channel->read_buf->str;
1109 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1112 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1113 &outbuf, &outbytes_left);
1115 g_string_erase (channel->read_buf, 0,
1116 channel->read_buf->len - inbytes_left);
1117 g_string_truncate (channel->encoded_read_buf,
1118 channel->encoded_read_buf->len - outbytes_left);
1120 if (errnum == (size_t) -1)
1125 if ((oldlen == channel->encoded_read_buf->len)
1126 && (status == G_IO_STATUS_EOF))
1127 status = G_IO_STATUS_EOF;
1129 status = G_IO_STATUS_NORMAL;
1134 if (oldlen < channel->encoded_read_buf->len)
1135 status = G_IO_STATUS_NORMAL;
1138 g_set_error (err, G_CONVERT_ERROR,
1139 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1140 _("Invalid byte sequence in conversion input"));
1141 return G_IO_STATUS_ERROR;
1145 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1146 _("Error during conversion: %s"), strerror (errno));
1147 return G_IO_STATUS_ERROR;
1150 g_assert ((status != G_IO_STATUS_NORMAL)
1151 || (channel->encoded_read_buf->len > 0));
1153 else if (channel->encoding) /* UTF-8 */
1155 gchar *nextchar, *lastchar;
1157 nextchar = channel->read_buf->str;
1158 lastchar = channel->read_buf->str + channel->read_buf->len;
1160 while (nextchar < lastchar)
1164 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1169 /* stop, leave partial character in buffer */
1170 lastchar = nextchar;
1173 if (oldlen > channel->encoded_read_buf->len)
1174 status = G_IO_STATUS_NORMAL;
1177 g_set_error (err, G_CONVERT_ERROR,
1178 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1179 _("Invalid byte sequence in conversion input"));
1180 status = G_IO_STATUS_ERROR;
1182 lastchar = nextchar;
1185 nextchar = g_utf8_next_char (nextchar);
1190 if (lastchar > channel->read_buf->str)
1192 gint copy_len = lastchar - channel->read_buf->str;
1194 if (!channel->encoded_read_buf)
1195 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1197 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1199 g_string_erase (channel->read_buf, 0, copy_len);
1207 * g_io_channel_read_line:
1208 * @channel: a #GIOChannel
1209 * @str_return: The line read from the #GIOChannel, not including the
1210 * line terminator. This data should be freed with g_free()
1211 * when no longer needed. This
1212 * is a null terminated string. If a @length of zero is
1213 * returned, this will be %NULL instead.
1214 * @length: location to store length of the read data, or %NULL
1215 * @terminator_pos: location to store position of line terminator, or %NULL
1216 * @error: A location to return an error of type #GConvertError
1217 * or #GIOChannelError
1219 * Read a line, not including the terminating character(s),
1220 * from a #GIOChannel into a newly allocated string.
1221 * @length will contain allocated memory if the return
1222 * is %G_IO_STATUS_NORMAL.
1224 * Return value: a newly allocated string. Free this string
1225 * with g_free() when you are done with it.
1228 g_io_channel_read_line (GIOChannel *channel,
1231 gsize *terminator_pos,
1236 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1237 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1238 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1240 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1242 status = g_io_channel_read_line_backend (channel, length, terminator_pos, error);
1244 if (status == G_IO_STATUS_NORMAL)
1248 if (channel->do_encode)
1249 use_buf = channel->encoded_read_buf;
1251 use_buf = channel->read_buf;
1255 *str_return = g_strndup (use_buf->str, *length);
1256 g_string_erase (use_buf, 0, *length);
1265 * g_io_channel_read_line_string:
1266 * @channel: a #GIOChannel
1267 * @buffer: a #GString into which the line will be written.
1268 * If @buffer already contains data, the new data will
1269 * be appended to it.
1270 * @terminator_pos: location to store position of line terminator, or %NULL
1271 * @error: a location to store an error of type #GConvertError
1272 * or #GIOChannelError
1274 * Read a line from a #GIOChannel, using a #GString as a buffer.
1279 g_io_channel_read_line_string (GIOChannel *channel,
1281 gsize *terminator_pos,
1287 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1288 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1289 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1291 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1293 if (buffer->len > 0)
1294 g_string_truncate (buffer, 0); /* clear out the buffer */
1296 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1298 if (status == G_IO_STATUS_NORMAL)
1302 if (channel->do_encode)
1303 use_buf = channel->encoded_read_buf;
1305 use_buf = channel->read_buf;
1309 g_string_append_len (buffer, use_buf->str, length);
1310 g_string_erase (use_buf, 0, length);
1318 g_io_channel_read_line_backend (GIOChannel *channel,
1320 gsize *terminator_pos,
1324 gsize checked_to, line_term_len, line_length, got_term_len;
1326 gboolean first_time = TRUE;
1328 if (!channel->use_buffer)
1330 /* Can't do a raw read in read_line */
1331 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1332 _("Can't do a raw read in g_io_channel_read_line_string"));
1333 return G_IO_STATUS_ERROR;
1336 if (channel->encoding)
1338 if (!channel->encoded_read_buf)
1339 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1340 use_buf = channel->encoded_read_buf;
1344 if (!channel->read_buf)
1345 channel->read_buf = g_string_sized_new (channel->buf_size);
1346 use_buf = channel->read_buf;
1349 status = G_IO_STATUS_NORMAL;
1351 if (channel->line_term)
1352 line_term_len = strlen (channel->line_term);
1355 /* This value used for setting checked_to, it's the longest of the four
1356 * we autodetect for.
1363 gchar *nextchar, *lastchar;
1365 if (!first_time || (use_buf->len == 0))
1368 status = g_io_channel_fill_buffer (channel, error);
1371 case G_IO_STATUS_NORMAL:
1372 if (use_buf->len == 0)
1373 /* Can happen when using conversion and only read
1374 * part of a character
1381 case G_IO_STATUS_EOF:
1382 if (use_buf->len == 0)
1386 if (channel->do_encode && channel->read_buf->len != 0)
1387 /* using GIConv conversion, unconverted chars */
1389 g_set_error (error, G_CONVERT_ERROR,
1390 G_CONVERT_ERROR_PARTIAL_INPUT,
1391 "Leftover unconverted data in read buffer");
1392 return G_IO_STATUS_ERROR;
1395 return G_IO_STATUS_EOF;
1404 g_assert (use_buf->len > 0);
1408 lastchar = use_buf->str + strlen (use_buf->str);
1410 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1411 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1413 if (channel->line_term)
1415 if (strncmp (channel->line_term, nextchar, line_term_len) == 0)
1417 line_length = nextchar - use_buf->str;
1418 got_term_len = line_term_len;
1422 else /* auto detect */
1426 case '\n': /* unix */
1427 line_length = nextchar - use_buf->str;
1430 case '\r': /* Warning: do not use with sockets */
1431 line_length = nextchar - use_buf->str;
1432 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1433 && (lastchar == use_buf->str + use_buf->len))
1434 goto read_again; /* Try to read more data */
1435 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1440 case '\xe2': /* Unicode paragraph separator */
1441 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1443 line_length = nextchar - use_buf->str;
1448 default: /* no match */
1454 g_assert (nextchar == lastchar); /* Valid UTF-8, didn't overshoot */
1456 /* Also terminate on '\0' */
1458 line_length = lastchar - use_buf->str;
1459 if (line_length < use_buf->len)
1467 if (status == G_IO_STATUS_EOF)
1469 if (channel->encoding && channel->read_buf->len > 0)
1471 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1472 "Channel terminates in a partial character");
1473 return G_IO_STATUS_ERROR;
1475 line_length = use_buf->len;
1480 if (use_buf->len > line_term_len - 1)
1481 checked_to = use_buf->len - (line_term_len - 1);
1489 *terminator_pos = line_length;
1491 *length = line_length + got_term_len;
1493 return G_IO_STATUS_NORMAL;
1497 * g_io_channel_read_to_end:
1498 * @channel: a #GIOChannel
1499 * @str_return: Location to store a pointer to a string holding
1500 * the remaining data in the #GIOChannel. This data should
1501 * be freed with g_free() when no longer needed. This
1502 * data is terminated by an extra null, but there may be other
1503 * nulls in the intervening data.
1504 * @length: Location to store length of the data
1505 * @error: A location to return an error of type #GConvertError
1506 * or #GIOChannelError
1508 * Read all the remaining data from the file. Parameters as
1509 * for g_io_channel_read_line.
1511 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1515 g_io_channel_read_to_end (GIOChannel *channel,
1523 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1524 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1525 g_return_val_if_fail (length != NULL, G_IO_STATUS_ERROR);
1526 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1528 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1533 if (!channel->use_buffer)
1535 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1536 _("Can't do a raw read in g_io_channel_read_to_end"));
1537 return G_IO_STATUS_ERROR;
1541 status = g_io_channel_fill_buffer (channel, error);
1542 while (status == G_IO_STATUS_NORMAL);
1544 if (status != G_IO_STATUS_EOF)
1547 if (channel->encoding)
1549 if (channel->read_buf->len > 0)
1551 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1552 "Channel terminates in a partial character");
1553 return G_IO_STATUS_ERROR;
1556 if (!channel->encoded_read_buf)
1561 *str_return = g_strdup ("");
1564 use_buf = &channel->encoded_read_buf;
1567 use_buf = &channel->read_buf;
1569 g_assert (*use_buf); /* Created by fill_buffer if it didn't previously exist */
1572 *length = (*use_buf)->len;
1575 *str_return = g_string_free (*use_buf, FALSE);
1577 g_string_free (*use_buf, TRUE);
1581 return G_IO_STATUS_NORMAL;
1585 * g_io_channel_read_chars:
1586 * @channel: a #GIOChannel
1587 * @buf: a buffer to read data into
1588 * @count: the size of the buffer. Note that the buffer may
1589 * not be complelely filled even if there is data
1590 * in the buffer if the remaining data is not a
1591 * complete character.
1592 * @bytes_read: The number of bytes read.
1593 * @error: A location to return an error of type #GConvertError
1594 * or #GIOChannelError.
1596 * Replacement for g_io_channel_read() with the new API.
1601 g_io_channel_read_chars (GIOChannel *channel,
1610 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1611 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1613 g_return_val_if_fail (bytes_read != NULL, G_IO_STATUS_ERROR);
1614 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1619 return G_IO_STATUS_NORMAL;
1621 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1623 if (!channel->use_buffer)
1625 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1627 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1630 if (channel->encoding)
1632 if (!channel->encoded_read_buf)
1633 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1634 use_buf = channel->encoded_read_buf;
1638 if (!channel->read_buf)
1639 channel->read_buf = g_string_sized_new (channel->buf_size);
1640 use_buf = channel->read_buf;
1643 status = G_IO_STATUS_NORMAL;
1645 while (use_buf->len < MAX (count, 6) && status == G_IO_STATUS_NORMAL)
1646 /* Be sure to read in at least one full UTF-8 character
1649 status = g_io_channel_fill_buffer (channel, error);
1653 case G_IO_STATUS_NORMAL:
1654 g_assert (use_buf->len > 0);
1656 case G_IO_STATUS_EOF:
1657 if (use_buf->len == 0)
1661 if (channel->encoding && channel->read_buf->len != 0)
1663 g_set_error (error, G_CONVERT_ERROR,
1664 G_CONVERT_ERROR_PARTIAL_INPUT,
1665 "Leftover unconverted data in read buffer");
1666 return G_IO_STATUS_ERROR;
1669 return G_IO_STATUS_EOF;
1672 case G_IO_STATUS_AGAIN:
1673 if (use_buf->len > 0)
1674 break; /* return what we have */
1678 return G_IO_STATUS_AGAIN;
1685 *bytes_read = MIN (count, use_buf->len);
1687 if (channel->encoding && *bytes_read > 0)
1688 /* Don't validate for NULL encoding, binary safe */
1690 gchar *nextchar, *prevchar = NULL;
1692 nextchar = use_buf->str;
1694 while (nextchar < use_buf->str + *bytes_read)
1696 prevchar = nextchar;
1697 nextchar = g_utf8_next_char (nextchar);
1700 if (nextchar > use_buf->str + *bytes_read)
1701 *bytes_read = prevchar - use_buf->str;
1703 g_assert (*bytes_read > 0 || count < 6);
1706 memcpy (buf, use_buf->str, *bytes_read);
1707 g_string_erase (use_buf, 0, *bytes_read);
1709 return G_IO_STATUS_NORMAL;
1713 * g_io_channel_write_chars:
1714 * @channel: a #GIOChannel
1715 * @buf: a buffer to write data from
1716 * @count: the size of the buffer. If -1, the buffer
1717 * is taken to be a nul terminated string.
1718 * @bytes_written: The number of bytes written. This can be nonzero
1719 * even if the return value is not %G_IO_STATUS_NORMAL.
1720 * @error: A location to return an error of type #GConvertError
1721 * or #GIOChannelError
1723 * Replacement for g_io_channel_write() with the new API.
1728 g_io_channel_write_chars (GIOChannel *channel,
1731 gsize *bytes_written,
1736 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1737 g_return_val_if_fail (bytes_written != NULL, G_IO_STATUS_ERROR);
1738 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1740 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1742 if ((count < 0) && buf)
1743 count = strlen (buf);
1746 return G_IO_STATUS_NORMAL;
1748 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1749 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1751 /* Raw write case */
1753 if (!channel->use_buffer)
1755 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1756 g_assert (channel->partial_write_buf[0] == '\0');
1757 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1762 if (channel->is_seekable && ((channel->read_buf && channel->read_buf->len > 0)
1763 || (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)))
1765 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1767 g_warning("Mixed reading and writing not allowed on encoded files");
1768 return G_IO_STATUS_ERROR;
1770 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1771 if (status != G_IO_STATUS_NORMAL)
1775 if (!channel->write_buf)
1776 channel->write_buf = g_string_sized_new (channel->buf_size);
1778 if (!channel->do_encode)
1782 if (channel->encoding)
1784 const gchar *badchar;
1786 if (channel->partial_write_buf[0] != '\0')
1788 guint partial_chars = strlen (channel->partial_write_buf);
1789 guint chars_in_buf = MIN (6, partial_chars + count);
1792 memcpy (channel->partial_write_buf + partial_chars, buf,
1793 chars_in_buf - partial_chars);
1795 g_utf8_validate (channel->partial_write_buf, chars_in_buf, &badchar);
1797 if (badchar < channel->partial_write_buf + partial_chars)
1800 gsize left_len = chars_in_buf;
1802 g_assert (badchar == channel->partial_write_buf);
1804 try_char = g_utf8_get_char_validated (badchar, left_len);
1809 g_assert (chars_in_buf < 6);
1810 channel->partial_write_buf[chars_in_buf] = '\0';
1811 *bytes_written = count;
1812 return G_IO_STATUS_NORMAL;
1814 g_set_error (error, G_CONVERT_ERROR,
1815 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1816 "Illegal UTF-8 sequence");
1818 return G_IO_STATUS_ERROR;
1820 g_assert_not_reached ();
1821 return G_IO_STATUS_ERROR; /* keep the compiler happy */
1825 skip = badchar - (channel->partial_write_buf + partial_chars);
1828 copy_bytes = count - skip;
1830 g_string_append_len (channel->write_buf, channel->partial_write_buf,
1831 badchar - channel->partial_write_buf);
1832 channel->partial_write_buf[0] = '\0';
1837 if (!g_utf8_validate (buf, copy_bytes, &badchar))
1840 gsize left_len = buf + copy_bytes - badchar;
1842 try_char = g_utf8_get_char_validated (badchar, left_len);
1847 g_assert (left_len < 6);
1850 g_set_error (error, G_CONVERT_ERROR,
1851 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1852 "Illegal UTF-8 sequence");
1853 *bytes_written = count - copy_bytes;
1854 return G_IO_STATUS_ERROR;
1856 g_assert_not_reached ();
1857 return G_IO_STATUS_ERROR; /* Don't confunse the compiler */
1860 g_string_append (channel->write_buf, channel->partial_write_buf);
1861 memcpy (channel->partial_write_buf, badchar, left_len);
1862 channel->partial_write_buf[left_len] = '\0';
1863 copy_bytes = count - left_len;
1867 g_string_append (channel->write_buf, channel->partial_write_buf);
1868 channel->partial_write_buf[0] = '\0';
1875 g_assert (channel->partial_write_buf[0] == '\0');
1878 while (copy_bytes > channel->write_buf->allocated_len - channel->write_buf->len)
1882 if (channel->write_buf->len > 0)
1884 status = channel->funcs->io_write (channel, channel->write_buf->str,
1885 channel->write_buf->len, &wrote_bytes,
1887 g_string_erase (channel->write_buf, 0, wrote_bytes);
1891 status = channel->funcs->io_write (channel, buf, copy_bytes, &wrote_bytes,
1893 copy_bytes -= wrote_bytes;
1897 if (status != G_IO_STATUS_NORMAL)
1899 *bytes_written = count - copy_bytes;
1900 if (channel->partial_write_buf[0] != '\0')
1902 *bytes_written -= strlen (channel->partial_write_buf);
1903 channel->partial_write_buf[0] = '\0';
1909 g_string_append_len (channel->write_buf, buf, copy_bytes);
1910 *bytes_written = count;
1914 gsize bytes_to_go = count, buf_space, err;
1916 gsize oldlen = channel->write_buf->len;
1918 if (channel->partial_write_buf[0] != '\0')
1920 gsize partial_chars = strlen (channel->partial_write_buf);
1921 gsize partial_buf_size = MIN (6, partial_chars + count);
1922 guint chars_in_buf = partial_buf_size;
1925 g_assert (partial_chars < 6);
1927 memcpy (channel->partial_write_buf + partial_chars, buf,
1928 chars_in_buf - partial_chars);
1929 inbuf = channel->partial_write_buf;
1931 buf_space = MIN (channel->buf_size / 4, channel->write_buf->allocated_len
1932 - channel->write_buf->len);
1933 g_string_set_size (channel->write_buf, channel->write_buf->len
1935 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1937 err = g_iconv (channel->write_cd, &inbuf, &partial_buf_size,
1938 &outbuf, &buf_space);
1940 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1942 if (err == (size_t) -1)
1947 if (errno == EINVAL)
1949 if (chars_in_buf == partial_buf_size)
1951 /* Didn't convert anything */
1953 g_assert (chars_in_buf < 6); /* More of the same character */
1954 g_assert (chars_in_buf == count + partial_chars);
1956 channel->partial_write_buf[chars_in_buf] = '\0';
1957 *bytes_written = count;
1958 return G_IO_STATUS_NORMAL;
1961 g_assert (chars_in_buf - partial_buf_size > partial_chars);
1962 /* Converted the character of which a part was sitting in
1963 * the partial character buffer before the write.
1972 g_assert_not_reached ();
1974 g_set_error (error, G_CONVERT_ERROR,
1975 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1976 _("Invalid byte sequence in conversion input"));
1978 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1979 _("Error during conversion: %s"), strerror (errno));
1982 g_string_truncate (channel->write_buf, oldlen);
1984 channel->partial_write_buf[partial_chars] = '\0';
1986 return G_IO_STATUS_ERROR;
1990 *bytes_written = (chars_in_buf - partial_buf_size) - partial_chars;
1991 buf += *bytes_written;
1992 bytes_to_go -= *bytes_written;
1993 channel->partial_write_buf[0] = '\0';
1998 buf_space = MIN (channel->write_buf->allocated_len
1999 - channel->write_buf->len, channel->buf_size / 4);
2001 g_string_set_size (channel->write_buf, channel->write_buf->len + buf_space);
2002 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
2004 err = g_iconv (channel->write_cd, (gchar**) &buf, &bytes_to_go,
2005 &outbuf, &buf_space);
2007 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
2008 *bytes_written = count - bytes_to_go;
2010 if (err == (size_t) -1)
2016 gint bytes_left = count - *bytes_written;
2018 g_assert (bytes_left < 6);
2020 memcpy (channel->partial_write_buf, buf, bytes_left);
2021 channel->partial_write_buf[bytes_left] = '\0';
2023 *bytes_written = count;
2025 return G_IO_STATUS_NORMAL;
2030 status = channel->funcs->io_write (channel,
2031 channel->write_buf->str, channel->write_buf->len,
2032 &wrote_bytes, error);
2033 g_string_erase (channel->write_buf, 0, wrote_bytes);
2035 if (status != G_IO_STATUS_NORMAL)
2040 g_set_error (error, G_CONVERT_ERROR,
2041 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2042 _("Invalid byte sequence in conversion input"));
2043 return G_IO_STATUS_ERROR;
2045 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2046 _("Error during conversion: %s"), strerror (errno));
2047 return G_IO_STATUS_ERROR;
2052 while (bytes_to_go > 0);
2055 if (channel->write_buf->len > channel->buf_size)
2059 status = channel->funcs->io_write (channel, channel->write_buf->str,
2060 channel->write_buf->len, &wrote_bytes, error);
2062 if (wrote_bytes > 0)
2063 g_string_erase (channel->write_buf, 0, wrote_bytes);
2068 return G_IO_STATUS_NORMAL;
2072 * g_io_channel_error_quark:
2074 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2077 g_io_channel_error_quark (void)
2079 static GQuark q = 0;
2081 q = g_quark_from_static_string ("g-io-channel-error-quark");