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->read_buf && (channel->read_buf->len > 0)) /* FIXME full chars how? */
481 || (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0)))
482 condition &= G_IO_IN;
484 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
485 condition &= G_IO_OUT;
491 * g_io_channel_error_from_errno:
492 * @en: An errno error number, e.g. EINVAL
494 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
497 g_io_channel_error_from_errno (gint en)
500 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
503 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
510 g_warning("Invalid file descriptor.\n");
511 return G_IO_CHANNEL_ERROR_FAILED;
516 g_warning("File descriptor outside valid address space.\n");
517 return G_IO_CHANNEL_ERROR_FAILED;
522 return G_IO_CHANNEL_ERROR_FBIG;
527 return G_IO_CHANNEL_ERROR_INVAL;
532 return G_IO_CHANNEL_ERROR_IO;
537 return G_IO_CHANNEL_ERROR_ISDIR;
542 return G_IO_CHANNEL_ERROR_NOSPC;
547 return G_IO_CHANNEL_ERROR_NXIO;
552 return G_IO_CHANNEL_ERROR_OVERFLOW;
557 return G_IO_CHANNEL_ERROR_PIPE;
561 return G_IO_CHANNEL_ERROR_FAILED;
566 * g_io_channel_set_buffer_size:
567 * @channel: a #GIOChannel
568 * @size: the size of the buffer. 0 == pick a good size
570 * Set the buffer size.
573 g_io_channel_set_buffer_size (GIOChannel *channel,
576 g_return_if_fail (channel != NULL);
579 size = G_IO_NICE_BUF_SIZE;
581 if (size < 10) /* Needs to be larger than the widest char in any encoding */
584 channel->buf_size = size;
588 * g_io_channel_get_buffer_size:
589 * @channel: a #GIOChannel
591 * Get the buffer size.
593 * Return value: the size of the buffer.
596 g_io_channel_get_buffer_size (GIOChannel *channel)
598 g_return_val_if_fail (channel != NULL, 0);
600 return channel->buf_size;
604 * g_io_channel_set_line_term:
605 * @channel: a #GIOChannel
606 * @line_term: The line termination string. Use %NULL for auto detect.
608 * This sets the string that #GIOChannel uses to determine
609 * where in the file a line break occurs.
612 g_io_channel_set_line_term (GIOChannel *channel,
613 const gchar *line_term)
615 g_return_if_fail (channel != NULL);
616 g_return_if_fail (!line_term || line_term[0]); /* Disallow "" */
617 g_return_if_fail (!line_term || g_utf8_validate (line_term, -1, NULL));
618 /* Require valid UTF-8 */
620 g_free (channel->line_term);
621 channel->line_term = g_strdup (line_term);
625 * g_io_channel_get_line_term:
626 * @channel: a #GIOChannel
628 * This returns the string that #GIOChannel uses to determine
629 * where in the file a line break occurs. A value of %NULL
630 * indicates auto detection.
632 * Return value: The line termination string. This value
633 * is owned by GLib and must not be freed.
635 G_CONST_RETURN gchar*
636 g_io_channel_get_line_term (GIOChannel *channel)
638 g_return_val_if_fail (channel != NULL, 0);
640 return channel->line_term;
644 * g_io_channel_set_flags:
645 * @channel: a #GIOChannel
646 * @flags: the flags to set on the channel
647 * @error: A location to return an error of type #GIOChannelError
652 g_io_channel_set_flags (GIOChannel *channel,
656 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
657 g_return_val_if_fail ((error == NULL) || (*error == NULL),
660 return (* channel->funcs->io_set_flags)(channel,
661 flags & G_IO_FLAG_SET_MASK,
666 * g_io_channel_get_flags:
667 * @channel: a #GIOChannel
669 * Gets the current flags for a #GIOChannel, including read-only
670 * flags such as %G_IO_FLAG_IS_READABLE.
672 * Return value: the flags which are set on the channel
675 g_io_channel_get_flags (GIOChannel *channel)
679 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
681 flags = (* channel->funcs->io_get_flags) (channel);
683 /* Cross implementation code */
685 if (channel->is_seekable)
686 flags |= G_IO_FLAG_IS_SEEKABLE;
687 if (channel->is_readable)
688 flags |= G_IO_FLAG_IS_READABLE;
689 if (channel->is_writeable)
690 flags |= G_IO_FLAG_IS_WRITEABLE;
696 * g_io_channel_seek_position:
697 * @channel: a #GIOChannel
698 * @offset: The offset in bytes from the position specified by @type
699 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed if
700 * the channel has the default encoding or the
701 * encoding %G_IO_CHANNEL_ENCODE_RAW for raw file access.
702 * @error: A location to return an error of type #GIOChannelError
704 * Replacement for g_io_channel_seek() with the new API.
709 g_io_channel_seek_position (GIOChannel* channel,
716 /* For files, only one of the read and write buffers can contain data.
717 * For sockets, both can contain data.
720 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
721 g_return_val_if_fail ((error == NULL) || (*error == NULL),
723 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
727 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
728 if (channel->use_buffer)
730 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
732 g_warning ("Seek type G_SEEK_CUR not allowed for this"
733 " channel's encoding.\n");
734 return G_IO_STATUS_ERROR;
736 if (channel->read_buf)
737 offset -= channel->read_buf->len;
744 g_warning ("g_io_channel_seek_position: unknown seek type");
745 return G_IO_STATUS_ERROR;
748 if (channel->use_buffer)
750 status = g_io_channel_flush (channel, error);
751 if (status != G_IO_STATUS_NORMAL)
755 status = channel->funcs->io_seek (channel, offset, type, error);
757 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
759 if (channel->read_buf)
760 g_string_truncate (channel->read_buf, 0);
761 if (channel->do_encode) /* Conversion state no longer matches position in file */
763 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
764 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
765 if (channel->encoded_read_buf)
766 g_string_truncate (channel->encoded_read_buf, 0);
768 if (channel->partial_write_buf[0] != '\0')
770 g_warning ("Partial character at end of write buffer not flushed.\n");
771 channel->partial_write_buf[0] = '\0';
779 * g_io_channel_flush:
780 * @channel: a #GIOChannel
781 * @error: location to store an error of type #GIOChannelError
783 * Flush the write buffer for the GIOChannel.
785 * Return value: the status of the operation: One of
786 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
787 * G_IO_CHANNEL_ERROR.
790 g_io_channel_flush (GIOChannel *channel,
794 gsize this_time = 1, bytes_written = 0;
796 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
797 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
799 if (channel->write_buf && channel->write_buf->len > 0)
800 return G_IO_STATUS_NORMAL;
804 g_assert (this_time > 0);
806 status = channel->funcs->io_write (channel,
807 channel->write_buf->str + bytes_written,
808 channel->write_buf->len - bytes_written,
810 bytes_written += this_time;
812 while ((bytes_written < channel->write_buf->len)
813 && (status == G_IO_STATUS_NORMAL));
815 g_string_erase (channel->write_buf, 0, bytes_written);
821 * g_io_channel_set_buffered:
822 * @channel: a #GIOChannel
823 * @buffered: whether to set the channel buffered or unbuffered
825 * The buffering state can only be set if the channel's encoding
826 * is %NULL. For any other encoding, the channel must be buffered.
828 * The default state of the channel is buffered.
831 g_io_channel_set_buffered (GIOChannel *channel,
834 g_return_if_fail (channel != NULL);
836 if (channel->encoding != NULL)
838 g_warning ("Need to have NULL encoding to set the buffering state of the "
843 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
844 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
846 channel->use_buffer = buffered;
850 * g_io_channel_get_buffered:
851 * @channel: a #GIOChannel
853 * Return Value: the buffering state of the channel
856 g_io_channel_get_buffered (GIOChannel *channel)
858 g_return_val_if_fail (channel != NULL, FALSE);
860 return channel->use_buffer;
864 * g_io_channel_set_encoding:
865 * @channel: a #GIOChannel
866 * @encoding: the encoding type
867 * @error: location to store an error of type #GConvertError.
869 * Set the encoding for the input/output of the channel. The internal
870 * encoding is always UTF-8. The default encoding for the
871 * external file is UTF-8.
873 * The encoding %NULL is safe to use with binary data.
874 * Encodings other than %NULL must use a buffered channel.
875 * Encodings other than %NULL and UTF-8 cannot
876 * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
877 * and cannot mix reading and writing if the channel is
878 * a file without first doing a seek of type %G_SEEK_SET or
881 * The encoding can only be set under three conditions:
883 * 1. The channel was just created, and has not been written to
886 * 2. The channel is a file, and the file pointer was just
887 * repositioned by a call to g_io_channel_seek_position().
888 * (This flushes all the internal buffers.)
890 * 3. The current encoding is %NULL or UTF-8.
892 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
895 g_io_channel_set_encoding (GIOChannel *channel,
896 const gchar *encoding,
899 GIConv read_cd, write_cd;
902 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
903 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
905 /* Make sure the encoded buffers are empty */
907 g_return_val_if_fail (!channel->encoded_read_buf ||
908 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
909 g_return_val_if_fail (channel->partial_write_buf[0] == '\0', G_IO_STATUS_ERROR);
911 if (!channel->use_buffer)
913 g_warning ("Need to set the channel buffered before setting the encoding.\n");
914 g_warning ("Assuming this is what you meant and acting accordingly.\n");
916 channel->use_buffer = TRUE;
919 did_encode = channel->do_encode;
921 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
923 channel->do_encode = FALSE;
924 read_cd = write_cd = (GIConv) -1;
929 const gchar *from_enc = NULL, *to_enc = NULL;
931 if (channel->is_readable)
933 read_cd = g_iconv_open ("UTF-8", encoding);
935 if (read_cd == (GIConv) -1)
943 read_cd = (GIConv) -1;
945 if (channel->is_writeable && err == 0)
947 write_cd = g_iconv_open (encoding, "UTF-8");
949 if (write_cd == (GIConv) -1)
957 write_cd = (GIConv) -1;
965 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
966 _("Conversion from character set `%s' to `%s' is not supported"),
969 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
970 _("Could not open converter from `%s' to `%s': %s"),
971 from_enc, to_enc, strerror (errno));
973 if (read_cd != (GIConv) -1)
974 g_iconv_close (read_cd);
975 if (write_cd != (GIConv) -1)
976 g_iconv_close (write_cd);
978 return G_IO_STATUS_ERROR;
981 channel->do_encode = TRUE;
984 /* The encoding is ok, so set the fields in channel */
986 if (channel->read_cd != (GIConv) -1)
987 g_iconv_close (channel->read_cd);
988 if (channel->write_cd != (GIConv) -1)
989 g_iconv_close (channel->write_cd);
991 channel->read_cd = read_cd;
992 channel->write_cd = write_cd;
994 g_free (channel->encoding);
995 channel->encoding = g_strdup (encoding);
997 return G_IO_STATUS_NORMAL;
1001 * g_io_channel_get_encoding:
1002 * @channel: a #GIOChannel
1004 * Get the encoding for the input/output of the channel. The internal
1005 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
1006 * disables encoding and turns off internal buffering. Both
1007 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
1008 * are safe to use with binary data.
1010 * Return value: A string containing the encoding, this string is
1011 * owned by GLib and must not be freed.
1013 G_CONST_RETURN gchar*
1014 g_io_channel_get_encoding (GIOChannel *channel)
1016 g_return_val_if_fail (channel != NULL, NULL);
1018 return channel->encoding;
1022 g_io_channel_fill_buffer (GIOChannel *channel,
1025 gsize read_size, cur_len, oldlen;
1028 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1030 status = g_io_channel_flush (channel, err);
1031 if (status != G_IO_STATUS_NORMAL)
1034 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1036 g_warning ("Partial character at end of write buffer not flushed.\n");
1037 channel->partial_write_buf[0] = '\0';
1040 if (!channel->read_buf)
1041 channel->read_buf = g_string_sized_new (channel->buf_size);
1043 cur_len = channel->read_buf->len;
1045 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1047 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1048 channel->buf_size, &read_size, err);
1050 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1052 g_string_truncate (channel->read_buf, read_size + cur_len);
1054 if ((status != G_IO_STATUS_NORMAL)
1055 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1058 g_assert (channel->read_buf->len > 0);
1060 if (channel->encoded_read_buf)
1061 oldlen = channel->encoded_read_buf->len;
1065 if (channel->do_encode)
1067 size_t errnum, inbytes_left, outbytes_left;
1068 gchar *inbuf, *outbuf;
1070 if (!channel->encoded_read_buf)
1071 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1075 inbytes_left = channel->read_buf->len;
1076 outbytes_left = MIN (channel->buf_size / 4,
1077 channel->encoded_read_buf->allocated_len
1078 - channel->encoded_read_buf->len);
1080 g_string_set_size (channel->encoded_read_buf,
1081 channel->encoded_read_buf->len + outbytes_left);
1083 inbuf = channel->read_buf->str;
1084 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1087 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1088 &outbuf, &outbytes_left);
1090 g_string_erase (channel->read_buf, 0,
1091 channel->read_buf->len - inbytes_left);
1092 g_string_truncate (channel->encoded_read_buf,
1093 channel->encoded_read_buf->len - outbytes_left);
1095 if (errnum == (size_t) -1)
1100 if ((oldlen == channel->encoded_read_buf->len)
1101 && (status == G_IO_STATUS_EOF))
1102 status = G_IO_STATUS_EOF;
1104 status = G_IO_STATUS_NORMAL;
1109 if (oldlen < channel->encoded_read_buf->len)
1110 status = G_IO_STATUS_NORMAL;
1113 g_set_error (err, G_CONVERT_ERROR,
1114 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1115 _("Invalid byte sequence in conversion input"));
1116 return G_IO_STATUS_ERROR;
1120 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1121 _("Error during conversion: %s"), strerror (errno));
1122 return G_IO_STATUS_ERROR;
1125 g_assert ((status != G_IO_STATUS_NORMAL)
1126 || (channel->encoded_read_buf->len > 0));
1128 else if (channel->encoding) /* UTF-8 */
1130 gchar *nextchar, *lastchar;
1132 nextchar = channel->read_buf->str;
1133 lastchar = channel->read_buf->str + channel->read_buf->len;
1135 while (nextchar < lastchar)
1139 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1144 /* stop, leave partial character in buffer */
1145 lastchar = nextchar;
1148 if (oldlen > channel->encoded_read_buf->len)
1149 status = G_IO_STATUS_NORMAL;
1152 g_set_error (err, G_CONVERT_ERROR,
1153 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1154 _("Invalid byte sequence in conversion input"));
1155 status = G_IO_STATUS_ERROR;
1157 lastchar = nextchar;
1160 nextchar = g_utf8_next_char (nextchar);
1165 if (lastchar > channel->read_buf->str)
1167 gint copy_len = lastchar - channel->read_buf->str;
1169 if (!channel->encoded_read_buf)
1170 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1172 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1174 g_string_erase (channel->read_buf, 0, copy_len);
1182 * g_io_channel_read_line:
1183 * @channel: a #GIOChannel
1184 * @str_return: The line read from the #GIOChannel, not including the
1185 * line terminator. This data should be freed with g_free()
1186 * when no longer needed. This
1187 * is a null terminated string. If a @length of zero is
1188 * returned, this will be %NULL instead.
1189 * @length: location to store length of the read data, or %NULL
1190 * @terminator_pos: location to store position of line terminator, or %NULL
1191 * @error: A location to return an error of type #GConvertError
1192 * or #GIOChannelError
1194 * Read a line, not including the terminating character(s),
1195 * from a #GIOChannel into a newly allocated string.
1196 * @length will contain allocated memory if the return
1197 * is %G_IO_STATUS_NORMAL.
1199 * Return value: a newly allocated string. Free this string
1200 * with g_free() when you are done with it.
1203 g_io_channel_read_line (GIOChannel *channel,
1206 gsize *terminator_pos,
1211 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1212 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1213 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1215 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1217 status = g_io_channel_read_line_backend (channel, length, terminator_pos, error);
1219 if (status == G_IO_STATUS_NORMAL)
1223 if (channel->do_encode)
1224 use_buf = channel->encoded_read_buf;
1226 use_buf = channel->read_buf;
1230 *str_return = g_strndup (use_buf->str, *length);
1231 g_string_erase (use_buf, 0, *length);
1240 * g_io_channel_read_line_string:
1241 * @channel: a #GIOChannel
1242 * @buffer: a #GString into which the line will be written.
1243 * If @buffer already contains data, the new data will
1244 * be appended to it.
1245 * @terminator_pos: location to store position of line terminator, or %NULL
1246 * @error: a location to store an error of type #GConvertError
1247 * or #GIOChannelError
1249 * Read a line from a #GIOChannel, using a #GString as a buffer.
1254 g_io_channel_read_line_string (GIOChannel *channel,
1256 gsize *terminator_pos,
1262 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1263 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1264 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1266 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1268 if (buffer->len > 0)
1269 g_string_truncate (buffer, 0); /* clear out the buffer */
1271 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1273 if (status == G_IO_STATUS_NORMAL)
1277 if (channel->do_encode)
1278 use_buf = channel->encoded_read_buf;
1280 use_buf = channel->read_buf;
1284 g_string_append_len (buffer, use_buf->str, length);
1285 g_string_erase (use_buf, 0, length);
1293 g_io_channel_read_line_backend (GIOChannel *channel,
1295 gsize *terminator_pos,
1299 gsize checked_to, line_term_len, line_length, got_term_len;
1301 gboolean first_time = TRUE;
1303 if (!channel->use_buffer)
1305 /* Can't do a raw read in read_line */
1306 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1307 _("Can't do a raw read in g_io_channel_read_line_string"));
1308 return G_IO_STATUS_ERROR;
1311 if (channel->encoding)
1313 if (!channel->encoded_read_buf)
1314 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1315 use_buf = channel->encoded_read_buf;
1319 if (!channel->read_buf)
1320 channel->read_buf = g_string_sized_new (channel->buf_size);
1321 use_buf = channel->read_buf;
1324 status = G_IO_STATUS_NORMAL;
1326 if (channel->line_term)
1327 line_term_len = strlen (channel->line_term);
1330 /* This value used for setting checked_to, it's the longest of the four
1331 * we autodetect for.
1338 gchar *nextchar, *lastchar;
1340 if (!first_time || (use_buf->len == 0))
1343 status = g_io_channel_fill_buffer (channel, error);
1346 case G_IO_STATUS_NORMAL:
1347 if (use_buf->len == 0)
1348 /* Can happen when using conversion and only read
1349 * part of a character
1356 case G_IO_STATUS_EOF:
1357 if (use_buf->len == 0)
1361 if (channel->do_encode && channel->read_buf->len != 0)
1362 /* using GIConv conversion, unconverted chars */
1364 g_set_error (error, G_CONVERT_ERROR,
1365 G_CONVERT_ERROR_PARTIAL_INPUT,
1366 "Leftover unconverted data in read buffer");
1367 return G_IO_STATUS_ERROR;
1370 return G_IO_STATUS_EOF;
1379 g_assert (use_buf->len > 0);
1383 lastchar = use_buf->str + strlen (use_buf->str);
1385 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1386 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1388 if (channel->line_term)
1390 if (strncmp (channel->line_term, nextchar, line_term_len) == 0)
1392 line_length = nextchar - use_buf->str;
1393 got_term_len = line_term_len;
1397 else /* auto detect */
1401 case '\n': /* unix */
1402 line_length = nextchar - use_buf->str;
1405 case '\r': /* Warning: do not use with sockets */
1406 line_length = nextchar - use_buf->str;
1407 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1408 && (lastchar == use_buf->str + use_buf->len))
1409 goto read_again; /* Try to read more data */
1410 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1415 case '\xe2': /* Unicode paragraph separator */
1416 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1418 line_length = nextchar - use_buf->str;
1423 default: /* no match */
1429 g_assert (nextchar == lastchar); /* Valid UTF-8, didn't overshoot */
1431 /* Also terminate on '\0' */
1433 line_length = lastchar - use_buf->str;
1434 if (line_length < use_buf->len)
1442 if (status == G_IO_STATUS_EOF)
1444 if (channel->encoding && channel->read_buf->len > 0)
1446 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1447 "Channel terminates in a partial character");
1448 return G_IO_STATUS_ERROR;
1450 line_length = use_buf->len;
1455 if (use_buf->len > line_term_len - 1)
1456 checked_to = use_buf->len - (line_term_len - 1);
1464 *terminator_pos = line_length;
1466 *length = line_length + got_term_len;
1468 return G_IO_STATUS_NORMAL;
1472 * g_io_channel_read_to_end:
1473 * @channel: a #GIOChannel
1474 * @str_return: Location to store a pointer to a string holding
1475 * the remaining data in the #GIOChannel. This data should
1476 * be freed with g_free() when no longer needed. This
1477 * data is terminated by an extra null, but there may be other
1478 * nulls in the intervening data.
1479 * @length: Location to store length of the data
1480 * @error: A location to return an error of type #GConvertError
1481 * or #GIOChannelError
1483 * Read all the remaining data from the file. Parameters as
1484 * for g_io_channel_read_line.
1486 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1490 g_io_channel_read_to_end (GIOChannel *channel,
1498 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1499 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1500 g_return_val_if_fail (length != NULL, G_IO_STATUS_ERROR);
1501 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1503 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1508 if (!channel->use_buffer)
1510 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1511 _("Can't do a raw read in g_io_channel_read_to_end"));
1512 return G_IO_STATUS_ERROR;
1516 status = g_io_channel_fill_buffer (channel, error);
1517 while (status == G_IO_STATUS_NORMAL);
1519 if (status != G_IO_STATUS_EOF)
1522 if (channel->encoding)
1524 if (channel->read_buf->len > 0)
1526 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1527 "Channel terminates in a partial character");
1528 return G_IO_STATUS_ERROR;
1531 if (!channel->encoded_read_buf)
1536 *str_return = g_strdup ("");
1539 use_buf = &channel->encoded_read_buf;
1542 use_buf = &channel->read_buf;
1544 g_assert (*use_buf); /* Created by fill_buffer if it didn't previously exist */
1547 *length = (*use_buf)->len;
1550 *str_return = g_string_free (*use_buf, FALSE);
1552 g_string_free (*use_buf, TRUE);
1556 return G_IO_STATUS_NORMAL;
1560 * g_io_channel_read_chars:
1561 * @channel: a #GIOChannel
1562 * @buf: a buffer to read data into
1563 * @count: the size of the buffer. Note that the buffer may
1564 * not be complelely filled even if there is data
1565 * in the buffer if the remaining data is not a
1566 * complete character.
1567 * @bytes_read: The number of bytes read.
1568 * @error: A location to return an error of type #GConvertError
1569 * or #GIOChannelError.
1571 * Replacement for g_io_channel_read() with the new API.
1576 g_io_channel_read_chars (GIOChannel *channel,
1585 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1586 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1588 g_return_val_if_fail (bytes_read != NULL, G_IO_STATUS_ERROR);
1589 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1594 return G_IO_STATUS_NORMAL;
1596 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1598 if (!channel->use_buffer)
1600 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1602 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1605 if (channel->encoding)
1607 if (!channel->encoded_read_buf)
1608 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1609 use_buf = channel->encoded_read_buf;
1613 if (!channel->read_buf)
1614 channel->read_buf = g_string_sized_new (channel->buf_size);
1615 use_buf = channel->read_buf;
1618 status = G_IO_STATUS_NORMAL;
1620 while (use_buf->len < MAX (count, 6) && status == G_IO_STATUS_NORMAL)
1621 /* Be sure to read in at least one full UTF-8 character
1624 status = g_io_channel_fill_buffer (channel, error);
1628 case G_IO_STATUS_NORMAL:
1629 g_assert (use_buf->len > 0);
1631 case G_IO_STATUS_EOF:
1632 if (use_buf->len == 0)
1636 if (channel->encoding && channel->read_buf->len != 0)
1638 g_set_error (error, G_CONVERT_ERROR,
1639 G_CONVERT_ERROR_PARTIAL_INPUT,
1640 "Leftover unconverted data in read buffer");
1641 return G_IO_STATUS_ERROR;
1644 return G_IO_STATUS_EOF;
1647 case G_IO_STATUS_AGAIN:
1648 if (use_buf->len > 0)
1649 break; /* return what we have */
1653 return G_IO_STATUS_AGAIN;
1660 *bytes_read = MIN (count, use_buf->len);
1662 if (channel->encoding && *bytes_read > 0)
1663 /* Don't validate for NULL encoding, binary safe */
1665 gchar *nextchar, *prevchar = NULL;
1667 nextchar = use_buf->str;
1669 while (nextchar < use_buf->str + *bytes_read)
1671 prevchar = nextchar;
1672 nextchar = g_utf8_next_char (nextchar);
1675 if (nextchar > use_buf->str + *bytes_read)
1676 *bytes_read = prevchar - use_buf->str;
1678 g_assert (*bytes_read > 0 || count < 6);
1681 memcpy (buf, use_buf->str, *bytes_read);
1682 g_string_erase (use_buf, 0, *bytes_read);
1684 return G_IO_STATUS_NORMAL;
1688 * g_io_channel_write_chars:
1689 * @channel: a #GIOChannel
1690 * @buf: a buffer to write data from
1691 * @count: the size of the buffer. If -1, the buffer
1692 * is taken to be a nul terminated string.
1693 * @bytes_written: The number of bytes written. This can be nonzero
1694 * even if the return value is not %G_IO_STATUS_NORMAL.
1695 * @error: A location to return an error of type #GConvertError
1696 * or #GIOChannelError
1698 * Replacement for g_io_channel_write() with the new API.
1703 g_io_channel_write_chars (GIOChannel *channel,
1706 gsize *bytes_written,
1711 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1712 g_return_val_if_fail (bytes_written != NULL, G_IO_STATUS_ERROR);
1713 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1715 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1717 if ((count < 0) && buf)
1718 count = strlen (buf);
1721 return G_IO_STATUS_NORMAL;
1723 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1724 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1726 /* Raw write case */
1728 if (!channel->use_buffer)
1730 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1731 g_assert (channel->partial_write_buf[0] == '\0');
1732 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1737 if (channel->is_seekable && ((channel->read_buf && channel->read_buf->len > 0)
1738 || (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)))
1740 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1742 g_warning("Mixed reading and writing not allowed on encoded files");
1743 return G_IO_STATUS_ERROR;
1745 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1746 if (status != G_IO_STATUS_NORMAL)
1750 if (!channel->write_buf)
1751 channel->write_buf = g_string_sized_new (channel->buf_size);
1753 if (!channel->do_encode)
1757 if (channel->encoding)
1759 const gchar *badchar;
1761 if (channel->partial_write_buf[0] != '\0')
1763 guint partial_chars = strlen (channel->partial_write_buf);
1764 guint chars_in_buf = MIN (6, partial_chars + count);
1767 memcpy (channel->partial_write_buf + partial_chars, buf,
1768 chars_in_buf - partial_chars);
1770 g_utf8_validate (channel->partial_write_buf, chars_in_buf, &badchar);
1772 if (badchar < channel->partial_write_buf + partial_chars)
1775 gsize left_len = chars_in_buf;
1777 g_assert (badchar == channel->partial_write_buf);
1779 try_char = g_utf8_get_char_validated (badchar, left_len);
1784 g_assert (chars_in_buf < 6);
1785 channel->partial_write_buf[chars_in_buf] = '\0';
1786 *bytes_written = count;
1787 return G_IO_STATUS_NORMAL;
1789 g_set_error (error, G_CONVERT_ERROR,
1790 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1791 "Illegal UTF-8 sequence");
1793 return G_IO_STATUS_ERROR;
1795 g_assert_not_reached ();
1796 return G_IO_STATUS_ERROR; /* keep the compiler happy */
1800 skip = badchar - (channel->partial_write_buf + partial_chars);
1803 copy_bytes = count - skip;
1805 g_string_append_len (channel->write_buf, channel->partial_write_buf,
1806 badchar - channel->partial_write_buf);
1807 channel->partial_write_buf[0] = '\0';
1812 if (!g_utf8_validate (buf, copy_bytes, &badchar))
1815 gsize left_len = buf + copy_bytes - badchar;
1817 try_char = g_utf8_get_char_validated (badchar, left_len);
1822 g_assert (left_len < 6);
1825 g_set_error (error, G_CONVERT_ERROR,
1826 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1827 "Illegal UTF-8 sequence");
1828 *bytes_written = count - copy_bytes;
1829 return G_IO_STATUS_ERROR;
1831 g_assert_not_reached ();
1832 return G_IO_STATUS_ERROR; /* Don't confunse the compiler */
1835 g_string_append (channel->write_buf, channel->partial_write_buf);
1836 memcpy (channel->partial_write_buf, badchar, left_len);
1837 channel->partial_write_buf[left_len] = '\0';
1838 copy_bytes = count - left_len;
1842 g_string_append (channel->write_buf, channel->partial_write_buf);
1843 channel->partial_write_buf[0] = '\0';
1850 g_assert (channel->partial_write_buf[0] == '\0');
1853 while (copy_bytes > channel->write_buf->allocated_len - channel->write_buf->len)
1857 if (channel->write_buf->len > 0)
1859 status = channel->funcs->io_write (channel, channel->write_buf->str,
1860 channel->write_buf->len, &wrote_bytes,
1862 g_string_erase (channel->write_buf, 0, wrote_bytes);
1866 status = channel->funcs->io_write (channel, buf, copy_bytes, &wrote_bytes,
1868 copy_bytes -= wrote_bytes;
1872 if (status != G_IO_STATUS_NORMAL)
1874 *bytes_written = count - copy_bytes;
1875 if (channel->partial_write_buf[0] != '\0')
1877 *bytes_written -= strlen (channel->partial_write_buf);
1878 channel->partial_write_buf[0] = '\0';
1884 g_string_append_len (channel->write_buf, buf, copy_bytes);
1885 *bytes_written = count;
1889 gsize bytes_to_go = count, buf_space, err;
1891 gsize oldlen = channel->write_buf->len;
1893 if (channel->partial_write_buf[0] != '\0')
1895 gsize partial_chars = strlen (channel->partial_write_buf);
1896 gsize partial_buf_size = MIN (6, partial_chars + count);
1897 guint chars_in_buf = partial_buf_size;
1900 g_assert (partial_chars < 6);
1902 memcpy (channel->partial_write_buf + partial_chars, buf,
1903 chars_in_buf - partial_chars);
1904 inbuf = channel->partial_write_buf;
1906 buf_space = MIN (channel->buf_size / 4, channel->write_buf->allocated_len
1907 - channel->write_buf->len);
1908 g_string_set_size (channel->write_buf, channel->write_buf->len
1910 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1912 err = g_iconv (channel->write_cd, &inbuf, &partial_buf_size,
1913 &outbuf, &buf_space);
1915 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1917 if (err == (size_t) -1)
1922 if (errno == EINVAL)
1924 if (chars_in_buf == partial_buf_size)
1926 /* Didn't convert anything */
1928 g_assert (chars_in_buf < 6); /* More of the same character */
1929 g_assert (chars_in_buf == count + partial_chars);
1931 channel->partial_write_buf[chars_in_buf] = '\0';
1932 *bytes_written = count;
1933 return G_IO_STATUS_NORMAL;
1936 g_assert (chars_in_buf - partial_buf_size > partial_chars);
1937 /* Converted the character of which a part was sitting in
1938 * the partial character buffer before the write.
1947 g_assert_not_reached ();
1949 g_set_error (error, G_CONVERT_ERROR,
1950 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1951 _("Invalid byte sequence in conversion input"));
1953 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1954 _("Error during conversion: %s"), strerror (errno));
1957 g_string_truncate (channel->write_buf, oldlen);
1959 channel->partial_write_buf[partial_chars] = '\0';
1961 return G_IO_STATUS_ERROR;
1965 *bytes_written = (chars_in_buf - partial_buf_size) - partial_chars;
1966 buf += *bytes_written;
1967 bytes_to_go -= *bytes_written;
1968 channel->partial_write_buf[0] = '\0';
1973 buf_space = MIN (channel->write_buf->allocated_len
1974 - channel->write_buf->len, channel->buf_size / 4);
1976 g_string_set_size (channel->write_buf, channel->write_buf->len + buf_space);
1977 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1979 err = g_iconv (channel->write_cd, (gchar**) &buf, &bytes_to_go,
1980 &outbuf, &buf_space);
1982 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1983 *bytes_written = count - bytes_to_go;
1985 if (err == (size_t) -1)
1991 gint bytes_left = count - *bytes_written;
1993 g_assert (bytes_left < 6);
1995 memcpy (channel->partial_write_buf, buf, bytes_left);
1996 channel->partial_write_buf[bytes_left] = '\0';
1998 *bytes_written = count;
2000 return G_IO_STATUS_NORMAL;
2005 status = channel->funcs->io_write (channel,
2006 channel->write_buf->str, channel->write_buf->len,
2007 &wrote_bytes, error);
2008 g_string_erase (channel->write_buf, 0, wrote_bytes);
2010 if (status != G_IO_STATUS_NORMAL)
2015 g_set_error (error, G_CONVERT_ERROR,
2016 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2017 _("Invalid byte sequence in conversion input"));
2018 return G_IO_STATUS_ERROR;
2020 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2021 _("Error during conversion: %s"), strerror (errno));
2022 return G_IO_STATUS_ERROR;
2027 while (bytes_to_go > 0);
2030 if (channel->write_buf->len > channel->buf_size)
2034 status = channel->funcs->io_write (channel, channel->write_buf->str,
2035 channel->write_buf->len, &wrote_bytes, error);
2037 if (wrote_bytes > 0)
2038 g_string_erase (channel->write_buf, 0, wrote_bytes);
2043 return G_IO_STATUS_NORMAL;
2047 * g_io_channel_error_quark:
2049 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2052 g_io_channel_error_quark (void)
2054 static GQuark q = 0;
2056 q = g_quark_from_static_string ("g-io-channel-error-quark");