1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giochannel.c: IO Channel abstraction
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 #include "giochannel.h"
44 #undef G_DISABLE_DEPRECATED
50 #define G_IO_NICE_BUF_SIZE 1024
52 /* This needs to be as wide as the largest character in any possible encoding */
53 #define MAX_CHAR_SIZE 10
55 /* Some simplifying macros, which reduce the need to worry whether the
56 * buffers have been allocated. These also make USE_BUF () an lvalue,
57 * which is used in g_io_channel_read_to_end ().
59 #define USE_BUF(channel) ((channel)->encoding ? (channel)->encoded_read_buf \
60 : (channel)->read_buf)
61 #define BUF_LEN(string) ((string) ? (string)->len : 0)
63 static GIOError g_io_error_get_from_g_error (GIOStatus status,
65 static void g_io_channel_purge (GIOChannel *channel);
66 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
68 static GIOStatus g_io_channel_read_line_backend (GIOChannel *channel,
70 gsize *terminator_pos,
74 g_io_channel_init (GIOChannel *channel)
76 channel->ref_count = 1;
77 channel->encoding = g_strdup ("UTF-8");
78 channel->line_term = NULL;
79 channel->line_term_len = 0;
80 channel->buf_size = G_IO_NICE_BUF_SIZE;
81 channel->read_cd = (GIConv) -1;
82 channel->write_cd = (GIConv) -1;
83 channel->read_buf = NULL; /* Lazy allocate buffers */
84 channel->encoded_read_buf = NULL;
85 channel->write_buf = NULL;
86 channel->partial_write_buf[0] = '\0';
87 channel->use_buffer = TRUE;
88 channel->do_encode = FALSE;
89 channel->close_on_unref = FALSE;
93 g_io_channel_ref (GIOChannel *channel)
95 g_return_if_fail (channel != NULL);
101 g_io_channel_unref (GIOChannel *channel)
103 g_return_if_fail (channel != NULL);
105 channel->ref_count--;
106 if (channel->ref_count == 0)
108 if (channel->close_on_unref)
109 g_io_channel_shutdown (channel, TRUE, NULL);
111 g_io_channel_purge (channel);
112 g_free (channel->encoding);
113 if (channel->read_cd != (GIConv) -1)
114 g_iconv_close (channel->read_cd);
115 if (channel->write_cd != (GIConv) -1)
116 g_iconv_close (channel->write_cd);
117 if (channel->line_term)
118 g_free (channel->line_term);
119 if (channel->read_buf)
120 g_string_free (channel->read_buf, TRUE);
121 if (channel->write_buf)
122 g_string_free (channel->write_buf, TRUE);
123 if (channel->encoded_read_buf)
124 g_string_free (channel->encoded_read_buf, TRUE);
125 channel->funcs->io_free (channel);
130 g_io_error_get_from_g_error (GIOStatus status,
135 case G_IO_STATUS_NORMAL:
136 case G_IO_STATUS_EOF:
137 return G_IO_ERROR_NONE;
138 case G_IO_STATUS_AGAIN:
139 return G_IO_ERROR_AGAIN;
140 case G_IO_STATUS_ERROR:
141 if (err->domain != G_IO_CHANNEL_ERROR)
142 return G_IO_ERROR_UNKNOWN;
145 case G_IO_CHANNEL_ERROR_INVAL:
146 return G_IO_ERROR_INVAL;
148 return G_IO_ERROR_UNKNOWN;
151 g_assert_not_reached ();
152 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
158 * @channel: a #GIOChannel.
159 * @buf: a buffer to read the data into (which should be at least count bytes long).
160 * @count: the number of bytes to read from the #GIOChannel.
161 * @bytes_read: returns the number of bytes actually read.
163 * Reads data from a #GIOChannel. This function is deprecated. New code should
164 * use g_io_channel_read_chars() instead.
166 * Return value: %G_IO_ERROR_NONE if the operation was successful.
169 g_io_channel_read (GIOChannel *channel,
178 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
179 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
181 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
183 error = g_io_error_get_from_g_error (status, err);
192 * g_io_channel_write:
193 * @channel: a #GIOChannel.
194 * @buf: the buffer containing the data to write.
195 * @count: the number of bytes to write.
196 * @bytes_written: the number of bytes actually written.
198 * Writes data to a #GIOChannel. This function is deprecated. New code should
199 * use g_io_channel_write_chars() instead.
201 * Return value: %G_IO_ERROR_NONE if the operation was successful.
204 g_io_channel_write (GIOChannel *channel,
207 gsize *bytes_written)
213 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
214 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
216 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
218 error = g_io_error_get_from_g_error (status, err);
228 * @channel: a #GIOChannel.
229 * @offset: an offset, in bytes, which is added to the position specified by @type
230 * @type: the position in the file, which can be %G_SEEK_CUR (the current
231 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
234 * Sets the current position in the #GIOChannel, similar to the standard library
235 * function <function>fseek()</function>. This function is deprecated. New
236 * code should use g_io_channel_seek_position() instead.
238 * Return value: %G_IO_ERROR_NONE if the operation was successful.
241 g_io_channel_seek (GIOChannel *channel,
249 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
250 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
259 g_warning ("g_io_channel_seek: unknown seek type");
260 return G_IO_ERROR_UNKNOWN;
263 status = channel->funcs->io_seek (channel, offset, type, &err);
265 error = g_io_error_get_from_g_error (status, err);
273 /* The function g_io_channel_new_file() is prototyped in both
274 * giounix.c and giowin32.c, so we stick its documentation here.
278 * g_io_channel_new_file:
279 * @filename: A string containing the name of a file.
280 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
281 * the same meaning as in <function>fopen()</function>.
282 * @error: A location to return an error of type %G_FILE_ERROR.
284 * Open a file @filename as a #GIOChannel using mode @mode. This
285 * channel will be closed when the last reference to it is dropped,
286 * so there is no need to call g_io_channel_close() (though doing
287 * so will not cause problems, as long as no attempt is made to
288 * access the channel after it is closed).
290 * Return value: A #GIOChannel on success, %NULL on failure.
294 * g_io_channel_close:
295 * @channel: A #GIOChannel
297 * Close an IO channel. Any pending data to be written will be
298 * flushed, ignoring errors. The channel will not be freed until the
299 * last reference is dropped using g_io_channel_unref(). This
300 * function is deprecated: you should use g_io_channel_shutdown()
304 g_io_channel_close (GIOChannel *channel)
308 g_return_if_fail (channel != NULL);
310 g_io_channel_purge (channel);
312 channel->funcs->io_close (channel, &err);
315 { /* No way to return the error */
316 g_warning ("Error closing channel: %s", err->message);
320 channel->close_on_unref = FALSE; /* Because we already did */
321 channel->is_readable = FALSE;
322 channel->is_writeable = FALSE;
323 channel->is_seekable = FALSE;
327 * g_io_channel_shutdown:
328 * @channel: a #GIOChannel
329 * @flush: if %TRUE, flush pending
330 * @err: location to store a #GIOChannelError
332 * Close an IO channel. Any pending data to be written will be
333 * flushed if @flush is %TRUE. The channel will not be freed until the
334 * last reference is dropped using g_io_channel_unref().
336 * Return value: the status of the operation.
339 g_io_channel_shutdown (GIOChannel *channel,
343 GIOStatus status, result;
344 GError *tmperr = NULL;
346 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
347 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
349 if (channel->write_buf && channel->write_buf->len > 0)
355 /* Set the channel to blocking, to avoid a busy loop
357 flags = g_io_channel_get_flags (channel);
358 /* Ignore any errors here, they're irrelevant */
359 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
361 result = g_io_channel_flush (channel, &tmperr);
364 result = G_IO_STATUS_NORMAL;
366 g_string_truncate(channel->write_buf, 0);
369 result = G_IO_STATUS_NORMAL;
371 if (channel->partial_write_buf[0] != '\0')
374 g_warning ("Partial character at end of write buffer not flushed.\n");
375 channel->partial_write_buf[0] = '\0';
378 status = channel->funcs->io_close (channel, err);
380 channel->close_on_unref = FALSE; /* Because we already did */
381 channel->is_readable = FALSE;
382 channel->is_writeable = FALSE;
383 channel->is_seekable = FALSE;
385 if (status != G_IO_STATUS_NORMAL)
387 g_clear_error (&tmperr);
390 else if (result != G_IO_STATUS_NORMAL)
392 g_propagate_error (err, tmperr);
396 return G_IO_STATUS_NORMAL;
399 /* This function is used for the final flush on close or unref */
401 g_io_channel_purge (GIOChannel *channel)
406 g_return_if_fail (channel != NULL);
408 if (channel->write_buf && channel->write_buf->len > 0)
412 /* Set the channel to blocking, to avoid a busy loop
414 flags = g_io_channel_get_flags (channel);
415 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
417 status = g_io_channel_flush (channel, &err);
420 { /* No way to return the error */
421 g_warning ("Error flushing string: %s", err->message);
426 /* Flush these in case anyone tries to close without unrefing */
428 if (channel->read_buf)
429 g_string_truncate (channel->read_buf, 0);
430 if (channel->write_buf)
431 g_string_truncate (channel->write_buf, 0);
432 if (channel->encoding)
434 if (channel->encoded_read_buf)
435 g_string_truncate (channel->encoded_read_buf, 0);
437 if (channel->partial_write_buf[0] != '\0')
439 g_warning ("Partial character at end of write buffer not flushed.\n");
440 channel->partial_write_buf[0] = '\0';
446 g_io_create_watch (GIOChannel *channel,
447 GIOCondition condition)
449 g_return_val_if_fail (channel != NULL, NULL);
451 return channel->funcs->io_create_watch (channel, condition);
455 g_io_add_watch_full (GIOChannel *channel,
457 GIOCondition condition,
460 GDestroyNotify notify)
465 g_return_val_if_fail (channel != NULL, 0);
467 source = g_io_create_watch (channel, condition);
469 if (priority != G_PRIORITY_DEFAULT)
470 g_source_set_priority (source, priority);
471 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
473 id = g_source_attach (source, NULL);
474 g_source_unref (source);
480 g_io_add_watch (GIOChannel *channel,
481 GIOCondition condition,
485 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
489 * g_io_channel_get_buffer_condition:
490 * @channel: A #GIOChannel
492 * This function returns a #GIOCondition depending on whether there
493 * is data to be read/space to write data in the
494 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
495 * %G_IO_OUT may be set.
497 * Return value: A #GIOCondition
500 g_io_channel_get_buffer_condition (GIOChannel *channel)
502 GIOCondition condition = 0;
504 if (channel->encoding)
506 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
507 condition |= G_IO_IN; /* Only return if we have full characters */
511 if (channel->read_buf && (channel->read_buf->len > 0))
512 condition |= G_IO_IN;
515 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
516 condition |= G_IO_OUT;
522 * g_io_channel_error_from_errno:
523 * @en: an <literal>errno</literal> error number, e.g. %EINVAL.
525 * Converts an <literal>errno</literal> error number to a #GIOChannelError.
527 * Return value: a #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL.
530 g_io_channel_error_from_errno (gint en)
533 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
536 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
543 g_warning("Invalid file descriptor.\n");
544 return G_IO_CHANNEL_ERROR_FAILED;
549 g_warning("File descriptor outside valid address space.\n");
550 return G_IO_CHANNEL_ERROR_FAILED;
555 return G_IO_CHANNEL_ERROR_FBIG;
560 return G_IO_CHANNEL_ERROR_INVAL;
565 return G_IO_CHANNEL_ERROR_IO;
570 return G_IO_CHANNEL_ERROR_ISDIR;
575 return G_IO_CHANNEL_ERROR_NOSPC;
580 return G_IO_CHANNEL_ERROR_NXIO;
585 return G_IO_CHANNEL_ERROR_OVERFLOW;
590 return G_IO_CHANNEL_ERROR_PIPE;
594 return G_IO_CHANNEL_ERROR_FAILED;
599 * g_io_channel_set_buffer_size:
600 * @channel: a #GIOChannel
601 * @size: the size of the buffer. 0 == pick a good size
603 * Sets the buffer size.
606 g_io_channel_set_buffer_size (GIOChannel *channel,
609 g_return_if_fail (channel != NULL);
612 size = G_IO_NICE_BUF_SIZE;
614 if (size < MAX_CHAR_SIZE)
615 size = MAX_CHAR_SIZE;
617 channel->buf_size = size;
621 * g_io_channel_get_buffer_size:
622 * @channel: a #GIOChannel
624 * Gets the buffer size.
626 * Return value: the size of the buffer.
629 g_io_channel_get_buffer_size (GIOChannel *channel)
631 g_return_val_if_fail (channel != NULL, 0);
633 return channel->buf_size;
637 * g_io_channel_set_line_term:
638 * @channel: a #GIOChannel
639 * @line_term: The line termination string. Use %NULL for auto detect.
640 * Auto detection breaks on "\n", "\r\n", "\r", "\0", and
641 * the Unicode paragraph separator. Auto detection should
642 * not be used for anything other than file-based channels.
643 * @length: The length of the termination string. If -1 is passed, the
644 * string is assumed to be nul-terminated. This option allows
645 * termination strings with embeded nuls.
647 * This sets the string that #GIOChannel uses to determine
648 * where in the file a line break occurs.
651 g_io_channel_set_line_term (GIOChannel *channel,
652 const gchar *line_term,
655 g_return_if_fail (channel != NULL);
656 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
658 if (line_term == NULL)
661 length = strlen (line_term);
663 if (channel->line_term)
664 g_free (channel->line_term);
665 channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
666 channel->line_term_len = length;
670 * g_io_channel_get_line_term:
671 * @channel: a #GIOChannel
672 * @length: a location to return the length of the line terminator
674 * This returns the string that #GIOChannel uses to determine
675 * where in the file a line break occurs. A value of %NULL
676 * indicates auto detection.
678 * Return value: The line termination string. This value
679 * is owned by GLib and must not be freed.
681 G_CONST_RETURN gchar*
682 g_io_channel_get_line_term (GIOChannel *channel,
685 g_return_val_if_fail (channel != NULL, 0);
688 *length = channel->line_term_len;
690 return channel->line_term;
694 * g_io_channel_set_flags:
695 * @channel: a #GIOChannel.
696 * @flags: the flags to set on the IO channel.
697 * @error: A location to return an error of type #GIOChannelError.
699 * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK).
701 * Return value: the status of the operation.
704 g_io_channel_set_flags (GIOChannel *channel,
708 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
709 g_return_val_if_fail ((error == NULL) || (*error == NULL),
712 return (* channel->funcs->io_set_flags)(channel,
713 flags & G_IO_FLAG_SET_MASK,
718 * g_io_channel_get_flags:
719 * @channel: a #GIOChannel
721 * Gets the current flags for a #GIOChannel, including read-only
722 * flags such as %G_IO_FLAG_IS_READABLE.
724 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
725 * are cached for internal use by the channel when it is created.
726 * If they should change at some later point (e.g. partial shutdown
727 * of a socket with the UNIX <function>shutdown()</function> function), the user
728 * should immediately call g_io_channel_get_flags () to update
729 * the internal values of these flags.
731 * Return value: the flags which are set on the channel
734 g_io_channel_get_flags (GIOChannel *channel)
738 g_return_val_if_fail (channel != NULL, 0);
740 flags = (* channel->funcs->io_get_flags) (channel);
742 /* Cross implementation code */
744 if (channel->is_seekable)
745 flags |= G_IO_FLAG_IS_SEEKABLE;
746 if (channel->is_readable)
747 flags |= G_IO_FLAG_IS_READABLE;
748 if (channel->is_writeable)
749 flags |= G_IO_FLAG_IS_WRITEABLE;
755 * g_io_channel_set_close_on_unref:
756 * @channel: a #GIOChannel
757 * @do_close: Whether to close the channel on the final unref of
758 * the GIOChannel data structure. The default value of
759 * this is %TRUE for channels created by g_io_channel_new_file (),
760 * and %FALSE for all other channels.
762 * Setting this flag to %TRUE for a channel you have already closed
763 * can cause problems.
766 g_io_channel_set_close_on_unref (GIOChannel *channel,
769 g_return_if_fail (channel != NULL);
771 channel->close_on_unref = do_close;
775 * g_io_channel_get_close_on_unref:
776 * @channel: a #GIOChannel.
778 * Returns whether the file/socket/whatever associated with @channel
779 * will be closed when @channel receives its final unref and is
780 * destroyed. The default value of this is %TRUE for channels created
781 * by g_io_channel_new_file (), and %FALSE for all other channels.
783 * Return value: Whether the channel will be closed on the final unref of
784 * the GIOChannel data structure.
787 g_io_channel_get_close_on_unref (GIOChannel *channel)
789 g_return_val_if_fail (channel != NULL, FALSE);
791 return channel->close_on_unref;
795 * g_io_channel_seek_position:
796 * @channel: a #GIOChannel
797 * @offset: The offset in bytes from the position specified by @type
798 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
799 * cases where a call to g_io_channel_set_encoding ()
800 * is allowed. See the documentation for
801 * g_io_channel_set_encoding () for details.
802 * @error: A location to return an error of type #GIOChannelError
804 * Replacement for g_io_channel_seek() with the new API.
806 * Return value: the status of the operation.
809 g_io_channel_seek_position (GIOChannel* channel,
816 /* For files, only one of the read and write buffers can contain data.
817 * For sockets, both can contain data.
820 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
821 g_return_val_if_fail ((error == NULL) || (*error == NULL),
823 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
827 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
828 if (channel->use_buffer)
830 if (channel->do_encode && channel->encoded_read_buf
831 && channel->encoded_read_buf->len > 0)
833 g_warning ("Seek type G_SEEK_CUR not allowed for this"
834 " channel's encoding.\n");
835 return G_IO_STATUS_ERROR;
837 if (channel->read_buf)
838 offset -= channel->read_buf->len;
839 if (channel->encoded_read_buf)
841 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
843 /* If there's anything here, it's because the encoding is UTF-8,
844 * so we can just subtract the buffer length, the same as for
845 * the unencoded data.
848 offset -= channel->encoded_read_buf->len;
856 g_warning ("g_io_channel_seek_position: unknown seek type");
857 return G_IO_STATUS_ERROR;
860 if (channel->use_buffer)
862 status = g_io_channel_flush (channel, error);
863 if (status != G_IO_STATUS_NORMAL)
867 status = channel->funcs->io_seek (channel, offset, type, error);
869 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
871 if (channel->read_buf)
872 g_string_truncate (channel->read_buf, 0);
874 /* Conversion state no longer matches position in file */
875 if (channel->read_cd != (GIConv) -1)
876 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
877 if (channel->write_cd != (GIConv) -1)
878 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
880 if (channel->encoded_read_buf)
882 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
883 g_string_truncate (channel->encoded_read_buf, 0);
886 if (channel->partial_write_buf[0] != '\0')
888 g_warning ("Partial character at end of write buffer not flushed.\n");
889 channel->partial_write_buf[0] = '\0';
897 * g_io_channel_flush:
898 * @channel: a #GIOChannel
899 * @error: location to store an error of type #GIOChannelError
901 * Flushes the write buffer for the GIOChannel.
903 * Return value: the status of the operation: One of
904 * #G_IO_CHANNEL_NORMAL, #G_IO_CHANNEL_AGAIN, or
905 * #G_IO_CHANNEL_ERROR.
908 g_io_channel_flush (GIOChannel *channel,
912 gsize this_time = 1, bytes_written = 0;
914 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
915 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
917 if (channel->write_buf == NULL || channel->write_buf->len == 0)
918 return G_IO_STATUS_NORMAL;
922 g_assert (this_time > 0);
924 status = channel->funcs->io_write (channel,
925 channel->write_buf->str + bytes_written,
926 channel->write_buf->len - bytes_written,
928 bytes_written += this_time;
930 while ((bytes_written < channel->write_buf->len)
931 && (status == G_IO_STATUS_NORMAL));
933 g_string_erase (channel->write_buf, 0, bytes_written);
939 * g_io_channel_set_buffered:
940 * @channel: a #GIOChannel
941 * @buffered: whether to set the channel buffered or unbuffered
943 * The buffering state can only be set if the channel's encoding
944 * is %NULL. For any other encoding, the channel must be buffered.
946 * A buffered channel can only be set unbuffered if the channel's
947 * internal buffers have been flushed. Newly created channels or
948 * channels which have returned %G_IO_STATUS_EOF
949 * not require such a flush. For write-only channels, a call to
950 * g_io_channel_flush () is sufficient. For all other channels,
951 * the buffers may be flushed by a call to g_io_channel_seek_position ().
952 * This includes the possibility of seeking with seek type %G_SEEK_CUR
953 * and an offset of zero. Note that this means that socket-based
954 * channels cannot be set unbuffered once they have had data
957 * On unbuffered channels, it is safe to mix read and write
958 * calls from the new and old APIs, if this is necessary for
959 * maintaining old code.
961 * The default state of the channel is buffered.
964 g_io_channel_set_buffered (GIOChannel *channel,
967 g_return_if_fail (channel != NULL);
969 if (channel->encoding != NULL)
971 g_warning ("Need to have NULL encoding to set the buffering state of the "
976 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
977 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
979 channel->use_buffer = buffered;
983 * g_io_channel_get_buffered:
984 * @channel: a #GIOChannel.
986 * Returns whether @channel is buffered.
988 * Return Value: %TRUE if the @channel is buffered.
991 g_io_channel_get_buffered (GIOChannel *channel)
993 g_return_val_if_fail (channel != NULL, FALSE);
995 return channel->use_buffer;
999 * g_io_channel_set_encoding:
1000 * @channel: a #GIOChannel
1001 * @encoding: the encoding type
1002 * @error: location to store an error of type #GConvertError.
1004 * Sets the encoding for the input/output of the channel. The internal
1005 * encoding is always UTF-8. The default encoding for the
1006 * external file is UTF-8.
1008 * The encoding %NULL is safe to use with binary data.
1010 * The encoding can only be set if one of the following conditions
1013 * 1. The channel was just created, and has not been written to
1016 * 2. The channel is write-only.
1018 * 3. The channel is a file, and the file pointer was just
1019 * repositioned by a call to g_io_channel_seek_position().
1020 * (This flushes all the internal buffers.)
1022 * 4. The current encoding is %NULL or UTF-8.
1024 * 5. One of the (new API) read functions has just returned %G_IO_STATUS_EOF
1025 * (or, in the case of g_io_channel_read_to_end (), %G_IO_STATUS_NORMAL).
1027 * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar ()
1028 * has returned %G_IO_STATUS_AGAIN or %G_IO_STATUS_ERROR. This may be
1029 * useful in the case of %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1030 * Returning one of these statuses from g_io_channel_read_line (),
1031 * g_io_channel_read_line_string (), or g_io_channel_read_to_end ()
1032 * does <emphasis>not</emphasis> guarantee that the encoding can be changed.
1034 * Channels which do not meet one of the above conditions cannot call
1035 * g_io_channel_seek_position () with an offset of %G_SEEK_CUR,
1036 * and, if they are "seekable", cannot
1037 * call g_io_channel_write_chars () after calling one
1038 * of the API "read" functions.
1040 * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set.
1043 g_io_channel_set_encoding (GIOChannel *channel,
1044 const gchar *encoding,
1047 GIConv read_cd, write_cd;
1048 gboolean did_encode;
1050 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1051 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1053 /* Make sure the encoded buffers are empty */
1055 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1056 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1058 if (!channel->use_buffer)
1060 g_warning ("Need to set the channel buffered before setting the encoding.\n");
1061 g_warning ("Assuming this is what you meant and acting accordingly.\n");
1063 channel->use_buffer = TRUE;
1066 if (channel->partial_write_buf[0] != '\0')
1068 g_warning ("Partial character at end of write buffer not flushed.\n");
1069 channel->partial_write_buf[0] = '\0';
1072 did_encode = channel->do_encode;
1074 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1076 channel->do_encode = FALSE;
1077 read_cd = write_cd = (GIConv) -1;
1082 const gchar *from_enc = NULL, *to_enc = NULL;
1084 if (channel->is_readable)
1086 read_cd = g_iconv_open ("UTF-8", encoding);
1088 if (read_cd == (GIConv) -1)
1096 read_cd = (GIConv) -1;
1098 if (channel->is_writeable && err == 0)
1100 write_cd = g_iconv_open (encoding, "UTF-8");
1102 if (write_cd == (GIConv) -1)
1105 from_enc = encoding;
1110 write_cd = (GIConv) -1;
1114 g_assert (from_enc);
1118 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1119 _("Conversion from character set `%s' to `%s' is not supported"),
1122 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1123 _("Could not open converter from `%s' to `%s': %s"),
1124 from_enc, to_enc, g_strerror (err));
1126 if (read_cd != (GIConv) -1)
1127 g_iconv_close (read_cd);
1128 if (write_cd != (GIConv) -1)
1129 g_iconv_close (write_cd);
1131 return G_IO_STATUS_ERROR;
1134 channel->do_encode = TRUE;
1137 /* The encoding is ok, so set the fields in channel */
1139 if (channel->read_cd != (GIConv) -1)
1140 g_iconv_close (channel->read_cd);
1141 if (channel->write_cd != (GIConv) -1)
1142 g_iconv_close (channel->write_cd);
1144 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1146 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1148 /* This is just validated UTF-8, so we can copy it back into read_buf
1149 * so it can be encoded in whatever the new encoding is.
1152 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1153 channel->encoded_read_buf->len);
1154 g_string_truncate (channel->encoded_read_buf, 0);
1157 channel->read_cd = read_cd;
1158 channel->write_cd = write_cd;
1160 g_free (channel->encoding);
1161 channel->encoding = g_strdup (encoding);
1163 return G_IO_STATUS_NORMAL;
1167 * g_io_channel_get_encoding:
1168 * @channel: a #GIOChannel
1170 * Gets the encoding for the input/output of the channel. The internal
1171 * encoding is always UTF-8. The encoding %NULL makes the
1172 * channel safe for binary data.
1174 * Return value: A string containing the encoding, this string is
1175 * owned by GLib and must not be freed.
1177 G_CONST_RETURN gchar*
1178 g_io_channel_get_encoding (GIOChannel *channel)
1180 g_return_val_if_fail (channel != NULL, NULL);
1182 return channel->encoding;
1186 g_io_channel_fill_buffer (GIOChannel *channel,
1189 gsize read_size, cur_len, oldlen;
1192 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1194 status = g_io_channel_flush (channel, err);
1195 if (status != G_IO_STATUS_NORMAL)
1198 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1200 g_warning ("Partial character at end of write buffer not flushed.\n");
1201 channel->partial_write_buf[0] = '\0';
1204 if (!channel->read_buf)
1205 channel->read_buf = g_string_sized_new (channel->buf_size);
1207 cur_len = channel->read_buf->len;
1209 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1211 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1212 channel->buf_size, &read_size, err);
1214 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1216 g_string_truncate (channel->read_buf, read_size + cur_len);
1218 if ((status != G_IO_STATUS_NORMAL)
1219 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1222 g_assert (channel->read_buf->len > 0);
1224 if (channel->encoded_read_buf)
1225 oldlen = channel->encoded_read_buf->len;
1229 if (channel->encoding)
1230 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1233 if (channel->do_encode)
1235 size_t errnum, inbytes_left, outbytes_left;
1236 gchar *inbuf, *outbuf;
1239 g_assert (channel->encoded_read_buf);
1243 inbytes_left = channel->read_buf->len;
1244 outbytes_left = MAX (channel->read_buf->len,
1245 channel->encoded_read_buf->allocated_len
1246 - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1247 outbytes_left = MAX (outbytes_left, 6);
1249 inbuf = channel->read_buf->str;
1250 g_string_set_size (channel->encoded_read_buf,
1251 channel->encoded_read_buf->len + outbytes_left);
1252 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1255 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1256 &outbuf, &outbytes_left);
1259 g_assert (inbuf + inbytes_left == channel->read_buf->str
1260 + channel->read_buf->len);
1261 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1262 + channel->encoded_read_buf->len);
1264 g_string_erase (channel->read_buf, 0,
1265 channel->read_buf->len - inbytes_left);
1266 g_string_truncate (channel->encoded_read_buf,
1267 channel->encoded_read_buf->len - outbytes_left);
1269 if (errnum == (size_t) -1)
1274 if ((oldlen == channel->encoded_read_buf->len)
1275 && (status == G_IO_STATUS_EOF))
1276 status = G_IO_STATUS_EOF;
1278 status = G_IO_STATUS_NORMAL;
1281 /* Buffer size at least 6, wrote at least on character */
1282 g_assert (inbuf != channel->read_buf->str);
1285 if (oldlen < channel->encoded_read_buf->len)
1286 status = G_IO_STATUS_NORMAL;
1289 g_set_error (err, G_CONVERT_ERROR,
1290 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1291 _("Invalid byte sequence in conversion input"));
1292 return G_IO_STATUS_ERROR;
1296 g_assert (errval != EBADF); /* The converter should be open */
1297 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1298 _("Error during conversion: %s"), g_strerror (errval));
1299 return G_IO_STATUS_ERROR;
1302 g_assert ((status != G_IO_STATUS_NORMAL)
1303 || (channel->encoded_read_buf->len > 0));
1305 else if (channel->encoding) /* UTF-8 */
1307 gchar *nextchar, *lastchar;
1309 g_assert (channel->encoded_read_buf);
1311 nextchar = channel->read_buf->str;
1312 lastchar = channel->read_buf->str + channel->read_buf->len;
1314 while (nextchar < lastchar)
1318 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1323 /* stop, leave partial character in buffer */
1324 lastchar = nextchar;
1327 if (oldlen < channel->encoded_read_buf->len)
1328 status = G_IO_STATUS_NORMAL;
1331 g_set_error (err, G_CONVERT_ERROR,
1332 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1333 _("Invalid byte sequence in conversion input"));
1334 status = G_IO_STATUS_ERROR;
1336 lastchar = nextchar;
1339 nextchar = g_utf8_next_char (nextchar);
1344 if (lastchar > channel->read_buf->str)
1346 gint copy_len = lastchar - channel->read_buf->str;
1348 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1350 g_string_erase (channel->read_buf, 0, copy_len);
1358 * g_io_channel_read_line:
1359 * @channel: a #GIOChannel
1360 * @str_return: The line read from the #GIOChannel, including the
1361 * line terminator. This data should be freed with g_free()
1362 * when no longer needed. This is a nul-terminated string.
1363 * If a @length of zero is returned, this will be %NULL instead.
1364 * @length: location to store length of the read data, or %NULL
1365 * @terminator_pos: location to store position of line terminator, or %NULL
1366 * @error: A location to return an error of type #GConvertError
1367 * or #GIOChannelError
1369 * Reads a line, including the terminating character(s),
1370 * from a #GIOChannel into a newly-allocated string.
1371 * @str_return will contain allocated memory if the return
1372 * is %G_IO_STATUS_NORMAL.
1374 * Return value: the status of the operation.
1377 g_io_channel_read_line (GIOChannel *channel,
1380 gsize *terminator_pos,
1386 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1387 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1388 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1390 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1392 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1395 *length = got_length;
1397 if (status == G_IO_STATUS_NORMAL)
1399 g_assert (USE_BUF (channel));
1400 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1401 g_string_erase (USE_BUF (channel), 0, got_length);
1410 * g_io_channel_read_line_string:
1411 * @channel: a #GIOChannel
1412 * @buffer: a #GString into which the line will be written.
1413 * If @buffer already contains data, the old data will
1415 * @terminator_pos: location to store position of line terminator, or %NULL
1416 * @error: a location to store an error of type #GConvertError
1417 * or #GIOChannelError
1419 * Reads a line from a #GIOChannel, using a #GString as a buffer.
1421 * Return value: the status of the operation.
1424 g_io_channel_read_line_string (GIOChannel *channel,
1426 gsize *terminator_pos,
1432 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1433 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1434 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1436 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1438 if (buffer->len > 0)
1439 g_string_truncate (buffer, 0); /* clear out the buffer */
1441 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1443 if (status == G_IO_STATUS_NORMAL)
1445 g_assert (USE_BUF (channel));
1446 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1447 g_string_erase (USE_BUF (channel), 0, length);
1455 g_io_channel_read_line_backend (GIOChannel *channel,
1457 gsize *terminator_pos,
1461 gsize checked_to, line_term_len, line_length, got_term_len;
1462 gboolean first_time = TRUE;
1464 if (!channel->use_buffer)
1466 /* Can't do a raw read in read_line */
1467 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1468 _("Can't do a raw read in g_io_channel_read_line_string"));
1469 return G_IO_STATUS_ERROR;
1472 status = G_IO_STATUS_NORMAL;
1474 if (channel->line_term)
1475 line_term_len = channel->line_term_len;
1478 /* This value used for setting checked_to, it's the longest of the four
1479 * we autodetect for.
1486 gchar *nextchar, *lastchar;
1489 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1492 status = g_io_channel_fill_buffer (channel, error);
1495 case G_IO_STATUS_NORMAL:
1496 if (BUF_LEN (USE_BUF (channel)) == 0)
1497 /* Can happen when using conversion and only read
1498 * part of a character
1505 case G_IO_STATUS_EOF:
1506 if (BUF_LEN (USE_BUF (channel)) == 0)
1511 if (channel->encoding && channel->read_buf->len != 0)
1513 g_set_error (error, G_CONVERT_ERROR,
1514 G_CONVERT_ERROR_PARTIAL_INPUT,
1515 _("Leftover unconverted data in read buffer"));
1516 return G_IO_STATUS_ERROR;
1519 return G_IO_STATUS_EOF;
1529 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1531 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1535 lastchar = use_buf->str + use_buf->len;
1537 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1538 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1540 if (channel->line_term)
1542 if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
1544 line_length = nextchar - use_buf->str;
1545 got_term_len = line_term_len;
1549 else /* auto detect */
1553 case '\n': /* unix */
1554 line_length = nextchar - use_buf->str;
1557 case '\r': /* Warning: do not use with sockets */
1558 line_length = nextchar - use_buf->str;
1559 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1560 && (lastchar == use_buf->str + use_buf->len))
1561 goto read_again; /* Try to read more data */
1562 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1567 case '\xe2': /* Unicode paragraph separator */
1568 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1570 line_length = nextchar - use_buf->str;
1575 case '\0': /* Embeded null in input */
1576 line_length = nextchar - use_buf->str;
1579 default: /* no match */
1585 /* If encoding != NULL, valid UTF-8, didn't overshoot */
1586 g_assert (nextchar == lastchar);
1590 if (status == G_IO_STATUS_EOF)
1592 if (channel->encoding && channel->read_buf->len > 0)
1594 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1595 _("Channel terminates in a partial character"));
1596 return G_IO_STATUS_ERROR;
1598 line_length = use_buf->len;
1603 if (use_buf->len > line_term_len - 1)
1604 checked_to = use_buf->len - (line_term_len - 1);
1612 *terminator_pos = line_length;
1615 *length = line_length + got_term_len;
1617 return G_IO_STATUS_NORMAL;
1621 * g_io_channel_read_to_end:
1622 * @channel: a #GIOChannel
1623 * @str_return: Location to store a pointer to a string holding
1624 * the remaining data in the #GIOChannel. This data should
1625 * be freed with g_free() when no longer needed. This
1626 * data is terminated by an extra nul character, but there
1627 * may be other nuls in the intervening data.
1628 * @length: Location to store length of the data
1629 * @error: A location to return an error of type #GConvertError
1630 * or #GIOChannelError
1632 * Reads all the remaining data from the file.
1634 * Return value: %G_IO_STATUS_NORMAL on success. This function never
1635 * returns %G_IO_STATUS_EOF.
1638 g_io_channel_read_to_end (GIOChannel *channel,
1645 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1646 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1648 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1655 if (!channel->use_buffer)
1657 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1658 _("Can't do a raw read in g_io_channel_read_to_end"));
1659 return G_IO_STATUS_ERROR;
1663 status = g_io_channel_fill_buffer (channel, error);
1664 while (status == G_IO_STATUS_NORMAL);
1666 if (status != G_IO_STATUS_EOF)
1669 if (channel->encoding && channel->read_buf->len > 0)
1671 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1672 _("Channel terminates in a partial character"));
1673 return G_IO_STATUS_ERROR;
1676 if (USE_BUF (channel) == NULL)
1678 /* length is already set to zero */
1680 *str_return = g_strdup ("");
1685 *length = USE_BUF (channel)->len;
1688 *str_return = g_string_free (USE_BUF (channel), FALSE);
1690 g_string_free (USE_BUF (channel), TRUE);
1692 if (channel->encoding)
1693 channel->encoded_read_buf = NULL;
1695 channel->read_buf = NULL;
1698 return G_IO_STATUS_NORMAL;
1702 * g_io_channel_read_chars:
1703 * @channel: a #GIOChannel
1704 * @buf: a buffer to read data into
1705 * @count: the size of the buffer. Note that the buffer may
1706 * not be complelely filled even if there is data
1707 * in the buffer if the remaining data is not a
1708 * complete character.
1709 * @bytes_read: The number of bytes read. This may be zero even on
1710 * success if count < 6 and the channel's encoding is non-%NULL.
1711 * This indicates that the next UTF-8 character is too wide for
1713 * @error: A location to return an error of type #GConvertError
1714 * or #GIOChannelError.
1716 * Replacement for g_io_channel_read() with the new API.
1718 * Return value: the status of the operation.
1721 g_io_channel_read_chars (GIOChannel *channel,
1730 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1731 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1733 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1738 return G_IO_STATUS_NORMAL;
1740 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1742 if (!channel->use_buffer)
1746 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1748 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
1751 *bytes_read = tmp_bytes;
1756 status = G_IO_STATUS_NORMAL;
1758 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1759 status = g_io_channel_fill_buffer (channel, error);
1761 /* Only return an error if we have no data */
1763 if (BUF_LEN (USE_BUF (channel)) == 0)
1765 g_assert (status != G_IO_STATUS_NORMAL);
1767 if (status == G_IO_STATUS_EOF && channel->encoding
1768 && BUF_LEN (channel->read_buf) > 0)
1770 g_set_error (error, G_CONVERT_ERROR,
1771 G_CONVERT_ERROR_PARTIAL_INPUT,
1772 _("Leftover unconverted data in read buffer"));
1773 status = G_IO_STATUS_ERROR;
1782 if (status == G_IO_STATUS_ERROR)
1783 g_clear_error (error);
1785 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1787 g_assert (got_bytes > 0);
1789 if (channel->encoding)
1790 /* Don't validate for NULL encoding, binary safe */
1792 gchar *nextchar, *prevchar;
1794 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1796 nextchar = channel->encoded_read_buf->str;
1800 prevchar = nextchar;
1801 nextchar = g_utf8_next_char (nextchar);
1802 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
1804 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1806 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1807 got_bytes = prevchar - channel->encoded_read_buf->str;
1809 g_assert (got_bytes > 0 || count < 6);
1812 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1813 g_string_erase (USE_BUF (channel), 0, got_bytes);
1816 *bytes_read = got_bytes;
1818 return G_IO_STATUS_NORMAL;
1822 * g_io_channel_read_unichar:
1823 * @channel: a #GIOChannel
1824 * @thechar: a location to return a character
1825 * @error: A location to return an error of type #GConvertError
1826 * or #GIOChannelError
1828 * This function cannot be called on a channel with %NULL encoding.
1830 * Return value: a #GIOStatus
1833 g_io_channel_read_unichar (GIOChannel *channel,
1837 GIOStatus status = G_IO_STATUS_NORMAL;
1839 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1840 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1841 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1843 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1845 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1846 status = g_io_channel_fill_buffer (channel, error);
1848 /* Only return an error if we have no data */
1850 if (BUF_LEN (USE_BUF (channel)) == 0)
1852 g_assert (status != G_IO_STATUS_NORMAL);
1854 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1856 g_set_error (error, G_CONVERT_ERROR,
1857 G_CONVERT_ERROR_PARTIAL_INPUT,
1858 _("Leftover unconverted data in read buffer"));
1859 status = G_IO_STATUS_ERROR;
1863 *thechar = (gunichar) -1;
1868 if (status == G_IO_STATUS_ERROR)
1869 g_clear_error (error);
1872 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1874 g_string_erase (channel->encoded_read_buf, 0,
1875 g_utf8_next_char (channel->encoded_read_buf->str)
1876 - channel->encoded_read_buf->str);
1878 return G_IO_STATUS_NORMAL;
1882 * g_io_channel_write_chars:
1883 * @channel: a #GIOChannel
1884 * @buf: a buffer to write data from
1885 * @count: the size of the buffer. If -1, the buffer
1886 * is taken to be a nul-terminated string.
1887 * @bytes_written: The number of bytes written. This can be nonzero
1888 * even if the return value is not %G_IO_STATUS_NORMAL.
1889 * If the return value is %G_IO_STATUS_NORMAL and the
1890 * channel is blocking, this will always be equal
1891 * to @count if @count >= 0.
1892 * @error: A location to return an error of type #GConvertError
1893 * or #GIOChannelError
1895 * Replacement for g_io_channel_write() with the new API.
1897 * On seekable channels with encodings other than %NULL or UTF-8, generic
1898 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
1899 * may only be made on a channel from which data has been read in the
1900 * cases described in the documentation for g_io_channel_set_encoding ().
1902 * Return value: the status of the operation.
1905 g_io_channel_write_chars (GIOChannel *channel,
1908 gsize *bytes_written,
1912 gssize wrote_bytes = 0;
1914 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1915 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1917 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1919 if ((count < 0) && buf)
1920 count = strlen (buf);
1926 return G_IO_STATUS_NORMAL;
1929 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1930 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1932 /* Raw write case */
1934 if (!channel->use_buffer)
1938 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1939 g_assert (channel->partial_write_buf[0] == '\0');
1941 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
1944 *bytes_written = tmp_bytes;
1951 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1952 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1954 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1956 g_warning("Mixed reading and writing not allowed on encoded files");
1957 return G_IO_STATUS_ERROR;
1959 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1960 if (status != G_IO_STATUS_NORMAL)
1968 if (!channel->write_buf)
1969 channel->write_buf = g_string_sized_new (channel->buf_size);
1971 while (wrote_bytes < count)
1975 /* If the buffer is full, try a write immediately. In
1976 * the nonblocking case, this prevents the user from
1977 * writing just a little bit to the buffer every time
1978 * and never receiving an EAGAIN.
1981 if (channel->write_buf->len >= channel->buf_size)
1983 gsize did_write = 0, this_time;
1987 status = channel->funcs->io_write (channel, channel->write_buf->str
1988 + did_write, channel->write_buf->len
1989 - did_write, &this_time, error);
1990 did_write += this_time;
1992 while (status == G_IO_STATUS_NORMAL &&
1993 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
1995 g_string_erase (channel->write_buf, 0, did_write);
1997 if (status != G_IO_STATUS_NORMAL)
1999 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
2000 status = G_IO_STATUS_NORMAL;
2002 *bytes_written = wrote_bytes;
2007 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
2008 - channel->write_buf->len; /* 1 for NULL */
2010 /* This is only true because g_io_channel_set_buffer_size ()
2011 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
2013 g_assert (space_in_buf >= MAX_CHAR_SIZE);
2015 if (!channel->encoding)
2017 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
2019 g_string_append_len (channel->write_buf, buf, write_this);
2021 wrote_bytes += write_this;
2025 const gchar *from_buf;
2026 gsize from_buf_len, from_buf_old_len, left_len;
2030 if (channel->partial_write_buf[0] != '\0')
2032 g_assert (wrote_bytes == 0);
2034 from_buf = channel->partial_write_buf;
2035 from_buf_old_len = strlen (channel->partial_write_buf);
2036 g_assert (from_buf_old_len > 0);
2037 from_buf_len = MIN (6, from_buf_old_len + count);
2039 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2040 from_buf_len - from_buf_old_len);
2045 from_buf_len = count - wrote_bytes;
2046 from_buf_old_len = 0;
2051 if (!channel->do_encode) /* UTF-8 encoding */
2053 const gchar *badchar;
2054 gsize try_len = MIN (from_buf_len, space_in_buf);
2056 /* UTF-8, just validate, emulate g_iconv */
2058 if (!g_utf8_validate (from_buf, try_len, &badchar))
2061 gsize incomplete_len = from_buf + try_len - badchar;
2063 left_len = from_buf + from_buf_len - badchar;
2065 try_char = g_utf8_get_char_validated (badchar, incomplete_len);
2070 g_assert (incomplete_len < 6);
2071 if (try_len == from_buf_len)
2083 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2084 /* FIXME bail here? */
2089 g_assert_not_reached ();
2091 errnum = 0; /* Don't confunse the compiler */
2098 left_len = from_buf_len - try_len;
2101 g_string_append_len (channel->write_buf, from_buf,
2102 from_buf_len - left_len);
2103 from_buf += from_buf_len - left_len;
2109 left_len = from_buf_len;
2110 g_string_set_size (channel->write_buf, channel->write_buf->len
2112 outbuf = channel->write_buf->str + channel->write_buf->len
2114 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2115 &outbuf, &space_in_buf);
2117 g_string_truncate (channel->write_buf, channel->write_buf->len
2121 if (err == (size_t) -1)
2126 g_assert (left_len < 6);
2128 if (from_buf_old_len == 0)
2130 /* Not from partial_write_buf */
2132 memcpy (channel->partial_write_buf, from_buf, left_len);
2133 channel->partial_write_buf[left_len] = '\0';
2135 *bytes_written = count;
2136 return G_IO_STATUS_NORMAL;
2139 /* Working in partial_write_buf */
2141 if (left_len == from_buf_len)
2143 /* Didn't convert anything, must still have
2144 * less than a full character
2147 g_assert (count == from_buf_len - from_buf_old_len);
2149 channel->partial_write_buf[from_buf_len] = '\0';
2152 *bytes_written = count;
2154 return G_IO_STATUS_NORMAL;
2157 g_assert (from_buf_len - left_len >= from_buf_old_len);
2159 /* We converted all the old data. This is fine */
2163 if (from_buf_len == left_len)
2165 /* Nothing was written, add enough space for
2166 * at least one character.
2168 space_in_buf += MAX_CHAR_SIZE;
2173 g_set_error (error, G_CONVERT_ERROR,
2174 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2175 _("Invalid byte sequence in conversion input"));
2176 if (from_buf_old_len > 0 && from_buf_len == left_len)
2177 g_warning ("Illegal sequence due to partial character "
2178 "at the end of a previous write.\n");
2180 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2182 *bytes_written = wrote_bytes;
2183 channel->partial_write_buf[0] = '\0';
2184 return G_IO_STATUS_ERROR;
2186 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2187 _("Error during conversion: %s"), g_strerror (errnum));
2188 if (from_buf_len >= left_len + from_buf_old_len)
2189 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2191 *bytes_written = wrote_bytes;
2192 channel->partial_write_buf[0] = '\0';
2193 return G_IO_STATUS_ERROR;
2197 g_assert (from_buf_len - left_len >= from_buf_old_len);
2199 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2201 if (from_buf_old_len > 0)
2203 /* We were working in partial_write_buf */
2205 buf += from_buf_len - left_len - from_buf_old_len;
2206 channel->partial_write_buf[0] = '\0';
2214 *bytes_written = count;
2216 return G_IO_STATUS_NORMAL;
2220 * g_io_channel_write_unichar:
2221 * @channel: a #GIOChannel
2222 * @thechar: a character
2223 * @error: A location to return an error of type #GConvertError
2224 * or #GIOChannelError
2226 * This function cannot be called on a channel with %NULL encoding.
2228 * Return value: a #GIOStatus
2231 g_io_channel_write_unichar (GIOChannel *channel,
2236 gchar static_buf[6];
2237 gsize char_len, wrote_len;
2239 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2240 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2241 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2243 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2245 char_len = g_unichar_to_utf8 (thechar, static_buf);
2247 if (channel->partial_write_buf[0] != '\0')
2249 g_warning ("Partial charater written before writing unichar.\n");
2250 channel->partial_write_buf[0] = '\0';
2253 status = g_io_channel_write_chars (channel, static_buf,
2254 char_len, &wrote_len, error);
2256 /* We validate UTF-8, so we can't get a partial write */
2258 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2264 * g_io_channel_error_quark:
2266 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2269 g_io_channel_error_quark (void)
2271 static GQuark q = 0;
2273 q = g_quark_from_static_string ("g-io-channel-error-quark");