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"
53 #define G_IO_NICE_BUF_SIZE 1024
55 /* This needs to be as wide as the largest character in any possible encoding */
56 #define MAX_CHAR_SIZE 10
58 /* Some simplifying macros, which reduce the need to worry whether the
59 * buffers have been allocated. These also make USE_BUF () an lvalue,
60 * which is used in g_io_channel_read_to_end ().
62 #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \
63 : (channel)->read_buf)
64 #define BUF_LEN(string) ((string) ? (string)->len : 0)
66 static GIOError g_io_error_get_from_g_error (GIOStatus status,
68 static void g_io_channel_purge (GIOChannel *channel);
69 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
71 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
73 gsize *terminator_pos,
77 g_io_channel_init (GIOChannel *channel)
79 channel->ref_count = 1;
80 channel->encoding = g_strdup ("UTF-8");
81 channel->line_term = NULL;
82 channel->line_term_len = 0;
83 channel->buf_size = G_IO_NICE_BUF_SIZE;
84 channel->read_cd = (GIConv) -1;
85 channel->write_cd = (GIConv) -1;
86 channel->read_buf = NULL; /* Lazy allocate buffers */
87 channel->encoded_read_buf = NULL;
88 channel->write_buf = NULL;
89 channel->partial_write_buf[0] = '\0';
90 channel->use_buffer = TRUE;
91 channel->do_encode = FALSE;
92 channel->close_on_unref = FALSE;
96 g_io_channel_ref (GIOChannel *channel)
98 g_return_val_if_fail (channel != NULL, NULL);
100 g_atomic_int_inc (&channel->ref_count);
106 g_io_channel_unref (GIOChannel *channel)
110 g_return_if_fail (channel != NULL);
112 is_zero = g_atomic_int_dec_and_test (&channel->ref_count);
114 if (G_UNLIKELY (is_zero))
116 if (channel->close_on_unref)
117 g_io_channel_shutdown (channel, TRUE, NULL);
119 g_io_channel_purge (channel);
120 g_free (channel->encoding);
121 if (channel->read_cd != (GIConv) -1)
122 g_iconv_close (channel->read_cd);
123 if (channel->write_cd != (GIConv) -1)
124 g_iconv_close (channel->write_cd);
125 g_free (channel->line_term);
126 if (channel->read_buf)
127 g_string_free (channel->read_buf, TRUE);
128 if (channel->write_buf)
129 g_string_free (channel->write_buf, TRUE);
130 if (channel->encoded_read_buf)
131 g_string_free (channel->encoded_read_buf, TRUE);
132 channel->funcs->io_free (channel);
137 g_io_error_get_from_g_error (GIOStatus status,
142 case G_IO_STATUS_NORMAL:
143 case G_IO_STATUS_EOF:
144 return G_IO_ERROR_NONE;
145 case G_IO_STATUS_AGAIN:
146 return G_IO_ERROR_AGAIN;
147 case G_IO_STATUS_ERROR:
148 g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
150 if (err->domain != G_IO_CHANNEL_ERROR)
151 return G_IO_ERROR_UNKNOWN;
154 case G_IO_CHANNEL_ERROR_INVAL:
155 return G_IO_ERROR_INVAL;
157 return G_IO_ERROR_UNKNOWN;
160 g_assert_not_reached ();
161 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
167 * @channel: a #GIOChannel
168 * @buf: a buffer to read the data into (which should be at least
170 * @count: the number of bytes to read from the #GIOChannel
171 * @bytes_read: returns the number of bytes actually read
173 * Reads data from a #GIOChannel.
175 * Return value: %G_IO_ERROR_NONE if the operation was successful.
177 * Deprecated:2.2: Use g_io_channel_read_chars() instead.
180 g_io_channel_read (GIOChannel *channel,
189 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
190 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
196 return G_IO_ERROR_NONE;
199 g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
201 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
203 error = g_io_error_get_from_g_error (status, err);
212 * g_io_channel_write:
213 * @channel: a #GIOChannel
214 * @buf: the buffer containing the data to write
215 * @count: the number of bytes to write
216 * @bytes_written: the number of bytes actually written
218 * Writes data to a #GIOChannel.
220 * Return value: %G_IO_ERROR_NONE if the operation was successful.
222 * Deprecated:2.2: Use g_io_channel_write_chars() instead.
225 g_io_channel_write (GIOChannel *channel,
228 gsize *bytes_written)
234 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
235 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
237 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
239 error = g_io_error_get_from_g_error (status, err);
249 * @channel: a #GIOChannel
250 * @offset: an offset, in bytes, which is added to the position specified
252 * @type: the position in the file, which can be %G_SEEK_CUR (the current
253 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END
254 * (the end of the file)
256 * Sets the current position in the #GIOChannel, similar to the standard
257 * library function fseek().
259 * Return value: %G_IO_ERROR_NONE if the operation was successful.
261 * Deprecated:2.2: Use g_io_channel_seek_position() instead.
264 g_io_channel_seek (GIOChannel *channel,
272 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
273 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
282 g_warning ("g_io_channel_seek: unknown seek type");
283 return G_IO_ERROR_UNKNOWN;
286 status = channel->funcs->io_seek (channel, offset, type, &err);
288 error = g_io_error_get_from_g_error (status, err);
296 /* The function g_io_channel_new_file() is prototyped in both
297 * giounix.c and giowin32.c, so we stick its documentation here.
301 * g_io_channel_new_file:
302 * @filename: A string containing the name of a file
303 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
304 * the same meaning as in fopen()
305 * @error: A location to return an error of type %G_FILE_ERROR
307 * Open a file @filename as a #GIOChannel using mode @mode. This
308 * channel will be closed when the last reference to it is dropped,
309 * so there is no need to call g_io_channel_close() (though doing
310 * so will not cause problems, as long as no attempt is made to
311 * access the channel after it is closed).
313 * Return value: A #GIOChannel on success, %NULL on failure.
317 * g_io_channel_close:
318 * @channel: A #GIOChannel
320 * Close an IO channel. Any pending data to be written will be
321 * flushed, ignoring errors. The channel will not be freed until the
322 * last reference is dropped using g_io_channel_unref().
324 * Deprecated:2.2: Use g_io_channel_shutdown() instead.
327 g_io_channel_close (GIOChannel *channel)
331 g_return_if_fail (channel != NULL);
333 g_io_channel_purge (channel);
335 channel->funcs->io_close (channel, &err);
338 { /* No way to return the error */
339 g_warning ("Error closing channel: %s", err->message);
343 channel->close_on_unref = FALSE; /* Because we already did */
344 channel->is_readable = FALSE;
345 channel->is_writeable = FALSE;
346 channel->is_seekable = FALSE;
350 * g_io_channel_shutdown:
351 * @channel: a #GIOChannel
352 * @flush: if %TRUE, flush pending
353 * @err: location to store a #GIOChannelError
355 * Close an IO channel. Any pending data to be written will be
356 * flushed if @flush is %TRUE. The channel will not be freed until the
357 * last reference is dropped using g_io_channel_unref().
359 * Return value: the status of the operation.
362 g_io_channel_shutdown (GIOChannel *channel,
366 GIOStatus status, result;
367 GError *tmperr = NULL;
369 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
370 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
372 if (channel->write_buf && channel->write_buf->len > 0)
378 /* Set the channel to blocking, to avoid a busy loop
380 flags = g_io_channel_get_flags (channel);
381 /* Ignore any errors here, they're irrelevant */
382 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
384 result = g_io_channel_flush (channel, &tmperr);
387 result = G_IO_STATUS_NORMAL;
389 g_string_truncate(channel->write_buf, 0);
392 result = G_IO_STATUS_NORMAL;
394 if (channel->partial_write_buf[0] != '\0')
397 g_warning ("Partial character at end of write buffer not flushed.\n");
398 channel->partial_write_buf[0] = '\0';
401 status = channel->funcs->io_close (channel, err);
403 channel->close_on_unref = FALSE; /* Because we already did */
404 channel->is_readable = FALSE;
405 channel->is_writeable = FALSE;
406 channel->is_seekable = FALSE;
408 if (status != G_IO_STATUS_NORMAL)
410 g_clear_error (&tmperr);
413 else if (result != G_IO_STATUS_NORMAL)
415 g_propagate_error (err, tmperr);
419 return G_IO_STATUS_NORMAL;
422 /* This function is used for the final flush on close or unref */
424 g_io_channel_purge (GIOChannel *channel)
429 g_return_if_fail (channel != NULL);
431 if (channel->write_buf && channel->write_buf->len > 0)
435 /* Set the channel to blocking, to avoid a busy loop
437 flags = g_io_channel_get_flags (channel);
438 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
440 status = g_io_channel_flush (channel, &err);
443 { /* No way to return the error */
444 g_warning ("Error flushing string: %s", err->message);
449 /* Flush these in case anyone tries to close without unrefing */
451 if (channel->read_buf)
452 g_string_truncate (channel->read_buf, 0);
453 if (channel->write_buf)
454 g_string_truncate (channel->write_buf, 0);
455 if (channel->encoding)
457 if (channel->encoded_read_buf)
458 g_string_truncate (channel->encoded_read_buf, 0);
460 if (channel->partial_write_buf[0] != '\0')
462 g_warning ("Partial character at end of write buffer not flushed.\n");
463 channel->partial_write_buf[0] = '\0';
469 g_io_create_watch (GIOChannel *channel,
470 GIOCondition condition)
472 g_return_val_if_fail (channel != NULL, NULL);
474 return channel->funcs->io_create_watch (channel, condition);
478 g_io_add_watch_full (GIOChannel *channel,
480 GIOCondition condition,
483 GDestroyNotify notify)
488 g_return_val_if_fail (channel != NULL, 0);
490 source = g_io_create_watch (channel, condition);
492 if (priority != G_PRIORITY_DEFAULT)
493 g_source_set_priority (source, priority);
494 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
496 id = g_source_attach (source, NULL);
497 g_source_unref (source);
503 g_io_add_watch (GIOChannel *channel,
504 GIOCondition condition,
508 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
512 * g_io_channel_get_buffer_condition:
513 * @channel: A #GIOChannel
515 * This function returns a #GIOCondition depending on whether there
516 * is data to be read/space to write data in the internal buffers in
517 * the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
519 * Return value: A #GIOCondition
522 g_io_channel_get_buffer_condition (GIOChannel *channel)
524 GIOCondition condition = 0;
526 if (channel->encoding)
528 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
529 condition |= G_IO_IN; /* Only return if we have full characters */
533 if (channel->read_buf && (channel->read_buf->len > 0))
534 condition |= G_IO_IN;
537 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
538 condition |= G_IO_OUT;
544 * g_io_channel_error_from_errno:
545 * @en: an <literal>errno</literal> error number, e.g. %EINVAL
547 * Converts an <literal>errno</literal> error number to a #GIOChannelError.
549 * Return value: a #GIOChannelError error number, e.g.
550 * %G_IO_CHANNEL_ERROR_INVAL.
553 g_io_channel_error_from_errno (gint en)
556 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
563 g_warning("Invalid file descriptor.\n");
564 return G_IO_CHANNEL_ERROR_FAILED;
569 g_warning("Buffer outside valid address space.\n");
570 return G_IO_CHANNEL_ERROR_FAILED;
575 return G_IO_CHANNEL_ERROR_FBIG;
579 /* In general, we should catch EINTR before we get here,
580 * but close() is allowed to return EINTR by POSIX, so
581 * we need to catch it here; EINTR from close() is
582 * unrecoverable, because it's undefined whether
583 * the fd was actually closed or not, so we just return
584 * a generic error code.
587 return G_IO_CHANNEL_ERROR_FAILED;
592 return G_IO_CHANNEL_ERROR_INVAL;
597 return G_IO_CHANNEL_ERROR_IO;
602 return G_IO_CHANNEL_ERROR_ISDIR;
607 return G_IO_CHANNEL_ERROR_NOSPC;
612 return G_IO_CHANNEL_ERROR_NXIO;
617 return G_IO_CHANNEL_ERROR_OVERFLOW;
622 return G_IO_CHANNEL_ERROR_PIPE;
626 return G_IO_CHANNEL_ERROR_FAILED;
631 * g_io_channel_set_buffer_size:
632 * @channel: a #GIOChannel
633 * @size: the size of the buffer, or 0 to let GLib pick a good size
635 * Sets the buffer size.
638 g_io_channel_set_buffer_size (GIOChannel *channel,
641 g_return_if_fail (channel != NULL);
644 size = G_IO_NICE_BUF_SIZE;
646 if (size < MAX_CHAR_SIZE)
647 size = MAX_CHAR_SIZE;
649 channel->buf_size = size;
653 * g_io_channel_get_buffer_size:
654 * @channel: a #GIOChannel
656 * Gets the buffer size.
658 * Return value: the size of the buffer.
661 g_io_channel_get_buffer_size (GIOChannel *channel)
663 g_return_val_if_fail (channel != NULL, 0);
665 return channel->buf_size;
669 * g_io_channel_set_line_term:
670 * @channel: a #GIOChannel
671 * @line_term: The line termination string. Use %NULL for autodetect.
672 * Autodetection breaks on "\n", "\r\n", "\r", "\0", and
673 * the Unicode paragraph separator. Autodetection should
674 * not be used for anything other than file-based channels.
675 * @length: The length of the termination string. If -1 is passed, the
676 * string is assumed to be nul-terminated. This option allows
677 * termination strings with embedded nuls.
679 * This sets the string that #GIOChannel uses to determine
680 * where in the file a line break occurs.
683 g_io_channel_set_line_term (GIOChannel *channel,
684 const gchar *line_term,
687 g_return_if_fail (channel != NULL);
688 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
690 if (line_term == NULL)
693 length = strlen (line_term);
695 g_free (channel->line_term);
696 channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
697 channel->line_term_len = length;
701 * g_io_channel_get_line_term:
702 * @channel: a #GIOChannel
703 * @length: a location to return the length of the line terminator
705 * This returns the string that #GIOChannel uses to determine
706 * where in the file a line break occurs. A value of %NULL
707 * indicates autodetection.
709 * Return value: The line termination string. This value
710 * is owned by GLib and must not be freed.
712 G_CONST_RETURN gchar*
713 g_io_channel_get_line_term (GIOChannel *channel,
716 g_return_val_if_fail (channel != NULL, NULL);
719 *length = channel->line_term_len;
721 return channel->line_term;
725 * g_io_channel_set_flags:
726 * @channel: a #GIOChannel
727 * @flags: the flags to set on the IO channel
728 * @error: A location to return an error of type #GIOChannelError
730 * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK).
732 * Return value: the status of the operation.
735 g_io_channel_set_flags (GIOChannel *channel,
739 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
740 g_return_val_if_fail ((error == NULL) || (*error == NULL),
743 return (*channel->funcs->io_set_flags) (channel,
744 flags & G_IO_FLAG_SET_MASK,
749 * g_io_channel_get_flags:
750 * @channel: a #GIOChannel
752 * Gets the current flags for a #GIOChannel, including read-only
753 * flags such as %G_IO_FLAG_IS_READABLE.
755 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
756 * are cached for internal use by the channel when it is created.
757 * If they should change at some later point (e.g. partial shutdown
758 * of a socket with the UNIX shutdown() function), the user
759 * should immediately call g_io_channel_get_flags() to update
760 * the internal values of these flags.
762 * Return value: the flags which are set on the channel
765 g_io_channel_get_flags (GIOChannel *channel)
769 g_return_val_if_fail (channel != NULL, 0);
771 flags = (* channel->funcs->io_get_flags) (channel);
773 /* Cross implementation code */
775 if (channel->is_seekable)
776 flags |= G_IO_FLAG_IS_SEEKABLE;
777 if (channel->is_readable)
778 flags |= G_IO_FLAG_IS_READABLE;
779 if (channel->is_writeable)
780 flags |= G_IO_FLAG_IS_WRITEABLE;
786 * g_io_channel_set_close_on_unref:
787 * @channel: a #GIOChannel
788 * @do_close: Whether to close the channel on the final unref of
789 * the GIOChannel data structure. The default value of
790 * this is %TRUE for channels created by g_io_channel_new_file (),
791 * and %FALSE for all other channels.
793 * Setting this flag to %TRUE for a channel you have already closed
794 * can cause problems.
797 g_io_channel_set_close_on_unref (GIOChannel *channel,
800 g_return_if_fail (channel != NULL);
802 channel->close_on_unref = do_close;
806 * g_io_channel_get_close_on_unref:
807 * @channel: a #GIOChannel.
809 * Returns whether the file/socket/whatever associated with @channel
810 * will be closed when @channel receives its final unref and is
811 * destroyed. The default value of this is %TRUE for channels created
812 * by g_io_channel_new_file (), and %FALSE for all other channels.
814 * Return value: Whether the channel will be closed on the final unref of
815 * the GIOChannel data structure.
818 g_io_channel_get_close_on_unref (GIOChannel *channel)
820 g_return_val_if_fail (channel != NULL, FALSE);
822 return channel->close_on_unref;
826 * g_io_channel_seek_position:
827 * @channel: a #GIOChannel
828 * @offset: The offset in bytes from the position specified by @type
829 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
830 * cases where a call to g_io_channel_set_encoding ()
831 * is allowed. See the documentation for
832 * g_io_channel_set_encoding () for details.
833 * @error: A location to return an error of type #GIOChannelError
835 * Replacement for g_io_channel_seek() with the new API.
837 * Return value: the status of the operation.
840 g_io_channel_seek_position (GIOChannel *channel,
847 /* For files, only one of the read and write buffers can contain data.
848 * For sockets, both can contain data.
851 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
852 g_return_val_if_fail ((error == NULL) || (*error == NULL),
854 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
858 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
859 if (channel->use_buffer)
861 if (channel->do_encode && channel->encoded_read_buf
862 && channel->encoded_read_buf->len > 0)
864 g_warning ("Seek type G_SEEK_CUR not allowed for this"
865 " channel's encoding.\n");
866 return G_IO_STATUS_ERROR;
868 if (channel->read_buf)
869 offset -= channel->read_buf->len;
870 if (channel->encoded_read_buf)
872 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
874 /* If there's anything here, it's because the encoding is UTF-8,
875 * so we can just subtract the buffer length, the same as for
876 * the unencoded data.
879 offset -= channel->encoded_read_buf->len;
887 g_warning ("g_io_channel_seek_position: unknown seek type");
888 return G_IO_STATUS_ERROR;
891 if (channel->use_buffer)
893 status = g_io_channel_flush (channel, error);
894 if (status != G_IO_STATUS_NORMAL)
898 status = channel->funcs->io_seek (channel, offset, type, error);
900 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
902 if (channel->read_buf)
903 g_string_truncate (channel->read_buf, 0);
905 /* Conversion state no longer matches position in file */
906 if (channel->read_cd != (GIConv) -1)
907 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
908 if (channel->write_cd != (GIConv) -1)
909 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
911 if (channel->encoded_read_buf)
913 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
914 g_string_truncate (channel->encoded_read_buf, 0);
917 if (channel->partial_write_buf[0] != '\0')
919 g_warning ("Partial character at end of write buffer not flushed.\n");
920 channel->partial_write_buf[0] = '\0';
928 * g_io_channel_flush:
929 * @channel: a #GIOChannel
930 * @error: location to store an error of type #GIOChannelError
932 * Flushes the write buffer for the GIOChannel.
934 * Return value: the status of the operation: One of
935 * #G_IO_CHANNEL_NORMAL, #G_IO_CHANNEL_AGAIN, or
936 * #G_IO_CHANNEL_ERROR.
939 g_io_channel_flush (GIOChannel *channel,
943 gsize this_time = 1, bytes_written = 0;
945 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
946 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
948 if (channel->write_buf == NULL || channel->write_buf->len == 0)
949 return G_IO_STATUS_NORMAL;
953 g_assert (this_time > 0);
955 status = channel->funcs->io_write (channel,
956 channel->write_buf->str + bytes_written,
957 channel->write_buf->len - bytes_written,
959 bytes_written += this_time;
961 while ((bytes_written < channel->write_buf->len)
962 && (status == G_IO_STATUS_NORMAL));
964 g_string_erase (channel->write_buf, 0, bytes_written);
970 * g_io_channel_set_buffered:
971 * @channel: a #GIOChannel
972 * @buffered: whether to set the channel buffered or unbuffered
974 * The buffering state can only be set if the channel's encoding
975 * is %NULL. For any other encoding, the channel must be buffered.
977 * A buffered channel can only be set unbuffered if the channel's
978 * internal buffers have been flushed. Newly created channels or
979 * channels which have returned %G_IO_STATUS_EOF
980 * not require such a flush. For write-only channels, a call to
981 * g_io_channel_flush () is sufficient. For all other channels,
982 * the buffers may be flushed by a call to g_io_channel_seek_position ().
983 * This includes the possibility of seeking with seek type %G_SEEK_CUR
984 * and an offset of zero. Note that this means that socket-based
985 * channels cannot be set unbuffered once they have had data
988 * On unbuffered channels, it is safe to mix read and write
989 * calls from the new and old APIs, if this is necessary for
990 * maintaining old code.
992 * The default state of the channel is buffered.
995 g_io_channel_set_buffered (GIOChannel *channel,
998 g_return_if_fail (channel != NULL);
1000 if (channel->encoding != NULL)
1002 g_warning ("Need to have NULL encoding to set the buffering state of the "
1007 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
1008 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
1010 channel->use_buffer = buffered;
1014 * g_io_channel_get_buffered:
1015 * @channel: a #GIOChannel
1017 * Returns whether @channel is buffered.
1019 * Return Value: %TRUE if the @channel is buffered.
1022 g_io_channel_get_buffered (GIOChannel *channel)
1024 g_return_val_if_fail (channel != NULL, FALSE);
1026 return channel->use_buffer;
1030 * g_io_channel_set_encoding:
1031 * @channel: a #GIOChannel
1032 * @encoding: the encoding type
1033 * @error: location to store an error of type #GConvertError
1035 * Sets the encoding for the input/output of the channel.
1036 * The internal encoding is always UTF-8. The default encoding
1037 * for the external file is UTF-8.
1039 * The encoding %NULL is safe to use with binary data.
1041 * The encoding can only be set if one of the following conditions
1045 * The channel was just created, and has not been written to or read
1047 * </para></listitem>
1049 * The channel is write-only.
1050 * </para></listitem>
1052 * The channel is a file, and the file pointer was just
1053 * repositioned by a call to g_io_channel_seek_position().
1054 * (This flushes all the internal buffers.)
1055 * </para></listitem>
1057 * The current encoding is %NULL or UTF-8.
1058 * </para></listitem>
1060 * One of the (new API) read functions has just returned %G_IO_STATUS_EOF
1061 * (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
1062 * </para></listitem>
1064 * One of the functions g_io_channel_read_chars() or
1065 * g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or
1066 * %G_IO_STATUS_ERROR. This may be useful in the case of
1067 * %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1068 * Returning one of these statuses from g_io_channel_read_line(),
1069 * g_io_channel_read_line_string(), or g_io_channel_read_to_end()
1070 * does <emphasis>not</emphasis> guarantee that the encoding can
1072 * </para></listitem>
1074 * Channels which do not meet one of the above conditions cannot call
1075 * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if
1076 * they are "seekable", cannot call g_io_channel_write_chars() after
1077 * calling one of the API "read" functions.
1079 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set.
1082 g_io_channel_set_encoding (GIOChannel *channel,
1083 const gchar *encoding,
1086 GIConv read_cd, write_cd;
1087 gboolean did_encode;
1089 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1090 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1092 /* Make sure the encoded buffers are empty */
1094 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1095 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1097 if (!channel->use_buffer)
1099 g_warning ("Need to set the channel buffered before setting the encoding.\n");
1100 g_warning ("Assuming this is what you meant and acting accordingly.\n");
1102 channel->use_buffer = TRUE;
1105 if (channel->partial_write_buf[0] != '\0')
1107 g_warning ("Partial character at end of write buffer not flushed.\n");
1108 channel->partial_write_buf[0] = '\0';
1111 did_encode = channel->do_encode;
1113 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1115 channel->do_encode = FALSE;
1116 read_cd = write_cd = (GIConv) -1;
1121 const gchar *from_enc = NULL, *to_enc = NULL;
1123 if (channel->is_readable)
1125 read_cd = g_iconv_open ("UTF-8", encoding);
1127 if (read_cd == (GIConv) -1)
1130 from_enc = encoding;
1135 read_cd = (GIConv) -1;
1137 if (channel->is_writeable && err == 0)
1139 write_cd = g_iconv_open (encoding, "UTF-8");
1141 if (write_cd == (GIConv) -1)
1149 write_cd = (GIConv) -1;
1153 g_assert (from_enc);
1157 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1158 _("Conversion from character set '%s' to '%s' is not supported"),
1161 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1162 _("Could not open converter from '%s' to '%s': %s"),
1163 from_enc, to_enc, g_strerror (err));
1165 if (read_cd != (GIConv) -1)
1166 g_iconv_close (read_cd);
1167 if (write_cd != (GIConv) -1)
1168 g_iconv_close (write_cd);
1170 return G_IO_STATUS_ERROR;
1173 channel->do_encode = TRUE;
1176 /* The encoding is ok, so set the fields in channel */
1178 if (channel->read_cd != (GIConv) -1)
1179 g_iconv_close (channel->read_cd);
1180 if (channel->write_cd != (GIConv) -1)
1181 g_iconv_close (channel->write_cd);
1183 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1185 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1187 /* This is just validated UTF-8, so we can copy it back into read_buf
1188 * so it can be encoded in whatever the new encoding is.
1191 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1192 channel->encoded_read_buf->len);
1193 g_string_truncate (channel->encoded_read_buf, 0);
1196 channel->read_cd = read_cd;
1197 channel->write_cd = write_cd;
1199 g_free (channel->encoding);
1200 channel->encoding = g_strdup (encoding);
1202 return G_IO_STATUS_NORMAL;
1206 * g_io_channel_get_encoding:
1207 * @channel: a #GIOChannel
1209 * Gets the encoding for the input/output of the channel.
1210 * The internal encoding is always UTF-8. The encoding %NULL
1211 * makes the channel safe for binary data.
1213 * Return value: A string containing the encoding, this string is
1214 * owned by GLib and must not be freed.
1216 G_CONST_RETURN gchar*
1217 g_io_channel_get_encoding (GIOChannel *channel)
1219 g_return_val_if_fail (channel != NULL, NULL);
1221 return channel->encoding;
1225 g_io_channel_fill_buffer (GIOChannel *channel,
1228 gsize read_size, cur_len, oldlen;
1231 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1233 status = g_io_channel_flush (channel, err);
1234 if (status != G_IO_STATUS_NORMAL)
1237 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1239 g_warning ("Partial character at end of write buffer not flushed.\n");
1240 channel->partial_write_buf[0] = '\0';
1243 if (!channel->read_buf)
1244 channel->read_buf = g_string_sized_new (channel->buf_size);
1246 cur_len = channel->read_buf->len;
1248 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1250 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1251 channel->buf_size, &read_size, err);
1253 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1255 g_string_truncate (channel->read_buf, read_size + cur_len);
1257 if ((status != G_IO_STATUS_NORMAL) &&
1258 ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1261 g_assert (channel->read_buf->len > 0);
1263 if (channel->encoded_read_buf)
1264 oldlen = channel->encoded_read_buf->len;
1268 if (channel->encoding)
1269 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1272 if (channel->do_encode)
1274 gsize errnum, inbytes_left, outbytes_left;
1275 gchar *inbuf, *outbuf;
1278 g_assert (channel->encoded_read_buf);
1282 inbytes_left = channel->read_buf->len;
1283 outbytes_left = MAX (channel->read_buf->len,
1284 channel->encoded_read_buf->allocated_len
1285 - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1286 outbytes_left = MAX (outbytes_left, 6);
1288 inbuf = channel->read_buf->str;
1289 g_string_set_size (channel->encoded_read_buf,
1290 channel->encoded_read_buf->len + outbytes_left);
1291 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1294 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1295 &outbuf, &outbytes_left);
1298 g_assert (inbuf + inbytes_left == channel->read_buf->str
1299 + channel->read_buf->len);
1300 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1301 + channel->encoded_read_buf->len);
1303 g_string_erase (channel->read_buf, 0,
1304 channel->read_buf->len - inbytes_left);
1305 g_string_truncate (channel->encoded_read_buf,
1306 channel->encoded_read_buf->len - outbytes_left);
1308 if (errnum == (gsize) -1)
1313 if ((oldlen == channel->encoded_read_buf->len)
1314 && (status == G_IO_STATUS_EOF))
1315 status = G_IO_STATUS_EOF;
1317 status = G_IO_STATUS_NORMAL;
1320 /* Buffer size at least 6, wrote at least on character */
1321 g_assert (inbuf != channel->read_buf->str);
1324 if (oldlen < channel->encoded_read_buf->len)
1325 status = G_IO_STATUS_NORMAL;
1328 g_set_error_literal (err, G_CONVERT_ERROR,
1329 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1330 _("Invalid byte sequence in conversion input"));
1331 return G_IO_STATUS_ERROR;
1335 g_assert (errval != EBADF); /* The converter should be open */
1336 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1337 _("Error during conversion: %s"), g_strerror (errval));
1338 return G_IO_STATUS_ERROR;
1341 g_assert ((status != G_IO_STATUS_NORMAL)
1342 || (channel->encoded_read_buf->len > 0));
1344 else if (channel->encoding) /* UTF-8 */
1346 gchar *nextchar, *lastchar;
1348 g_assert (channel->encoded_read_buf);
1350 nextchar = channel->read_buf->str;
1351 lastchar = channel->read_buf->str + channel->read_buf->len;
1353 while (nextchar < lastchar)
1357 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1362 /* stop, leave partial character in buffer */
1363 lastchar = nextchar;
1366 if (oldlen < channel->encoded_read_buf->len)
1367 status = G_IO_STATUS_NORMAL;
1370 g_set_error_literal (err, G_CONVERT_ERROR,
1371 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1372 _("Invalid byte sequence in conversion input"));
1373 status = G_IO_STATUS_ERROR;
1375 lastchar = nextchar;
1378 nextchar = g_utf8_next_char (nextchar);
1383 if (lastchar > channel->read_buf->str)
1385 gint copy_len = lastchar - channel->read_buf->str;
1387 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1389 g_string_erase (channel->read_buf, 0, copy_len);
1397 * g_io_channel_read_line:
1398 * @channel: a #GIOChannel
1399 * @str_return: The line read from the #GIOChannel, including the
1400 * line terminator. This data should be freed with g_free()
1401 * when no longer needed. This is a nul-terminated string.
1402 * If a @length of zero is returned, this will be %NULL instead.
1403 * @length: location to store length of the read data, or %NULL
1404 * @terminator_pos: location to store position of line terminator, or %NULL
1405 * @error: A location to return an error of type #GConvertError
1406 * or #GIOChannelError
1408 * Reads a line, including the terminating character(s),
1409 * from a #GIOChannel into a newly-allocated string.
1410 * @str_return will contain allocated memory if the return
1411 * is %G_IO_STATUS_NORMAL.
1413 * Return value: the status of the operation.
1416 g_io_channel_read_line (GIOChannel *channel,
1419 gsize *terminator_pos,
1425 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1426 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1427 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1429 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1431 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1434 *length = got_length;
1436 if (status == G_IO_STATUS_NORMAL)
1438 g_assert (USE_BUF (channel));
1439 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1440 g_string_erase (USE_BUF (channel), 0, got_length);
1449 * g_io_channel_read_line_string:
1450 * @channel: a #GIOChannel
1451 * @buffer: a #GString into which the line will be written.
1452 * If @buffer already contains data, the old data will
1454 * @terminator_pos: location to store position of line terminator, or %NULL
1455 * @error: a location to store an error of type #GConvertError
1456 * or #GIOChannelError
1458 * Reads a line from a #GIOChannel, using a #GString as a buffer.
1460 * Return value: the status of the operation.
1463 g_io_channel_read_line_string (GIOChannel *channel,
1465 gsize *terminator_pos,
1471 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1472 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1473 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1475 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1477 if (buffer->len > 0)
1478 g_string_truncate (buffer, 0); /* clear out the buffer */
1480 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1482 if (status == G_IO_STATUS_NORMAL)
1484 g_assert (USE_BUF (channel));
1485 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1486 g_string_erase (USE_BUF (channel), 0, length);
1494 g_io_channel_read_line_backend (GIOChannel *channel,
1496 gsize *terminator_pos,
1500 gsize checked_to, line_term_len, line_length, got_term_len;
1501 gboolean first_time = TRUE;
1503 if (!channel->use_buffer)
1505 /* Can't do a raw read in read_line */
1506 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1507 _("Can't do a raw read in g_io_channel_read_line_string"));
1508 return G_IO_STATUS_ERROR;
1511 status = G_IO_STATUS_NORMAL;
1513 if (channel->line_term)
1514 line_term_len = channel->line_term_len;
1517 /* This value used for setting checked_to, it's the longest of the four
1518 * we autodetect for.
1525 gchar *nextchar, *lastchar;
1528 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1531 status = g_io_channel_fill_buffer (channel, error);
1534 case G_IO_STATUS_NORMAL:
1535 if (BUF_LEN (USE_BUF (channel)) == 0)
1536 /* Can happen when using conversion and only read
1537 * part of a character
1544 case G_IO_STATUS_EOF:
1545 if (BUF_LEN (USE_BUF (channel)) == 0)
1550 if (channel->encoding && channel->read_buf->len != 0)
1552 g_set_error_literal (error, G_CONVERT_ERROR,
1553 G_CONVERT_ERROR_PARTIAL_INPUT,
1554 _("Leftover unconverted data in "
1556 return G_IO_STATUS_ERROR;
1559 return G_IO_STATUS_EOF;
1569 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1571 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1575 lastchar = use_buf->str + use_buf->len;
1577 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1578 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1580 if (channel->line_term)
1582 if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
1584 line_length = nextchar - use_buf->str;
1585 got_term_len = line_term_len;
1589 else /* auto detect */
1593 case '\n': /* unix */
1594 line_length = nextchar - use_buf->str;
1597 case '\r': /* Warning: do not use with sockets */
1598 line_length = nextchar - use_buf->str;
1599 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1600 && (lastchar == use_buf->str + use_buf->len))
1601 goto read_again; /* Try to read more data */
1602 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1607 case '\xe2': /* Unicode paragraph separator */
1608 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1610 line_length = nextchar - use_buf->str;
1615 case '\0': /* Embeded null in input */
1616 line_length = nextchar - use_buf->str;
1619 default: /* no match */
1625 /* If encoding != NULL, valid UTF-8, didn't overshoot */
1626 g_assert (nextchar == lastchar);
1630 if (status == G_IO_STATUS_EOF)
1632 if (channel->encoding && channel->read_buf->len > 0)
1634 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1635 _("Channel terminates in a partial character"));
1636 return G_IO_STATUS_ERROR;
1638 line_length = use_buf->len;
1643 if (use_buf->len > line_term_len - 1)
1644 checked_to = use_buf->len - (line_term_len - 1);
1652 *terminator_pos = line_length;
1655 *length = line_length + got_term_len;
1657 return G_IO_STATUS_NORMAL;
1661 * g_io_channel_read_to_end:
1662 * @channel: a #GIOChannel
1663 * @str_return: Location to store a pointer to a string holding
1664 * the remaining data in the #GIOChannel. This data should
1665 * be freed with g_free() when no longer needed. This
1666 * data is terminated by an extra nul character, but there
1667 * may be other nuls in the intervening data.
1668 * @length: location to store length of the data
1669 * @error: location to return an error of type #GConvertError
1670 * or #GIOChannelError
1672 * Reads all the remaining data from the file.
1674 * Return value: %G_IO_STATUS_NORMAL on success.
1675 * This function never returns %G_IO_STATUS_EOF.
1678 g_io_channel_read_to_end (GIOChannel *channel,
1685 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1686 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1688 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1695 if (!channel->use_buffer)
1697 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1698 _("Can't do a raw read in g_io_channel_read_to_end"));
1699 return G_IO_STATUS_ERROR;
1703 status = g_io_channel_fill_buffer (channel, error);
1704 while (status == G_IO_STATUS_NORMAL);
1706 if (status != G_IO_STATUS_EOF)
1709 if (channel->encoding && channel->read_buf->len > 0)
1711 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1712 _("Channel terminates in a partial character"));
1713 return G_IO_STATUS_ERROR;
1716 if (USE_BUF (channel) == NULL)
1718 /* length is already set to zero */
1720 *str_return = g_strdup ("");
1725 *length = USE_BUF (channel)->len;
1728 *str_return = g_string_free (USE_BUF (channel), FALSE);
1730 g_string_free (USE_BUF (channel), TRUE);
1732 if (channel->encoding)
1733 channel->encoded_read_buf = NULL;
1735 channel->read_buf = NULL;
1738 return G_IO_STATUS_NORMAL;
1742 * g_io_channel_read_chars:
1743 * @channel: a #GIOChannel
1744 * @buf: a buffer to read data into
1745 * @count: the size of the buffer. Note that the buffer may
1746 * not be complelely filled even if there is data
1747 * in the buffer if the remaining data is not a
1748 * complete character.
1749 * @bytes_read: The number of bytes read. This may be zero even on
1750 * success if count < 6 and the channel's encoding is non-%NULL.
1751 * This indicates that the next UTF-8 character is too wide for
1753 * @error: a location to return an error of type #GConvertError
1754 * or #GIOChannelError.
1756 * Replacement for g_io_channel_read() with the new API.
1758 * Return value: the status of the operation.
1761 g_io_channel_read_chars (GIOChannel *channel,
1770 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1771 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1773 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1778 return G_IO_STATUS_NORMAL;
1780 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1782 if (!channel->use_buffer)
1786 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1788 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
1791 *bytes_read = tmp_bytes;
1796 status = G_IO_STATUS_NORMAL;
1798 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1799 status = g_io_channel_fill_buffer (channel, error);
1801 /* Only return an error if we have no data */
1803 if (BUF_LEN (USE_BUF (channel)) == 0)
1805 g_assert (status != G_IO_STATUS_NORMAL);
1807 if (status == G_IO_STATUS_EOF && channel->encoding
1808 && BUF_LEN (channel->read_buf) > 0)
1810 g_set_error_literal (error, G_CONVERT_ERROR,
1811 G_CONVERT_ERROR_PARTIAL_INPUT,
1812 _("Leftover unconverted data in read buffer"));
1813 status = G_IO_STATUS_ERROR;
1822 if (status == G_IO_STATUS_ERROR)
1823 g_clear_error (error);
1825 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1827 g_assert (got_bytes > 0);
1829 if (channel->encoding)
1830 /* Don't validate for NULL encoding, binary safe */
1832 gchar *nextchar, *prevchar;
1834 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1836 nextchar = channel->encoded_read_buf->str;
1840 prevchar = nextchar;
1841 nextchar = g_utf8_next_char (nextchar);
1842 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
1844 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1846 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1847 got_bytes = prevchar - channel->encoded_read_buf->str;
1849 g_assert (got_bytes > 0 || count < 6);
1852 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1853 g_string_erase (USE_BUF (channel), 0, got_bytes);
1856 *bytes_read = got_bytes;
1858 return G_IO_STATUS_NORMAL;
1862 * g_io_channel_read_unichar:
1863 * @channel: a #GIOChannel
1864 * @thechar: a location to return a character
1865 * @error: a location to return an error of type #GConvertError
1866 * or #GIOChannelError
1868 * Reads a Unicode character from @channel.
1869 * This function cannot be called on a channel with %NULL encoding.
1871 * Return value: a #GIOStatus
1874 g_io_channel_read_unichar (GIOChannel *channel,
1878 GIOStatus status = G_IO_STATUS_NORMAL;
1880 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1881 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1882 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1884 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1886 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1887 status = g_io_channel_fill_buffer (channel, error);
1889 /* Only return an error if we have no data */
1891 if (BUF_LEN (USE_BUF (channel)) == 0)
1893 g_assert (status != G_IO_STATUS_NORMAL);
1895 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1897 g_set_error_literal (error, G_CONVERT_ERROR,
1898 G_CONVERT_ERROR_PARTIAL_INPUT,
1899 _("Leftover unconverted data in read buffer"));
1900 status = G_IO_STATUS_ERROR;
1904 *thechar = (gunichar) -1;
1909 if (status == G_IO_STATUS_ERROR)
1910 g_clear_error (error);
1913 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1915 g_string_erase (channel->encoded_read_buf, 0,
1916 g_utf8_next_char (channel->encoded_read_buf->str)
1917 - channel->encoded_read_buf->str);
1919 return G_IO_STATUS_NORMAL;
1923 * g_io_channel_write_chars:
1924 * @channel: a #GIOChannel
1925 * @buf: a buffer to write data from
1926 * @count: the size of the buffer. If -1, the buffer
1927 * is taken to be a nul-terminated string.
1928 * @bytes_written: The number of bytes written. This can be nonzero
1929 * even if the return value is not %G_IO_STATUS_NORMAL.
1930 * If the return value is %G_IO_STATUS_NORMAL and the
1931 * channel is blocking, this will always be equal
1932 * to @count if @count >= 0.
1933 * @error: a location to return an error of type #GConvertError
1934 * or #GIOChannelError
1936 * Replacement for g_io_channel_write() with the new API.
1938 * On seekable channels with encodings other than %NULL or UTF-8, generic
1939 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
1940 * may only be made on a channel from which data has been read in the
1941 * cases described in the documentation for g_io_channel_set_encoding ().
1943 * Return value: the status of the operation.
1946 g_io_channel_write_chars (GIOChannel *channel,
1949 gsize *bytes_written,
1953 gssize wrote_bytes = 0;
1955 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1956 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1958 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1960 if ((count < 0) && buf)
1961 count = strlen (buf);
1967 return G_IO_STATUS_NORMAL;
1970 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1971 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1973 /* Raw write case */
1975 if (!channel->use_buffer)
1979 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1980 g_assert (channel->partial_write_buf[0] == '\0');
1982 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
1985 *bytes_written = tmp_bytes;
1992 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1993 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1995 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1997 g_warning("Mixed reading and writing not allowed on encoded files");
1998 return G_IO_STATUS_ERROR;
2000 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
2001 if (status != G_IO_STATUS_NORMAL)
2009 if (!channel->write_buf)
2010 channel->write_buf = g_string_sized_new (channel->buf_size);
2012 while (wrote_bytes < count)
2016 /* If the buffer is full, try a write immediately. In
2017 * the nonblocking case, this prevents the user from
2018 * writing just a little bit to the buffer every time
2019 * and never receiving an EAGAIN.
2022 if (channel->write_buf->len >= channel->buf_size - MAX_CHAR_SIZE)
2024 gsize did_write = 0, this_time;
2028 status = channel->funcs->io_write (channel, channel->write_buf->str
2029 + did_write, channel->write_buf->len
2030 - did_write, &this_time, error);
2031 did_write += this_time;
2033 while (status == G_IO_STATUS_NORMAL &&
2034 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
2036 g_string_erase (channel->write_buf, 0, did_write);
2038 if (status != G_IO_STATUS_NORMAL)
2040 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
2041 status = G_IO_STATUS_NORMAL;
2043 *bytes_written = wrote_bytes;
2048 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
2049 - channel->write_buf->len; /* 1 for NULL */
2051 /* This is only true because g_io_channel_set_buffer_size ()
2052 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
2054 g_assert (space_in_buf >= MAX_CHAR_SIZE);
2056 if (!channel->encoding)
2058 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
2060 g_string_append_len (channel->write_buf, buf, write_this);
2062 wrote_bytes += write_this;
2066 const gchar *from_buf;
2067 gsize from_buf_len, from_buf_old_len, left_len;
2071 if (channel->partial_write_buf[0] != '\0')
2073 g_assert (wrote_bytes == 0);
2075 from_buf = channel->partial_write_buf;
2076 from_buf_old_len = strlen (channel->partial_write_buf);
2077 g_assert (from_buf_old_len > 0);
2078 from_buf_len = MIN (6, from_buf_old_len + count);
2080 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2081 from_buf_len - from_buf_old_len);
2086 from_buf_len = count - wrote_bytes;
2087 from_buf_old_len = 0;
2092 if (!channel->do_encode) /* UTF-8 encoding */
2094 const gchar *badchar;
2095 gsize try_len = MIN (from_buf_len, space_in_buf);
2097 /* UTF-8, just validate, emulate g_iconv */
2099 if (!g_utf8_validate (from_buf, try_len, &badchar))
2102 gsize incomplete_len = from_buf + try_len - badchar;
2104 left_len = from_buf + from_buf_len - badchar;
2106 try_char = g_utf8_get_char_validated (badchar, incomplete_len);
2111 g_assert (incomplete_len < 6);
2112 if (try_len == from_buf_len)
2124 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2125 /* FIXME bail here? */
2130 g_assert_not_reached ();
2132 errnum = 0; /* Don't confunse the compiler */
2139 left_len = from_buf_len - try_len;
2142 g_string_append_len (channel->write_buf, from_buf,
2143 from_buf_len - left_len);
2144 from_buf += from_buf_len - left_len;
2150 left_len = from_buf_len;
2151 g_string_set_size (channel->write_buf, channel->write_buf->len
2153 outbuf = channel->write_buf->str + channel->write_buf->len
2155 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2156 &outbuf, &space_in_buf);
2158 g_string_truncate (channel->write_buf, channel->write_buf->len
2162 if (err == (gsize) -1)
2167 g_assert (left_len < 6);
2169 if (from_buf_old_len == 0)
2171 /* Not from partial_write_buf */
2173 memcpy (channel->partial_write_buf, from_buf, left_len);
2174 channel->partial_write_buf[left_len] = '\0';
2176 *bytes_written = count;
2177 return G_IO_STATUS_NORMAL;
2180 /* Working in partial_write_buf */
2182 if (left_len == from_buf_len)
2184 /* Didn't convert anything, must still have
2185 * less than a full character
2188 g_assert (count == from_buf_len - from_buf_old_len);
2190 channel->partial_write_buf[from_buf_len] = '\0';
2193 *bytes_written = count;
2195 return G_IO_STATUS_NORMAL;
2198 g_assert (from_buf_len - left_len >= from_buf_old_len);
2200 /* We converted all the old data. This is fine */
2204 if (from_buf_len == left_len)
2206 /* Nothing was written, add enough space for
2207 * at least one character.
2209 space_in_buf += MAX_CHAR_SIZE;
2214 g_set_error_literal (error, G_CONVERT_ERROR,
2215 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2216 _("Invalid byte sequence in conversion input"));
2217 if (from_buf_old_len > 0 && from_buf_len == left_len)
2218 g_warning ("Illegal sequence due to partial character "
2219 "at the end of a previous write.\n");
2221 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2223 *bytes_written = wrote_bytes;
2224 channel->partial_write_buf[0] = '\0';
2225 return G_IO_STATUS_ERROR;
2227 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2228 _("Error during conversion: %s"), g_strerror (errnum));
2229 if (from_buf_len >= left_len + from_buf_old_len)
2230 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2232 *bytes_written = wrote_bytes;
2233 channel->partial_write_buf[0] = '\0';
2234 return G_IO_STATUS_ERROR;
2238 g_assert (from_buf_len - left_len >= from_buf_old_len);
2240 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2242 if (from_buf_old_len > 0)
2244 /* We were working in partial_write_buf */
2246 buf += from_buf_len - left_len - from_buf_old_len;
2247 channel->partial_write_buf[0] = '\0';
2255 *bytes_written = count;
2257 return G_IO_STATUS_NORMAL;
2261 * g_io_channel_write_unichar:
2262 * @channel: a #GIOChannel
2263 * @thechar: a character
2264 * @error: location to return an error of type #GConvertError
2265 * or #GIOChannelError
2267 * Writes a Unicode character to @channel.
2268 * This function cannot be called on a channel with %NULL encoding.
2270 * Return value: a #GIOStatus
2273 g_io_channel_write_unichar (GIOChannel *channel,
2278 gchar static_buf[6];
2279 gsize char_len, wrote_len;
2281 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2282 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2283 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2285 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2287 char_len = g_unichar_to_utf8 (thechar, static_buf);
2289 if (channel->partial_write_buf[0] != '\0')
2291 g_warning ("Partial charater written before writing unichar.\n");
2292 channel->partial_write_buf[0] = '\0';
2295 status = g_io_channel_write_chars (channel, static_buf,
2296 char_len, &wrote_len, error);
2298 /* We validate UTF-8, so we can't get a partial write */
2300 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2306 * g_io_channel_error_quark:
2308 * Return value: the quark used as %G_IO_CHANNEL_ERROR
2311 g_io_channel_error_quark (void)
2313 return g_quark_from_static_string ("g-io-channel-error-quark");
2316 #define __G_IOCHANNEL_C__
2317 #include "galiasdef.c"