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/.
42 #undef G_DISABLE_DEPRECATED
48 #define G_IO_NICE_BUF_SIZE 1024
50 static GIOError g_io_error_get_from_g_error (GIOStatus status,
52 static void g_io_channel_purge (GIOChannel *channel);
53 static GIOStatus g_io_channel_fill_buffer (GIOChannel *channel,
57 g_io_channel_init (GIOChannel *channel)
59 channel->channel_flags = 0;
60 channel->ref_count = 1;
61 channel->encoding = NULL;
62 channel->line_term = NULL;
63 channel->buf_size = G_IO_NICE_BUF_SIZE;
64 channel->read_buf = g_string_sized_new (G_IO_NICE_BUF_SIZE);
65 channel->encoded_read_buf = NULL; /* Allocate if we get an encoding */
66 channel->write_buf = g_string_sized_new (G_IO_NICE_BUF_SIZE);
67 channel->use_buffer = TRUE;
68 channel->do_encode = FALSE;
69 channel->ready_to_read = TRUE;
70 channel->ready_to_write = TRUE;
71 channel->close_on_unref = FALSE;
72 channel->seekable_cached = FALSE;
76 g_io_channel_ref (GIOChannel *channel)
78 g_return_if_fail (channel != NULL);
84 g_io_channel_unref (GIOChannel *channel)
86 g_return_if_fail (channel != NULL);
89 if (channel->ref_count == 0)
91 if (channel->close_on_unref)
92 g_io_channel_close (channel);
94 g_io_channel_purge (channel);
95 g_free (channel->encoding);
96 if (channel->do_encode)
98 g_iconv_close (channel->read_cd);
99 g_iconv_close (channel->write_cd);
100 g_string_free (channel->encoded_read_buf, TRUE);
102 g_free (channel->line_term);
103 g_string_free (channel->read_buf, TRUE);
104 g_string_free (channel->write_buf, TRUE);
105 channel->funcs->io_free (channel);
110 g_io_error_get_from_g_error (GIOStatus status,
115 case G_IO_STATUS_NORMAL:
116 case G_IO_STATUS_EOF:
117 return G_IO_ERROR_NONE;
118 case G_IO_STATUS_AGAIN:
119 return G_IO_ERROR_AGAIN;
120 case G_IO_STATUS_ERROR:
121 if (err->domain != G_IO_CHANNEL_ERROR)
122 return G_IO_ERROR_UNKNOWN;
125 case G_IO_CHANNEL_ERROR_INVAL:
126 return G_IO_ERROR_INVAL;
128 return G_IO_ERROR_UNKNOWN;
131 g_assert_not_reached ();
132 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
138 * @channel: a #GIOChannel.
139 * @buf: a buffer to read the data into (which should be at least count bytes long).
140 * @count: the number of bytes to read from the #GIOChannel.
141 * @bytes_read: returns the number of bytes actually read.
143 * Reads data from a #GIOChannel. This function is depricated. New code should
144 * use g_io_channel_read_chars() instead.
146 * Return value: %G_IO_ERROR_NONE if the operation was successful.
149 g_io_channel_read (GIOChannel *channel,
158 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
159 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
161 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
163 error = g_io_error_get_from_g_error (status, err);
172 * g_io_channel_write:
173 * @channel: a #GIOChannel.
174 * @buf: the buffer containing the data to write.
175 * @count: the number of bytes to write.
176 * @bytes_written: the number of bytes actually written.
178 * Writes data to a #GIOChannel. This function is depricated. New code should
179 * use g_io_channel_write_chars() instead.
181 * Return value: %G_IO_ERROR_NONE if the operation was successful.
184 g_io_channel_write (GIOChannel *channel,
187 gsize *bytes_written)
193 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
194 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
196 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
198 error = g_io_error_get_from_g_error (status, err);
208 * @channel: a #GIOChannel.
209 * @offset: an offset, in bytes, which is added to the position specified by @type
210 * @type: the position in the file, which can be %G_SEEK_CUR (the current
211 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
214 * Sets the current position in the #GIOChannel, similar to the standard library
215 * function fseek(). This function is depricated. New code should
216 * use g_io_channel_seek_position() instead.
218 * Return value: %G_IO_ERROR_NONE if the operation was successful.
221 g_io_channel_seek (GIOChannel *channel,
229 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
238 g_warning ("g_io_channel_seek: unknown seek type");
239 return G_IO_ERROR_UNKNOWN;
242 status = channel->funcs->io_seek (channel, offset, type, &err);
244 error = g_io_error_get_from_g_error (status, err);
252 /* The function g_io_channel_new_file() is prototyped in both
253 * giounix.c and giowin32.c, so we stick its documentation here.
257 * g_io_channel_new_file:
258 * @filename: A string containing the name of a file.
259 * @mode: A #GIOFileMode
260 * @error: A location to return an error of type %G_IO_CHANNEL_ERROR.
262 * Open a file @filename as a #GIOChannel using mode @mode. This
263 * channel will be closed when the last reference to it is dropped,
264 * so there is no need to call g_io_channel_close() (though doing
265 * so will not cause problems, as long as no attempt is made to
266 * access the channel after it is closed).
268 * Return value: A #GIOChannel on success, %NULL on failure.
272 * g_io_channel_close:
273 * @channel: A #GIOChannel
275 * Close an IO channel. Any pending data to be written will be
276 * flushed, ignoring errors. The channel will not be freed until the
277 * last reference is dropped using g_io_channel_unref(). This
278 * function is deprecated: you should use g_io_channel_shutdown()
282 g_io_channel_close (GIOChannel *channel)
286 g_return_if_fail (channel != NULL);
288 g_io_channel_purge (channel);
290 channel->funcs->io_close (channel, &err);
293 { /* No way to return the error */
294 g_warning ("Error closing channel: %s", err->message);
298 channel->close_on_unref = FALSE; /* Because we already did */
299 channel->ready_to_read = FALSE;
300 channel->ready_to_write = FALSE;
304 * g_io_channel_shutdown:
305 * @channel: a #GIOChannel
306 * @flush: if %TRUE, flush pending
307 * @err: location to store a #GIOChannelError
309 * Close an IO channel. Any pending data to be written will be
310 * flushed. The channel will not be freed until the
311 * last reference is dropped using g_io_channel_unref().
314 g_io_channel_shutdown (GIOChannel *channel,
319 GIOStatus result = G_IO_STATUS_NORMAL;
321 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
323 if (flush && channel->write_buf->len > 0)
327 /* Set the channel to blocking, to avoid a busy loop
329 flags = g_io_channel_get_flags (channel);
330 status = g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
331 if (status != G_IO_STATUS_NORMAL)
332 result = G_IO_STATUS_ERROR;
334 g_io_channel_flush (channel, err);
337 status = channel->funcs->io_close (channel, err && *err ? NULL : err);
338 if (status != G_IO_STATUS_NORMAL)
339 result = G_IO_STATUS_ERROR;
341 channel->close_on_unref = FALSE; /* Because we already did */
342 channel->ready_to_read = FALSE;
343 channel->ready_to_write = FALSE;
348 /* This function is used for the final flush on close or unref */
350 g_io_channel_purge (GIOChannel *channel)
354 g_return_if_fail (channel != NULL);
356 if (channel->write_buf->len > 0)
360 /* Set the channel to blocking, to avoid a busy loop
362 flags = g_io_channel_get_flags (channel);
363 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
365 g_io_channel_flush (channel, &err);
368 { /* No way to return the error */
369 g_warning ("Error flushing string: %s", err->message);
374 /* Flush these in case anyone tries to close without unrefing */
376 g_string_truncate (channel->read_buf, 0);
377 if (channel->do_encode)
378 g_string_truncate (channel->encoded_read_buf, 0);
379 g_string_truncate (channel->write_buf, 0);
383 g_io_create_watch (GIOChannel *channel,
384 GIOCondition condition)
386 g_return_val_if_fail (channel != NULL, NULL);
388 return channel->funcs->io_create_watch (channel, condition);
392 g_io_add_watch_full (GIOChannel *channel,
394 GIOCondition condition,
397 GDestroyNotify notify)
402 g_return_val_if_fail (channel != NULL, 0);
404 source = g_io_create_watch (channel, condition);
406 if (priority != G_PRIORITY_DEFAULT)
407 g_source_set_priority (source, priority);
408 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
410 id = g_source_attach (source, NULL);
411 g_source_unref (source);
417 g_io_add_watch (GIOChannel *channel,
418 GIOCondition condition,
422 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
426 * g_io_channel_get_buffer_condition:
427 * @channel: A #GIOChannel
429 * This function returns a #GIOCondition depending on the status of the
430 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
431 * %G_IO_OUT may be set.
433 * Return value: A #GIOCondition
436 g_io_channel_get_buffer_condition (GIOChannel *channel)
438 return (((channel->read_buf->len > 0) || (channel->encoded_read_buf->len > 0))
439 ? G_IO_IN : 0) & ((channel->write_buf->len > 0) ? G_IO_OUT : 0);
443 * g_channel_error_from_errno:
444 * @en: An errno error number, e.g. EINVAL
446 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
449 g_channel_error_from_errno (gint en)
452 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
455 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
462 return G_IO_CHANNEL_ERROR_ACCES;
467 return G_IO_CHANNEL_ERROR_BADF;
472 return G_IO_CHANNEL_ERROR_FAULT;
477 return G_IO_CHANNEL_ERROR_INVAL;
482 return G_IO_CHANNEL_ERROR_IO;
487 return G_IO_CHANNEL_ERROR_ISDIR;
492 return G_IO_CHANNEL_ERROR_MFILE;
497 return G_IO_CHANNEL_ERROR_NOLCK;
502 return G_IO_CHANNEL_ERROR_NOSPC;
507 return G_IO_CHANNEL_ERROR_PERM;
512 return G_IO_CHANNEL_ERROR_PIPE;
517 return G_IO_CHANNEL_ERROR_SPIPE;
521 return G_IO_CHANNEL_ERROR_FAILED;
526 * g_io_channel_set_buffer_size:
527 * @channel: a #GIOChannel
528 * @size: the size of the buffer. 0 == pick a good size
530 * Set the buffer size.
533 g_io_channel_set_buffer_size (GIOChannel *channel,
536 g_return_if_fail (channel != NULL);
539 size = G_IO_NICE_BUF_SIZE;
541 channel->buf_size = size;
545 * g_io_channel_get_buffer_size:
546 * @channel: a #GIOChannel
548 * Get the buffer size.
550 * Return value: the size of the buffer.
553 g_io_channel_get_buffer_size (GIOChannel *channel)
555 g_return_val_if_fail (channel != NULL, 0);
557 return channel->buf_size;
561 * g_io_channel_set_line_term:
562 * @channel: a #GIOChannel
563 * @line_term: The line termination string. Use %NULL for auto detect.
565 * This sets the string that #GIOChannel uses to determine
566 * where in the file a line break occurs.
569 g_io_channel_set_line_term (GIOChannel *channel,
570 const gchar *line_term)
572 g_return_if_fail (channel != NULL);
573 g_return_if_fail (!line_term || line_term[0]); /* Disallow "" */
575 g_free (channel->line_term);
576 channel->line_term = g_strdup (line_term);
580 * g_io_channel_get_line_term:
581 * @channel: a #GIOChannel
583 * This returns the string that #GIOChannel uses to determine
584 * where in the file a line break occurs. A value of %NULL
585 * indicates auto detection.
587 * Return value: The line termination string. This value
588 * is owned by GLib and must not be freed.
590 G_CONST_RETURN gchar*
591 g_io_channel_get_line_term (GIOChannel *channel)
593 g_return_val_if_fail (channel != NULL, 0);
595 return channel->line_term;
599 * g_io_channel_set_flags:
600 * @channel: a #GIOChannel
601 * @flags: the flags to set on the channel
602 * @error: A location to return an error of type #GIOChannelError
607 g_io_channel_set_flags (GIOChannel *channel,
611 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
612 g_return_val_if_fail ((error == NULL) || (*error == NULL),
615 return (* channel->funcs->io_set_flags)(channel,
616 flags & G_IO_FLAG_SET_MASK,
621 * g_io_channel_get_flags:
622 * @channel: a #GIOChannel
623 * @flags: the flags which are set on the channel
625 * Gets the current flags for a #GIOChannel, including read-only
626 * flags such as %G_IO_FLAG_IS_READABLE.
629 g_io_channel_get_flags (GIOChannel *channel)
631 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
633 return (* channel->funcs->io_get_flags) (channel);
637 * g_io_channel_seek_position:
638 * @channel: a #GIOChannel
639 * @offset: The offset in bytes from the position specified by @type
640 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed if
641 * the channel has the default encoding or the
642 * encoding %G_IO_CHANNEL_ENCODE_RAW for raw file access.
643 * @error: A location to return an error of type #GIOChannelError
645 * Replacement for g_io_channel_seek() with the new API.
650 g_io_channel_seek_position (GIOChannel* channel,
657 /* For files, only one of the read and write buffers can contain data.
658 * For sockets, both can contain data.
661 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
662 g_return_val_if_fail ((error == NULL) || (*error == NULL),
667 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
668 if (channel->do_encode)
670 g_warning ("Seek type G_SEEK_CUR not allowed for this channel's encoding.\n");
671 return G_IO_STATUS_ERROR;
678 g_warning ("g_io_channel_seek_position: unknown seek type");
679 return G_IO_STATUS_ERROR;
682 /* Do a quick check for easy failure
684 if (!channel->seekable_cached)
685 g_io_channel_get_flags (channel);
687 g_assert (channel->seekable_cached);
689 if (!channel->is_seekable)
691 g_set_error (error, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_SPIPE,
692 _("Seeking not allowed on this type of channel"));
693 return G_IO_STATUS_ERROR;
696 if (channel->use_buffer)
698 status = g_io_channel_flush (channel, error);
699 if (status != G_IO_STATUS_NORMAL)
702 if (type == G_SEEK_CUR)
704 g_assert (!channel->do_encode);
705 offset -= channel->read_buf->len;
710 status = channel->funcs->io_seek (channel, offset, type, error);
712 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
714 g_string_erase (channel->read_buf, 0, channel->read_buf->len);
715 g_string_erase (channel->encoded_read_buf, 0, channel->read_buf->len);
716 channel->ready_to_write = TRUE;
717 if (channel->do_encode) /* Conversion state no longer matches position in file */
719 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
720 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
728 * g_io_channel_flush:
729 * @channel: a #GIOChannel
730 * @error: location to store an error of type #GIOChannelError
732 * Flush the write buffer for the GIOChannel.
734 * Return value: the status of the operation: One of
735 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
736 * G_IO_CHANNEL_ERROR.
739 g_io_channel_flush (GIOChannel *channel,
743 gsize this_time, bytes_written = 0;
745 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
746 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
748 if (channel->write_buf->len == 0)
750 channel->ready_to_read = TRUE;
751 return G_IO_STATUS_NORMAL;
756 status = channel->funcs->io_write (channel,
757 channel->write_buf->str + bytes_written,
758 channel->write_buf->len - bytes_written,
760 bytes_written += this_time;
762 while ((bytes_written < channel->write_buf->len)
763 && (status == G_IO_STATUS_NORMAL)
764 && (this_time > 0)); /* Prevent an infinite loop */
766 g_string_erase (channel->write_buf, 0, bytes_written);
768 if (status != G_IO_STATUS_NORMAL)
771 if (channel->write_buf->len > 0)
772 return G_IO_STATUS_AGAIN;
774 channel->ready_to_read = TRUE;
776 return G_IO_STATUS_NORMAL;
780 * g_io_channel_set_encoding:
781 * @channel: a #GIOChannel
782 * @encoding: the encoding type
783 * @error: location to store an error of type #GConvertError.
785 * Set the encoding for the input/output of the channel. The internal
786 * encoding is always UTF-8.
788 * The encoding %G_IO_CHANNEL_ENCODE_RAW disables encoding and turns
789 * off internal buffering. Data can be read from it only with
790 * g_io_channel_read_chars(). Both %G_IO_CHANNEL_ENCODE_RAW and
791 * the default state (no encoding, but buffered)
792 * are safe to use with binary data.
795 * other than the default and %G_IO_CHANNEL_ENCODE_RAW cannot
796 b * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
797 * and cannot mix reading and writing if the channel is
798 * a file without first doing a seek of type %G_SEEK_SET or
801 * The encoding can only be set under three conditions:
803 * 1. The channel was just created, and has not been written to
806 * 2. The channel is a file, and the file pointer was just
807 * repositioned by a call to g_io_channel_seek_position().
808 * (This flushes all the internal buffers.)
810 * 3. The current encoding is %G_IO_CHANNEL_ENCODE_RAW.
812 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
815 g_io_channel_set_encoding (GIOChannel *channel,
816 const gchar *encoding,
819 GIConv read_cd, write_cd;
822 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
823 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
825 /* Make sure the buffers are empty */
827 g_return_val_if_fail (channel->read_buf->len == 0, G_IO_STATUS_ERROR);
828 g_return_val_if_fail (channel->write_buf->len == 0, G_IO_STATUS_ERROR);
829 g_return_val_if_fail (channel->encoded_read_buf == NULL ||
830 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
832 did_encode = channel->do_encode;
834 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
836 channel->use_buffer = TRUE;
837 channel->do_encode = FALSE;
838 read_cd = write_cd = (GIConv) -1; /* keep the compiler happy */
840 else if (!strcmp (encoding, G_IO_CHANNEL_ENCODE_RAW))
842 channel->use_buffer = FALSE;
843 channel->do_encode = FALSE;
844 read_cd = write_cd = (GIConv) -1; /* keep the compiler happy */
849 const gchar *from_enc, *to_enc;
851 read_cd = g_iconv_open ("UTF-8", encoding);
853 if (read_cd == (GIConv) -1)
858 write_cd = (GIConv) -1; /* keep the compiler happy */
862 write_cd = g_iconv_open (encoding, "UTF-8");
863 if (write_cd == (GIConv) -1)
866 g_iconv_close (read_cd);
873 from_enc = to_enc = NULL; /* Keep the compiler happy */
879 /* Something went wrong. */
881 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
882 _("Conversion from character set `%s' to `%s' is not supported"),
885 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
886 _("Could not open converter from `%s' to `%s': %s"),
887 from_enc, to_enc, strerror (errno));
888 return G_IO_STATUS_ERROR;
891 channel->use_buffer = TRUE;
892 channel->do_encode = TRUE;
894 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
897 /* The encoding is ok, so set the fields in channel */
901 g_iconv_close (channel->read_cd);
902 g_iconv_close (channel->write_cd);
903 if (!channel->do_encode)
905 g_string_free (channel->encoded_read_buf, TRUE);
906 channel->encoded_read_buf = NULL;
910 if (channel->do_encode)
912 channel->read_cd = read_cd;
913 channel->write_cd = write_cd;
916 g_free (channel->encoding);
917 channel->encoding = g_strdup (encoding);
919 return G_IO_STATUS_NORMAL;
923 * g_io_channel_get_encoding:
924 * @channel: a #GIOChannel
926 * Get the encoding for the input/output of the channel. The internal
927 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
928 * disables encoding and turns off internal buffering. Both
929 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
930 * are safe to use with binary data.
932 * Return value: A string containing the encoding, this string is
933 * owned by GLib and must not be freed.
935 G_CONST_RETURN gchar*
936 g_io_channel_get_encoding (GIOChannel *channel)
938 g_return_val_if_fail (channel != NULL, NULL);
940 return channel->encoding;
944 g_io_channel_fill_buffer (GIOChannel *channel,
947 gsize read_size, cur_len, oldlen;
950 if (!channel->ready_to_read)
952 status = g_io_channel_flush (channel, err);
953 if (status != G_IO_STATUS_NORMAL)
956 g_assert (channel->ready_to_read);
958 if (!channel->seekable_cached)
959 g_io_channel_get_flags (channel);
961 g_assert (channel->seekable_cached);
963 if (channel->is_seekable)
964 channel->ready_to_write = FALSE;
966 cur_len = channel->read_buf->len;
968 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
970 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
971 channel->buf_size, &read_size, err);
973 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
975 g_string_truncate (channel->read_buf, read_size + cur_len);
977 if ((status != G_IO_STATUS_NORMAL)
978 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
981 g_assert (channel->read_buf->len > 0);
983 if (channel->do_encode)
985 size_t errnum, inbytes_left, outbytes_left;
986 gchar *inbuf, *outbuf;
988 oldlen = channel->encoded_read_buf->len;
992 inbytes_left = channel->read_buf->len;
993 outbytes_left = channel->buf_size;
995 g_string_set_size (channel->encoded_read_buf, channel->encoded_read_buf->len + outbytes_left);
997 inbuf = channel->read_buf->str;
998 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len - outbytes_left;
1000 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1001 &outbuf, &outbytes_left);
1003 g_string_erase (channel->read_buf, 0,
1004 channel->read_buf->len - inbytes_left);
1005 g_string_truncate (channel->encoded_read_buf,
1006 channel->encoded_read_buf->len - outbytes_left);
1008 if (errnum == (size_t) -1)
1013 if ((oldlen == channel->encoded_read_buf->len)
1014 && (status == G_IO_STATUS_EOF))
1015 status = G_IO_STATUS_PARTIAL_CHARS;
1017 status = G_IO_STATUS_NORMAL;
1022 if (oldlen < channel->encoded_read_buf->len)
1023 status = G_IO_STATUS_NORMAL;
1026 g_set_error (err, G_CONVERT_ERROR,
1027 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1028 _("Invalid byte sequence in conversion input"));
1029 return G_IO_STATUS_ERROR;
1033 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1034 _("Error during conversion: %s"), strerror (errno));
1035 return G_IO_STATUS_ERROR;
1038 if (channel->encoded_read_buf->len == 0)
1040 g_assert (status != G_IO_STATUS_NORMAL);
1049 * g_io_channel_read_line:
1050 * @channel: a #GIOChannel
1051 * @str_return: The line read from the #GIOChannel, not including the
1052 * line terminator. This data should be freed with g_free()
1053 * when no longer needed. This
1054 * is a null terminated string. If a @length of zero is
1055 * returned, this will be %NULL instead.
1056 * @length: location to store length of the read data, or %NULL
1057 * @terminator_pos: location to store position of line terminator, or %NULL
1058 * @error: A location to return an error of type #GConvertError
1059 * or #GIOChannelError
1061 * Read a line, not including the terminating character(s),
1062 * from a #GIOChannel into a newly allocated string.
1063 * @length will contain allocated memory if the return
1064 * is %G_IO_STATUS_NORMAL.
1066 * Return value: a newly allocated string. Free this string
1067 * with g_free() when you are done with it.
1070 g_io_channel_read_line (GIOChannel *channel,
1073 gsize *terminator_pos,
1079 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1080 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1081 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1084 string = g_string_sized_new (channel->buf_size);
1086 status = g_io_channel_read_line_string (channel, string, terminator_pos, error);
1089 *length = string->len;
1092 *str_return = g_string_free (string, FALSE);
1094 g_string_free (string, TRUE);
1100 * g_io_channel_read_line_string:
1101 * @channel: a #GIOChannel
1102 * @buffer: a #GString into which the line will be written.
1103 * If @buffer already contains data, the new data will
1104 * be appended to it.
1105 * @terminator_pos: location to store position of line terminator, or %NULL
1106 * @error: a location to store an error of type #GConvertError
1107 * or #GIOChannelError
1109 * Read a line from a #GIOChannel, using a #GString as a buffer.
1114 g_io_channel_read_line_string (GIOChannel *channel,
1116 gsize *terminator_pos,
1120 gsize checked_to, line_term_len, line_length;
1122 gboolean first_time = TRUE;
1124 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1125 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1126 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1129 if (!channel->use_buffer)
1131 /* Can't do a raw read in read_line */
1132 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1133 _("Can't do a raw read in g_io_channel_read_line_string"));
1134 return G_IO_STATUS_ERROR;
1137 if (!channel->ready_to_read)
1139 status = g_io_channel_flush (channel, error);
1140 if (status != G_IO_STATUS_NORMAL)
1143 g_assert (channel->ready_to_read);
1145 if (channel->do_encode)
1146 use_buf = channel->encoded_read_buf;
1148 use_buf = channel->read_buf;
1150 status = G_IO_STATUS_NORMAL;
1152 if (channel->line_term)
1153 line_term_len = strlen (channel->line_term);
1161 if (!first_time || (use_buf->len == 0))
1163 status = g_io_channel_fill_buffer (channel, error);
1166 case G_IO_STATUS_NORMAL:
1167 g_assert (use_buf->len > 0);
1169 case G_IO_STATUS_EOF:
1170 case G_IO_STATUS_PARTIAL_CHARS:
1171 if (use_buf->len == 0)
1179 g_assert (use_buf->len > 0);
1183 if (channel->line_term)
1185 gchar *line_end = strstr (use_buf->str + checked_to, channel->line_term);
1188 line_length = line_end - use_buf->str;
1192 else /* auto detect */
1194 gchar *newline_pos, *linefeed_pos;
1196 newline_pos = strchr (use_buf->str + checked_to, '\n');
1197 linefeed_pos = strchr (use_buf->str + checked_to, '\r');
1199 g_assert (!newline_pos || !linefeed_pos || (newline_pos != linefeed_pos));
1201 if (newline_pos && (!linefeed_pos || (linefeed_pos > newline_pos))) /* unix */
1203 line_length = newline_pos - use_buf->str;
1207 else if (linefeed_pos &&
1208 (linefeed_pos < use_buf->str + use_buf->len - 1 ||
1209 status == G_IO_STATUS_EOF))
1211 line_length = linefeed_pos - use_buf->str;
1212 if (newline_pos && (newline_pos == linefeed_pos + 1)) /* dos */
1220 /* Also terminate on '\0' */
1222 line_length = strlen (use_buf->str);
1223 if (line_length < use_buf->len)
1231 if (status != G_IO_STATUS_NORMAL)
1233 line_length = use_buf->len;
1238 if (use_buf->len > line_term_len - 1)
1239 checked_to = use_buf->len - (line_term_len - 1);
1244 g_string_append_len (buffer, use_buf->str, line_length + line_term_len);
1245 g_string_erase (use_buf, 0, line_length + line_term_len);
1248 *terminator_pos = line_length;
1250 return G_IO_STATUS_NORMAL;
1254 * g_io_channel_read_to_end:
1255 * @channel: a #GIOChannel
1256 * @str_return: Location to store a pointer to a string holding
1257 * the remaining data in the #GIOChannel. This data should
1258 * be freed with g_free() when no longer needed. This
1259 * data is terminated by an extra null, but there may be other
1260 * nulls in the intervening data.
1261 * @length: Location to store length of the data
1262 * @error: A location to return an error of type #GConvertError
1263 * or #GIOChannelError
1265 * Read all the remaining data from the file. Parameters as
1266 * for g_io_channel_read_line.
1268 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1272 g_io_channel_read_to_end (GIOChannel *channel,
1280 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1281 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1282 g_return_val_if_fail (length != NULL, G_IO_STATUS_ERROR);
1283 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1289 if (!channel->use_buffer)
1291 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1292 _("Can't do a raw read in g_io_channel_read_to_end"));
1293 return G_IO_STATUS_ERROR;
1297 status = g_io_channel_fill_buffer (channel, error);
1298 while (status == G_IO_STATUS_NORMAL);
1300 if ((status != G_IO_STATUS_EOF)
1301 && (status != G_IO_STATUS_PARTIAL_CHARS))
1304 if (channel->do_encode)
1305 use_buf = &channel->encoded_read_buf;
1307 use_buf = &channel->read_buf;
1310 *length = (*use_buf)->len;
1313 *str_return = g_string_free (*use_buf, FALSE);
1315 g_string_free (*use_buf, TRUE);
1317 *use_buf = g_string_sized_new (channel->buf_size);
1323 * g_io_channel_read_chars:
1324 * @channel: a #GIOChannel
1325 * @buf: a buffer to read data into
1326 * @count: the size of the buffer. Note that the buffer may
1327 * not be complelely filled even if there is data
1328 * in the buffer if the remaining data is not a
1329 * complete character.
1330 * @bytes_read: The number of bytes read.
1331 * @error: A location to return an error of type #GConvertError
1332 * or #GIOChannelError.
1334 * Replacement for g_io_channel_read() with the new API.
1339 g_io_channel_read_chars (GIOChannel *channel,
1348 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1349 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1351 g_return_val_if_fail (bytes_read != NULL, G_IO_STATUS_ERROR);
1356 return G_IO_STATUS_NORMAL;
1358 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1360 if (!channel->use_buffer)
1362 g_assert (channel->ready_to_read);
1363 g_assert (channel->read_buf->len == 0);
1365 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1368 if (!channel->ready_to_read)
1370 status = g_io_channel_flush (channel, error);
1371 if (status != G_IO_STATUS_NORMAL)
1374 g_assert (channel->ready_to_read);
1376 if (channel->do_encode)
1377 use_buf = channel->encoded_read_buf;
1379 use_buf = channel->read_buf;
1381 status = G_IO_STATUS_NORMAL;
1383 while (use_buf->len < count && status == G_IO_STATUS_NORMAL)
1385 status = g_io_channel_fill_buffer (channel, error);
1390 case G_IO_STATUS_NORMAL:
1391 g_assert (use_buf->len > 0);
1393 case G_IO_STATUS_EOF:
1394 case G_IO_STATUS_PARTIAL_CHARS:
1395 if (use_buf->len == 0)
1406 *bytes_read = MIN (count, use_buf->len);
1407 memcpy (buf, use_buf->str, *bytes_read);
1408 g_string_erase (use_buf, 0, *bytes_read);
1410 return G_IO_STATUS_NORMAL;
1414 * g_io_channel_write_chars:
1415 * @channel: a #GIOChannel
1416 * @buf: a buffer to write data from
1417 * @count: the size of the buffer. If -1, the buffer
1418 * is taken to be a nul terminated string.
1419 * @bytes_written: The number of bytes written.
1420 * @error: A location to return an error of type #GConvertError
1421 * or #GIOChannelError
1423 * Replacement for g_io_channel_write() with the new API.
1425 * Return value: a return value of %G_IO_STATUS_PARTIAL_CHARS
1426 * indicates that @buf contains less than one
1427 * complete UTF-8 character.
1430 g_io_channel_write_chars (GIOChannel *channel,
1433 gsize *bytes_written,
1438 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1439 g_return_val_if_fail (bytes_written != NULL, G_IO_STATUS_ERROR);
1440 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1444 count = strlen (buf);
1447 return G_IO_STATUS_NORMAL;
1449 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1450 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1452 /* Raw write case */
1454 if (!channel->use_buffer)
1456 g_assert (channel->write_buf->len == 0);
1457 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1462 if (channel->write_buf->len == 0)
1464 /* Check to make sure the channel's writeable before we stick
1465 * the first data in the buffer.
1467 GIOFlags flags = g_io_channel_get_flags (channel);
1469 if (!(flags & G_IO_FLAG_IS_WRITEABLE))
1471 /* Mimic a failure of channel->funcs->io_write */
1472 g_set_error (error, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_BADF,
1473 _("Channel is not writeable"));
1474 return G_IO_STATUS_ERROR;
1477 g_assert (channel->seekable_cached);
1479 if (!channel->ready_to_write)
1481 if (channel->is_seekable)
1483 if (channel->do_encode)
1485 g_set_error (error, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_ENCODE_RW,
1486 _("Mixed reading and writing not allowed on encoded files"));
1487 return G_IO_STATUS_ERROR;
1489 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1490 if (status != G_IO_STATUS_NORMAL)
1493 channel->ready_to_write = TRUE;
1496 if (channel->is_seekable)
1497 channel->ready_to_read = FALSE;
1499 if (!channel->do_encode)
1501 g_string_append_len (channel->write_buf, buf, count);
1502 *bytes_written = count;
1506 gsize bytes_to_go = count, buf_space, err;
1508 gsize oldlen = channel->write_buf->len;
1512 g_string_set_size (channel->write_buf, channel->write_buf->len + channel->buf_size);
1513 buf_space = channel->buf_size;
1514 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1516 err = g_iconv (channel->write_cd, (gchar**) &buf, &bytes_to_go,
1517 &outbuf, &buf_space);
1519 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1520 *bytes_written = count - bytes_to_go;
1522 if (err == (size_t) -1)
1527 if (*bytes_written > 0)
1528 return G_IO_STATUS_NORMAL;
1530 return G_IO_STATUS_PARTIAL_CHARS;
1534 if (*bytes_written > 0)
1535 return G_IO_STATUS_NORMAL;
1538 g_set_error (error, G_CONVERT_ERROR,
1539 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1540 _("Invalid byte sequence in conversion input"));
1541 return G_IO_STATUS_ERROR;
1544 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1545 _("Error during conversion: %s"), strerror (errno));
1546 g_string_truncate (channel->write_buf, oldlen);
1548 return G_IO_STATUS_ERROR;
1552 while (bytes_to_go > 0);
1555 if (channel->write_buf->len > channel->buf_size)
1556 return g_io_channel_flush (channel, error);
1558 return G_IO_STATUS_NORMAL;
1562 * g_channel_error_quark:
1564 * Return value: The quark used as %G_IO_CHANNEL_ERROR
1567 g_channel_error_quark (void)
1569 static GQuark q = 0;
1571 q = g_quark_from_static_string ("g-channel-error-quark");