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/.
35 #include "giochannel.h"
44 #undef G_DISABLE_DEPRECATED
50 #define G_IO_NICE_BUF_SIZE 1024
52 static GIOError g_io_error_get_from_g_error (GIOStatus status,
54 static void g_io_channel_purge (GIOChannel *channel);
55 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
57 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
59 gsize *terminator_pos,
63 g_io_channel_init (GIOChannel *channel)
65 channel->ref_count = 1;
66 channel->encoding = g_strdup ("UTF-8");
67 channel->line_term = NULL;
68 channel->buf_size = G_IO_NICE_BUF_SIZE;
69 channel->read_cd = (GIConv) -1;
70 channel->write_cd = (GIConv) -1;
71 channel->read_buf = NULL; /* Lazy allocate buffers */
72 channel->encoded_read_buf = NULL;
73 channel->write_buf = NULL;
74 channel->partial_write_buf[0] = '\0';
75 channel->use_buffer = TRUE;
76 channel->do_encode = FALSE;
77 channel->close_on_unref = FALSE;
81 g_io_channel_ref (GIOChannel *channel)
83 g_return_if_fail (channel != NULL);
89 g_io_channel_unref (GIOChannel *channel)
91 g_return_if_fail (channel != NULL);
94 if (channel->ref_count == 0)
96 if (channel->close_on_unref)
97 g_io_channel_close (channel);
99 g_io_channel_purge (channel);
100 g_free (channel->encoding);
101 if (channel->read_cd != (GIConv) -1)
102 g_iconv_close (channel->read_cd);
103 if (channel->write_cd != (GIConv) -1)
104 g_iconv_close (channel->write_cd);
105 g_free (channel->line_term);
106 if (channel->read_buf)
107 g_string_free (channel->read_buf, TRUE);
108 if (channel->write_buf)
109 g_string_free (channel->write_buf, TRUE);
110 if (channel->encoded_read_buf)
111 g_string_free (channel->encoded_read_buf, TRUE);
112 channel->funcs->io_free (channel);
117 g_io_error_get_from_g_error (GIOStatus status,
122 case G_IO_STATUS_NORMAL:
123 case G_IO_STATUS_EOF:
124 return G_IO_ERROR_NONE;
125 case G_IO_STATUS_AGAIN:
126 return G_IO_ERROR_AGAIN;
127 case G_IO_STATUS_ERROR:
128 if (err->domain != G_IO_CHANNEL_ERROR)
129 return G_IO_ERROR_UNKNOWN;
132 case G_IO_CHANNEL_ERROR_INVAL:
133 return G_IO_ERROR_INVAL;
135 return G_IO_ERROR_UNKNOWN;
138 g_assert_not_reached ();
139 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
145 * @channel: a #GIOChannel.
146 * @buf: a buffer to read the data into (which should be at least count bytes long).
147 * @count: the number of bytes to read from the #GIOChannel.
148 * @bytes_read: returns the number of bytes actually read.
150 * Reads data from a #GIOChannel. This function is depricated. New code should
151 * use g_io_channel_read_chars() instead.
153 * Return value: %G_IO_ERROR_NONE if the operation was successful.
156 g_io_channel_read (GIOChannel *channel,
165 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
166 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
168 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
170 error = g_io_error_get_from_g_error (status, err);
179 * g_io_channel_write:
180 * @channel: a #GIOChannel.
181 * @buf: the buffer containing the data to write.
182 * @count: the number of bytes to write.
183 * @bytes_written: the number of bytes actually written.
185 * Writes data to a #GIOChannel. This function is depricated. New code should
186 * use g_io_channel_write_chars() instead.
188 * Return value: %G_IO_ERROR_NONE if the operation was successful.
191 g_io_channel_write (GIOChannel *channel,
194 gsize *bytes_written)
200 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
201 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
203 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
205 error = g_io_error_get_from_g_error (status, err);
215 * @channel: a #GIOChannel.
216 * @offset: an offset, in bytes, which is added to the position specified by @type
217 * @type: the position in the file, which can be %G_SEEK_CUR (the current
218 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
221 * Sets the current position in the #GIOChannel, similar to the standard library
222 * function fseek(). This function is depricated. New code should
223 * use g_io_channel_seek_position() instead.
225 * Return value: %G_IO_ERROR_NONE if the operation was successful.
228 g_io_channel_seek (GIOChannel *channel,
236 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
237 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
246 g_warning ("g_io_channel_seek: unknown seek type");
247 return G_IO_ERROR_UNKNOWN;
250 status = channel->funcs->io_seek (channel, offset, type, &err);
252 error = g_io_error_get_from_g_error (status, err);
260 /* The function g_io_channel_new_file() is prototyped in both
261 * giounix.c and giowin32.c, so we stick its documentation here.
265 * g_io_channel_new_file:
266 * @filename: A string containing the name of a file.
267 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
268 * the same meaning as in fopen().
269 * @error: A location to return an error of type %G_IO_FILE_ERROR.
271 * Open a file @filename as a #GIOChannel using mode @mode. This
272 * channel will be closed when the last reference to it is dropped,
273 * so there is no need to call g_io_channel_close() (though doing
274 * so will not cause problems, as long as no attempt is made to
275 * access the channel after it is closed).
277 * Return value: A #GIOChannel on success, %NULL on failure.
281 * g_io_channel_close:
282 * @channel: A #GIOChannel
284 * Close an IO channel. Any pending data to be written will be
285 * flushed, ignoring errors. The channel will not be freed until the
286 * last reference is dropped using g_io_channel_unref(). This
287 * function is deprecated: you should use g_io_channel_shutdown()
291 g_io_channel_close (GIOChannel *channel)
295 g_return_if_fail (channel != NULL);
297 g_io_channel_purge (channel);
299 channel->funcs->io_close (channel, &err);
302 { /* No way to return the error */
303 g_warning ("Error closing channel: %s", err->message);
307 channel->close_on_unref = FALSE; /* Because we already did */
308 channel->is_readable = FALSE;
309 channel->is_writeable = FALSE;
310 channel->is_seekable = FALSE;
314 * g_io_channel_shutdown:
315 * @channel: a #GIOChannel
316 * @flush: if %TRUE, flush pending
317 * @err: location to store a #GIOChannelError
319 * Close an IO channel. Any pending data to be written will be
320 * flushed. The channel will not be freed until the
321 * last reference is dropped using g_io_channel_unref().
326 g_io_channel_shutdown (GIOChannel *channel,
330 GIOStatus status, result;
331 GError *tmperr = NULL;
333 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
334 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
336 if (flush && channel->write_buf && channel->write_buf->len > 0)
340 /* Set the channel to blocking, to avoid a busy loop
342 flags = g_io_channel_get_flags (channel);
343 /* Ignore any errors here, they're irrelevant */
344 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
346 result = g_io_channel_flush (channel, &tmperr);
348 if (channel->partial_write_buf[0] != '\0')
350 g_warning ("Partial character at end of write buffer not flushed.\n");
351 channel->partial_write_buf[0] = '\0';
355 result = G_IO_STATUS_NORMAL;
357 status = channel->funcs->io_close (channel, err);
359 channel->close_on_unref = FALSE; /* Because we already did */
360 channel->is_readable = FALSE;
361 channel->is_writeable = FALSE;
362 channel->is_seekable = FALSE;
364 if (status != G_IO_STATUS_NORMAL)
366 g_clear_error (&tmperr);
369 else if (result != G_IO_STATUS_NORMAL)
371 g_propagate_error (err, tmperr);
375 return G_IO_STATUS_NORMAL;
378 /* This function is used for the final flush on close or unref */
380 g_io_channel_purge (GIOChannel *channel)
385 g_return_if_fail (channel != NULL);
387 if (channel->write_buf && channel->write_buf->len > 0)
391 /* Set the channel to blocking, to avoid a busy loop
393 flags = g_io_channel_get_flags (channel);
394 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
396 status = g_io_channel_flush (channel, &err);
399 { /* No way to return the error */
400 g_warning ("Error flushing string: %s", err->message);
405 /* Flush these in case anyone tries to close without unrefing */
407 if (channel->read_buf)
408 g_string_truncate (channel->read_buf, 0);
409 if (channel->write_buf)
410 g_string_truncate (channel->write_buf, 0);
411 if (channel->do_encode)
413 if (channel->encoded_read_buf)
414 g_string_truncate (channel->encoded_read_buf, 0);
416 if (channel->partial_write_buf[0] != '\0')
418 g_warning ("Partial character at end of write buffer not flushed.\n");
419 channel->partial_write_buf[0] = '\0';
425 g_io_create_watch (GIOChannel *channel,
426 GIOCondition condition)
428 g_return_val_if_fail (channel != NULL, NULL);
430 return channel->funcs->io_create_watch (channel, condition);
434 g_io_add_watch_full (GIOChannel *channel,
436 GIOCondition condition,
439 GDestroyNotify notify)
444 g_return_val_if_fail (channel != NULL, 0);
446 source = g_io_create_watch (channel, condition);
448 if (priority != G_PRIORITY_DEFAULT)
449 g_source_set_priority (source, priority);
450 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
452 id = g_source_attach (source, NULL);
453 g_source_unref (source);
459 g_io_add_watch (GIOChannel *channel,
460 GIOCondition condition,
464 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
468 * g_io_channel_get_buffer_condition:
469 * @channel: A #GIOChannel
471 * This function returns a #GIOCondition depending on the status of the
472 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
473 * %G_IO_OUT will be set.
475 * Return value: A #GIOCondition
478 g_io_channel_get_buffer_condition (GIOChannel *channel)
480 GIOCondition condition = 0;
482 if (channel->encoding)
484 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
485 condition |= G_IO_IN; /* Only return if we have full characters */
489 if (channel->read_buf && (channel->read_buf->len > 0))
490 condition |= G_IO_IN;
493 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
494 condition |= G_IO_OUT;
500 * g_io_channel_error_from_errno:
501 * @en: An errno error number, e.g. EINVAL
503 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
506 g_io_channel_error_from_errno (gint en)
509 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
512 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
519 g_warning("Invalid file descriptor.\n");
520 return G_IO_CHANNEL_ERROR_FAILED;
525 g_warning("File descriptor outside valid address space.\n");
526 return G_IO_CHANNEL_ERROR_FAILED;
531 return G_IO_CHANNEL_ERROR_FBIG;
536 return G_IO_CHANNEL_ERROR_INVAL;
541 return G_IO_CHANNEL_ERROR_IO;
546 return G_IO_CHANNEL_ERROR_ISDIR;
551 return G_IO_CHANNEL_ERROR_NOSPC;
556 return G_IO_CHANNEL_ERROR_NXIO;
561 return G_IO_CHANNEL_ERROR_OVERFLOW;
566 return G_IO_CHANNEL_ERROR_PIPE;
570 return G_IO_CHANNEL_ERROR_FAILED;
575 * g_io_channel_set_buffer_size:
576 * @channel: a #GIOChannel
577 * @size: the size of the buffer. 0 == pick a good size
579 * Set the buffer size.
582 g_io_channel_set_buffer_size (GIOChannel *channel,
585 g_return_if_fail (channel != NULL);
588 size = G_IO_NICE_BUF_SIZE;
590 if (size < 10) /* Needs to be larger than the widest char in any encoding */
593 channel->buf_size = size;
597 * g_io_channel_get_buffer_size:
598 * @channel: a #GIOChannel
600 * Get the buffer size.
602 * Return value: the size of the buffer.
605 g_io_channel_get_buffer_size (GIOChannel *channel)
607 g_return_val_if_fail (channel != NULL, 0);
609 return channel->buf_size;
613 * g_io_channel_set_line_term:
614 * @channel: a #GIOChannel
615 * @line_term: The line termination string. Use %NULL for auto detect.
617 * This sets the string that #GIOChannel uses to determine
618 * where in the file a line break occurs.
621 g_io_channel_set_line_term (GIOChannel *channel,
622 const gchar *line_term)
624 g_return_if_fail (channel != NULL);
625 g_return_if_fail (!line_term || line_term[0]); /* Disallow "" */
626 g_return_if_fail (!line_term || g_utf8_validate (line_term, -1, NULL));
627 /* Require valid UTF-8 */
629 g_free (channel->line_term);
630 channel->line_term = g_strdup (line_term);
634 * g_io_channel_get_line_term:
635 * @channel: a #GIOChannel
637 * This returns the string that #GIOChannel uses to determine
638 * where in the file a line break occurs. A value of %NULL
639 * indicates auto detection.
641 * Return value: The line termination string. This value
642 * is owned by GLib and must not be freed.
644 G_CONST_RETURN gchar*
645 g_io_channel_get_line_term (GIOChannel *channel)
647 g_return_val_if_fail (channel != NULL, 0);
649 return channel->line_term;
653 * g_io_channel_set_flags:
654 * @channel: a #GIOChannel
655 * @flags: the flags to set on the channel
656 * @error: A location to return an error of type #GIOChannelError
661 g_io_channel_set_flags (GIOChannel *channel,
665 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
666 g_return_val_if_fail ((error == NULL) || (*error == NULL),
669 return (* channel->funcs->io_set_flags)(channel,
670 flags & G_IO_FLAG_SET_MASK,
675 * g_io_channel_get_flags:
676 * @channel: a #GIOChannel
678 * Gets the current flags for a #GIOChannel, including read-only
679 * flags such as %G_IO_FLAG_IS_READABLE.
681 * Return value: the flags which are set on the channel
684 g_io_channel_get_flags (GIOChannel *channel)
688 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
690 flags = (* channel->funcs->io_get_flags) (channel);
692 /* Cross implementation code */
694 if (channel->is_seekable)
695 flags |= G_IO_FLAG_IS_SEEKABLE;
696 if (channel->is_readable)
697 flags |= G_IO_FLAG_IS_READABLE;
698 if (channel->is_writeable)
699 flags |= G_IO_FLAG_IS_WRITEABLE;
705 * g_io_channel_seek_position:
706 * @channel: a #GIOChannel
707 * @offset: The offset in bytes from the position specified by @type
708 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed if
709 * the channel has the default encoding or the
710 * encoding %G_IO_CHANNEL_ENCODE_RAW for raw file access.
711 * @error: A location to return an error of type #GIOChannelError
713 * Replacement for g_io_channel_seek() with the new API.
718 g_io_channel_seek_position (GIOChannel* channel,
725 /* For files, only one of the read and write buffers can contain data.
726 * For sockets, both can contain data.
729 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
730 g_return_val_if_fail ((error == NULL) || (*error == NULL),
732 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
736 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
737 if (channel->use_buffer)
739 if (channel->do_encode && channel->encoded_read_buf
740 && channel->encoded_read_buf->len > 0)
742 g_warning ("Seek type G_SEEK_CUR not allowed for this"
743 " channel's encoding.\n");
744 return G_IO_STATUS_ERROR;
746 if (channel->read_buf)
747 offset -= channel->read_buf->len;
748 if (channel->encoded_read_buf)
750 g_assert (channel->encoded_read_buf->len == 0 && !channel->do_encode);
752 /* If there's anything here, it's because the encoding is UTF-8,
753 * so we can just subtract the buffer length, the same as for
754 * the unencoded data.
757 offset -= channel->encoded_read_buf->len;
765 g_warning ("g_io_channel_seek_position: unknown seek type");
766 return G_IO_STATUS_ERROR;
769 if (channel->use_buffer)
771 status = g_io_channel_flush (channel, error);
772 if (status != G_IO_STATUS_NORMAL)
776 status = channel->funcs->io_seek (channel, offset, type, error);
778 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
780 if (channel->read_buf)
781 g_string_truncate (channel->read_buf, 0);
783 /* Conversion state no longer matches position in file */
784 if (channel->read_cd != (GIConv) -1)
785 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
786 if (channel->write_cd != (GIConv) -1)
787 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
789 if (channel->encoded_read_buf)
791 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
792 g_string_truncate (channel->encoded_read_buf, 0);
795 if (channel->partial_write_buf[0] != '\0')
797 g_warning ("Partial character at end of write buffer not flushed.\n");
798 channel->partial_write_buf[0] = '\0';
806 * g_io_channel_flush:
807 * @channel: a #GIOChannel
808 * @error: location to store an error of type #GIOChannelError
810 * Flush the write buffer for the GIOChannel.
812 * Return value: the status of the operation: One of
813 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
814 * G_IO_CHANNEL_ERROR.
817 g_io_channel_flush (GIOChannel *channel,
821 gsize this_time = 1, bytes_written = 0;
823 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
824 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
826 if (channel->write_buf == NULL || channel->write_buf->len == 0)
827 return G_IO_STATUS_NORMAL;
831 g_assert (this_time > 0);
833 status = channel->funcs->io_write (channel,
834 channel->write_buf->str + bytes_written,
835 channel->write_buf->len - bytes_written,
837 bytes_written += this_time;
839 while ((bytes_written < channel->write_buf->len)
840 && (status == G_IO_STATUS_NORMAL));
842 g_string_erase (channel->write_buf, 0, bytes_written);
848 * g_io_channel_set_buffered:
849 * @channel: a #GIOChannel
850 * @buffered: whether to set the channel buffered or unbuffered
852 * The buffering state can only be set if the channel's encoding
853 * is %NULL. For any other encoding, the channel must be buffered.
855 * The default state of the channel is buffered.
858 g_io_channel_set_buffered (GIOChannel *channel,
861 g_return_if_fail (channel != NULL);
863 if (channel->encoding != NULL)
865 g_warning ("Need to have NULL encoding to set the buffering state of the "
870 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
871 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
873 channel->use_buffer = buffered;
877 * g_io_channel_get_buffered:
878 * @channel: a #GIOChannel
880 * Return Value: the buffering state of the channel
883 g_io_channel_get_buffered (GIOChannel *channel)
885 g_return_val_if_fail (channel != NULL, FALSE);
887 return channel->use_buffer;
891 * g_io_channel_set_encoding:
892 * @channel: a #GIOChannel
893 * @encoding: the encoding type
894 * @error: location to store an error of type #GConvertError.
896 * Set the encoding for the input/output of the channel. The internal
897 * encoding is always UTF-8. The default encoding for the
898 * external file is UTF-8.
900 * The encoding %NULL is safe to use with binary data.
901 * Encodings other than %NULL must use a buffered channel.
902 * Encodings other than %NULL and UTF-8 cannot
903 * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
904 * and cannot mix reading and writing if the channel is
905 * a file without first doing a seek of type %G_SEEK_SET or
908 * The encoding can only be set under three conditions:
910 * 1. The channel was just created, and has not been written to
913 * 2. The channel is a file, and the file pointer was just
914 * repositioned by a call to g_io_channel_seek_position().
915 * (This flushes all the internal buffers.)
917 * 3. The current encoding is %NULL or UTF-8.
919 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
922 g_io_channel_set_encoding (GIOChannel *channel,
923 const gchar *encoding,
926 GIConv read_cd, write_cd;
929 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
930 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
932 /* Make sure the encoded buffers are empty */
934 g_return_val_if_fail (!channel->encoded_read_buf ||
935 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
936 g_return_val_if_fail (channel->partial_write_buf[0] == '\0', G_IO_STATUS_ERROR);
938 if (!channel->use_buffer)
940 g_warning ("Need to set the channel buffered before setting the encoding.\n");
941 g_warning ("Assuming this is what you meant and acting accordingly.\n");
943 channel->use_buffer = TRUE;
946 did_encode = channel->do_encode;
948 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
950 channel->do_encode = FALSE;
951 read_cd = write_cd = (GIConv) -1;
956 const gchar *from_enc = NULL, *to_enc = NULL;
958 if (channel->is_readable)
960 read_cd = g_iconv_open ("UTF-8", encoding);
962 if (read_cd == (GIConv) -1)
970 read_cd = (GIConv) -1;
972 if (channel->is_writeable && err == 0)
974 write_cd = g_iconv_open (encoding, "UTF-8");
976 if (write_cd == (GIConv) -1)
984 write_cd = (GIConv) -1;
992 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
993 _("Conversion from character set `%s' to `%s' is not supported"),
996 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
997 _("Could not open converter from `%s' to `%s': %s"),
998 from_enc, to_enc, strerror (errno));
1000 if (read_cd != (GIConv) -1)
1001 g_iconv_close (read_cd);
1002 if (write_cd != (GIConv) -1)
1003 g_iconv_close (write_cd);
1005 return G_IO_STATUS_ERROR;
1008 channel->do_encode = TRUE;
1011 /* The encoding is ok, so set the fields in channel */
1013 if (channel->read_cd != (GIConv) -1)
1014 g_iconv_close (channel->read_cd);
1015 if (channel->write_cd != (GIConv) -1)
1016 g_iconv_close (channel->write_cd);
1018 channel->read_cd = read_cd;
1019 channel->write_cd = write_cd;
1021 g_free (channel->encoding);
1022 channel->encoding = g_strdup (encoding);
1024 return G_IO_STATUS_NORMAL;
1028 * g_io_channel_get_encoding:
1029 * @channel: a #GIOChannel
1031 * Get the encoding for the input/output of the channel. The internal
1032 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
1033 * disables encoding and turns off internal buffering. Both
1034 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
1035 * are safe to use with binary data.
1037 * Return value: A string containing the encoding, this string is
1038 * owned by GLib and must not be freed.
1040 G_CONST_RETURN gchar*
1041 g_io_channel_get_encoding (GIOChannel *channel)
1043 g_return_val_if_fail (channel != NULL, NULL);
1045 return channel->encoding;
1049 g_io_channel_fill_buffer (GIOChannel *channel,
1052 gsize read_size, cur_len, oldlen;
1055 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1057 status = g_io_channel_flush (channel, err);
1058 if (status != G_IO_STATUS_NORMAL)
1061 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1063 g_warning ("Partial character at end of write buffer not flushed.\n");
1064 channel->partial_write_buf[0] = '\0';
1067 if (!channel->read_buf)
1068 channel->read_buf = g_string_sized_new (channel->buf_size);
1070 cur_len = channel->read_buf->len;
1072 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1074 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1075 channel->buf_size, &read_size, err);
1077 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1079 g_string_truncate (channel->read_buf, read_size + cur_len);
1081 if ((status != G_IO_STATUS_NORMAL)
1082 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1085 g_assert (channel->read_buf->len > 0);
1087 if (channel->encoded_read_buf)
1088 oldlen = channel->encoded_read_buf->len;
1092 if (channel->do_encode)
1094 size_t errnum, inbytes_left, outbytes_left;
1095 gchar *inbuf, *outbuf;
1097 if (!channel->encoded_read_buf)
1098 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1102 inbytes_left = channel->read_buf->len;
1103 outbytes_left = MIN (channel->buf_size / 4,
1104 channel->encoded_read_buf->allocated_len
1105 - channel->encoded_read_buf->len);
1107 g_string_set_size (channel->encoded_read_buf,
1108 channel->encoded_read_buf->len + outbytes_left);
1110 inbuf = channel->read_buf->str;
1111 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1114 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1115 &outbuf, &outbytes_left);
1117 g_string_erase (channel->read_buf, 0,
1118 channel->read_buf->len - inbytes_left);
1119 g_string_truncate (channel->encoded_read_buf,
1120 channel->encoded_read_buf->len - outbytes_left);
1122 if (errnum == (size_t) -1)
1127 if ((oldlen == channel->encoded_read_buf->len)
1128 && (status == G_IO_STATUS_EOF))
1129 status = G_IO_STATUS_EOF;
1131 status = G_IO_STATUS_NORMAL;
1136 if (oldlen < channel->encoded_read_buf->len)
1137 status = G_IO_STATUS_NORMAL;
1140 g_set_error (err, G_CONVERT_ERROR,
1141 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1142 _("Invalid byte sequence in conversion input"));
1143 return G_IO_STATUS_ERROR;
1147 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1148 _("Error during conversion: %s"), strerror (errno));
1149 return G_IO_STATUS_ERROR;
1152 g_assert ((status != G_IO_STATUS_NORMAL)
1153 || (channel->encoded_read_buf->len > 0));
1155 else if (channel->encoding) /* UTF-8 */
1157 gchar *nextchar, *lastchar;
1159 nextchar = channel->read_buf->str;
1160 lastchar = channel->read_buf->str + channel->read_buf->len;
1162 while (nextchar < lastchar)
1166 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1171 /* stop, leave partial character in buffer */
1172 lastchar = nextchar;
1175 if (oldlen > channel->encoded_read_buf->len)
1176 status = G_IO_STATUS_NORMAL;
1179 g_set_error (err, G_CONVERT_ERROR,
1180 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1181 _("Invalid byte sequence in conversion input"));
1182 status = G_IO_STATUS_ERROR;
1184 lastchar = nextchar;
1187 nextchar = g_utf8_next_char (nextchar);
1192 if (lastchar > channel->read_buf->str)
1194 gint copy_len = lastchar - channel->read_buf->str;
1196 if (!channel->encoded_read_buf)
1197 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1199 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1201 g_string_erase (channel->read_buf, 0, copy_len);
1209 * g_io_channel_read_line:
1210 * @channel: a #GIOChannel
1211 * @str_return: The line read from the #GIOChannel, not including the
1212 * line terminator. This data should be freed with g_free()
1213 * when no longer needed. This
1214 * is a null terminated string. If a @length of zero is
1215 * returned, this will be %NULL instead.
1216 * @length: location to store length of the read data, or %NULL
1217 * @terminator_pos: location to store position of line terminator, or %NULL
1218 * @error: A location to return an error of type #GConvertError
1219 * or #GIOChannelError
1221 * Read a line, not including the terminating character(s),
1222 * from a #GIOChannel into a newly allocated string.
1223 * @length will contain allocated memory if the return
1224 * is %G_IO_STATUS_NORMAL.
1226 * Return value: a newly allocated string. Free this string
1227 * with g_free() when you are done with it.
1230 g_io_channel_read_line (GIOChannel *channel,
1233 gsize *terminator_pos,
1238 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1239 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1240 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1242 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1244 status = g_io_channel_read_line_backend (channel, length, terminator_pos, error);
1246 if (status == G_IO_STATUS_NORMAL)
1250 if (channel->do_encode)
1251 use_buf = channel->encoded_read_buf;
1253 use_buf = channel->read_buf;
1257 *str_return = g_strndup (use_buf->str, *length);
1258 g_string_erase (use_buf, 0, *length);
1267 * g_io_channel_read_line_string:
1268 * @channel: a #GIOChannel
1269 * @buffer: a #GString into which the line will be written.
1270 * If @buffer already contains data, the new data will
1271 * be appended to it.
1272 * @terminator_pos: location to store position of line terminator, or %NULL
1273 * @error: a location to store an error of type #GConvertError
1274 * or #GIOChannelError
1276 * Read a line from a #GIOChannel, using a #GString as a buffer.
1281 g_io_channel_read_line_string (GIOChannel *channel,
1283 gsize *terminator_pos,
1289 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1290 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1291 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1293 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1295 if (buffer->len > 0)
1296 g_string_truncate (buffer, 0); /* clear out the buffer */
1298 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1300 if (status == G_IO_STATUS_NORMAL)
1304 if (channel->do_encode)
1305 use_buf = channel->encoded_read_buf;
1307 use_buf = channel->read_buf;
1311 g_string_append_len (buffer, use_buf->str, length);
1312 g_string_erase (use_buf, 0, length);
1320 g_io_channel_read_line_backend (GIOChannel *channel,
1322 gsize *terminator_pos,
1326 gsize checked_to, line_term_len, line_length, got_term_len;
1328 gboolean first_time = TRUE;
1330 if (!channel->use_buffer)
1332 /* Can't do a raw read in read_line */
1333 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1334 _("Can't do a raw read in g_io_channel_read_line_string"));
1335 return G_IO_STATUS_ERROR;
1338 if (channel->encoding)
1340 if (!channel->encoded_read_buf)
1341 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1342 use_buf = channel->encoded_read_buf;
1346 if (!channel->read_buf)
1347 channel->read_buf = g_string_sized_new (channel->buf_size);
1348 use_buf = channel->read_buf;
1351 status = G_IO_STATUS_NORMAL;
1353 if (channel->line_term)
1354 line_term_len = strlen (channel->line_term);
1357 /* This value used for setting checked_to, it's the longest of the four
1358 * we autodetect for.
1365 gchar *nextchar, *lastchar;
1367 if (!first_time || (use_buf->len == 0))
1370 status = g_io_channel_fill_buffer (channel, error);
1373 case G_IO_STATUS_NORMAL:
1374 if (use_buf->len == 0)
1375 /* Can happen when using conversion and only read
1376 * part of a character
1383 case G_IO_STATUS_EOF:
1384 if (use_buf->len == 0)
1388 if (channel->do_encode && channel->read_buf->len != 0)
1389 /* using GIConv conversion, unconverted chars */
1391 g_set_error (error, G_CONVERT_ERROR,
1392 G_CONVERT_ERROR_PARTIAL_INPUT,
1393 "Leftover unconverted data in read buffer");
1394 return G_IO_STATUS_ERROR;
1397 return G_IO_STATUS_EOF;
1406 g_assert (use_buf->len > 0);
1410 lastchar = use_buf->str + strlen (use_buf->str);
1412 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1413 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1415 if (channel->line_term)
1417 if (strncmp (channel->line_term, nextchar, line_term_len) == 0)
1419 line_length = nextchar - use_buf->str;
1420 got_term_len = line_term_len;
1424 else /* auto detect */
1428 case '\n': /* unix */
1429 line_length = nextchar - use_buf->str;
1432 case '\r': /* Warning: do not use with sockets */
1433 line_length = nextchar - use_buf->str;
1434 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1435 && (lastchar == use_buf->str + use_buf->len))
1436 goto read_again; /* Try to read more data */
1437 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1442 case '\xe2': /* Unicode paragraph separator */
1443 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1445 line_length = nextchar - use_buf->str;
1450 default: /* no match */
1456 g_assert (nextchar == lastchar); /* Valid UTF-8, didn't overshoot */
1458 /* Also terminate on '\0' */
1460 line_length = lastchar - use_buf->str;
1461 if (line_length < use_buf->len)
1469 if (status == G_IO_STATUS_EOF)
1471 if (channel->encoding && channel->read_buf->len > 0)
1473 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1474 "Channel terminates in a partial character");
1475 return G_IO_STATUS_ERROR;
1477 line_length = use_buf->len;
1482 if (use_buf->len > line_term_len - 1)
1483 checked_to = use_buf->len - (line_term_len - 1);
1491 *terminator_pos = line_length;
1493 *length = line_length + got_term_len;
1495 return G_IO_STATUS_NORMAL;
1499 * g_io_channel_read_to_end:
1500 * @channel: a #GIOChannel
1501 * @str_return: Location to store a pointer to a string holding
1502 * the remaining data in the #GIOChannel. This data should
1503 * be freed with g_free() when no longer needed. This
1504 * data is terminated by an extra null, but there may be other
1505 * nulls in the intervening data.
1506 * @length: Location to store length of the data
1507 * @error: A location to return an error of type #GConvertError
1508 * or #GIOChannelError
1510 * Read all the remaining data from the file. Parameters as
1511 * for g_io_channel_read_line.
1513 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1517 g_io_channel_read_to_end (GIOChannel *channel,
1525 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1526 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1527 g_return_val_if_fail (length != NULL, G_IO_STATUS_ERROR);
1528 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1530 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1535 if (!channel->use_buffer)
1537 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1538 _("Can't do a raw read in g_io_channel_read_to_end"));
1539 return G_IO_STATUS_ERROR;
1543 status = g_io_channel_fill_buffer (channel, error);
1544 while (status == G_IO_STATUS_NORMAL);
1546 if (status != G_IO_STATUS_EOF)
1549 if (channel->encoding)
1551 if (channel->read_buf->len > 0)
1553 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1554 "Channel terminates in a partial character");
1555 return G_IO_STATUS_ERROR;
1558 if (!channel->encoded_read_buf)
1563 *str_return = g_strdup ("");
1566 use_buf = &channel->encoded_read_buf;
1569 use_buf = &channel->read_buf;
1571 g_assert (*use_buf); /* Created by fill_buffer if it didn't previously exist */
1574 *length = (*use_buf)->len;
1577 *str_return = g_string_free (*use_buf, FALSE);
1579 g_string_free (*use_buf, TRUE);
1583 return G_IO_STATUS_NORMAL;
1587 * g_io_channel_read_chars:
1588 * @channel: a #GIOChannel
1589 * @buf: a buffer to read data into
1590 * @count: the size of the buffer. Note that the buffer may
1591 * not be complelely filled even if there is data
1592 * in the buffer if the remaining data is not a
1593 * complete character.
1594 * @bytes_read: The number of bytes read.
1595 * @error: A location to return an error of type #GConvertError
1596 * or #GIOChannelError.
1598 * Replacement for g_io_channel_read() with the new API.
1603 g_io_channel_read_chars (GIOChannel *channel,
1612 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1613 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1615 g_return_val_if_fail (bytes_read != NULL, G_IO_STATUS_ERROR);
1616 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1621 return G_IO_STATUS_NORMAL;
1623 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1625 if (!channel->use_buffer)
1627 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1629 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1632 if (channel->encoding)
1634 if (!channel->encoded_read_buf)
1635 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1636 use_buf = channel->encoded_read_buf;
1640 if (!channel->read_buf)
1641 channel->read_buf = g_string_sized_new (channel->buf_size);
1642 use_buf = channel->read_buf;
1645 status = G_IO_STATUS_NORMAL;
1647 while (use_buf->len < MAX (count, 6) && status == G_IO_STATUS_NORMAL)
1648 /* Be sure to read in at least one full UTF-8 character
1651 status = g_io_channel_fill_buffer (channel, error);
1655 case G_IO_STATUS_NORMAL:
1656 g_assert (use_buf->len > 0);
1658 case G_IO_STATUS_EOF:
1659 if (use_buf->len == 0)
1663 if (channel->encoding && channel->read_buf->len != 0)
1665 g_set_error (error, G_CONVERT_ERROR,
1666 G_CONVERT_ERROR_PARTIAL_INPUT,
1667 "Leftover unconverted data in read buffer");
1668 return G_IO_STATUS_ERROR;
1671 return G_IO_STATUS_EOF;
1674 case G_IO_STATUS_AGAIN:
1675 if (use_buf->len > 0)
1676 break; /* return what we have */
1680 return G_IO_STATUS_AGAIN;
1687 *bytes_read = MIN (count, use_buf->len);
1689 if (channel->encoding && *bytes_read > 0)
1690 /* Don't validate for NULL encoding, binary safe */
1692 gchar *nextchar, *prevchar = NULL;
1694 nextchar = use_buf->str;
1696 while (nextchar < use_buf->str + *bytes_read)
1698 prevchar = nextchar;
1699 nextchar = g_utf8_next_char (nextchar);
1702 if (nextchar > use_buf->str + *bytes_read)
1703 *bytes_read = prevchar - use_buf->str;
1705 g_assert (*bytes_read > 0 || count < 6);
1708 memcpy (buf, use_buf->str, *bytes_read);
1709 g_string_erase (use_buf, 0, *bytes_read);
1711 return G_IO_STATUS_NORMAL;
1715 * g_io_channel_write_chars:
1716 * @channel: a #GIOChannel
1717 * @buf: a buffer to write data from
1718 * @count: the size of the buffer. If -1, the buffer
1719 * is taken to be a nul terminated string.
1720 * @bytes_written: The number of bytes written. This can be nonzero
1721 * even if the return value is not %G_IO_STATUS_NORMAL.
1722 * @error: A location to return an error of type #GConvertError
1723 * or #GIOChannelError
1725 * Replacement for g_io_channel_write() with the new API.
1730 g_io_channel_write_chars (GIOChannel *channel,
1733 gsize *bytes_written,
1738 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1739 g_return_val_if_fail (bytes_written != NULL, G_IO_STATUS_ERROR);
1740 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1742 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1744 if ((count < 0) && buf)
1745 count = strlen (buf);
1748 return G_IO_STATUS_NORMAL;
1750 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1751 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1753 /* Raw write case */
1755 if (!channel->use_buffer)
1757 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1758 g_assert (channel->partial_write_buf[0] == '\0');
1759 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1764 if (channel->is_seekable && ((channel->read_buf && channel->read_buf->len > 0)
1765 || (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)))
1767 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1769 g_warning("Mixed reading and writing not allowed on encoded files");
1770 return G_IO_STATUS_ERROR;
1772 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1773 if (status != G_IO_STATUS_NORMAL)
1777 if (!channel->write_buf)
1778 channel->write_buf = g_string_sized_new (channel->buf_size);
1780 if (!channel->do_encode)
1784 if (channel->encoding)
1786 const gchar *badchar;
1788 if (channel->partial_write_buf[0] != '\0')
1790 guint partial_chars = strlen (channel->partial_write_buf);
1791 guint chars_in_buf = MIN (6, partial_chars + count);
1794 memcpy (channel->partial_write_buf + partial_chars, buf,
1795 chars_in_buf - partial_chars);
1797 g_utf8_validate (channel->partial_write_buf, chars_in_buf, &badchar);
1799 if (badchar < channel->partial_write_buf + partial_chars)
1802 gsize left_len = chars_in_buf;
1804 g_assert (badchar == channel->partial_write_buf);
1806 try_char = g_utf8_get_char_validated (badchar, left_len);
1811 g_assert (chars_in_buf < 6);
1812 channel->partial_write_buf[chars_in_buf] = '\0';
1813 *bytes_written = count;
1814 return G_IO_STATUS_NORMAL;
1816 g_set_error (error, G_CONVERT_ERROR,
1817 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1818 "Illegal UTF-8 sequence");
1820 return G_IO_STATUS_ERROR;
1822 g_assert_not_reached ();
1823 return G_IO_STATUS_ERROR; /* keep the compiler happy */
1827 skip = badchar - (channel->partial_write_buf + partial_chars);
1830 copy_bytes = count - skip;
1832 g_string_append_len (channel->write_buf, channel->partial_write_buf,
1833 badchar - channel->partial_write_buf);
1834 channel->partial_write_buf[0] = '\0';
1839 if (!g_utf8_validate (buf, copy_bytes, &badchar))
1842 gsize left_len = buf + copy_bytes - badchar;
1844 try_char = g_utf8_get_char_validated (badchar, left_len);
1849 g_assert (left_len < 6);
1852 g_set_error (error, G_CONVERT_ERROR,
1853 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1854 "Illegal UTF-8 sequence");
1855 *bytes_written = count - copy_bytes;
1856 return G_IO_STATUS_ERROR;
1858 g_assert_not_reached ();
1859 return G_IO_STATUS_ERROR; /* Don't confunse the compiler */
1862 g_string_append (channel->write_buf, channel->partial_write_buf);
1863 memcpy (channel->partial_write_buf, badchar, left_len);
1864 channel->partial_write_buf[left_len] = '\0';
1865 copy_bytes = count - left_len;
1869 g_string_append (channel->write_buf, channel->partial_write_buf);
1870 channel->partial_write_buf[0] = '\0';
1877 g_assert (channel->partial_write_buf[0] == '\0');
1880 while (copy_bytes > channel->write_buf->allocated_len - channel->write_buf->len)
1884 if (channel->write_buf->len > 0)
1886 status = channel->funcs->io_write (channel, channel->write_buf->str,
1887 channel->write_buf->len, &wrote_bytes,
1889 g_string_erase (channel->write_buf, 0, wrote_bytes);
1893 status = channel->funcs->io_write (channel, buf, copy_bytes, &wrote_bytes,
1895 copy_bytes -= wrote_bytes;
1899 if (status != G_IO_STATUS_NORMAL)
1901 *bytes_written = count - copy_bytes;
1902 if (channel->partial_write_buf[0] != '\0')
1904 *bytes_written -= strlen (channel->partial_write_buf);
1905 channel->partial_write_buf[0] = '\0';
1911 g_string_append_len (channel->write_buf, buf, copy_bytes);
1912 *bytes_written = count;
1916 gsize bytes_to_go = count, buf_space, err;
1918 gsize oldlen = channel->write_buf->len;
1920 if (channel->partial_write_buf[0] != '\0')
1922 gsize partial_chars = strlen (channel->partial_write_buf);
1923 gsize partial_buf_size = MIN (6, partial_chars + count);
1924 guint chars_in_buf = partial_buf_size;
1927 g_assert (partial_chars < 6);
1929 memcpy (channel->partial_write_buf + partial_chars, buf,
1930 chars_in_buf - partial_chars);
1931 inbuf = channel->partial_write_buf;
1933 buf_space = MIN (channel->buf_size / 4, channel->write_buf->allocated_len
1934 - channel->write_buf->len);
1935 g_string_set_size (channel->write_buf, channel->write_buf->len
1937 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1939 err = g_iconv (channel->write_cd, &inbuf, &partial_buf_size,
1940 &outbuf, &buf_space);
1942 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1944 if (err == (size_t) -1)
1949 if (errno == EINVAL)
1951 if (chars_in_buf == partial_buf_size)
1953 /* Didn't convert anything */
1955 g_assert (chars_in_buf < 6); /* More of the same character */
1956 g_assert (chars_in_buf == count + partial_chars);
1958 channel->partial_write_buf[chars_in_buf] = '\0';
1959 *bytes_written = count;
1960 return G_IO_STATUS_NORMAL;
1963 g_assert (chars_in_buf - partial_buf_size > partial_chars);
1964 /* Converted the character of which a part was sitting in
1965 * the partial character buffer before the write.
1974 g_assert_not_reached ();
1976 g_set_error (error, G_CONVERT_ERROR,
1977 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1978 _("Invalid byte sequence in conversion input"));
1980 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1981 _("Error during conversion: %s"), strerror (errno));
1984 g_string_truncate (channel->write_buf, oldlen);
1986 channel->partial_write_buf[partial_chars] = '\0';
1988 return G_IO_STATUS_ERROR;
1992 *bytes_written = (chars_in_buf - partial_buf_size) - partial_chars;
1993 buf += *bytes_written;
1994 bytes_to_go -= *bytes_written;
1995 channel->partial_write_buf[0] = '\0';
2000 buf_space = MIN (channel->write_buf->allocated_len
2001 - channel->write_buf->len, channel->buf_size / 4);
2003 g_string_set_size (channel->write_buf, channel->write_buf->len + buf_space);
2004 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
2006 err = g_iconv (channel->write_cd, (gchar**) &buf, &bytes_to_go,
2007 &outbuf, &buf_space);
2009 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
2010 *bytes_written = count - bytes_to_go;
2012 if (err == (size_t) -1)
2018 gint bytes_left = count - *bytes_written;
2020 g_assert (bytes_left < 6);
2022 memcpy (channel->partial_write_buf, buf, bytes_left);
2023 channel->partial_write_buf[bytes_left] = '\0';
2025 *bytes_written = count;
2027 return G_IO_STATUS_NORMAL;
2032 status = channel->funcs->io_write (channel,
2033 channel->write_buf->str, channel->write_buf->len,
2034 &wrote_bytes, error);
2035 g_string_erase (channel->write_buf, 0, wrote_bytes);
2037 if (status != G_IO_STATUS_NORMAL)
2042 g_set_error (error, G_CONVERT_ERROR,
2043 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2044 _("Invalid byte sequence in conversion input"));
2045 return G_IO_STATUS_ERROR;
2047 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2048 _("Error during conversion: %s"), strerror (errno));
2049 return G_IO_STATUS_ERROR;
2054 while (bytes_to_go > 0);
2057 if (channel->write_buf->len > channel->buf_size)
2061 status = channel->funcs->io_write (channel, channel->write_buf->str,
2062 channel->write_buf->len, &wrote_bytes, error);
2064 if (wrote_bytes > 0)
2065 g_string_erase (channel->write_buf, 0, wrote_bytes);
2070 return G_IO_STATUS_NORMAL;
2074 * g_io_channel_error_quark:
2076 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2079 g_io_channel_error_quark (void)
2081 static GQuark q = 0;
2083 q = g_quark_from_static_string ("g-io-channel-error-quark");