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
48 #include "giochannel.h"
52 #define G_IO_NICE_BUF_SIZE 1024
54 /* This needs to be as wide as the largest character in any possible encoding */
55 #define MAX_CHAR_SIZE 10
57 /* Some simplifying macros, which reduce the need to worry whether the
58 * buffers have been allocated. These also make USE_BUF () an lvalue,
59 * which is used in g_io_channel_read_to_end ().
61 #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \
62 : (channel)->read_buf)
63 #define BUF_LEN(string) ((string) ? (string)->len : 0)
65 static GIOError g_io_error_get_from_g_error (GIOStatus status,
67 static void g_io_channel_purge (GIOChannel *channel);
68 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
70 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
72 gsize *terminator_pos,
76 g_io_channel_init (GIOChannel *channel)
78 channel->ref_count = 1;
79 channel->encoding = g_strdup ("UTF-8");
80 channel->line_term = NULL;
81 channel->line_term_len = 0;
82 channel->buf_size = G_IO_NICE_BUF_SIZE;
83 channel->read_cd = (GIConv) -1;
84 channel->write_cd = (GIConv) -1;
85 channel->read_buf = NULL; /* Lazy allocate buffers */
86 channel->encoded_read_buf = NULL;
87 channel->write_buf = NULL;
88 channel->partial_write_buf[0] = '\0';
89 channel->use_buffer = TRUE;
90 channel->do_encode = FALSE;
91 channel->close_on_unref = FALSE;
95 g_io_channel_ref (GIOChannel *channel)
97 g_return_if_fail (channel != NULL);
105 g_io_channel_unref (GIOChannel *channel)
107 g_return_if_fail (channel != NULL);
109 channel->ref_count--;
110 if (channel->ref_count == 0)
112 if (channel->close_on_unref)
113 g_io_channel_shutdown (channel, TRUE, NULL);
115 g_io_channel_purge (channel);
116 g_free (channel->encoding);
117 if (channel->read_cd != (GIConv) -1)
118 g_iconv_close (channel->read_cd);
119 if (channel->write_cd != (GIConv) -1)
120 g_iconv_close (channel->write_cd);
121 if (channel->line_term)
122 g_free (channel->line_term);
123 if (channel->read_buf)
124 g_string_free (channel->read_buf, TRUE);
125 if (channel->write_buf)
126 g_string_free (channel->write_buf, TRUE);
127 if (channel->encoded_read_buf)
128 g_string_free (channel->encoded_read_buf, TRUE);
129 channel->funcs->io_free (channel);
134 g_io_error_get_from_g_error (GIOStatus status,
139 case G_IO_STATUS_NORMAL:
140 case G_IO_STATUS_EOF:
141 return G_IO_ERROR_NONE;
142 case G_IO_STATUS_AGAIN:
143 return G_IO_ERROR_AGAIN;
144 case G_IO_STATUS_ERROR:
145 g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
147 if (err->domain != G_IO_CHANNEL_ERROR)
148 return G_IO_ERROR_UNKNOWN;
151 case G_IO_CHANNEL_ERROR_INVAL:
152 return G_IO_ERROR_INVAL;
154 return G_IO_ERROR_UNKNOWN;
157 g_assert_not_reached ();
158 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
164 * @channel: a #GIOChannel.
165 * @buf: a buffer to read the data into (which should be at least count bytes long).
166 * @count: the number of bytes to read from the #GIOChannel.
167 * @bytes_read: returns the number of bytes actually read.
169 * Reads data from a #GIOChannel.
171 * Return value: %G_IO_ERROR_NONE if the operation was successful.
173 * Deprecated: Use g_io_channel_read_chars() instead.
176 g_io_channel_read (GIOChannel *channel,
185 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
186 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
192 return G_IO_ERROR_NONE;
195 g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
197 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
199 error = g_io_error_get_from_g_error (status, err);
208 * g_io_channel_write:
209 * @channel: a #GIOChannel.
210 * @buf: the buffer containing the data to write.
211 * @count: the number of bytes to write.
212 * @bytes_written: the number of bytes actually written.
214 * Writes data to a #GIOChannel.
216 * Return value: %G_IO_ERROR_NONE if the operation was successful.
218 * Deprecated: Use g_io_channel_write_chars() instead.
221 g_io_channel_write (GIOChannel *channel,
224 gsize *bytes_written)
230 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
231 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
233 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
235 error = g_io_error_get_from_g_error (status, err);
245 * @channel: a #GIOChannel.
246 * @offset: an offset, in bytes, which is added to the position specified by @type
247 * @type: the position in the file, which can be %G_SEEK_CUR (the current
248 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
251 * Sets the current position in the #GIOChannel, similar to the standard library
254 * Return value: %G_IO_ERROR_NONE if the operation was successful.
256 * Deprecated: Use g_io_channel_seek_position() instead.
259 g_io_channel_seek (GIOChannel *channel,
267 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
268 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
277 g_warning ("g_io_channel_seek: unknown seek type");
278 return G_IO_ERROR_UNKNOWN;
281 status = channel->funcs->io_seek (channel, offset, type, &err);
283 error = g_io_error_get_from_g_error (status, err);
291 /* The function g_io_channel_new_file() is prototyped in both
292 * giounix.c and giowin32.c, so we stick its documentation here.
296 * g_io_channel_new_file:
297 * @filename: A string containing the name of a file.
298 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
299 * the same meaning as in fopen().
300 * @error: A location to return an error of type %G_FILE_ERROR.
302 * Open a file @filename as a #GIOChannel using mode @mode. This
303 * channel will be closed when the last reference to it is dropped,
304 * so there is no need to call g_io_channel_close() (though doing
305 * so will not cause problems, as long as no attempt is made to
306 * access the channel after it is closed).
308 * Return value: A #GIOChannel on success, %NULL on failure.
312 * g_io_channel_close:
313 * @channel: A #GIOChannel
315 * Close an IO channel. Any pending data to be written will be
316 * flushed, ignoring errors. The channel will not be freed until the
317 * last reference is dropped using g_io_channel_unref().
319 * Deprecated: Use g_io_channel_shutdown() instead.
322 g_io_channel_close (GIOChannel *channel)
326 g_return_if_fail (channel != NULL);
328 g_io_channel_purge (channel);
330 channel->funcs->io_close (channel, &err);
333 { /* No way to return the error */
334 g_warning ("Error closing channel: %s", err->message);
338 channel->close_on_unref = FALSE; /* Because we already did */
339 channel->is_readable = FALSE;
340 channel->is_writeable = FALSE;
341 channel->is_seekable = FALSE;
345 * g_io_channel_shutdown:
346 * @channel: a #GIOChannel
347 * @flush: if %TRUE, flush pending
348 * @err: location to store a #GIOChannelError
350 * Close an IO channel. Any pending data to be written will be
351 * flushed if @flush is %TRUE. The channel will not be freed until the
352 * last reference is dropped using g_io_channel_unref().
354 * Return value: the status of the operation.
357 g_io_channel_shutdown (GIOChannel *channel,
361 GIOStatus status, result;
362 GError *tmperr = NULL;
364 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
365 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
367 if (channel->write_buf && channel->write_buf->len > 0)
373 /* Set the channel to blocking, to avoid a busy loop
375 flags = g_io_channel_get_flags (channel);
376 /* Ignore any errors here, they're irrelevant */
377 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
379 result = g_io_channel_flush (channel, &tmperr);
382 result = G_IO_STATUS_NORMAL;
384 g_string_truncate(channel->write_buf, 0);
387 result = G_IO_STATUS_NORMAL;
389 if (channel->partial_write_buf[0] != '\0')
392 g_warning ("Partial character at end of write buffer not flushed.\n");
393 channel->partial_write_buf[0] = '\0';
396 status = channel->funcs->io_close (channel, err);
398 channel->close_on_unref = FALSE; /* Because we already did */
399 channel->is_readable = FALSE;
400 channel->is_writeable = FALSE;
401 channel->is_seekable = FALSE;
403 if (status != G_IO_STATUS_NORMAL)
405 g_clear_error (&tmperr);
408 else if (result != G_IO_STATUS_NORMAL)
410 g_propagate_error (err, tmperr);
414 return G_IO_STATUS_NORMAL;
417 /* This function is used for the final flush on close or unref */
419 g_io_channel_purge (GIOChannel *channel)
424 g_return_if_fail (channel != NULL);
426 if (channel->write_buf && channel->write_buf->len > 0)
430 /* Set the channel to blocking, to avoid a busy loop
432 flags = g_io_channel_get_flags (channel);
433 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
435 status = g_io_channel_flush (channel, &err);
438 { /* No way to return the error */
439 g_warning ("Error flushing string: %s", err->message);
444 /* Flush these in case anyone tries to close without unrefing */
446 if (channel->read_buf)
447 g_string_truncate (channel->read_buf, 0);
448 if (channel->write_buf)
449 g_string_truncate (channel->write_buf, 0);
450 if (channel->encoding)
452 if (channel->encoded_read_buf)
453 g_string_truncate (channel->encoded_read_buf, 0);
455 if (channel->partial_write_buf[0] != '\0')
457 g_warning ("Partial character at end of write buffer not flushed.\n");
458 channel->partial_write_buf[0] = '\0';
464 g_io_create_watch (GIOChannel *channel,
465 GIOCondition condition)
467 g_return_val_if_fail (channel != NULL, NULL);
469 return channel->funcs->io_create_watch (channel, condition);
473 g_io_add_watch_full (GIOChannel *channel,
475 GIOCondition condition,
478 GDestroyNotify notify)
483 g_return_val_if_fail (channel != NULL, 0);
485 source = g_io_create_watch (channel, condition);
487 if (priority != G_PRIORITY_DEFAULT)
488 g_source_set_priority (source, priority);
489 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
491 id = g_source_attach (source, NULL);
492 g_source_unref (source);
498 g_io_add_watch (GIOChannel *channel,
499 GIOCondition condition,
503 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
507 * g_io_channel_get_buffer_condition:
508 * @channel: A #GIOChannel
510 * This function returns a #GIOCondition depending on whether there
511 * is data to be read/space to write data in the
512 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
513 * %G_IO_OUT may be set.
515 * Return value: A #GIOCondition
518 g_io_channel_get_buffer_condition (GIOChannel *channel)
520 GIOCondition condition = 0;
522 if (channel->encoding)
524 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
525 condition |= G_IO_IN; /* Only return if we have full characters */
529 if (channel->read_buf && (channel->read_buf->len > 0))
530 condition |= G_IO_IN;
533 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
534 condition |= G_IO_OUT;
540 * g_io_channel_error_from_errno:
541 * @en: an <literal>errno</literal> error number, e.g. %EINVAL.
543 * Converts an <literal>errno</literal> error number to a #GIOChannelError.
545 * Return value: a #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL.
548 g_io_channel_error_from_errno (gint en)
551 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
558 g_warning("Invalid file descriptor.\n");
559 return G_IO_CHANNEL_ERROR_FAILED;
564 g_warning("Buffer outside valid address space.\n");
565 return G_IO_CHANNEL_ERROR_FAILED;
570 return G_IO_CHANNEL_ERROR_FBIG;
574 /* In general, we should catch EINTR before we get here,
575 * but close() is allowed to return EINTR by POSIX, so
576 * we need to catch it here; EINTR from close() is
577 * unrecoverable, because it's undefined whether
578 * the fd was actually closed or not, so we just return
579 * a generic error code.
582 return G_IO_CHANNEL_ERROR_FAILED;
587 return G_IO_CHANNEL_ERROR_INVAL;
592 return G_IO_CHANNEL_ERROR_IO;
597 return G_IO_CHANNEL_ERROR_ISDIR;
602 return G_IO_CHANNEL_ERROR_NOSPC;
607 return G_IO_CHANNEL_ERROR_NXIO;
612 return G_IO_CHANNEL_ERROR_OVERFLOW;
617 return G_IO_CHANNEL_ERROR_PIPE;
621 return G_IO_CHANNEL_ERROR_FAILED;
626 * g_io_channel_set_buffer_size:
627 * @channel: a #GIOChannel
628 * @size: the size of the buffer. 0 == pick a good size
630 * Sets the buffer size.
633 g_io_channel_set_buffer_size (GIOChannel *channel,
636 g_return_if_fail (channel != NULL);
639 size = G_IO_NICE_BUF_SIZE;
641 if (size < MAX_CHAR_SIZE)
642 size = MAX_CHAR_SIZE;
644 channel->buf_size = size;
648 * g_io_channel_get_buffer_size:
649 * @channel: a #GIOChannel
651 * Gets the buffer size.
653 * Return value: the size of the buffer.
656 g_io_channel_get_buffer_size (GIOChannel *channel)
658 g_return_val_if_fail (channel != NULL, 0);
660 return channel->buf_size;
664 * g_io_channel_set_line_term:
665 * @channel: a #GIOChannel
666 * @line_term: The line termination string. Use %NULL for auto detect.
667 * Auto detection breaks on "\n", "\r\n", "\r", "\0", and
668 * the Unicode paragraph separator. Auto detection should
669 * not be used for anything other than file-based channels.
670 * @length: The length of the termination string. If -1 is passed, the
671 * string is assumed to be nul-terminated. This option allows
672 * termination strings with embeded nuls.
674 * This sets the string that #GIOChannel uses to determine
675 * where in the file a line break occurs.
678 g_io_channel_set_line_term (GIOChannel *channel,
679 const gchar *line_term,
682 g_return_if_fail (channel != NULL);
683 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
685 if (line_term == NULL)
688 length = strlen (line_term);
690 if (channel->line_term)
691 g_free (channel->line_term);
692 channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
693 channel->line_term_len = length;
697 * g_io_channel_get_line_term:
698 * @channel: a #GIOChannel
699 * @length: a location to return the length of the line terminator
701 * This returns the string that #GIOChannel uses to determine
702 * where in the file a line break occurs. A value of %NULL
703 * indicates auto detection.
705 * Return value: The line termination string. This value
706 * is owned by GLib and must not be freed.
708 G_CONST_RETURN gchar*
709 g_io_channel_get_line_term (GIOChannel *channel,
712 g_return_val_if_fail (channel != NULL, NULL);
715 *length = channel->line_term_len;
717 return channel->line_term;
721 * g_io_channel_set_flags:
722 * @channel: a #GIOChannel.
723 * @flags: the flags to set on the IO channel.
724 * @error: A location to return an error of type #GIOChannelError.
726 * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK).
728 * Return value: the status of the operation.
731 g_io_channel_set_flags (GIOChannel *channel,
735 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
736 g_return_val_if_fail ((error == NULL) || (*error == NULL),
739 return (* channel->funcs->io_set_flags)(channel,
740 flags & G_IO_FLAG_SET_MASK,
745 * g_io_channel_get_flags:
746 * @channel: a #GIOChannel
748 * Gets the current flags for a #GIOChannel, including read-only
749 * flags such as %G_IO_FLAG_IS_READABLE.
751 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
752 * are cached for internal use by the channel when it is created.
753 * If they should change at some later point (e.g. partial shutdown
754 * of a socket with the UNIX shutdown() function), the user
755 * should immediately call g_io_channel_get_flags () to update
756 * the internal values of these flags.
758 * Return value: the flags which are set on the channel
761 g_io_channel_get_flags (GIOChannel *channel)
765 g_return_val_if_fail (channel != NULL, 0);
767 flags = (* channel->funcs->io_get_flags) (channel);
769 /* Cross implementation code */
771 if (channel->is_seekable)
772 flags |= G_IO_FLAG_IS_SEEKABLE;
773 if (channel->is_readable)
774 flags |= G_IO_FLAG_IS_READABLE;
775 if (channel->is_writeable)
776 flags |= G_IO_FLAG_IS_WRITEABLE;
782 * g_io_channel_set_close_on_unref:
783 * @channel: a #GIOChannel
784 * @do_close: Whether to close the channel on the final unref of
785 * the GIOChannel data structure. The default value of
786 * this is %TRUE for channels created by g_io_channel_new_file (),
787 * and %FALSE for all other channels.
789 * Setting this flag to %TRUE for a channel you have already closed
790 * can cause problems.
793 g_io_channel_set_close_on_unref (GIOChannel *channel,
796 g_return_if_fail (channel != NULL);
798 channel->close_on_unref = do_close;
802 * g_io_channel_get_close_on_unref:
803 * @channel: a #GIOChannel.
805 * Returns whether the file/socket/whatever associated with @channel
806 * will be closed when @channel receives its final unref and is
807 * destroyed. The default value of this is %TRUE for channels created
808 * by g_io_channel_new_file (), and %FALSE for all other channels.
810 * Return value: Whether the channel will be closed on the final unref of
811 * the GIOChannel data structure.
814 g_io_channel_get_close_on_unref (GIOChannel *channel)
816 g_return_val_if_fail (channel != NULL, FALSE);
818 return channel->close_on_unref;
822 * g_io_channel_seek_position:
823 * @channel: a #GIOChannel
824 * @offset: The offset in bytes from the position specified by @type
825 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
826 * cases where a call to g_io_channel_set_encoding ()
827 * is allowed. See the documentation for
828 * g_io_channel_set_encoding () for details.
829 * @error: A location to return an error of type #GIOChannelError
831 * Replacement for g_io_channel_seek() with the new API.
833 * Return value: the status of the operation.
836 g_io_channel_seek_position (GIOChannel* channel,
843 /* For files, only one of the read and write buffers can contain data.
844 * For sockets, both can contain data.
847 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
848 g_return_val_if_fail ((error == NULL) || (*error == NULL),
850 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
854 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
855 if (channel->use_buffer)
857 if (channel->do_encode && channel->encoded_read_buf
858 && channel->encoded_read_buf->len > 0)
860 g_warning ("Seek type G_SEEK_CUR not allowed for this"
861 " channel's encoding.\n");
862 return G_IO_STATUS_ERROR;
864 if (channel->read_buf)
865 offset -= channel->read_buf->len;
866 if (channel->encoded_read_buf)
868 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
870 /* If there's anything here, it's because the encoding is UTF-8,
871 * so we can just subtract the buffer length, the same as for
872 * the unencoded data.
875 offset -= channel->encoded_read_buf->len;
883 g_warning ("g_io_channel_seek_position: unknown seek type");
884 return G_IO_STATUS_ERROR;
887 if (channel->use_buffer)
889 status = g_io_channel_flush (channel, error);
890 if (status != G_IO_STATUS_NORMAL)
894 status = channel->funcs->io_seek (channel, offset, type, error);
896 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
898 if (channel->read_buf)
899 g_string_truncate (channel->read_buf, 0);
901 /* Conversion state no longer matches position in file */
902 if (channel->read_cd != (GIConv) -1)
903 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
904 if (channel->write_cd != (GIConv) -1)
905 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
907 if (channel->encoded_read_buf)
909 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
910 g_string_truncate (channel->encoded_read_buf, 0);
913 if (channel->partial_write_buf[0] != '\0')
915 g_warning ("Partial character at end of write buffer not flushed.\n");
916 channel->partial_write_buf[0] = '\0';
924 * g_io_channel_flush:
925 * @channel: a #GIOChannel
926 * @error: location to store an error of type #GIOChannelError
928 * Flushes the write buffer for the GIOChannel.
930 * Return value: the status of the operation: One of
931 * #G_IO_CHANNEL_NORMAL, #G_IO_CHANNEL_AGAIN, or
932 * #G_IO_CHANNEL_ERROR.
935 g_io_channel_flush (GIOChannel *channel,
939 gsize this_time = 1, bytes_written = 0;
941 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
942 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
944 if (channel->write_buf == NULL || channel->write_buf->len == 0)
945 return G_IO_STATUS_NORMAL;
949 g_assert (this_time > 0);
951 status = channel->funcs->io_write (channel,
952 channel->write_buf->str + bytes_written,
953 channel->write_buf->len - bytes_written,
955 bytes_written += this_time;
957 while ((bytes_written < channel->write_buf->len)
958 && (status == G_IO_STATUS_NORMAL));
960 g_string_erase (channel->write_buf, 0, bytes_written);
966 * g_io_channel_set_buffered:
967 * @channel: a #GIOChannel
968 * @buffered: whether to set the channel buffered or unbuffered
970 * The buffering state can only be set if the channel's encoding
971 * is %NULL. For any other encoding, the channel must be buffered.
973 * A buffered channel can only be set unbuffered if the channel's
974 * internal buffers have been flushed. Newly created channels or
975 * channels which have returned %G_IO_STATUS_EOF
976 * not require such a flush. For write-only channels, a call to
977 * g_io_channel_flush () is sufficient. For all other channels,
978 * the buffers may be flushed by a call to g_io_channel_seek_position ().
979 * This includes the possibility of seeking with seek type %G_SEEK_CUR
980 * and an offset of zero. Note that this means that socket-based
981 * channels cannot be set unbuffered once they have had data
984 * On unbuffered channels, it is safe to mix read and write
985 * calls from the new and old APIs, if this is necessary for
986 * maintaining old code.
988 * The default state of the channel is buffered.
991 g_io_channel_set_buffered (GIOChannel *channel,
994 g_return_if_fail (channel != NULL);
996 if (channel->encoding != NULL)
998 g_warning ("Need to have NULL encoding to set the buffering state of the "
1003 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
1004 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
1006 channel->use_buffer = buffered;
1010 * g_io_channel_get_buffered:
1011 * @channel: a #GIOChannel.
1013 * Returns whether @channel is buffered.
1015 * Return Value: %TRUE if the @channel is buffered.
1018 g_io_channel_get_buffered (GIOChannel *channel)
1020 g_return_val_if_fail (channel != NULL, FALSE);
1022 return channel->use_buffer;
1026 * g_io_channel_set_encoding:
1027 * @channel: a #GIOChannel
1028 * @encoding: the encoding type
1029 * @error: location to store an error of type #GConvertError.
1031 * Sets the encoding for the input/output of the channel. The internal
1032 * encoding is always UTF-8. The default encoding for the
1033 * external file is UTF-8.
1035 * The encoding %NULL is safe to use with binary data.
1037 * The encoding can only be set if one of the following conditions
1040 * 1. The channel was just created, and has not been written to
1043 * 2. The channel is write-only.
1045 * 3. The channel is a file, and the file pointer was just
1046 * repositioned by a call to g_io_channel_seek_position().
1047 * (This flushes all the internal buffers.)
1049 * 4. The current encoding is %NULL or UTF-8.
1051 * 5. One of the (new API) read functions has just returned %G_IO_STATUS_EOF
1052 * (or, in the case of g_io_channel_read_to_end (), %G_IO_STATUS_NORMAL).
1054 * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar ()
1055 * has returned %G_IO_STATUS_AGAIN or %G_IO_STATUS_ERROR. This may be
1056 * useful in the case of %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1057 * Returning one of these statuses from g_io_channel_read_line (),
1058 * g_io_channel_read_line_string (), or g_io_channel_read_to_end ()
1059 * does <emphasis>not</emphasis> guarantee that the encoding can be changed.
1061 * Channels which do not meet one of the above conditions cannot call
1062 * g_io_channel_seek_position () with an offset of %G_SEEK_CUR,
1063 * and, if they are "seekable", cannot
1064 * call g_io_channel_write_chars () after calling one
1065 * of the API "read" functions.
1067 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set.
1070 g_io_channel_set_encoding (GIOChannel *channel,
1071 const gchar *encoding,
1074 GIConv read_cd, write_cd;
1075 gboolean did_encode;
1077 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1078 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1080 /* Make sure the encoded buffers are empty */
1082 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1083 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1085 if (!channel->use_buffer)
1087 g_warning ("Need to set the channel buffered before setting the encoding.\n");
1088 g_warning ("Assuming this is what you meant and acting accordingly.\n");
1090 channel->use_buffer = TRUE;
1093 if (channel->partial_write_buf[0] != '\0')
1095 g_warning ("Partial character at end of write buffer not flushed.\n");
1096 channel->partial_write_buf[0] = '\0';
1099 did_encode = channel->do_encode;
1101 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1103 channel->do_encode = FALSE;
1104 read_cd = write_cd = (GIConv) -1;
1109 const gchar *from_enc = NULL, *to_enc = NULL;
1111 if (channel->is_readable)
1113 read_cd = g_iconv_open ("UTF-8", encoding);
1115 if (read_cd == (GIConv) -1)
1123 read_cd = (GIConv) -1;
1125 if (channel->is_writeable && err == 0)
1127 write_cd = g_iconv_open (encoding, "UTF-8");
1129 if (write_cd == (GIConv) -1)
1132 from_enc = encoding;
1137 write_cd = (GIConv) -1;
1141 g_assert (from_enc);
1145 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1146 _("Conversion from character set `%s' to `%s' is not supported"),
1149 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1150 _("Could not open converter from `%s' to `%s': %s"),
1151 from_enc, to_enc, g_strerror (err));
1153 if (read_cd != (GIConv) -1)
1154 g_iconv_close (read_cd);
1155 if (write_cd != (GIConv) -1)
1156 g_iconv_close (write_cd);
1158 return G_IO_STATUS_ERROR;
1161 channel->do_encode = TRUE;
1164 /* The encoding is ok, so set the fields in channel */
1166 if (channel->read_cd != (GIConv) -1)
1167 g_iconv_close (channel->read_cd);
1168 if (channel->write_cd != (GIConv) -1)
1169 g_iconv_close (channel->write_cd);
1171 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1173 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1175 /* This is just validated UTF-8, so we can copy it back into read_buf
1176 * so it can be encoded in whatever the new encoding is.
1179 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1180 channel->encoded_read_buf->len);
1181 g_string_truncate (channel->encoded_read_buf, 0);
1184 channel->read_cd = read_cd;
1185 channel->write_cd = write_cd;
1187 g_free (channel->encoding);
1188 channel->encoding = g_strdup (encoding);
1190 return G_IO_STATUS_NORMAL;
1194 * g_io_channel_get_encoding:
1195 * @channel: a #GIOChannel
1197 * Gets the encoding for the input/output of the channel. The internal
1198 * encoding is always UTF-8. The encoding %NULL makes the
1199 * channel safe for binary data.
1201 * Return value: A string containing the encoding, this string is
1202 * owned by GLib and must not be freed.
1204 G_CONST_RETURN gchar*
1205 g_io_channel_get_encoding (GIOChannel *channel)
1207 g_return_val_if_fail (channel != NULL, NULL);
1209 return channel->encoding;
1213 g_io_channel_fill_buffer (GIOChannel *channel,
1216 gsize read_size, cur_len, oldlen;
1219 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1221 status = g_io_channel_flush (channel, err);
1222 if (status != G_IO_STATUS_NORMAL)
1225 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1227 g_warning ("Partial character at end of write buffer not flushed.\n");
1228 channel->partial_write_buf[0] = '\0';
1231 if (!channel->read_buf)
1232 channel->read_buf = g_string_sized_new (channel->buf_size);
1234 cur_len = channel->read_buf->len;
1236 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1238 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1239 channel->buf_size, &read_size, err);
1241 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1243 g_string_truncate (channel->read_buf, read_size + cur_len);
1245 if ((status != G_IO_STATUS_NORMAL)
1246 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1249 g_assert (channel->read_buf->len > 0);
1251 if (channel->encoded_read_buf)
1252 oldlen = channel->encoded_read_buf->len;
1256 if (channel->encoding)
1257 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1260 if (channel->do_encode)
1262 size_t errnum, inbytes_left, outbytes_left;
1263 gchar *inbuf, *outbuf;
1266 g_assert (channel->encoded_read_buf);
1270 inbytes_left = channel->read_buf->len;
1271 outbytes_left = MAX (channel->read_buf->len,
1272 channel->encoded_read_buf->allocated_len
1273 - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1274 outbytes_left = MAX (outbytes_left, 6);
1276 inbuf = channel->read_buf->str;
1277 g_string_set_size (channel->encoded_read_buf,
1278 channel->encoded_read_buf->len + outbytes_left);
1279 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1282 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1283 &outbuf, &outbytes_left);
1286 g_assert (inbuf + inbytes_left == channel->read_buf->str
1287 + channel->read_buf->len);
1288 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1289 + channel->encoded_read_buf->len);
1291 g_string_erase (channel->read_buf, 0,
1292 channel->read_buf->len - inbytes_left);
1293 g_string_truncate (channel->encoded_read_buf,
1294 channel->encoded_read_buf->len - outbytes_left);
1296 if (errnum == (size_t) -1)
1301 if ((oldlen == channel->encoded_read_buf->len)
1302 && (status == G_IO_STATUS_EOF))
1303 status = G_IO_STATUS_EOF;
1305 status = G_IO_STATUS_NORMAL;
1308 /* Buffer size at least 6, wrote at least on character */
1309 g_assert (inbuf != channel->read_buf->str);
1312 if (oldlen < channel->encoded_read_buf->len)
1313 status = G_IO_STATUS_NORMAL;
1316 g_set_error (err, G_CONVERT_ERROR,
1317 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1318 _("Invalid byte sequence in conversion input"));
1319 return G_IO_STATUS_ERROR;
1323 g_assert (errval != EBADF); /* The converter should be open */
1324 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1325 _("Error during conversion: %s"), g_strerror (errval));
1326 return G_IO_STATUS_ERROR;
1329 g_assert ((status != G_IO_STATUS_NORMAL)
1330 || (channel->encoded_read_buf->len > 0));
1332 else if (channel->encoding) /* UTF-8 */
1334 gchar *nextchar, *lastchar;
1336 g_assert (channel->encoded_read_buf);
1338 nextchar = channel->read_buf->str;
1339 lastchar = channel->read_buf->str + channel->read_buf->len;
1341 while (nextchar < lastchar)
1345 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1350 /* stop, leave partial character in buffer */
1351 lastchar = nextchar;
1354 if (oldlen < channel->encoded_read_buf->len)
1355 status = G_IO_STATUS_NORMAL;
1358 g_set_error (err, G_CONVERT_ERROR,
1359 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1360 _("Invalid byte sequence in conversion input"));
1361 status = G_IO_STATUS_ERROR;
1363 lastchar = nextchar;
1366 nextchar = g_utf8_next_char (nextchar);
1371 if (lastchar > channel->read_buf->str)
1373 gint copy_len = lastchar - channel->read_buf->str;
1375 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1377 g_string_erase (channel->read_buf, 0, copy_len);
1385 * g_io_channel_read_line:
1386 * @channel: a #GIOChannel
1387 * @str_return: The line read from the #GIOChannel, including the
1388 * line terminator. This data should be freed with g_free()
1389 * when no longer needed. This is a nul-terminated string.
1390 * If a @length of zero is returned, this will be %NULL instead.
1391 * @length: location to store length of the read data, or %NULL
1392 * @terminator_pos: location to store position of line terminator, or %NULL
1393 * @error: A location to return an error of type #GConvertError
1394 * or #GIOChannelError
1396 * Reads a line, including the terminating character(s),
1397 * from a #GIOChannel into a newly-allocated string.
1398 * @str_return will contain allocated memory if the return
1399 * is %G_IO_STATUS_NORMAL.
1401 * Return value: the status of the operation.
1404 g_io_channel_read_line (GIOChannel *channel,
1407 gsize *terminator_pos,
1413 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1414 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1415 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1417 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1419 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1422 *length = got_length;
1424 if (status == G_IO_STATUS_NORMAL)
1426 g_assert (USE_BUF (channel));
1427 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1428 g_string_erase (USE_BUF (channel), 0, got_length);
1437 * g_io_channel_read_line_string:
1438 * @channel: a #GIOChannel
1439 * @buffer: a #GString into which the line will be written.
1440 * If @buffer already contains data, the old data will
1442 * @terminator_pos: location to store position of line terminator, or %NULL
1443 * @error: a location to store an error of type #GConvertError
1444 * or #GIOChannelError
1446 * Reads a line from a #GIOChannel, using a #GString as a buffer.
1448 * Return value: the status of the operation.
1451 g_io_channel_read_line_string (GIOChannel *channel,
1453 gsize *terminator_pos,
1459 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1460 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1461 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1463 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1465 if (buffer->len > 0)
1466 g_string_truncate (buffer, 0); /* clear out the buffer */
1468 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1470 if (status == G_IO_STATUS_NORMAL)
1472 g_assert (USE_BUF (channel));
1473 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1474 g_string_erase (USE_BUF (channel), 0, length);
1482 g_io_channel_read_line_backend (GIOChannel *channel,
1484 gsize *terminator_pos,
1488 gsize checked_to, line_term_len, line_length, got_term_len;
1489 gboolean first_time = TRUE;
1491 if (!channel->use_buffer)
1493 /* Can't do a raw read in read_line */
1494 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1495 _("Can't do a raw read in g_io_channel_read_line_string"));
1496 return G_IO_STATUS_ERROR;
1499 status = G_IO_STATUS_NORMAL;
1501 if (channel->line_term)
1502 line_term_len = channel->line_term_len;
1505 /* This value used for setting checked_to, it's the longest of the four
1506 * we autodetect for.
1513 gchar *nextchar, *lastchar;
1516 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1519 status = g_io_channel_fill_buffer (channel, error);
1522 case G_IO_STATUS_NORMAL:
1523 if (BUF_LEN (USE_BUF (channel)) == 0)
1524 /* Can happen when using conversion and only read
1525 * part of a character
1532 case G_IO_STATUS_EOF:
1533 if (BUF_LEN (USE_BUF (channel)) == 0)
1538 if (channel->encoding && channel->read_buf->len != 0)
1540 g_set_error (error, G_CONVERT_ERROR,
1541 G_CONVERT_ERROR_PARTIAL_INPUT,
1542 _("Leftover unconverted data in read buffer"));
1543 return G_IO_STATUS_ERROR;
1546 return G_IO_STATUS_EOF;
1556 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1558 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1562 lastchar = use_buf->str + use_buf->len;
1564 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1565 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1567 if (channel->line_term)
1569 if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
1571 line_length = nextchar - use_buf->str;
1572 got_term_len = line_term_len;
1576 else /* auto detect */
1580 case '\n': /* unix */
1581 line_length = nextchar - use_buf->str;
1584 case '\r': /* Warning: do not use with sockets */
1585 line_length = nextchar - use_buf->str;
1586 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1587 && (lastchar == use_buf->str + use_buf->len))
1588 goto read_again; /* Try to read more data */
1589 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1594 case '\xe2': /* Unicode paragraph separator */
1595 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1597 line_length = nextchar - use_buf->str;
1602 case '\0': /* Embeded null in input */
1603 line_length = nextchar - use_buf->str;
1606 default: /* no match */
1612 /* If encoding != NULL, valid UTF-8, didn't overshoot */
1613 g_assert (nextchar == lastchar);
1617 if (status == G_IO_STATUS_EOF)
1619 if (channel->encoding && channel->read_buf->len > 0)
1621 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1622 _("Channel terminates in a partial character"));
1623 return G_IO_STATUS_ERROR;
1625 line_length = use_buf->len;
1630 if (use_buf->len > line_term_len - 1)
1631 checked_to = use_buf->len - (line_term_len - 1);
1639 *terminator_pos = line_length;
1642 *length = line_length + got_term_len;
1644 return G_IO_STATUS_NORMAL;
1648 * g_io_channel_read_to_end:
1649 * @channel: a #GIOChannel
1650 * @str_return: Location to store a pointer to a string holding
1651 * the remaining data in the #GIOChannel. This data should
1652 * be freed with g_free() when no longer needed. This
1653 * data is terminated by an extra nul character, but there
1654 * may be other nuls in the intervening data.
1655 * @length: Location to store length of the data
1656 * @error: A location to return an error of type #GConvertError
1657 * or #GIOChannelError
1659 * Reads all the remaining data from the file.
1661 * Return value: %G_IO_STATUS_NORMAL on success.
1662 * This function never returns %G_IO_STATUS_EOF.
1665 g_io_channel_read_to_end (GIOChannel *channel,
1672 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1673 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1675 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1682 if (!channel->use_buffer)
1684 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1685 _("Can't do a raw read in g_io_channel_read_to_end"));
1686 return G_IO_STATUS_ERROR;
1690 status = g_io_channel_fill_buffer (channel, error);
1691 while (status == G_IO_STATUS_NORMAL);
1693 if (status != G_IO_STATUS_EOF)
1696 if (channel->encoding && channel->read_buf->len > 0)
1698 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1699 _("Channel terminates in a partial character"));
1700 return G_IO_STATUS_ERROR;
1703 if (USE_BUF (channel) == NULL)
1705 /* length is already set to zero */
1707 *str_return = g_strdup ("");
1712 *length = USE_BUF (channel)->len;
1715 *str_return = g_string_free (USE_BUF (channel), FALSE);
1717 g_string_free (USE_BUF (channel), TRUE);
1719 if (channel->encoding)
1720 channel->encoded_read_buf = NULL;
1722 channel->read_buf = NULL;
1725 return G_IO_STATUS_NORMAL;
1729 * g_io_channel_read_chars:
1730 * @channel: a #GIOChannel
1731 * @buf: a buffer to read data into
1732 * @count: the size of the buffer. Note that the buffer may
1733 * not be complelely filled even if there is data
1734 * in the buffer if the remaining data is not a
1735 * complete character.
1736 * @bytes_read: The number of bytes read. This may be zero even on
1737 * success if count < 6 and the channel's encoding is non-%NULL.
1738 * This indicates that the next UTF-8 character is too wide for
1740 * @error: A location to return an error of type #GConvertError
1741 * or #GIOChannelError.
1743 * Replacement for g_io_channel_read() with the new API.
1745 * Return value: the status of the operation.
1748 g_io_channel_read_chars (GIOChannel *channel,
1757 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1758 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1760 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1765 return G_IO_STATUS_NORMAL;
1767 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1769 if (!channel->use_buffer)
1773 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1775 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
1778 *bytes_read = tmp_bytes;
1783 status = G_IO_STATUS_NORMAL;
1785 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1786 status = g_io_channel_fill_buffer (channel, error);
1788 /* Only return an error if we have no data */
1790 if (BUF_LEN (USE_BUF (channel)) == 0)
1792 g_assert (status != G_IO_STATUS_NORMAL);
1794 if (status == G_IO_STATUS_EOF && channel->encoding
1795 && BUF_LEN (channel->read_buf) > 0)
1797 g_set_error (error, G_CONVERT_ERROR,
1798 G_CONVERT_ERROR_PARTIAL_INPUT,
1799 _("Leftover unconverted data in read buffer"));
1800 status = G_IO_STATUS_ERROR;
1809 if (status == G_IO_STATUS_ERROR)
1810 g_clear_error (error);
1812 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1814 g_assert (got_bytes > 0);
1816 if (channel->encoding)
1817 /* Don't validate for NULL encoding, binary safe */
1819 gchar *nextchar, *prevchar;
1821 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1823 nextchar = channel->encoded_read_buf->str;
1827 prevchar = nextchar;
1828 nextchar = g_utf8_next_char (nextchar);
1829 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
1831 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1833 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1834 got_bytes = prevchar - channel->encoded_read_buf->str;
1836 g_assert (got_bytes > 0 || count < 6);
1839 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1840 g_string_erase (USE_BUF (channel), 0, got_bytes);
1843 *bytes_read = got_bytes;
1845 return G_IO_STATUS_NORMAL;
1849 * g_io_channel_read_unichar:
1850 * @channel: a #GIOChannel
1851 * @thechar: a location to return a character
1852 * @error: A location to return an error of type #GConvertError
1853 * or #GIOChannelError
1855 * This function cannot be called on a channel with %NULL encoding.
1857 * Return value: a #GIOStatus
1860 g_io_channel_read_unichar (GIOChannel *channel,
1864 GIOStatus status = G_IO_STATUS_NORMAL;
1866 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1867 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1868 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1870 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1872 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1873 status = g_io_channel_fill_buffer (channel, error);
1875 /* Only return an error if we have no data */
1877 if (BUF_LEN (USE_BUF (channel)) == 0)
1879 g_assert (status != G_IO_STATUS_NORMAL);
1881 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1883 g_set_error (error, G_CONVERT_ERROR,
1884 G_CONVERT_ERROR_PARTIAL_INPUT,
1885 _("Leftover unconverted data in read buffer"));
1886 status = G_IO_STATUS_ERROR;
1890 *thechar = (gunichar) -1;
1895 if (status == G_IO_STATUS_ERROR)
1896 g_clear_error (error);
1899 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1901 g_string_erase (channel->encoded_read_buf, 0,
1902 g_utf8_next_char (channel->encoded_read_buf->str)
1903 - channel->encoded_read_buf->str);
1905 return G_IO_STATUS_NORMAL;
1909 * g_io_channel_write_chars:
1910 * @channel: a #GIOChannel
1911 * @buf: a buffer to write data from
1912 * @count: the size of the buffer. If -1, the buffer
1913 * is taken to be a nul-terminated string.
1914 * @bytes_written: The number of bytes written. This can be nonzero
1915 * even if the return value is not %G_IO_STATUS_NORMAL.
1916 * If the return value is %G_IO_STATUS_NORMAL and the
1917 * channel is blocking, this will always be equal
1918 * to @count if @count >= 0.
1919 * @error: A location to return an error of type #GConvertError
1920 * or #GIOChannelError
1922 * Replacement for g_io_channel_write() with the new API.
1924 * On seekable channels with encodings other than %NULL or UTF-8, generic
1925 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
1926 * may only be made on a channel from which data has been read in the
1927 * cases described in the documentation for g_io_channel_set_encoding ().
1929 * Return value: the status of the operation.
1932 g_io_channel_write_chars (GIOChannel *channel,
1935 gsize *bytes_written,
1939 gssize wrote_bytes = 0;
1941 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1942 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1944 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1946 if ((count < 0) && buf)
1947 count = strlen (buf);
1953 return G_IO_STATUS_NORMAL;
1956 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1957 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1959 /* Raw write case */
1961 if (!channel->use_buffer)
1965 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1966 g_assert (channel->partial_write_buf[0] == '\0');
1968 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
1971 *bytes_written = tmp_bytes;
1978 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1979 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1981 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1983 g_warning("Mixed reading and writing not allowed on encoded files");
1984 return G_IO_STATUS_ERROR;
1986 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1987 if (status != G_IO_STATUS_NORMAL)
1995 if (!channel->write_buf)
1996 channel->write_buf = g_string_sized_new (channel->buf_size);
1998 while (wrote_bytes < count)
2002 /* If the buffer is full, try a write immediately. In
2003 * the nonblocking case, this prevents the user from
2004 * writing just a little bit to the buffer every time
2005 * and never receiving an EAGAIN.
2008 if (channel->write_buf->len >= channel->buf_size)
2010 gsize did_write = 0, this_time;
2014 status = channel->funcs->io_write (channel, channel->write_buf->str
2015 + did_write, channel->write_buf->len
2016 - did_write, &this_time, error);
2017 did_write += this_time;
2019 while (status == G_IO_STATUS_NORMAL &&
2020 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
2022 g_string_erase (channel->write_buf, 0, did_write);
2024 if (status != G_IO_STATUS_NORMAL)
2026 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
2027 status = G_IO_STATUS_NORMAL;
2029 *bytes_written = wrote_bytes;
2034 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
2035 - channel->write_buf->len; /* 1 for NULL */
2037 /* This is only true because g_io_channel_set_buffer_size ()
2038 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
2040 g_assert (space_in_buf >= MAX_CHAR_SIZE);
2042 if (!channel->encoding)
2044 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
2046 g_string_append_len (channel->write_buf, buf, write_this);
2048 wrote_bytes += write_this;
2052 const gchar *from_buf;
2053 gsize from_buf_len, from_buf_old_len, left_len;
2057 if (channel->partial_write_buf[0] != '\0')
2059 g_assert (wrote_bytes == 0);
2061 from_buf = channel->partial_write_buf;
2062 from_buf_old_len = strlen (channel->partial_write_buf);
2063 g_assert (from_buf_old_len > 0);
2064 from_buf_len = MIN (6, from_buf_old_len + count);
2066 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2067 from_buf_len - from_buf_old_len);
2072 from_buf_len = count - wrote_bytes;
2073 from_buf_old_len = 0;
2078 if (!channel->do_encode) /* UTF-8 encoding */
2080 const gchar *badchar;
2081 gsize try_len = MIN (from_buf_len, space_in_buf);
2083 /* UTF-8, just validate, emulate g_iconv */
2085 if (!g_utf8_validate (from_buf, try_len, &badchar))
2088 gsize incomplete_len = from_buf + try_len - badchar;
2090 left_len = from_buf + from_buf_len - badchar;
2092 try_char = g_utf8_get_char_validated (badchar, incomplete_len);
2097 g_assert (incomplete_len < 6);
2098 if (try_len == from_buf_len)
2110 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2111 /* FIXME bail here? */
2116 g_assert_not_reached ();
2118 errnum = 0; /* Don't confunse the compiler */
2125 left_len = from_buf_len - try_len;
2128 g_string_append_len (channel->write_buf, from_buf,
2129 from_buf_len - left_len);
2130 from_buf += from_buf_len - left_len;
2136 left_len = from_buf_len;
2137 g_string_set_size (channel->write_buf, channel->write_buf->len
2139 outbuf = channel->write_buf->str + channel->write_buf->len
2141 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2142 &outbuf, &space_in_buf);
2144 g_string_truncate (channel->write_buf, channel->write_buf->len
2148 if (err == (size_t) -1)
2153 g_assert (left_len < 6);
2155 if (from_buf_old_len == 0)
2157 /* Not from partial_write_buf */
2159 memcpy (channel->partial_write_buf, from_buf, left_len);
2160 channel->partial_write_buf[left_len] = '\0';
2162 *bytes_written = count;
2163 return G_IO_STATUS_NORMAL;
2166 /* Working in partial_write_buf */
2168 if (left_len == from_buf_len)
2170 /* Didn't convert anything, must still have
2171 * less than a full character
2174 g_assert (count == from_buf_len - from_buf_old_len);
2176 channel->partial_write_buf[from_buf_len] = '\0';
2179 *bytes_written = count;
2181 return G_IO_STATUS_NORMAL;
2184 g_assert (from_buf_len - left_len >= from_buf_old_len);
2186 /* We converted all the old data. This is fine */
2190 if (from_buf_len == left_len)
2192 /* Nothing was written, add enough space for
2193 * at least one character.
2195 space_in_buf += MAX_CHAR_SIZE;
2200 g_set_error (error, G_CONVERT_ERROR,
2201 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2202 _("Invalid byte sequence in conversion input"));
2203 if (from_buf_old_len > 0 && from_buf_len == left_len)
2204 g_warning ("Illegal sequence due to partial character "
2205 "at the end of a previous write.\n");
2207 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2209 *bytes_written = wrote_bytes;
2210 channel->partial_write_buf[0] = '\0';
2211 return G_IO_STATUS_ERROR;
2213 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2214 _("Error during conversion: %s"), g_strerror (errnum));
2215 if (from_buf_len >= left_len + from_buf_old_len)
2216 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2218 *bytes_written = wrote_bytes;
2219 channel->partial_write_buf[0] = '\0';
2220 return G_IO_STATUS_ERROR;
2224 g_assert (from_buf_len - left_len >= from_buf_old_len);
2226 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2228 if (from_buf_old_len > 0)
2230 /* We were working in partial_write_buf */
2232 buf += from_buf_len - left_len - from_buf_old_len;
2233 channel->partial_write_buf[0] = '\0';
2241 *bytes_written = count;
2243 return G_IO_STATUS_NORMAL;
2247 * g_io_channel_write_unichar:
2248 * @channel: a #GIOChannel
2249 * @thechar: a character
2250 * @error: A location to return an error of type #GConvertError
2251 * or #GIOChannelError
2253 * This function cannot be called on a channel with %NULL encoding.
2255 * Return value: a #GIOStatus
2258 g_io_channel_write_unichar (GIOChannel *channel,
2263 gchar static_buf[6];
2264 gsize char_len, wrote_len;
2266 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2267 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2268 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2270 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2272 char_len = g_unichar_to_utf8 (thechar, static_buf);
2274 if (channel->partial_write_buf[0] != '\0')
2276 g_warning ("Partial charater written before writing unichar.\n");
2277 channel->partial_write_buf[0] = '\0';
2280 status = g_io_channel_write_chars (channel, static_buf,
2281 char_len, &wrote_len, error);
2283 /* We validate UTF-8, so we can't get a partial write */
2285 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2291 * g_io_channel_error_quark:
2293 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2296 g_io_channel_error_quark (void)
2298 static GQuark q = 0;
2300 q = g_quark_from_static_string ("g-io-channel-error-quark");