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/.
43 #undef G_DISABLE_DEPRECATED
47 #include "giochannel.h"
51 #define G_IO_NICE_BUF_SIZE 1024
53 /* This needs to be as wide as the largest character in any possible encoding */
54 #define MAX_CHAR_SIZE 10
56 /* Some simplifying macros, which reduce the need to worry whether the
57 * buffers have been allocated. These also make USE_BUF () an lvalue,
58 * which is used in g_io_channel_read_to_end ().
60 #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \
61 : (channel)->read_buf)
62 #define BUF_LEN(string) ((string) ? (string)->len : 0)
64 static GIOError g_io_error_get_from_g_error (GIOStatus status,
66 static void g_io_channel_purge (GIOChannel *channel);
67 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
69 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
71 gsize *terminator_pos,
75 g_io_channel_init (GIOChannel *channel)
77 channel->ref_count = 1;
78 channel->encoding = g_strdup ("UTF-8");
79 channel->line_term = NULL;
80 channel->line_term_len = 0;
81 channel->buf_size = G_IO_NICE_BUF_SIZE;
82 channel->read_cd = (GIConv) -1;
83 channel->write_cd = (GIConv) -1;
84 channel->read_buf = NULL; /* Lazy allocate buffers */
85 channel->encoded_read_buf = NULL;
86 channel->write_buf = NULL;
87 channel->partial_write_buf[0] = '\0';
88 channel->use_buffer = TRUE;
89 channel->do_encode = FALSE;
90 channel->close_on_unref = FALSE;
94 g_io_channel_ref (GIOChannel *channel)
96 g_return_if_fail (channel != NULL);
102 g_io_channel_unref (GIOChannel *channel)
104 g_return_if_fail (channel != NULL);
106 channel->ref_count--;
107 if (channel->ref_count == 0)
109 if (channel->close_on_unref)
110 g_io_channel_shutdown (channel, TRUE, NULL);
112 g_io_channel_purge (channel);
113 g_free (channel->encoding);
114 if (channel->read_cd != (GIConv) -1)
115 g_iconv_close (channel->read_cd);
116 if (channel->write_cd != (GIConv) -1)
117 g_iconv_close (channel->write_cd);
118 if (channel->line_term)
119 g_free (channel->line_term);
120 if (channel->read_buf)
121 g_string_free (channel->read_buf, TRUE);
122 if (channel->write_buf)
123 g_string_free (channel->write_buf, TRUE);
124 if (channel->encoded_read_buf)
125 g_string_free (channel->encoded_read_buf, TRUE);
126 channel->funcs->io_free (channel);
131 g_io_error_get_from_g_error (GIOStatus status,
136 case G_IO_STATUS_NORMAL:
137 case G_IO_STATUS_EOF:
138 return G_IO_ERROR_NONE;
139 case G_IO_STATUS_AGAIN:
140 return G_IO_ERROR_AGAIN;
141 case G_IO_STATUS_ERROR:
142 g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
144 if (err->domain != G_IO_CHANNEL_ERROR)
145 return G_IO_ERROR_UNKNOWN;
148 case G_IO_CHANNEL_ERROR_INVAL:
149 return G_IO_ERROR_INVAL;
151 return G_IO_ERROR_UNKNOWN;
154 g_assert_not_reached ();
155 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
161 * @channel: a #GIOChannel.
162 * @buf: a buffer to read the data into (which should be at least count bytes long).
163 * @count: the number of bytes to read from the #GIOChannel.
164 * @bytes_read: returns the number of bytes actually read.
166 * Reads data from a #GIOChannel.
168 * Return value: %G_IO_ERROR_NONE if the operation was successful.
170 * Deprecated: Use g_io_channel_read_chars() instead.
173 g_io_channel_read (GIOChannel *channel,
182 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
183 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
189 return G_IO_STATUS_NORMAL;
192 g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
194 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
196 error = g_io_error_get_from_g_error (status, err);
205 * g_io_channel_write:
206 * @channel: a #GIOChannel.
207 * @buf: the buffer containing the data to write.
208 * @count: the number of bytes to write.
209 * @bytes_written: the number of bytes actually written.
211 * Writes data to a #GIOChannel.
213 * Return value: %G_IO_ERROR_NONE if the operation was successful.
215 * Deprecated: Use g_io_channel_write_chars() instead.
218 g_io_channel_write (GIOChannel *channel,
221 gsize *bytes_written)
227 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
228 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
230 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
232 error = g_io_error_get_from_g_error (status, err);
242 * @channel: a #GIOChannel.
243 * @offset: an offset, in bytes, which is added to the position specified by @type
244 * @type: the position in the file, which can be %G_SEEK_CUR (the current
245 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
248 * Sets the current position in the #GIOChannel, similar to the standard library
251 * Return value: %G_IO_ERROR_NONE if the operation was successful.
253 * Deprecated: Use g_io_channel_seek_position() instead.
256 g_io_channel_seek (GIOChannel *channel,
264 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
265 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
274 g_warning ("g_io_channel_seek: unknown seek type");
275 return G_IO_ERROR_UNKNOWN;
278 status = channel->funcs->io_seek (channel, offset, type, &err);
280 error = g_io_error_get_from_g_error (status, err);
288 /* The function g_io_channel_new_file() is prototyped in both
289 * giounix.c and giowin32.c, so we stick its documentation here.
293 * g_io_channel_new_file:
294 * @filename: A string containing the name of a file.
295 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
296 * the same meaning as in fopen().
297 * @error: A location to return an error of type %G_FILE_ERROR.
299 * Open a file @filename as a #GIOChannel using mode @mode. This
300 * channel will be closed when the last reference to it is dropped,
301 * so there is no need to call g_io_channel_close() (though doing
302 * so will not cause problems, as long as no attempt is made to
303 * access the channel after it is closed).
305 * Return value: A #GIOChannel on success, %NULL on failure.
309 * g_io_channel_close:
310 * @channel: A #GIOChannel
312 * Close an IO channel. Any pending data to be written will be
313 * flushed, ignoring errors. The channel will not be freed until the
314 * last reference is dropped using g_io_channel_unref().
316 * Deprecated: Use g_io_channel_shutdown() instead.
319 g_io_channel_close (GIOChannel *channel)
323 g_return_if_fail (channel != NULL);
325 g_io_channel_purge (channel);
327 channel->funcs->io_close (channel, &err);
330 { /* No way to return the error */
331 g_warning ("Error closing channel: %s", err->message);
335 channel->close_on_unref = FALSE; /* Because we already did */
336 channel->is_readable = FALSE;
337 channel->is_writeable = FALSE;
338 channel->is_seekable = FALSE;
342 * g_io_channel_shutdown:
343 * @channel: a #GIOChannel
344 * @flush: if %TRUE, flush pending
345 * @err: location to store a #GIOChannelError
347 * Close an IO channel. Any pending data to be written will be
348 * flushed if @flush is %TRUE. The channel will not be freed until the
349 * last reference is dropped using g_io_channel_unref().
351 * Return value: the status of the operation.
354 g_io_channel_shutdown (GIOChannel *channel,
358 GIOStatus status, result;
359 GError *tmperr = NULL;
361 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
362 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
364 if (channel->write_buf && channel->write_buf->len > 0)
370 /* Set the channel to blocking, to avoid a busy loop
372 flags = g_io_channel_get_flags (channel);
373 /* Ignore any errors here, they're irrelevant */
374 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
376 result = g_io_channel_flush (channel, &tmperr);
379 result = G_IO_STATUS_NORMAL;
381 g_string_truncate(channel->write_buf, 0);
384 result = G_IO_STATUS_NORMAL;
386 if (channel->partial_write_buf[0] != '\0')
389 g_warning ("Partial character at end of write buffer not flushed.\n");
390 channel->partial_write_buf[0] = '\0';
393 status = channel->funcs->io_close (channel, err);
395 channel->close_on_unref = FALSE; /* Because we already did */
396 channel->is_readable = FALSE;
397 channel->is_writeable = FALSE;
398 channel->is_seekable = FALSE;
400 if (status != G_IO_STATUS_NORMAL)
402 g_clear_error (&tmperr);
405 else if (result != G_IO_STATUS_NORMAL)
407 g_propagate_error (err, tmperr);
411 return G_IO_STATUS_NORMAL;
414 /* This function is used for the final flush on close or unref */
416 g_io_channel_purge (GIOChannel *channel)
421 g_return_if_fail (channel != NULL);
423 if (channel->write_buf && channel->write_buf->len > 0)
427 /* Set the channel to blocking, to avoid a busy loop
429 flags = g_io_channel_get_flags (channel);
430 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
432 status = g_io_channel_flush (channel, &err);
435 { /* No way to return the error */
436 g_warning ("Error flushing string: %s", err->message);
441 /* Flush these in case anyone tries to close without unrefing */
443 if (channel->read_buf)
444 g_string_truncate (channel->read_buf, 0);
445 if (channel->write_buf)
446 g_string_truncate (channel->write_buf, 0);
447 if (channel->encoding)
449 if (channel->encoded_read_buf)
450 g_string_truncate (channel->encoded_read_buf, 0);
452 if (channel->partial_write_buf[0] != '\0')
454 g_warning ("Partial character at end of write buffer not flushed.\n");
455 channel->partial_write_buf[0] = '\0';
461 g_io_create_watch (GIOChannel *channel,
462 GIOCondition condition)
464 g_return_val_if_fail (channel != NULL, NULL);
466 return channel->funcs->io_create_watch (channel, condition);
470 g_io_add_watch_full (GIOChannel *channel,
472 GIOCondition condition,
475 GDestroyNotify notify)
480 g_return_val_if_fail (channel != NULL, 0);
482 source = g_io_create_watch (channel, condition);
484 if (priority != G_PRIORITY_DEFAULT)
485 g_source_set_priority (source, priority);
486 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
488 id = g_source_attach (source, NULL);
489 g_source_unref (source);
495 g_io_add_watch (GIOChannel *channel,
496 GIOCondition condition,
500 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
504 * g_io_channel_get_buffer_condition:
505 * @channel: A #GIOChannel
507 * This function returns a #GIOCondition depending on whether there
508 * is data to be read/space to write data in the
509 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
510 * %G_IO_OUT may be set.
512 * Return value: A #GIOCondition
515 g_io_channel_get_buffer_condition (GIOChannel *channel)
517 GIOCondition condition = 0;
519 if (channel->encoding)
521 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
522 condition |= G_IO_IN; /* Only return if we have full characters */
526 if (channel->read_buf && (channel->read_buf->len > 0))
527 condition |= G_IO_IN;
530 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
531 condition |= G_IO_OUT;
537 * g_io_channel_error_from_errno:
538 * @en: an <literal>errno</literal> error number, e.g. %EINVAL.
540 * Converts an <literal>errno</literal> error number to a #GIOChannelError.
542 * Return value: a #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL.
545 g_io_channel_error_from_errno (gint en)
548 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
555 g_warning("Invalid file descriptor.\n");
556 return G_IO_CHANNEL_ERROR_FAILED;
561 g_warning("Buffer outside valid address space.\n");
562 return G_IO_CHANNEL_ERROR_FAILED;
567 return G_IO_CHANNEL_ERROR_FBIG;
571 /* In general, we should catch EINTR before we get here,
572 * but close() is allowed to return EINTR by POSIX, so
573 * we need to catch it here; EINTR from close() is
574 * unrecoverable, because it's undefined whether
575 * the fd was actually closed or not, so we just return
576 * a generic error code.
579 return G_IO_CHANNEL_ERROR_FAILED;
584 return G_IO_CHANNEL_ERROR_INVAL;
589 return G_IO_CHANNEL_ERROR_IO;
594 return G_IO_CHANNEL_ERROR_ISDIR;
599 return G_IO_CHANNEL_ERROR_NOSPC;
604 return G_IO_CHANNEL_ERROR_NXIO;
609 return G_IO_CHANNEL_ERROR_OVERFLOW;
614 return G_IO_CHANNEL_ERROR_PIPE;
618 return G_IO_CHANNEL_ERROR_FAILED;
623 * g_io_channel_set_buffer_size:
624 * @channel: a #GIOChannel
625 * @size: the size of the buffer. 0 == pick a good size
627 * Sets the buffer size.
630 g_io_channel_set_buffer_size (GIOChannel *channel,
633 g_return_if_fail (channel != NULL);
636 size = G_IO_NICE_BUF_SIZE;
638 if (size < MAX_CHAR_SIZE)
639 size = MAX_CHAR_SIZE;
641 channel->buf_size = size;
645 * g_io_channel_get_buffer_size:
646 * @channel: a #GIOChannel
648 * Gets the buffer size.
650 * Return value: the size of the buffer.
653 g_io_channel_get_buffer_size (GIOChannel *channel)
655 g_return_val_if_fail (channel != NULL, 0);
657 return channel->buf_size;
661 * g_io_channel_set_line_term:
662 * @channel: a #GIOChannel
663 * @line_term: The line termination string. Use %NULL for auto detect.
664 * Auto detection breaks on "\n", "\r\n", "\r", "\0", and
665 * the Unicode paragraph separator. Auto detection should
666 * not be used for anything other than file-based channels.
667 * @length: The length of the termination string. If -1 is passed, the
668 * string is assumed to be nul-terminated. This option allows
669 * termination strings with embeded nuls.
671 * This sets the string that #GIOChannel uses to determine
672 * where in the file a line break occurs.
675 g_io_channel_set_line_term (GIOChannel *channel,
676 const gchar *line_term,
679 g_return_if_fail (channel != NULL);
680 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
682 if (line_term == NULL)
685 length = strlen (line_term);
687 if (channel->line_term)
688 g_free (channel->line_term);
689 channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
690 channel->line_term_len = length;
694 * g_io_channel_get_line_term:
695 * @channel: a #GIOChannel
696 * @length: a location to return the length of the line terminator
698 * This returns the string that #GIOChannel uses to determine
699 * where in the file a line break occurs. A value of %NULL
700 * indicates auto detection.
702 * Return value: The line termination string. This value
703 * is owned by GLib and must not be freed.
705 G_CONST_RETURN gchar*
706 g_io_channel_get_line_term (GIOChannel *channel,
709 g_return_val_if_fail (channel != NULL, 0);
712 *length = channel->line_term_len;
714 return channel->line_term;
718 * g_io_channel_set_flags:
719 * @channel: a #GIOChannel.
720 * @flags: the flags to set on the IO channel.
721 * @error: A location to return an error of type #GIOChannelError.
723 * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK).
725 * Return value: the status of the operation.
728 g_io_channel_set_flags (GIOChannel *channel,
732 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
733 g_return_val_if_fail ((error == NULL) || (*error == NULL),
736 return (* channel->funcs->io_set_flags)(channel,
737 flags & G_IO_FLAG_SET_MASK,
742 * g_io_channel_get_flags:
743 * @channel: a #GIOChannel
745 * Gets the current flags for a #GIOChannel, including read-only
746 * flags such as %G_IO_FLAG_IS_READABLE.
748 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
749 * are cached for internal use by the channel when it is created.
750 * If they should change at some later point (e.g. partial shutdown
751 * of a socket with the UNIX shutdown() function), the user
752 * should immediately call g_io_channel_get_flags () to update
753 * the internal values of these flags.
755 * Return value: the flags which are set on the channel
758 g_io_channel_get_flags (GIOChannel *channel)
762 g_return_val_if_fail (channel != NULL, 0);
764 flags = (* channel->funcs->io_get_flags) (channel);
766 /* Cross implementation code */
768 if (channel->is_seekable)
769 flags |= G_IO_FLAG_IS_SEEKABLE;
770 if (channel->is_readable)
771 flags |= G_IO_FLAG_IS_READABLE;
772 if (channel->is_writeable)
773 flags |= G_IO_FLAG_IS_WRITEABLE;
779 * g_io_channel_set_close_on_unref:
780 * @channel: a #GIOChannel
781 * @do_close: Whether to close the channel on the final unref of
782 * the GIOChannel data structure. The default value of
783 * this is %TRUE for channels created by g_io_channel_new_file (),
784 * and %FALSE for all other channels.
786 * Setting this flag to %TRUE for a channel you have already closed
787 * can cause problems.
790 g_io_channel_set_close_on_unref (GIOChannel *channel,
793 g_return_if_fail (channel != NULL);
795 channel->close_on_unref = do_close;
799 * g_io_channel_get_close_on_unref:
800 * @channel: a #GIOChannel.
802 * Returns whether the file/socket/whatever associated with @channel
803 * will be closed when @channel receives its final unref and is
804 * destroyed. The default value of this is %TRUE for channels created
805 * by g_io_channel_new_file (), and %FALSE for all other channels.
807 * Return value: Whether the channel will be closed on the final unref of
808 * the GIOChannel data structure.
811 g_io_channel_get_close_on_unref (GIOChannel *channel)
813 g_return_val_if_fail (channel != NULL, FALSE);
815 return channel->close_on_unref;
819 * g_io_channel_seek_position:
820 * @channel: a #GIOChannel
821 * @offset: The offset in bytes from the position specified by @type
822 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
823 * cases where a call to g_io_channel_set_encoding ()
824 * is allowed. See the documentation for
825 * g_io_channel_set_encoding () for details.
826 * @error: A location to return an error of type #GIOChannelError
828 * Replacement for g_io_channel_seek() with the new API.
830 * Return value: the status of the operation.
833 g_io_channel_seek_position (GIOChannel* channel,
840 /* For files, only one of the read and write buffers can contain data.
841 * For sockets, both can contain data.
844 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
845 g_return_val_if_fail ((error == NULL) || (*error == NULL),
847 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
851 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
852 if (channel->use_buffer)
854 if (channel->do_encode && channel->encoded_read_buf
855 && channel->encoded_read_buf->len > 0)
857 g_warning ("Seek type G_SEEK_CUR not allowed for this"
858 " channel's encoding.\n");
859 return G_IO_STATUS_ERROR;
861 if (channel->read_buf)
862 offset -= channel->read_buf->len;
863 if (channel->encoded_read_buf)
865 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
867 /* If there's anything here, it's because the encoding is UTF-8,
868 * so we can just subtract the buffer length, the same as for
869 * the unencoded data.
872 offset -= channel->encoded_read_buf->len;
880 g_warning ("g_io_channel_seek_position: unknown seek type");
881 return G_IO_STATUS_ERROR;
884 if (channel->use_buffer)
886 status = g_io_channel_flush (channel, error);
887 if (status != G_IO_STATUS_NORMAL)
891 status = channel->funcs->io_seek (channel, offset, type, error);
893 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
895 if (channel->read_buf)
896 g_string_truncate (channel->read_buf, 0);
898 /* Conversion state no longer matches position in file */
899 if (channel->read_cd != (GIConv) -1)
900 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
901 if (channel->write_cd != (GIConv) -1)
902 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
904 if (channel->encoded_read_buf)
906 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
907 g_string_truncate (channel->encoded_read_buf, 0);
910 if (channel->partial_write_buf[0] != '\0')
912 g_warning ("Partial character at end of write buffer not flushed.\n");
913 channel->partial_write_buf[0] = '\0';
921 * g_io_channel_flush:
922 * @channel: a #GIOChannel
923 * @error: location to store an error of type #GIOChannelError
925 * Flushes the write buffer for the GIOChannel.
927 * Return value: the status of the operation: One of
928 * #G_IO_CHANNEL_NORMAL, #G_IO_CHANNEL_AGAIN, or
929 * #G_IO_CHANNEL_ERROR.
932 g_io_channel_flush (GIOChannel *channel,
936 gsize this_time = 1, bytes_written = 0;
938 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
939 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
941 if (channel->write_buf == NULL || channel->write_buf->len == 0)
942 return G_IO_STATUS_NORMAL;
946 g_assert (this_time > 0);
948 status = channel->funcs->io_write (channel,
949 channel->write_buf->str + bytes_written,
950 channel->write_buf->len - bytes_written,
952 bytes_written += this_time;
954 while ((bytes_written < channel->write_buf->len)
955 && (status == G_IO_STATUS_NORMAL));
957 g_string_erase (channel->write_buf, 0, bytes_written);
963 * g_io_channel_set_buffered:
964 * @channel: a #GIOChannel
965 * @buffered: whether to set the channel buffered or unbuffered
967 * The buffering state can only be set if the channel's encoding
968 * is %NULL. For any other encoding, the channel must be buffered.
970 * A buffered channel can only be set unbuffered if the channel's
971 * internal buffers have been flushed. Newly created channels or
972 * channels which have returned %G_IO_STATUS_EOF
973 * not require such a flush. For write-only channels, a call to
974 * g_io_channel_flush () is sufficient. For all other channels,
975 * the buffers may be flushed by a call to g_io_channel_seek_position ().
976 * This includes the possibility of seeking with seek type %G_SEEK_CUR
977 * and an offset of zero. Note that this means that socket-based
978 * channels cannot be set unbuffered once they have had data
981 * On unbuffered channels, it is safe to mix read and write
982 * calls from the new and old APIs, if this is necessary for
983 * maintaining old code.
985 * The default state of the channel is buffered.
988 g_io_channel_set_buffered (GIOChannel *channel,
991 g_return_if_fail (channel != NULL);
993 if (channel->encoding != NULL)
995 g_warning ("Need to have NULL encoding to set the buffering state of the "
1000 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
1001 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
1003 channel->use_buffer = buffered;
1007 * g_io_channel_get_buffered:
1008 * @channel: a #GIOChannel.
1010 * Returns whether @channel is buffered.
1012 * Return Value: %TRUE if the @channel is buffered.
1015 g_io_channel_get_buffered (GIOChannel *channel)
1017 g_return_val_if_fail (channel != NULL, FALSE);
1019 return channel->use_buffer;
1023 * g_io_channel_set_encoding:
1024 * @channel: a #GIOChannel
1025 * @encoding: the encoding type
1026 * @error: location to store an error of type #GConvertError.
1028 * Sets the encoding for the input/output of the channel. The internal
1029 * encoding is always UTF-8. The default encoding for the
1030 * external file is UTF-8.
1032 * The encoding %NULL is safe to use with binary data.
1034 * The encoding can only be set if one of the following conditions
1037 * 1. The channel was just created, and has not been written to
1040 * 2. The channel is write-only.
1042 * 3. The channel is a file, and the file pointer was just
1043 * repositioned by a call to g_io_channel_seek_position().
1044 * (This flushes all the internal buffers.)
1046 * 4. The current encoding is %NULL or UTF-8.
1048 * 5. One of the (new API) read functions has just returned %G_IO_STATUS_EOF
1049 * (or, in the case of g_io_channel_read_to_end (), %G_IO_STATUS_NORMAL).
1051 * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar ()
1052 * has returned %G_IO_STATUS_AGAIN or %G_IO_STATUS_ERROR. This may be
1053 * useful in the case of %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1054 * Returning one of these statuses from g_io_channel_read_line (),
1055 * g_io_channel_read_line_string (), or g_io_channel_read_to_end ()
1056 * does <emphasis>not</emphasis> guarantee that the encoding can be changed.
1058 * Channels which do not meet one of the above conditions cannot call
1059 * g_io_channel_seek_position () with an offset of %G_SEEK_CUR,
1060 * and, if they are "seekable", cannot
1061 * call g_io_channel_write_chars () after calling one
1062 * of the API "read" functions.
1064 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set.
1067 g_io_channel_set_encoding (GIOChannel *channel,
1068 const gchar *encoding,
1071 GIConv read_cd, write_cd;
1072 gboolean did_encode;
1074 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1075 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1077 /* Make sure the encoded buffers are empty */
1079 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1080 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1082 if (!channel->use_buffer)
1084 g_warning ("Need to set the channel buffered before setting the encoding.\n");
1085 g_warning ("Assuming this is what you meant and acting accordingly.\n");
1087 channel->use_buffer = TRUE;
1090 if (channel->partial_write_buf[0] != '\0')
1092 g_warning ("Partial character at end of write buffer not flushed.\n");
1093 channel->partial_write_buf[0] = '\0';
1096 did_encode = channel->do_encode;
1098 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1100 channel->do_encode = FALSE;
1101 read_cd = write_cd = (GIConv) -1;
1106 const gchar *from_enc = NULL, *to_enc = NULL;
1108 if (channel->is_readable)
1110 read_cd = g_iconv_open ("UTF-8", encoding);
1112 if (read_cd == (GIConv) -1)
1120 read_cd = (GIConv) -1;
1122 if (channel->is_writeable && err == 0)
1124 write_cd = g_iconv_open (encoding, "UTF-8");
1126 if (write_cd == (GIConv) -1)
1129 from_enc = encoding;
1134 write_cd = (GIConv) -1;
1138 g_assert (from_enc);
1142 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1143 _("Conversion from character set `%s' to `%s' is not supported"),
1146 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1147 _("Could not open converter from `%s' to `%s': %s"),
1148 from_enc, to_enc, g_strerror (err));
1150 if (read_cd != (GIConv) -1)
1151 g_iconv_close (read_cd);
1152 if (write_cd != (GIConv) -1)
1153 g_iconv_close (write_cd);
1155 return G_IO_STATUS_ERROR;
1158 channel->do_encode = TRUE;
1161 /* The encoding is ok, so set the fields in channel */
1163 if (channel->read_cd != (GIConv) -1)
1164 g_iconv_close (channel->read_cd);
1165 if (channel->write_cd != (GIConv) -1)
1166 g_iconv_close (channel->write_cd);
1168 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1170 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1172 /* This is just validated UTF-8, so we can copy it back into read_buf
1173 * so it can be encoded in whatever the new encoding is.
1176 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1177 channel->encoded_read_buf->len);
1178 g_string_truncate (channel->encoded_read_buf, 0);
1181 channel->read_cd = read_cd;
1182 channel->write_cd = write_cd;
1184 g_free (channel->encoding);
1185 channel->encoding = g_strdup (encoding);
1187 return G_IO_STATUS_NORMAL;
1191 * g_io_channel_get_encoding:
1192 * @channel: a #GIOChannel
1194 * Gets the encoding for the input/output of the channel. The internal
1195 * encoding is always UTF-8. The encoding %NULL makes the
1196 * channel safe for binary data.
1198 * Return value: A string containing the encoding, this string is
1199 * owned by GLib and must not be freed.
1201 G_CONST_RETURN gchar*
1202 g_io_channel_get_encoding (GIOChannel *channel)
1204 g_return_val_if_fail (channel != NULL, NULL);
1206 return channel->encoding;
1210 g_io_channel_fill_buffer (GIOChannel *channel,
1213 gsize read_size, cur_len, oldlen;
1216 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1218 status = g_io_channel_flush (channel, err);
1219 if (status != G_IO_STATUS_NORMAL)
1222 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1224 g_warning ("Partial character at end of write buffer not flushed.\n");
1225 channel->partial_write_buf[0] = '\0';
1228 if (!channel->read_buf)
1229 channel->read_buf = g_string_sized_new (channel->buf_size);
1231 cur_len = channel->read_buf->len;
1233 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1235 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1236 channel->buf_size, &read_size, err);
1238 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1240 g_string_truncate (channel->read_buf, read_size + cur_len);
1242 if ((status != G_IO_STATUS_NORMAL)
1243 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1246 g_assert (channel->read_buf->len > 0);
1248 if (channel->encoded_read_buf)
1249 oldlen = channel->encoded_read_buf->len;
1253 if (channel->encoding)
1254 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1257 if (channel->do_encode)
1259 size_t errnum, inbytes_left, outbytes_left;
1260 gchar *inbuf, *outbuf;
1263 g_assert (channel->encoded_read_buf);
1267 inbytes_left = channel->read_buf->len;
1268 outbytes_left = MAX (channel->read_buf->len,
1269 channel->encoded_read_buf->allocated_len
1270 - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1271 outbytes_left = MAX (outbytes_left, 6);
1273 inbuf = channel->read_buf->str;
1274 g_string_set_size (channel->encoded_read_buf,
1275 channel->encoded_read_buf->len + outbytes_left);
1276 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1279 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1280 &outbuf, &outbytes_left);
1283 g_assert (inbuf + inbytes_left == channel->read_buf->str
1284 + channel->read_buf->len);
1285 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1286 + channel->encoded_read_buf->len);
1288 g_string_erase (channel->read_buf, 0,
1289 channel->read_buf->len - inbytes_left);
1290 g_string_truncate (channel->encoded_read_buf,
1291 channel->encoded_read_buf->len - outbytes_left);
1293 if (errnum == (size_t) -1)
1298 if ((oldlen == channel->encoded_read_buf->len)
1299 && (status == G_IO_STATUS_EOF))
1300 status = G_IO_STATUS_EOF;
1302 status = G_IO_STATUS_NORMAL;
1305 /* Buffer size at least 6, wrote at least on character */
1306 g_assert (inbuf != channel->read_buf->str);
1309 if (oldlen < channel->encoded_read_buf->len)
1310 status = G_IO_STATUS_NORMAL;
1313 g_set_error (err, G_CONVERT_ERROR,
1314 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1315 _("Invalid byte sequence in conversion input"));
1316 return G_IO_STATUS_ERROR;
1320 g_assert (errval != EBADF); /* The converter should be open */
1321 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1322 _("Error during conversion: %s"), g_strerror (errval));
1323 return G_IO_STATUS_ERROR;
1326 g_assert ((status != G_IO_STATUS_NORMAL)
1327 || (channel->encoded_read_buf->len > 0));
1329 else if (channel->encoding) /* UTF-8 */
1331 gchar *nextchar, *lastchar;
1333 g_assert (channel->encoded_read_buf);
1335 nextchar = channel->read_buf->str;
1336 lastchar = channel->read_buf->str + channel->read_buf->len;
1338 while (nextchar < lastchar)
1342 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1347 /* stop, leave partial character in buffer */
1348 lastchar = nextchar;
1351 if (oldlen < channel->encoded_read_buf->len)
1352 status = G_IO_STATUS_NORMAL;
1355 g_set_error (err, G_CONVERT_ERROR,
1356 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1357 _("Invalid byte sequence in conversion input"));
1358 status = G_IO_STATUS_ERROR;
1360 lastchar = nextchar;
1363 nextchar = g_utf8_next_char (nextchar);
1368 if (lastchar > channel->read_buf->str)
1370 gint copy_len = lastchar - channel->read_buf->str;
1372 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1374 g_string_erase (channel->read_buf, 0, copy_len);
1382 * g_io_channel_read_line:
1383 * @channel: a #GIOChannel
1384 * @str_return: The line read from the #GIOChannel, including the
1385 * line terminator. This data should be freed with g_free()
1386 * when no longer needed. This is a nul-terminated string.
1387 * If a @length of zero is returned, this will be %NULL instead.
1388 * @length: location to store length of the read data, or %NULL
1389 * @terminator_pos: location to store position of line terminator, or %NULL
1390 * @error: A location to return an error of type #GConvertError
1391 * or #GIOChannelError
1393 * Reads a line, including the terminating character(s),
1394 * from a #GIOChannel into a newly-allocated string.
1395 * @str_return will contain allocated memory if the return
1396 * is %G_IO_STATUS_NORMAL.
1398 * Return value: the status of the operation.
1401 g_io_channel_read_line (GIOChannel *channel,
1404 gsize *terminator_pos,
1410 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1411 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1412 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1414 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1416 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1419 *length = got_length;
1421 if (status == G_IO_STATUS_NORMAL)
1423 g_assert (USE_BUF (channel));
1424 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1425 g_string_erase (USE_BUF (channel), 0, got_length);
1434 * g_io_channel_read_line_string:
1435 * @channel: a #GIOChannel
1436 * @buffer: a #GString into which the line will be written.
1437 * If @buffer already contains data, the old data will
1439 * @terminator_pos: location to store position of line terminator, or %NULL
1440 * @error: a location to store an error of type #GConvertError
1441 * or #GIOChannelError
1443 * Reads a line from a #GIOChannel, using a #GString as a buffer.
1445 * Return value: the status of the operation.
1448 g_io_channel_read_line_string (GIOChannel *channel,
1450 gsize *terminator_pos,
1456 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1457 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1458 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1460 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1462 if (buffer->len > 0)
1463 g_string_truncate (buffer, 0); /* clear out the buffer */
1465 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1467 if (status == G_IO_STATUS_NORMAL)
1469 g_assert (USE_BUF (channel));
1470 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1471 g_string_erase (USE_BUF (channel), 0, length);
1479 g_io_channel_read_line_backend (GIOChannel *channel,
1481 gsize *terminator_pos,
1485 gsize checked_to, line_term_len, line_length, got_term_len;
1486 gboolean first_time = TRUE;
1488 if (!channel->use_buffer)
1490 /* Can't do a raw read in read_line */
1491 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1492 _("Can't do a raw read in g_io_channel_read_line_string"));
1493 return G_IO_STATUS_ERROR;
1496 status = G_IO_STATUS_NORMAL;
1498 if (channel->line_term)
1499 line_term_len = channel->line_term_len;
1502 /* This value used for setting checked_to, it's the longest of the four
1503 * we autodetect for.
1510 gchar *nextchar, *lastchar;
1513 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1516 status = g_io_channel_fill_buffer (channel, error);
1519 case G_IO_STATUS_NORMAL:
1520 if (BUF_LEN (USE_BUF (channel)) == 0)
1521 /* Can happen when using conversion and only read
1522 * part of a character
1529 case G_IO_STATUS_EOF:
1530 if (BUF_LEN (USE_BUF (channel)) == 0)
1535 if (channel->encoding && channel->read_buf->len != 0)
1537 g_set_error (error, G_CONVERT_ERROR,
1538 G_CONVERT_ERROR_PARTIAL_INPUT,
1539 _("Leftover unconverted data in read buffer"));
1540 return G_IO_STATUS_ERROR;
1543 return G_IO_STATUS_EOF;
1553 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1555 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1559 lastchar = use_buf->str + use_buf->len;
1561 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1562 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1564 if (channel->line_term)
1566 if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
1568 line_length = nextchar - use_buf->str;
1569 got_term_len = line_term_len;
1573 else /* auto detect */
1577 case '\n': /* unix */
1578 line_length = nextchar - use_buf->str;
1581 case '\r': /* Warning: do not use with sockets */
1582 line_length = nextchar - use_buf->str;
1583 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1584 && (lastchar == use_buf->str + use_buf->len))
1585 goto read_again; /* Try to read more data */
1586 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1591 case '\xe2': /* Unicode paragraph separator */
1592 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1594 line_length = nextchar - use_buf->str;
1599 case '\0': /* Embeded null in input */
1600 line_length = nextchar - use_buf->str;
1603 default: /* no match */
1609 /* If encoding != NULL, valid UTF-8, didn't overshoot */
1610 g_assert (nextchar == lastchar);
1614 if (status == G_IO_STATUS_EOF)
1616 if (channel->encoding && channel->read_buf->len > 0)
1618 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1619 _("Channel terminates in a partial character"));
1620 return G_IO_STATUS_ERROR;
1622 line_length = use_buf->len;
1627 if (use_buf->len > line_term_len - 1)
1628 checked_to = use_buf->len - (line_term_len - 1);
1636 *terminator_pos = line_length;
1639 *length = line_length + got_term_len;
1641 return G_IO_STATUS_NORMAL;
1645 * g_io_channel_read_to_end:
1646 * @channel: a #GIOChannel
1647 * @str_return: Location to store a pointer to a string holding
1648 * the remaining data in the #GIOChannel. This data should
1649 * be freed with g_free() when no longer needed. This
1650 * data is terminated by an extra nul character, but there
1651 * may be other nuls in the intervening data.
1652 * @length: Location to store length of the data
1653 * @error: A location to return an error of type #GConvertError
1654 * or #GIOChannelError
1656 * Reads all the remaining data from the file.
1658 * Return value: %G_IO_STATUS_NORMAL on success.
1659 * This function never returns %G_IO_STATUS_EOF.
1662 g_io_channel_read_to_end (GIOChannel *channel,
1669 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1670 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1672 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1679 if (!channel->use_buffer)
1681 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1682 _("Can't do a raw read in g_io_channel_read_to_end"));
1683 return G_IO_STATUS_ERROR;
1687 status = g_io_channel_fill_buffer (channel, error);
1688 while (status == G_IO_STATUS_NORMAL);
1690 if (status != G_IO_STATUS_EOF)
1693 if (channel->encoding && channel->read_buf->len > 0)
1695 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1696 _("Channel terminates in a partial character"));
1697 return G_IO_STATUS_ERROR;
1700 if (USE_BUF (channel) == NULL)
1702 /* length is already set to zero */
1704 *str_return = g_strdup ("");
1709 *length = USE_BUF (channel)->len;
1712 *str_return = g_string_free (USE_BUF (channel), FALSE);
1714 g_string_free (USE_BUF (channel), TRUE);
1716 if (channel->encoding)
1717 channel->encoded_read_buf = NULL;
1719 channel->read_buf = NULL;
1722 return G_IO_STATUS_NORMAL;
1726 * g_io_channel_read_chars:
1727 * @channel: a #GIOChannel
1728 * @buf: a buffer to read data into
1729 * @count: the size of the buffer. Note that the buffer may
1730 * not be complelely filled even if there is data
1731 * in the buffer if the remaining data is not a
1732 * complete character.
1733 * @bytes_read: The number of bytes read. This may be zero even on
1734 * success if count < 6 and the channel's encoding is non-%NULL.
1735 * This indicates that the next UTF-8 character is too wide for
1737 * @error: A location to return an error of type #GConvertError
1738 * or #GIOChannelError.
1740 * Replacement for g_io_channel_read() with the new API.
1742 * Return value: the status of the operation.
1745 g_io_channel_read_chars (GIOChannel *channel,
1754 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1755 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1757 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1762 return G_IO_STATUS_NORMAL;
1764 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1766 if (!channel->use_buffer)
1770 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1772 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
1775 *bytes_read = tmp_bytes;
1780 status = G_IO_STATUS_NORMAL;
1782 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1783 status = g_io_channel_fill_buffer (channel, error);
1785 /* Only return an error if we have no data */
1787 if (BUF_LEN (USE_BUF (channel)) == 0)
1789 g_assert (status != G_IO_STATUS_NORMAL);
1791 if (status == G_IO_STATUS_EOF && channel->encoding
1792 && BUF_LEN (channel->read_buf) > 0)
1794 g_set_error (error, G_CONVERT_ERROR,
1795 G_CONVERT_ERROR_PARTIAL_INPUT,
1796 _("Leftover unconverted data in read buffer"));
1797 status = G_IO_STATUS_ERROR;
1806 if (status == G_IO_STATUS_ERROR)
1807 g_clear_error (error);
1809 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1811 g_assert (got_bytes > 0);
1813 if (channel->encoding)
1814 /* Don't validate for NULL encoding, binary safe */
1816 gchar *nextchar, *prevchar;
1818 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1820 nextchar = channel->encoded_read_buf->str;
1824 prevchar = nextchar;
1825 nextchar = g_utf8_next_char (nextchar);
1826 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
1828 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1830 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1831 got_bytes = prevchar - channel->encoded_read_buf->str;
1833 g_assert (got_bytes > 0 || count < 6);
1836 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1837 g_string_erase (USE_BUF (channel), 0, got_bytes);
1840 *bytes_read = got_bytes;
1842 return G_IO_STATUS_NORMAL;
1846 * g_io_channel_read_unichar:
1847 * @channel: a #GIOChannel
1848 * @thechar: a location to return a character
1849 * @error: A location to return an error of type #GConvertError
1850 * or #GIOChannelError
1852 * This function cannot be called on a channel with %NULL encoding.
1854 * Return value: a #GIOStatus
1857 g_io_channel_read_unichar (GIOChannel *channel,
1861 GIOStatus status = G_IO_STATUS_NORMAL;
1863 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1864 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1865 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1867 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1869 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1870 status = g_io_channel_fill_buffer (channel, error);
1872 /* Only return an error if we have no data */
1874 if (BUF_LEN (USE_BUF (channel)) == 0)
1876 g_assert (status != G_IO_STATUS_NORMAL);
1878 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1880 g_set_error (error, G_CONVERT_ERROR,
1881 G_CONVERT_ERROR_PARTIAL_INPUT,
1882 _("Leftover unconverted data in read buffer"));
1883 status = G_IO_STATUS_ERROR;
1887 *thechar = (gunichar) -1;
1892 if (status == G_IO_STATUS_ERROR)
1893 g_clear_error (error);
1896 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1898 g_string_erase (channel->encoded_read_buf, 0,
1899 g_utf8_next_char (channel->encoded_read_buf->str)
1900 - channel->encoded_read_buf->str);
1902 return G_IO_STATUS_NORMAL;
1906 * g_io_channel_write_chars:
1907 * @channel: a #GIOChannel
1908 * @buf: a buffer to write data from
1909 * @count: the size of the buffer. If -1, the buffer
1910 * is taken to be a nul-terminated string.
1911 * @bytes_written: The number of bytes written. This can be nonzero
1912 * even if the return value is not %G_IO_STATUS_NORMAL.
1913 * If the return value is %G_IO_STATUS_NORMAL and the
1914 * channel is blocking, this will always be equal
1915 * to @count if @count >= 0.
1916 * @error: A location to return an error of type #GConvertError
1917 * or #GIOChannelError
1919 * Replacement for g_io_channel_write() with the new API.
1921 * On seekable channels with encodings other than %NULL or UTF-8, generic
1922 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
1923 * may only be made on a channel from which data has been read in the
1924 * cases described in the documentation for g_io_channel_set_encoding ().
1926 * Return value: the status of the operation.
1929 g_io_channel_write_chars (GIOChannel *channel,
1932 gsize *bytes_written,
1936 gssize wrote_bytes = 0;
1938 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1939 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1941 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1943 if ((count < 0) && buf)
1944 count = strlen (buf);
1950 return G_IO_STATUS_NORMAL;
1953 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1954 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1956 /* Raw write case */
1958 if (!channel->use_buffer)
1962 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1963 g_assert (channel->partial_write_buf[0] == '\0');
1965 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
1968 *bytes_written = tmp_bytes;
1975 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1976 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1978 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1980 g_warning("Mixed reading and writing not allowed on encoded files");
1981 return G_IO_STATUS_ERROR;
1983 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1984 if (status != G_IO_STATUS_NORMAL)
1992 if (!channel->write_buf)
1993 channel->write_buf = g_string_sized_new (channel->buf_size);
1995 while (wrote_bytes < count)
1999 /* If the buffer is full, try a write immediately. In
2000 * the nonblocking case, this prevents the user from
2001 * writing just a little bit to the buffer every time
2002 * and never receiving an EAGAIN.
2005 if (channel->write_buf->len >= channel->buf_size)
2007 gsize did_write = 0, this_time;
2011 status = channel->funcs->io_write (channel, channel->write_buf->str
2012 + did_write, channel->write_buf->len
2013 - did_write, &this_time, error);
2014 did_write += this_time;
2016 while (status == G_IO_STATUS_NORMAL &&
2017 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
2019 g_string_erase (channel->write_buf, 0, did_write);
2021 if (status != G_IO_STATUS_NORMAL)
2023 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
2024 status = G_IO_STATUS_NORMAL;
2026 *bytes_written = wrote_bytes;
2031 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
2032 - channel->write_buf->len; /* 1 for NULL */
2034 /* This is only true because g_io_channel_set_buffer_size ()
2035 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
2037 g_assert (space_in_buf >= MAX_CHAR_SIZE);
2039 if (!channel->encoding)
2041 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
2043 g_string_append_len (channel->write_buf, buf, write_this);
2045 wrote_bytes += write_this;
2049 const gchar *from_buf;
2050 gsize from_buf_len, from_buf_old_len, left_len;
2054 if (channel->partial_write_buf[0] != '\0')
2056 g_assert (wrote_bytes == 0);
2058 from_buf = channel->partial_write_buf;
2059 from_buf_old_len = strlen (channel->partial_write_buf);
2060 g_assert (from_buf_old_len > 0);
2061 from_buf_len = MIN (6, from_buf_old_len + count);
2063 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2064 from_buf_len - from_buf_old_len);
2069 from_buf_len = count - wrote_bytes;
2070 from_buf_old_len = 0;
2075 if (!channel->do_encode) /* UTF-8 encoding */
2077 const gchar *badchar;
2078 gsize try_len = MIN (from_buf_len, space_in_buf);
2080 /* UTF-8, just validate, emulate g_iconv */
2082 if (!g_utf8_validate (from_buf, try_len, &badchar))
2085 gsize incomplete_len = from_buf + try_len - badchar;
2087 left_len = from_buf + from_buf_len - badchar;
2089 try_char = g_utf8_get_char_validated (badchar, incomplete_len);
2094 g_assert (incomplete_len < 6);
2095 if (try_len == from_buf_len)
2107 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2108 /* FIXME bail here? */
2113 g_assert_not_reached ();
2115 errnum = 0; /* Don't confunse the compiler */
2122 left_len = from_buf_len - try_len;
2125 g_string_append_len (channel->write_buf, from_buf,
2126 from_buf_len - left_len);
2127 from_buf += from_buf_len - left_len;
2133 left_len = from_buf_len;
2134 g_string_set_size (channel->write_buf, channel->write_buf->len
2136 outbuf = channel->write_buf->str + channel->write_buf->len
2138 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2139 &outbuf, &space_in_buf);
2141 g_string_truncate (channel->write_buf, channel->write_buf->len
2145 if (err == (size_t) -1)
2150 g_assert (left_len < 6);
2152 if (from_buf_old_len == 0)
2154 /* Not from partial_write_buf */
2156 memcpy (channel->partial_write_buf, from_buf, left_len);
2157 channel->partial_write_buf[left_len] = '\0';
2159 *bytes_written = count;
2160 return G_IO_STATUS_NORMAL;
2163 /* Working in partial_write_buf */
2165 if (left_len == from_buf_len)
2167 /* Didn't convert anything, must still have
2168 * less than a full character
2171 g_assert (count == from_buf_len - from_buf_old_len);
2173 channel->partial_write_buf[from_buf_len] = '\0';
2176 *bytes_written = count;
2178 return G_IO_STATUS_NORMAL;
2181 g_assert (from_buf_len - left_len >= from_buf_old_len);
2183 /* We converted all the old data. This is fine */
2187 if (from_buf_len == left_len)
2189 /* Nothing was written, add enough space for
2190 * at least one character.
2192 space_in_buf += MAX_CHAR_SIZE;
2197 g_set_error (error, G_CONVERT_ERROR,
2198 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2199 _("Invalid byte sequence in conversion input"));
2200 if (from_buf_old_len > 0 && from_buf_len == left_len)
2201 g_warning ("Illegal sequence due to partial character "
2202 "at the end of a previous write.\n");
2204 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2206 *bytes_written = wrote_bytes;
2207 channel->partial_write_buf[0] = '\0';
2208 return G_IO_STATUS_ERROR;
2210 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2211 _("Error during conversion: %s"), g_strerror (errnum));
2212 if (from_buf_len >= left_len + from_buf_old_len)
2213 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2215 *bytes_written = wrote_bytes;
2216 channel->partial_write_buf[0] = '\0';
2217 return G_IO_STATUS_ERROR;
2221 g_assert (from_buf_len - left_len >= from_buf_old_len);
2223 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2225 if (from_buf_old_len > 0)
2227 /* We were working in partial_write_buf */
2229 buf += from_buf_len - left_len - from_buf_old_len;
2230 channel->partial_write_buf[0] = '\0';
2238 *bytes_written = count;
2240 return G_IO_STATUS_NORMAL;
2244 * g_io_channel_write_unichar:
2245 * @channel: a #GIOChannel
2246 * @thechar: a character
2247 * @error: A location to return an error of type #GConvertError
2248 * or #GIOChannelError
2250 * This function cannot be called on a channel with %NULL encoding.
2252 * Return value: a #GIOStatus
2255 g_io_channel_write_unichar (GIOChannel *channel,
2260 gchar static_buf[6];
2261 gsize char_len, wrote_len;
2263 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2264 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2265 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2267 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2269 char_len = g_unichar_to_utf8 (thechar, static_buf);
2271 if (channel->partial_write_buf[0] != '\0')
2273 g_warning ("Partial charater written before writing unichar.\n");
2274 channel->partial_write_buf[0] = '\0';
2277 status = g_io_channel_write_chars (channel, static_buf,
2278 char_len, &wrote_len, error);
2280 /* We validate UTF-8, so we can't get a partial write */
2282 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2288 * g_io_channel_error_quark:
2290 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2293 g_io_channel_error_quark (void)
2295 static GQuark q = 0;
2297 q = g_quark_from_static_string ("g-io-channel-error-quark");