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 depricated. 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 depricated. 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 fseek(). This function is depricated. New code should
236 * 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 fopen().
282 * @error: A location to return an error of type %G_IO_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().
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 (flush && channel->write_buf && channel->write_buf->len > 0)
353 /* Set the channel to blocking, to avoid a busy loop
355 flags = g_io_channel_get_flags (channel);
356 /* Ignore any errors here, they're irrelevant */
357 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
359 result = g_io_channel_flush (channel, &tmperr);
361 if (channel->partial_write_buf[0] != '\0')
363 g_warning ("Partial character at end of write buffer not flushed.\n");
364 channel->partial_write_buf[0] = '\0';
368 result = G_IO_STATUS_NORMAL;
370 status = channel->funcs->io_close (channel, err);
372 channel->close_on_unref = FALSE; /* Because we already did */
373 channel->is_readable = FALSE;
374 channel->is_writeable = FALSE;
375 channel->is_seekable = FALSE;
377 if (status != G_IO_STATUS_NORMAL)
379 g_clear_error (&tmperr);
382 else if (result != G_IO_STATUS_NORMAL)
384 g_propagate_error (err, tmperr);
388 return G_IO_STATUS_NORMAL;
391 /* This function is used for the final flush on close or unref */
393 g_io_channel_purge (GIOChannel *channel)
398 g_return_if_fail (channel != NULL);
400 if (channel->write_buf && channel->write_buf->len > 0)
404 /* Set the channel to blocking, to avoid a busy loop
406 flags = g_io_channel_get_flags (channel);
407 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
409 status = g_io_channel_flush (channel, &err);
412 { /* No way to return the error */
413 g_warning ("Error flushing string: %s", err->message);
418 /* Flush these in case anyone tries to close without unrefing */
420 if (channel->read_buf)
421 g_string_truncate (channel->read_buf, 0);
422 if (channel->write_buf)
423 g_string_truncate (channel->write_buf, 0);
424 if (channel->encoding)
426 if (channel->encoded_read_buf)
427 g_string_truncate (channel->encoded_read_buf, 0);
429 if (channel->partial_write_buf[0] != '\0')
431 g_warning ("Partial character at end of write buffer not flushed.\n");
432 channel->partial_write_buf[0] = '\0';
438 g_io_create_watch (GIOChannel *channel,
439 GIOCondition condition)
441 g_return_val_if_fail (channel != NULL, NULL);
443 return channel->funcs->io_create_watch (channel, condition);
447 g_io_add_watch_full (GIOChannel *channel,
449 GIOCondition condition,
452 GDestroyNotify notify)
457 g_return_val_if_fail (channel != NULL, 0);
459 source = g_io_create_watch (channel, condition);
461 if (priority != G_PRIORITY_DEFAULT)
462 g_source_set_priority (source, priority);
463 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
465 id = g_source_attach (source, NULL);
466 g_source_unref (source);
472 g_io_add_watch (GIOChannel *channel,
473 GIOCondition condition,
477 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
481 * g_io_channel_get_buffer_condition:
482 * @channel: A #GIOChannel
484 * This function returns a #GIOCondition depending on whether there
485 * is data to be read/space to write data in the
486 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
487 * %G_IO_OUT may be set.
489 * Return value: A #GIOCondition
492 g_io_channel_get_buffer_condition (GIOChannel *channel)
494 GIOCondition condition = 0;
496 if (channel->encoding)
498 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
499 condition |= G_IO_IN; /* Only return if we have full characters */
503 if (channel->read_buf && (channel->read_buf->len > 0))
504 condition |= G_IO_IN;
507 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
508 condition |= G_IO_OUT;
514 * g_io_channel_error_from_errno:
515 * @en: An errno error number, e.g. EINVAL
517 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
520 g_io_channel_error_from_errno (gint en)
523 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
526 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
533 g_warning("Invalid file descriptor.\n");
534 return G_IO_CHANNEL_ERROR_FAILED;
539 g_warning("File descriptor outside valid address space.\n");
540 return G_IO_CHANNEL_ERROR_FAILED;
545 return G_IO_CHANNEL_ERROR_FBIG;
550 return G_IO_CHANNEL_ERROR_INVAL;
555 return G_IO_CHANNEL_ERROR_IO;
560 return G_IO_CHANNEL_ERROR_ISDIR;
565 return G_IO_CHANNEL_ERROR_NOSPC;
570 return G_IO_CHANNEL_ERROR_NXIO;
575 return G_IO_CHANNEL_ERROR_OVERFLOW;
580 return G_IO_CHANNEL_ERROR_PIPE;
584 return G_IO_CHANNEL_ERROR_FAILED;
589 * g_io_channel_set_buffer_size:
590 * @channel: a #GIOChannel
591 * @size: the size of the buffer. 0 == pick a good size
593 * Set the buffer size.
596 g_io_channel_set_buffer_size (GIOChannel *channel,
599 g_return_if_fail (channel != NULL);
602 size = G_IO_NICE_BUF_SIZE;
604 if (size < MAX_CHAR_SIZE)
605 size = MAX_CHAR_SIZE;
607 channel->buf_size = size;
611 * g_io_channel_get_buffer_size:
612 * @channel: a #GIOChannel
614 * Get the buffer size.
616 * Return value: the size of the buffer.
619 g_io_channel_get_buffer_size (GIOChannel *channel)
621 g_return_val_if_fail (channel != NULL, 0);
623 return channel->buf_size;
627 * g_io_channel_set_line_term:
628 * @channel: a #GIOChannel
629 * @line_term: The line termination string. Use %NULL for auto detect.
630 * Auto detection breaks on "\n", "\r\n", "\r", "\0", and
631 * the unicode paragraph separator. Auto detection should
632 * not be used for anything other than file-based channels.
633 * @length: The length of the termination string. If -1 is passed, the
634 * string is assumed to be null terminated. This option allows
635 * termination strings with embeded nulls.
637 * This sets the string that #GIOChannel uses to determine
638 * where in the file a line break occurs.
641 g_io_channel_set_line_term (GIOChannel *channel,
642 const gchar *line_term,
645 g_return_if_fail (channel != NULL);
646 g_return_if_fail (line_term == NULL || length != 0); /* Disallow "" */
648 if (line_term == NULL)
651 length = strlen (line_term);
653 if (channel->line_term)
654 g_free (channel->line_term);
655 channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
656 channel->line_term_len = length;
660 * g_io_channel_get_line_term:
661 * @channel: a #GIOChannel
662 * @length: a location to return the length of the line terminator
664 * This returns the string that #GIOChannel uses to determine
665 * where in the file a line break occurs. A value of %NULL
666 * indicates auto detection.
668 * Return value: The line termination string. This value
669 * is owned by GLib and must not be freed.
671 G_CONST_RETURN gchar*
672 g_io_channel_get_line_term (GIOChannel *channel,
675 g_return_val_if_fail (channel != NULL, 0);
678 *length = channel->line_term_len;
680 return channel->line_term;
684 * g_io_channel_set_flags:
685 * @channel: a #GIOChannel
686 * @flags: the flags to set on the channel
687 * @error: A location to return an error of type #GIOChannelError
692 g_io_channel_set_flags (GIOChannel *channel,
696 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
697 g_return_val_if_fail ((error == NULL) || (*error == NULL),
700 return (* channel->funcs->io_set_flags)(channel,
701 flags & G_IO_FLAG_SET_MASK,
706 * g_io_channel_get_flags:
707 * @channel: a #GIOChannel
709 * Gets the current flags for a #GIOChannel, including read-only
710 * flags such as %G_IO_FLAG_IS_READABLE.
712 * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
713 * are cached for internal use by the channel when it is created.
714 * If they should change at some later point (e.g. partial shutdown
715 * of a socket with the unix shutdown () function), the user
716 * should immediately call g_io_channel_get_flags () to update
717 * the internal values of these flags.
719 * Return value: the flags which are set on the channel
722 g_io_channel_get_flags (GIOChannel *channel)
726 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
728 flags = (* channel->funcs->io_get_flags) (channel);
730 /* Cross implementation code */
732 if (channel->is_seekable)
733 flags |= G_IO_FLAG_IS_SEEKABLE;
734 if (channel->is_readable)
735 flags |= G_IO_FLAG_IS_READABLE;
736 if (channel->is_writeable)
737 flags |= G_IO_FLAG_IS_WRITEABLE;
743 * g_io_channel_set_close_on_unref:
744 * @channel: a #GIOChannel
745 * @do_close: Whether to close the channel on the final unref of
746 * the GIOChannel data structure. The default value of
747 * this is %TRUE for channels created by g_io_channel_new_file (),
748 * and %FALSE for all other channels.
750 * Setting this flag to %TRUE for a channel you have already closed
751 * can cause problems.
754 g_io_channel_set_close_on_unref (GIOChannel *channel,
757 g_return_if_fail (channel != NULL);
759 channel->close_on_unref = do_close;
763 * g_io_channel_get_close_on_unref:
764 * @channel: a #GIOChannel
766 * Return value: Whether the channel will be closedi on the final unref of
767 * the GIOChannel data structure. The default value of
768 * this is %TRUE for channels created by g_io_channel_new_file (),
769 * and %FALSE for all other channels.
772 g_io_channel_get_close_on_unref (GIOChannel *channel)
774 g_return_val_if_fail (channel != NULL, FALSE);
776 return channel->close_on_unref;
780 * g_io_channel_seek_position:
781 * @channel: a #GIOChannel
782 * @offset: The offset in bytes from the position specified by @type
783 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed in those
784 * cases where a call to g_io_channel_set_encoding ()
785 * is allowed. See the documentation for
786 * g_io_channel_set_encoding () for details.
787 * @error: A location to return an error of type #GIOChannelError
789 * Replacement for g_io_channel_seek() with the new API.
794 g_io_channel_seek_position (GIOChannel* channel,
801 /* For files, only one of the read and write buffers can contain data.
802 * For sockets, both can contain data.
805 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
806 g_return_val_if_fail ((error == NULL) || (*error == NULL),
808 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
812 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
813 if (channel->use_buffer)
815 if (channel->do_encode && channel->encoded_read_buf
816 && channel->encoded_read_buf->len > 0)
818 g_warning ("Seek type G_SEEK_CUR not allowed for this"
819 " channel's encoding.\n");
820 return G_IO_STATUS_ERROR;
822 if (channel->read_buf)
823 offset -= channel->read_buf->len;
824 if (channel->encoded_read_buf)
826 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
828 /* If there's anything here, it's because the encoding is UTF-8,
829 * so we can just subtract the buffer length, the same as for
830 * the unencoded data.
833 offset -= channel->encoded_read_buf->len;
841 g_warning ("g_io_channel_seek_position: unknown seek type");
842 return G_IO_STATUS_ERROR;
845 if (channel->use_buffer)
847 status = g_io_channel_flush (channel, error);
848 if (status != G_IO_STATUS_NORMAL)
852 status = channel->funcs->io_seek (channel, offset, type, error);
854 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
856 if (channel->read_buf)
857 g_string_truncate (channel->read_buf, 0);
859 /* Conversion state no longer matches position in file */
860 if (channel->read_cd != (GIConv) -1)
861 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
862 if (channel->write_cd != (GIConv) -1)
863 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
865 if (channel->encoded_read_buf)
867 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
868 g_string_truncate (channel->encoded_read_buf, 0);
871 if (channel->partial_write_buf[0] != '\0')
873 g_warning ("Partial character at end of write buffer not flushed.\n");
874 channel->partial_write_buf[0] = '\0';
882 * g_io_channel_flush:
883 * @channel: a #GIOChannel
884 * @error: location to store an error of type #GIOChannelError
886 * Flush the write buffer for the GIOChannel.
888 * Return value: the status of the operation: One of
889 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
890 * G_IO_CHANNEL_ERROR.
893 g_io_channel_flush (GIOChannel *channel,
897 gsize this_time = 1, bytes_written = 0;
899 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
900 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
902 if (channel->write_buf == NULL || channel->write_buf->len == 0)
903 return G_IO_STATUS_NORMAL;
907 g_assert (this_time > 0);
909 status = channel->funcs->io_write (channel,
910 channel->write_buf->str + bytes_written,
911 channel->write_buf->len - bytes_written,
913 bytes_written += this_time;
915 while ((bytes_written < channel->write_buf->len)
916 && (status == G_IO_STATUS_NORMAL));
918 g_string_erase (channel->write_buf, 0, bytes_written);
924 * g_io_channel_set_buffered:
925 * @channel: a #GIOChannel
926 * @buffered: whether to set the channel buffered or unbuffered
928 * The buffering state can only be set if the channel's encoding
929 * is %NULL. For any other encoding, the channel must be buffered.
931 * A buffered channel can only be set unbuffered if the channel's
932 * internal buffers have been flushed. Newly created channels or
933 * channels which have returned G_IO_STATUS_EOF
934 * not require such a flush. For write-only channels, a call to
935 * g_io_channel_flush () is sufficient. For all other channels,
936 * the buffers may be flushed by a call to g_io_channel_seek_position ().
937 * This includes the possibility of seeking with seek type %G_SEEK_CUR
938 * and an offset of zero. Note that this means that socket-based
939 * channels cannot be set unbuffered once they have had data
942 * On unbuffered channels, it is safe to mix read and write
943 * calls from the new and old APIs, if this is necessary for
944 * maintaining old code.
946 * The default state of the channel is buffered.
949 g_io_channel_set_buffered (GIOChannel *channel,
952 g_return_if_fail (channel != NULL);
954 if (channel->encoding != NULL)
956 g_warning ("Need to have NULL encoding to set the buffering state of the "
961 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
962 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
964 channel->use_buffer = buffered;
968 * g_io_channel_get_buffered:
969 * @channel: a #GIOChannel
971 * Return Value: the buffering state of the channel
974 g_io_channel_get_buffered (GIOChannel *channel)
976 g_return_val_if_fail (channel != NULL, FALSE);
978 return channel->use_buffer;
982 * g_io_channel_set_encoding:
983 * @channel: a #GIOChannel
984 * @encoding: the encoding type
985 * @error: location to store an error of type #GConvertError.
987 * Set the encoding for the input/output of the channel. The internal
988 * encoding is always UTF-8. The default encoding for the
989 * external file is UTF-8.
991 * The encoding %NULL is safe to use with binary data.
993 * The encoding can only be set under the following conditions:
995 * 1. The channel was just created, and has not been written to
998 * 2. The channel is write-only.
1000 * 3. The channel is a file, and the file pointer was just
1001 * repositioned by a call to g_io_channel_seek_position().
1002 * (This flushes all the internal buffers.)
1004 * 4. The current encoding is %NULL or UTF-8.
1006 * 5. One of the (new API) read functions has just returned G_IO_STATUS_EOF
1007 * (or, in the case of g_io_channel_read_to_end (), G_IO_STATUS_NORMAL).
1009 * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar ()
1010 * has returned G_IO_STATUS_AGAIN or G_IO_STATUS_ERROR. This may be
1011 * useful in the case of G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
1012 * Returning one of these statuses from g_io_channel_read_line (),
1013 * g_io_channel_read_line_string (), or g_io_channel_read_to_end ()
1014 * does _not_ guarantee that the encoding can be changed.
1016 * Channels which do not meet the above conditions cannot call
1017 * g_io_channel_seek_position () with an offset of %G_SEEK_CUR,
1018 * and if they are "seekable" cannot
1019 * call g_io_channel_write_chars () after calling one
1020 * of the API "read" functions.
1022 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
1025 g_io_channel_set_encoding (GIOChannel *channel,
1026 const gchar *encoding,
1029 GIConv read_cd, write_cd;
1030 gboolean did_encode;
1032 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1033 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
1035 /* Make sure the encoded buffers are empty */
1037 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
1038 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
1040 if (!channel->use_buffer)
1042 g_warning ("Need to set the channel buffered before setting the encoding.\n");
1043 g_warning ("Assuming this is what you meant and acting accordingly.\n");
1045 channel->use_buffer = TRUE;
1048 if (channel->partial_write_buf[0] != '\0')
1050 g_warning ("Partial character at end of write buffer not flushed.\n");
1051 channel->partial_write_buf[0] = '\0';
1054 did_encode = channel->do_encode;
1056 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
1058 channel->do_encode = FALSE;
1059 read_cd = write_cd = (GIConv) -1;
1064 const gchar *from_enc = NULL, *to_enc = NULL;
1066 if (channel->is_readable)
1068 read_cd = g_iconv_open ("UTF-8", encoding);
1070 if (read_cd == (GIConv) -1)
1078 read_cd = (GIConv) -1;
1080 if (channel->is_writeable && err == 0)
1082 write_cd = g_iconv_open (encoding, "UTF-8");
1084 if (write_cd == (GIConv) -1)
1087 from_enc = encoding;
1092 write_cd = (GIConv) -1;
1096 g_assert (from_enc);
1100 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1101 _("Conversion from character set `%s' to `%s' is not supported"),
1104 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1105 _("Could not open converter from `%s' to `%s': %s"),
1106 from_enc, to_enc, strerror (err));
1108 if (read_cd != (GIConv) -1)
1109 g_iconv_close (read_cd);
1110 if (write_cd != (GIConv) -1)
1111 g_iconv_close (write_cd);
1113 return G_IO_STATUS_ERROR;
1116 channel->do_encode = TRUE;
1119 /* The encoding is ok, so set the fields in channel */
1121 if (channel->read_cd != (GIConv) -1)
1122 g_iconv_close (channel->read_cd);
1123 if (channel->write_cd != (GIConv) -1)
1124 g_iconv_close (channel->write_cd);
1126 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1128 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1130 /* This is just validated UTF-8, so we can copy it back into read_buf
1131 * so it can be encoded in whatever the new encoding is.
1134 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1135 channel->encoded_read_buf->len);
1136 g_string_truncate (channel->encoded_read_buf, 0);
1139 channel->read_cd = read_cd;
1140 channel->write_cd = write_cd;
1142 g_free (channel->encoding);
1143 channel->encoding = g_strdup (encoding);
1145 return G_IO_STATUS_NORMAL;
1149 * g_io_channel_get_encoding:
1150 * @channel: a #GIOChannel
1152 * Get the encoding for the input/output of the channel. The internal
1153 * encoding is always UTF-8. The encoding %NULL makes the
1154 * channel safe for binary data.
1156 * Return value: A string containing the encoding, this string is
1157 * owned by GLib and must not be freed.
1159 G_CONST_RETURN gchar*
1160 g_io_channel_get_encoding (GIOChannel *channel)
1162 g_return_val_if_fail (channel != NULL, NULL);
1164 return channel->encoding;
1168 g_io_channel_fill_buffer (GIOChannel *channel,
1171 gsize read_size, cur_len, oldlen;
1174 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1176 status = g_io_channel_flush (channel, err);
1177 if (status != G_IO_STATUS_NORMAL)
1180 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1182 g_warning ("Partial character at end of write buffer not flushed.\n");
1183 channel->partial_write_buf[0] = '\0';
1186 if (!channel->read_buf)
1187 channel->read_buf = g_string_sized_new (channel->buf_size);
1189 cur_len = channel->read_buf->len;
1191 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1193 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1194 channel->buf_size, &read_size, err);
1196 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1198 g_string_truncate (channel->read_buf, read_size + cur_len);
1200 if ((status != G_IO_STATUS_NORMAL)
1201 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1204 g_assert (channel->read_buf->len > 0);
1206 if (channel->encoded_read_buf)
1207 oldlen = channel->encoded_read_buf->len;
1211 if (channel->encoding)
1212 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1215 if (channel->do_encode)
1217 size_t errnum, inbytes_left, outbytes_left;
1218 gchar *inbuf, *outbuf;
1221 g_assert (channel->encoded_read_buf);
1225 inbytes_left = channel->read_buf->len;
1226 outbytes_left = MAX (channel->read_buf->len,
1227 channel->encoded_read_buf->allocated_len
1228 - channel->encoded_read_buf->len - 1); /* 1 for NULL */
1229 outbytes_left = MAX (outbytes_left, 6);
1231 inbuf = channel->read_buf->str;
1232 g_string_set_size (channel->encoded_read_buf,
1233 channel->encoded_read_buf->len + outbytes_left);
1234 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len
1237 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1238 &outbuf, &outbytes_left);
1241 g_assert (inbuf + inbytes_left == channel->read_buf->str
1242 + channel->read_buf->len);
1243 g_assert (outbuf + outbytes_left == channel->encoded_read_buf->str
1244 + channel->encoded_read_buf->len);
1246 g_string_erase (channel->read_buf, 0,
1247 channel->read_buf->len - inbytes_left);
1248 g_string_truncate (channel->encoded_read_buf,
1249 channel->encoded_read_buf->len - outbytes_left);
1251 if (errnum == (size_t) -1)
1256 if ((oldlen == channel->encoded_read_buf->len)
1257 && (status == G_IO_STATUS_EOF))
1258 status = G_IO_STATUS_EOF;
1260 status = G_IO_STATUS_NORMAL;
1263 /* Buffer size at least 6, wrote at least on character */
1264 g_assert (inbuf != channel->read_buf->str);
1267 if (oldlen < channel->encoded_read_buf->len)
1268 status = G_IO_STATUS_NORMAL;
1271 g_set_error (err, G_CONVERT_ERROR,
1272 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1273 _("Invalid byte sequence in conversion input"));
1274 return G_IO_STATUS_ERROR;
1278 g_assert (errval != EBADF); /* The converter should be open */
1279 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1280 _("Error during conversion: %s"), strerror (errval));
1281 return G_IO_STATUS_ERROR;
1284 g_assert ((status != G_IO_STATUS_NORMAL)
1285 || (channel->encoded_read_buf->len > 0));
1287 else if (channel->encoding) /* UTF-8 */
1289 gchar *nextchar, *lastchar;
1291 g_assert (channel->encoded_read_buf);
1293 nextchar = channel->read_buf->str;
1294 lastchar = channel->read_buf->str + channel->read_buf->len;
1296 while (nextchar < lastchar)
1300 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1305 /* stop, leave partial character in buffer */
1306 lastchar = nextchar;
1309 if (oldlen < channel->encoded_read_buf->len)
1310 status = G_IO_STATUS_NORMAL;
1313 g_set_error (err, G_CONVERT_ERROR,
1314 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1315 _("Invalid byte sequence in conversion input"));
1316 status = G_IO_STATUS_ERROR;
1318 lastchar = nextchar;
1321 nextchar = g_utf8_next_char (nextchar);
1326 if (lastchar > channel->read_buf->str)
1328 gint copy_len = lastchar - channel->read_buf->str;
1330 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1332 g_string_erase (channel->read_buf, 0, copy_len);
1340 * g_io_channel_read_line:
1341 * @channel: a #GIOChannel
1342 * @str_return: The line read from the #GIOChannel, not including the
1343 * line terminator. This data should be freed with g_free()
1344 * when no longer needed. This
1345 * is a null terminated string. If a @length of zero is
1346 * returned, this will be %NULL instead.
1347 * @length: location to store length of the read data, or %NULL
1348 * @terminator_pos: location to store position of line terminator, or %NULL
1349 * @error: A location to return an error of type #GConvertError
1350 * or #GIOChannelError
1352 * Read a line, including the terminating character(s),
1353 * from a #GIOChannel into a newly allocated string.
1354 * @length will contain allocated memory if the return
1355 * is %G_IO_STATUS_NORMAL.
1357 * Return value: a newly allocated string. Free this string
1358 * with g_free() when you are done with it.
1361 g_io_channel_read_line (GIOChannel *channel,
1364 gsize *terminator_pos,
1370 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1371 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1372 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1374 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1376 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1379 *length = got_length;
1381 if (status == G_IO_STATUS_NORMAL)
1383 g_assert (USE_BUF (channel));
1384 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1385 g_string_erase (USE_BUF (channel), 0, got_length);
1394 * g_io_channel_read_line_string:
1395 * @channel: a #GIOChannel
1396 * @buffer: a #GString into which the line will be written.
1397 * If @buffer already contains data, the old data will
1399 * @terminator_pos: location to store position of line terminator, or %NULL
1400 * @error: a location to store an error of type #GConvertError
1401 * or #GIOChannelError
1403 * Read a line from a #GIOChannel, using a #GString as a buffer.
1408 g_io_channel_read_line_string (GIOChannel *channel,
1410 gsize *terminator_pos,
1416 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1417 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1418 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1420 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1422 if (buffer->len > 0)
1423 g_string_truncate (buffer, 0); /* clear out the buffer */
1425 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1427 if (status == G_IO_STATUS_NORMAL)
1429 g_assert (USE_BUF (channel));
1430 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1431 g_string_erase (USE_BUF (channel), 0, length);
1439 g_io_channel_read_line_backend (GIOChannel *channel,
1441 gsize *terminator_pos,
1445 gsize checked_to, line_term_len, line_length, got_term_len;
1446 gboolean first_time = TRUE;
1448 if (!channel->use_buffer)
1450 /* Can't do a raw read in read_line */
1451 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1452 _("Can't do a raw read in g_io_channel_read_line_string"));
1453 return G_IO_STATUS_ERROR;
1456 status = G_IO_STATUS_NORMAL;
1458 if (channel->line_term)
1459 line_term_len = channel->line_term_len;
1462 /* This value used for setting checked_to, it's the longest of the four
1463 * we autodetect for.
1470 gchar *nextchar, *lastchar;
1473 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1476 status = g_io_channel_fill_buffer (channel, error);
1479 case G_IO_STATUS_NORMAL:
1480 if (BUF_LEN (USE_BUF (channel)) == 0)
1481 /* Can happen when using conversion and only read
1482 * part of a character
1489 case G_IO_STATUS_EOF:
1490 if (BUF_LEN (USE_BUF (channel)) == 0)
1495 if (channel->encoding && channel->read_buf->len != 0)
1497 g_set_error (error, G_CONVERT_ERROR,
1498 G_CONVERT_ERROR_PARTIAL_INPUT,
1499 "Leftover unconverted data in read buffer");
1500 return G_IO_STATUS_ERROR;
1503 return G_IO_STATUS_EOF;
1513 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1515 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1519 lastchar = use_buf->str + use_buf->len;
1521 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1522 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1524 if (channel->line_term)
1526 if (memcmp (channel->line_term, nextchar, line_term_len) == 0)
1528 line_length = nextchar - use_buf->str;
1529 got_term_len = line_term_len;
1533 else /* auto detect */
1537 case '\n': /* unix */
1538 line_length = nextchar - use_buf->str;
1541 case '\r': /* Warning: do not use with sockets */
1542 line_length = nextchar - use_buf->str;
1543 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1544 && (lastchar == use_buf->str + use_buf->len))
1545 goto read_again; /* Try to read more data */
1546 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1551 case '\xe2': /* Unicode paragraph separator */
1552 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1554 line_length = nextchar - use_buf->str;
1559 case '\0': /* Embeded null in input */
1560 line_length = nextchar - use_buf->str;
1563 default: /* no match */
1569 /* If encoding != NULL, valid UTF-8, didn't overshoot */
1570 g_assert (nextchar == lastchar);
1574 if (status == G_IO_STATUS_EOF)
1576 if (channel->encoding && channel->read_buf->len > 0)
1578 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1579 "Channel terminates in a partial character");
1580 return G_IO_STATUS_ERROR;
1582 line_length = use_buf->len;
1587 checked_to = MAX (use_buf->len - (line_term_len - 1), 0);
1593 *terminator_pos = line_length;
1596 *length = line_length + got_term_len;
1598 return G_IO_STATUS_NORMAL;
1602 * g_io_channel_read_to_end:
1603 * @channel: a #GIOChannel
1604 * @str_return: Location to store a pointer to a string holding
1605 * the remaining data in the #GIOChannel. This data should
1606 * be freed with g_free() when no longer needed. This
1607 * data is terminated by an extra null, but there may be other
1608 * nulls in the intervening data.
1609 * @length: Location to store length of the data
1610 * @error: A location to return an error of type #GConvertError
1611 * or #GIOChannelError
1613 * Read all the remaining data from the file.
1615 * Return value: %G_IO_STATUS_NORMAL on success. This function never
1616 * returns %G_IO_STATUS_EOF.
1619 g_io_channel_read_to_end (GIOChannel *channel,
1626 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1627 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1629 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1636 if (!channel->use_buffer)
1638 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1639 _("Can't do a raw read in g_io_channel_read_to_end"));
1640 return G_IO_STATUS_ERROR;
1644 status = g_io_channel_fill_buffer (channel, error);
1645 while (status == G_IO_STATUS_NORMAL);
1647 if (status != G_IO_STATUS_EOF)
1650 if (channel->encoding && channel->read_buf->len > 0)
1652 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1653 "Channel terminates in a partial character");
1654 return G_IO_STATUS_ERROR;
1657 if (USE_BUF (channel) == NULL)
1659 /* length is already set to zero */
1661 *str_return = g_strdup ("");
1666 *length = USE_BUF (channel)->len;
1669 *str_return = g_string_free (USE_BUF (channel), FALSE);
1671 g_string_free (USE_BUF (channel), TRUE);
1673 if (channel->encoding)
1674 channel->encoded_read_buf = NULL;
1676 channel->read_buf = NULL;
1679 return G_IO_STATUS_NORMAL;
1683 * g_io_channel_read_chars:
1684 * @channel: a #GIOChannel
1685 * @buf: a buffer to read data into
1686 * @count: the size of the buffer. Note that the buffer may
1687 * not be complelely filled even if there is data
1688 * in the buffer if the remaining data is not a
1689 * complete character.
1690 * @bytes_read: The number of bytes read. This may be zero even on
1691 * success if count < 6 and the channel's encoding is non-%NULL.
1692 * This indicates that the next UTF-8 character is too wide for
1694 * @error: A location to return an error of type #GConvertError
1695 * or #GIOChannelError.
1697 * Replacement for g_io_channel_read() with the new API.
1702 g_io_channel_read_chars (GIOChannel *channel,
1711 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1712 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1714 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1719 return G_IO_STATUS_NORMAL;
1721 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1723 if (!channel->use_buffer)
1727 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1729 status = channel->funcs->io_read (channel, buf, count, &tmp_bytes, error);
1732 *bytes_read = tmp_bytes;
1737 status = G_IO_STATUS_NORMAL;
1739 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1740 status = g_io_channel_fill_buffer (channel, error);
1742 /* Only return an error if we have no data */
1744 if (BUF_LEN (USE_BUF (channel)) == 0)
1746 g_assert (status != G_IO_STATUS_NORMAL);
1748 if (status == G_IO_STATUS_EOF && channel->encoding
1749 && BUF_LEN (channel->read_buf) > 0)
1751 g_set_error (error, G_CONVERT_ERROR,
1752 G_CONVERT_ERROR_PARTIAL_INPUT,
1753 "Leftover unconverted data in read buffer");
1754 status = G_IO_STATUS_ERROR;
1763 if (status == G_IO_STATUS_ERROR)
1764 g_clear_error (error);
1766 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1768 g_assert (got_bytes > 0);
1770 if (channel->encoding)
1771 /* Don't validate for NULL encoding, binary safe */
1773 gchar *nextchar, *prevchar;
1775 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1777 nextchar = channel->encoded_read_buf->str;
1781 prevchar = nextchar;
1782 nextchar = g_utf8_next_char (nextchar);
1783 g_assert (nextchar != prevchar); /* Possible for *prevchar of -1 or -2 */
1785 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1787 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1788 got_bytes = prevchar - channel->encoded_read_buf->str;
1790 g_assert (got_bytes > 0 || count < 6);
1793 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1794 g_string_erase (USE_BUF (channel), 0, got_bytes);
1797 *bytes_read = got_bytes;
1799 return G_IO_STATUS_NORMAL;
1803 * g_io_channel_read_unichar:
1804 * @channel: a #GIOChannel
1805 * @thechar: a location to return a character
1806 * @error: A location to return an error of type #GConvertError
1807 * or #GIOChannelError
1809 * This function cannot be called on a channel with %NULL encoding.
1811 * Return value: a #GIOStatus
1814 g_io_channel_read_unichar (GIOChannel *channel,
1818 GIOStatus status = G_IO_STATUS_NORMAL;
1820 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1821 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1822 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1824 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1826 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1827 status = g_io_channel_fill_buffer (channel, error);
1829 /* Only return an error if we have no data */
1831 if (BUF_LEN (USE_BUF (channel)) == 0)
1833 g_assert (status != G_IO_STATUS_NORMAL);
1835 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1837 g_set_error (error, G_CONVERT_ERROR,
1838 G_CONVERT_ERROR_PARTIAL_INPUT,
1839 "Leftover unconverted data in read buffer");
1840 status = G_IO_STATUS_ERROR;
1844 *thechar = (gunichar) -1;
1849 if (status == G_IO_STATUS_ERROR)
1850 g_clear_error (error);
1853 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1855 g_string_erase (channel->encoded_read_buf, 0,
1856 g_utf8_next_char (channel->encoded_read_buf->str)
1857 - channel->encoded_read_buf->str);
1859 return G_IO_STATUS_NORMAL;
1863 * g_io_channel_write_chars:
1864 * @channel: a #GIOChannel
1865 * @buf: a buffer to write data from
1866 * @count: the size of the buffer. If -1, the buffer
1867 * is taken to be a nul terminated string.
1868 * @bytes_written: The number of bytes written. This can be nonzero
1869 * even if the return value is not %G_IO_STATUS_NORMAL.
1870 * If the return value is %G_IO_STATUS_NORMAL and the
1871 * channel is blocking, this will always be equal
1872 * to @count if @count >= 0.
1873 * @error: A location to return an error of type #GConvertError
1874 * or #GIOChannelError
1876 * Replacement for g_io_channel_write() with the new API.
1878 * On seekable channels with encodings other than %NULL or UTF-8, generic
1879 * mixing of reading and writing is not allowed. A call to g_io_channel_write_chars ()
1880 * may only be made on a channel from which data has been read in the
1881 * cases described in the documentation for g_io_channel_set_encoding ().
1886 g_io_channel_write_chars (GIOChannel *channel,
1889 gsize *bytes_written,
1893 gssize wrote_bytes = 0;
1895 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1896 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1898 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1900 if ((count < 0) && buf)
1901 count = strlen (buf);
1907 return G_IO_STATUS_NORMAL;
1910 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1911 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1913 /* Raw write case */
1915 if (!channel->use_buffer)
1919 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1920 g_assert (channel->partial_write_buf[0] == '\0');
1922 status = channel->funcs->io_write (channel, buf, count, &tmp_bytes, error);
1925 *bytes_written = tmp_bytes;
1932 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1933 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1935 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1937 g_warning("Mixed reading and writing not allowed on encoded files");
1938 return G_IO_STATUS_ERROR;
1940 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1941 if (status != G_IO_STATUS_NORMAL)
1949 if (!channel->write_buf)
1950 channel->write_buf = g_string_sized_new (channel->buf_size);
1952 while (wrote_bytes < count)
1956 /* If the buffer is full, try a write immediately. In
1957 * the nonblocking case, this prevents the user from
1958 * writing just a little bit to the buffer every time
1959 * and never receiving an EAGAIN.
1962 if (channel->write_buf->len >= channel->buf_size)
1964 gsize did_write = 0, this_time;
1968 status = channel->funcs->io_write (channel, channel->write_buf->str
1969 + did_write, channel->write_buf->len
1970 - did_write, &this_time, error);
1971 did_write += this_time;
1973 while (status == G_IO_STATUS_NORMAL &&
1974 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
1976 g_string_erase (channel->write_buf, 0, did_write);
1978 if (status != G_IO_STATUS_NORMAL)
1980 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
1981 status = G_IO_STATUS_NORMAL;
1983 *bytes_written = wrote_bytes;
1988 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len - 1)
1989 - channel->write_buf->len; /* 1 for NULL */
1991 /* This is only true because g_io_channel_set_buffer_size ()
1992 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
1994 g_assert (space_in_buf >= MAX_CHAR_SIZE);
1996 if (!channel->encoding)
1998 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
2000 g_string_append_len (channel->write_buf, buf, write_this);
2002 wrote_bytes += write_this;
2006 const gchar *from_buf;
2007 gsize from_buf_len, from_buf_old_len, left_len;
2011 if (channel->partial_write_buf[0] != '\0')
2013 g_assert (wrote_bytes == 0);
2015 from_buf = channel->partial_write_buf;
2016 from_buf_old_len = strlen (channel->partial_write_buf);
2017 g_assert (from_buf_old_len > 0);
2018 from_buf_len = MIN (6, from_buf_old_len + count);
2020 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
2021 from_buf_len - from_buf_old_len);
2026 from_buf_len = count - wrote_bytes;
2027 from_buf_old_len = 0;
2032 if (!channel->do_encode) /* UTF-8 encoding */
2034 const gchar *badchar;
2035 gsize try_len = MIN (from_buf_len, space_in_buf);
2037 /* UTF-8, just validate, emulate g_iconv */
2039 if (!g_utf8_validate (from_buf, try_len, &badchar))
2043 left_len = from_buf + try_len - badchar;
2045 try_char = g_utf8_get_char_validated (badchar, left_len);
2050 g_assert (left_len < 6);
2051 if (try_len == from_buf_len)
2063 g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
2064 /* FIXME bail here? */
2069 g_assert_not_reached ();
2071 errnum = 0; /* Don't confunse the compiler */
2081 g_string_append_len (channel->write_buf, from_buf,
2082 try_len - left_len);
2083 from_buf += try_len - left_len;
2089 left_len = from_buf_len;
2090 g_string_set_size (channel->write_buf, channel->write_buf->len
2092 outbuf = channel->write_buf->str + channel->write_buf->len
2094 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
2095 &outbuf, &space_in_buf);
2097 g_string_truncate (channel->write_buf, channel->write_buf->len
2101 if (err == (size_t) -1)
2106 g_assert (left_len < 6);
2108 if (from_buf_old_len == 0)
2110 /* Not from partial_write_buf */
2112 memcpy (channel->partial_write_buf, from_buf, left_len);
2113 channel->partial_write_buf[left_len] = '\0';
2115 *bytes_written = count;
2116 return G_IO_STATUS_NORMAL;
2119 /* Working in partial_write_buf */
2121 if (left_len == from_buf_len)
2123 /* Didn't convert anything, must still have
2124 * less than a full character
2127 g_assert (count == from_buf_len - from_buf_old_len);
2129 channel->partial_write_buf[from_buf_len] = '\0';
2132 *bytes_written = count;
2134 return G_IO_STATUS_NORMAL;
2137 g_assert (from_buf_len - left_len >= from_buf_old_len);
2139 /* We converted all the old data. This is fine */
2143 if (from_buf_len == left_len)
2145 /* Nothing was written, add enough space for
2146 * at least one character.
2148 space_in_buf += MAX_CHAR_SIZE;
2153 g_set_error (error, G_CONVERT_ERROR,
2154 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2155 _("Invalid byte sequence in conversion input"));
2156 if (from_buf_old_len > 0 && from_buf_len == left_len)
2157 g_warning ("Illegal sequence due to partial character "
2158 "at the end of a previous write.\n");
2160 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2162 *bytes_written = wrote_bytes;
2163 channel->partial_write_buf[0] = '\0';
2164 return G_IO_STATUS_ERROR;
2166 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2167 _("Error during conversion: %s"), strerror (errnum));
2168 if (from_buf_len >= left_len + from_buf_old_len)
2169 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2171 *bytes_written = wrote_bytes;
2172 channel->partial_write_buf[0] = '\0';
2173 return G_IO_STATUS_ERROR;
2177 g_assert (from_buf_len - left_len >= from_buf_old_len);
2179 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2181 if (from_buf_old_len > 0)
2183 /* We were working in partial_write_buf */
2185 buf += from_buf_len - left_len - from_buf_old_len;
2186 channel->partial_write_buf[0] = '\0';
2194 *bytes_written = count;
2196 return G_IO_STATUS_NORMAL;
2200 * g_io_channel_write_unichar:
2201 * @channel: a #GIOChannel
2202 * @thechar: a character
2203 * @error: A location to return an error of type #GConvertError
2204 * or #GIOChannelError
2206 * This function cannot be called on a channel with %NULL encoding.
2208 * Return value: a #GIOStatus
2211 g_io_channel_write_unichar (GIOChannel *channel,
2216 gchar static_buf[6];
2217 gsize char_len, wrote_len;
2219 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2220 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2221 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2223 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2225 char_len = g_unichar_to_utf8 (thechar, static_buf);
2227 if (channel->partial_write_buf[0] != '\0')
2229 g_warning ("Partial charater written before writing unichar.\n");
2230 channel->partial_write_buf[0] = '\0';
2233 status = g_io_channel_write_chars (channel, static_buf,
2234 char_len, &wrote_len, error);
2236 /* We validate UTF-8, so we can't get a partial write */
2238 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2244 * g_io_channel_error_quark:
2246 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2249 g_io_channel_error_quark (void)
2251 static GQuark q = 0;
2253 q = g_quark_from_static_string ("g-io-channel-error-quark");