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;
709 status = channel->funcs->io_seek (channel, offset, type, error);
711 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
713 g_string_erase (channel->read_buf, 0, channel->read_buf->len);
714 g_string_erase (channel->encoded_read_buf, 0, channel->read_buf->len);
715 channel->ready_to_write = TRUE;
716 if (channel->do_encode) /* Conversion state no longer matches position in file */
718 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
719 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
727 * g_io_channel_flush:
728 * @channel: a #GIOChannel
729 * @error: location to store an error of type #GIOChannelError
731 * Flush the write buffer for the GIOChannel.
733 * Return value: the status of the operation: One of
734 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
735 * G_IO_CHANNEL_ERROR.
738 g_io_channel_flush (GIOChannel *channel,
742 gsize this_time, bytes_written = 0;
744 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
745 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
747 if (channel->write_buf->len == 0)
749 channel->ready_to_read = TRUE;
750 return G_IO_STATUS_NORMAL;
755 status = channel->funcs->io_write (channel,
756 channel->write_buf->str + bytes_written,
757 channel->write_buf->len - bytes_written,
759 bytes_written += this_time;
761 while ((bytes_written < channel->write_buf->len)
762 && (status == G_IO_STATUS_NORMAL)
763 && (this_time > 0)); /* Prevent an infinite loop */
765 g_string_erase (channel->write_buf, 0, bytes_written);
767 if (status != G_IO_STATUS_NORMAL)
770 if (channel->write_buf->len > 0)
771 return G_IO_STATUS_AGAIN;
773 channel->ready_to_read = TRUE;
775 return G_IO_STATUS_NORMAL;
779 * g_io_channel_set_encoding:
780 * @channel: a #GIOChannel
781 * @encoding: the encoding type
782 * @error: location to store an error of type #GConvertError.
784 * Set the encoding for the input/output of the channel. The internal
785 * encoding is always UTF-8.
787 * The encoding %G_IO_CHANNEL_ENCODE_RAW disables encoding and turns
788 * off internal buffering. Data can be read from it only with
789 * g_io_channel_read_chars(). Both %G_IO_CHANNEL_ENCODE_RAW and
790 * the default state (no encoding, but buffered)
791 * are safe to use with binary data.
794 * other than the default and %G_IO_CHANNEL_ENCODE_RAW cannot
795 b * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
796 * and cannot mix reading and writing if the channel is
797 * a file without first doing a seek of type %G_SEEK_SET or
800 * The encoding can only be set under three conditions:
802 * 1. The channel was just created, and has not been written to
805 * 2. The channel is a file, and the file pointer was just
806 * repositioned by a call to g_io_channel_seek_position().
807 * (This flushes all the internal buffers.)
809 * 3. The current encoding is %G_IO_CHANNEL_ENCODE_RAW.
811 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
814 g_io_channel_set_encoding (GIOChannel *channel,
815 const gchar *encoding,
818 GIConv read_cd, write_cd;
821 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
822 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
824 /* Make sure the buffers are empty */
826 g_return_val_if_fail (channel->read_buf->len == 0, G_IO_STATUS_ERROR);
827 g_return_val_if_fail (channel->write_buf->len == 0, G_IO_STATUS_ERROR);
828 g_return_val_if_fail (channel->encoded_read_buf == NULL ||
829 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
831 did_encode = channel->do_encode;
833 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
835 channel->use_buffer = TRUE;
836 channel->do_encode = FALSE;
837 read_cd = write_cd = (GIConv) -1; /* keep the compiler happy */
839 else if (!strcmp (encoding, G_IO_CHANNEL_ENCODE_RAW))
841 channel->use_buffer = FALSE;
842 channel->do_encode = FALSE;
843 read_cd = write_cd = (GIConv) -1; /* keep the compiler happy */
848 const gchar *from_enc, *to_enc;
850 read_cd = g_iconv_open ("UTF-8", encoding);
852 if (read_cd == (GIConv) -1)
857 write_cd = (GIConv) -1; /* keep the compiler happy */
861 write_cd = g_iconv_open (encoding, "UTF-8");
862 if (write_cd == (GIConv) -1)
865 g_iconv_close (read_cd);
872 from_enc = to_enc = NULL; /* Keep the compiler happy */
878 /* Something went wrong. */
880 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
881 _("Conversion from character set `%s' to `%s' is not supported"),
884 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
885 _("Could not open converter from `%s' to `%s': %s"),
886 from_enc, to_enc, strerror (errno));
887 return G_IO_STATUS_ERROR;
890 channel->use_buffer = TRUE;
891 channel->do_encode = TRUE;
893 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
896 /* The encoding is ok, so set the fields in channel */
900 g_iconv_close (channel->read_cd);
901 g_iconv_close (channel->write_cd);
902 if (!channel->do_encode)
904 g_string_free (channel->encoded_read_buf, TRUE);
905 channel->encoded_read_buf = NULL;
909 if (channel->do_encode)
911 channel->read_cd = read_cd;
912 channel->write_cd = write_cd;
915 g_free (channel->encoding);
916 channel->encoding = g_strdup (encoding);
918 return G_IO_STATUS_NORMAL;
922 * g_io_channel_get_encoding:
923 * @channel: a #GIOChannel
925 * Get the encoding for the input/output of the channel. The internal
926 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
927 * disables encoding and turns off internal buffering. Both
928 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
929 * are safe to use with binary data.
931 * Return value: A string containing the encoding, this string is
932 * owned by GLib and must not be freed.
934 G_CONST_RETURN gchar*
935 g_io_channel_get_encoding (GIOChannel *channel)
937 g_return_val_if_fail (channel != NULL, NULL);
939 return channel->encoding;
943 g_io_channel_fill_buffer (GIOChannel *channel,
946 gsize read_size, cur_len, oldlen;
949 if (!channel->ready_to_read)
951 status = g_io_channel_flush (channel, err);
952 if (status != G_IO_STATUS_NORMAL)
955 g_assert (channel->ready_to_read);
957 if (!channel->seekable_cached)
958 g_io_channel_get_flags (channel);
960 g_assert (channel->seekable_cached);
962 if (channel->is_seekable)
963 channel->ready_to_write = FALSE;
965 cur_len = channel->read_buf->len;
967 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
969 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
970 channel->buf_size, &read_size, err);
972 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
974 g_string_truncate (channel->read_buf, read_size + cur_len);
976 if ((status != G_IO_STATUS_NORMAL)
977 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
980 g_assert (channel->read_buf->len > 0);
982 if (channel->do_encode)
984 size_t errnum, inbytes_left, outbytes_left;
985 gchar *inbuf, *outbuf;
987 oldlen = channel->encoded_read_buf->len;
991 inbytes_left = channel->read_buf->len;
992 outbytes_left = channel->buf_size;
994 g_string_set_size (channel->encoded_read_buf, channel->encoded_read_buf->len + outbytes_left);
996 inbuf = channel->read_buf->str;
997 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len - outbytes_left;
999 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1000 &outbuf, &outbytes_left);
1002 g_string_erase (channel->read_buf, 0,
1003 channel->read_buf->len - inbytes_left);
1004 g_string_truncate (channel->encoded_read_buf,
1005 channel->encoded_read_buf->len - outbytes_left);
1007 if (errnum == (size_t) -1)
1012 if ((oldlen == channel->encoded_read_buf->len)
1013 && (status == G_IO_STATUS_EOF))
1014 status = G_IO_STATUS_PARTIAL_CHARS;
1016 status = G_IO_STATUS_NORMAL;
1021 if (oldlen < channel->encoded_read_buf->len)
1022 status = G_IO_STATUS_NORMAL;
1025 g_set_error (err, G_CONVERT_ERROR,
1026 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1027 _("Invalid byte sequence in conversion input"));
1028 return G_IO_STATUS_ERROR;
1032 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1033 _("Error during conversion: %s"), strerror (errno));
1034 return G_IO_STATUS_ERROR;
1037 if (channel->encoded_read_buf->len == 0)
1039 g_assert (status != G_IO_STATUS_NORMAL);
1048 * g_io_channel_read_line:
1049 * @channel: a #GIOChannel
1050 * @str_return: The line read from the #GIOChannel, not including the
1051 * line terminator. This data should be freed with g_free()
1052 * when no longer needed. This
1053 * is a null terminated string. If a @length of zero is
1054 * returned, this will be %NULL instead.
1055 * @length: location to store length of the read data, or %NULL
1056 * @terminator_pos: location to store position of line terminator, or %NULL
1057 * @error: A location to return an error of type #GConvertError
1058 * or #GIOChannelError
1060 * Read a line, not including the terminating character(s),
1061 * from a #GIOChannel into a newly allocated string.
1062 * @length will contain allocated memory if the return
1063 * is %G_IO_STATUS_NORMAL.
1065 * Return value: a newly allocated string. Free this string
1066 * with g_free() when you are done with it.
1069 g_io_channel_read_line (GIOChannel *channel,
1072 gsize *terminator_pos,
1078 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1079 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1080 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1083 string = g_string_sized_new (channel->buf_size);
1085 status = g_io_channel_read_line_string (channel, string, terminator_pos, error);
1088 *length = string->len;
1091 *str_return = g_string_free (string, FALSE);
1093 g_string_free (string, TRUE);
1099 * g_io_channel_read_line_string:
1100 * @channel: a #GIOChannel
1101 * @buffer: a #GString into which the line will be written.
1102 * If @buffer already contains data, the new data will
1103 * be appended to it.
1104 * @terminator_pos: location to store position of line terminator, or %NULL
1105 * @error: a location to store an error of type #GConvertError
1106 * or #GIOChannelError
1108 * Read a line from a #GIOChannel, using a #GString as a buffer.
1113 g_io_channel_read_line_string (GIOChannel *channel,
1115 gsize *terminator_pos,
1119 gsize checked_to, line_term_len, line_length;
1121 gboolean first_time = TRUE;
1123 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1124 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1125 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1128 if (!channel->use_buffer)
1130 /* Can't do a raw read in read_line */
1131 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1132 _("Can't do a raw read in g_io_channel_read_line_string"));
1133 return G_IO_STATUS_ERROR;
1136 if (!channel->ready_to_read)
1138 status = g_io_channel_flush (channel, error);
1139 if (status != G_IO_STATUS_NORMAL)
1142 g_assert (channel->ready_to_read);
1144 if (channel->do_encode)
1145 use_buf = channel->encoded_read_buf;
1147 use_buf = channel->read_buf;
1149 status = G_IO_STATUS_NORMAL;
1151 if (channel->line_term)
1152 line_term_len = strlen (channel->line_term);
1160 if (!first_time || (use_buf->len == 0))
1162 status = g_io_channel_fill_buffer (channel, error);
1165 case G_IO_STATUS_NORMAL:
1166 g_assert (use_buf->len > 0);
1168 case G_IO_STATUS_EOF:
1169 case G_IO_STATUS_PARTIAL_CHARS:
1170 if (use_buf->len == 0)
1178 g_assert (use_buf->len > 0);
1182 if (channel->line_term)
1184 gchar *line_end = strstr (use_buf->str + checked_to, channel->line_term);
1187 line_length = line_end - use_buf->str;
1191 else /* auto detect */
1193 gchar *newline_pos, *linefeed_pos;
1195 newline_pos = strchr (use_buf->str + checked_to, '\n');
1196 linefeed_pos = strchr (use_buf->str + checked_to, '\r');
1198 g_assert (!newline_pos || !linefeed_pos || (newline_pos != linefeed_pos));
1200 if (newline_pos && (!linefeed_pos || (linefeed_pos > newline_pos))) /* unix */
1202 line_length = newline_pos - use_buf->str;
1206 else if (linefeed_pos &&
1207 (linefeed_pos < use_buf->str + use_buf->len - 1 ||
1208 status == G_IO_STATUS_EOF))
1210 line_length = linefeed_pos - use_buf->str;
1211 if (newline_pos && (newline_pos == linefeed_pos + 1)) /* dos */
1219 /* Also terminate on '\0' */
1221 line_length = strlen (use_buf->str);
1222 if (line_length < use_buf->len)
1230 if (status != G_IO_STATUS_NORMAL)
1232 line_length = use_buf->len;
1237 if (use_buf->len > line_term_len - 1)
1238 checked_to = use_buf->len - (line_term_len - 1);
1243 g_string_append_len (buffer, use_buf->str, line_length + line_term_len);
1244 g_string_erase (use_buf, 0, line_length + line_term_len);
1247 *terminator_pos = line_length;
1249 return G_IO_STATUS_NORMAL;
1253 * g_io_channel_read_to_end:
1254 * @channel: a #GIOChannel
1255 * @str_return: Location to store a pointer to a string holding
1256 * the remaining data in the #GIOChannel. This data should
1257 * be freed with g_free() when no longer needed. This
1258 * data is terminated by an extra null, but there may be other
1259 * nulls in the intervening data.
1260 * @length: Location to store length of the data
1261 * @error: A location to return an error of type #GConvertError
1262 * or #GIOChannelError
1264 * Read all the remaining data from the file. Parameters as
1265 * for g_io_channel_read_line.
1267 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1271 g_io_channel_read_to_end (GIOChannel *channel,
1279 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1280 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1281 g_return_val_if_fail (length != NULL, G_IO_STATUS_ERROR);
1282 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1288 if (!channel->use_buffer)
1290 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1291 _("Can't do a raw read in g_io_channel_read_to_end"));
1292 return G_IO_STATUS_ERROR;
1296 status = g_io_channel_fill_buffer (channel, error);
1297 while (status == G_IO_STATUS_NORMAL);
1299 if ((status != G_IO_STATUS_EOF)
1300 && (status != G_IO_STATUS_PARTIAL_CHARS))
1303 if (channel->do_encode)
1304 use_buf = &channel->encoded_read_buf;
1306 use_buf = &channel->read_buf;
1309 *length = (*use_buf)->len;
1312 *str_return = g_string_free (*use_buf, FALSE);
1314 g_string_free (*use_buf, TRUE);
1316 *use_buf = g_string_sized_new (channel->buf_size);
1322 * g_io_channel_read_chars:
1323 * @channel: a #GIOChannel
1324 * @buf: a buffer to read data into
1325 * @count: the size of the buffer. Note that the buffer may
1326 * not be complelely filled even if there is data
1327 * in the buffer if the remaining data is not a
1328 * complete character.
1329 * @bytes_read: The number of bytes read.
1330 * @error: A location to return an error of type #GConvertError
1331 * or #GIOChannelError.
1333 * Replacement for g_io_channel_read() with the new API.
1338 g_io_channel_read_chars (GIOChannel *channel,
1347 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1348 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1350 g_return_val_if_fail (bytes_read != NULL, G_IO_STATUS_ERROR);
1355 return G_IO_STATUS_NORMAL;
1357 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1359 if (!channel->use_buffer)
1361 g_assert (channel->ready_to_read);
1362 g_assert (channel->read_buf->len == 0);
1364 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1367 if (!channel->ready_to_read)
1369 status = g_io_channel_flush (channel, error);
1370 if (status != G_IO_STATUS_NORMAL)
1373 g_assert (channel->ready_to_read);
1375 if (channel->do_encode)
1376 use_buf = channel->encoded_read_buf;
1378 use_buf = channel->read_buf;
1380 status = G_IO_STATUS_NORMAL;
1382 while (use_buf->len < count && status == G_IO_STATUS_NORMAL)
1384 status = g_io_channel_fill_buffer (channel, error);
1389 case G_IO_STATUS_NORMAL:
1390 g_assert (use_buf->len > 0);
1392 case G_IO_STATUS_EOF:
1393 case G_IO_STATUS_PARTIAL_CHARS:
1394 if (use_buf->len == 0)
1405 *bytes_read = MIN (count, use_buf->len);
1406 memcpy (buf, use_buf->str, *bytes_read);
1407 g_string_erase (use_buf, 0, *bytes_read);
1409 return G_IO_STATUS_NORMAL;
1413 * g_io_channel_write_chars:
1414 * @channel: a #GIOChannel
1415 * @buf: a buffer to write data from
1416 * @count: the size of the buffer. If -1, the buffer
1417 * is taken to be a nul terminated string.
1418 * @bytes_written: The number of bytes written.
1419 * @error: A location to return an error of type #GConvertError
1420 * or #GIOChannelError
1422 * Replacement for g_io_channel_write() with the new API.
1424 * Return value: a return value of %G_IO_STATUS_PARTIAL_CHARS
1425 * indicates that @buf contains less than one
1426 * complete UTF-8 character.
1429 g_io_channel_write_chars (GIOChannel *channel,
1432 gsize *bytes_written,
1437 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1438 g_return_val_if_fail (bytes_written != NULL, G_IO_STATUS_ERROR);
1439 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1443 count = strlen (buf);
1446 return G_IO_STATUS_NORMAL;
1448 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1449 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1451 /* Raw write case */
1453 if (!channel->use_buffer)
1455 g_assert (channel->write_buf->len == 0);
1456 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1461 if (channel->write_buf->len == 0)
1463 /* Check to make sure the channel's writeable before we stick
1464 * the first data in the buffer.
1466 GIOFlags flags = g_io_channel_get_flags (channel);
1468 if (!(flags & G_IO_FLAG_IS_WRITEABLE))
1470 /* Mimic a failure of channel->funcs->io_write */
1471 g_set_error (error, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_BADF,
1472 _("Channel is not writeable"));
1473 return G_IO_STATUS_ERROR;
1476 g_assert (channel->seekable_cached);
1478 if (!channel->ready_to_write)
1480 if (channel->is_seekable)
1482 if (channel->do_encode)
1484 g_set_error (error, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_ENCODE_RW,
1485 _("Mixed reading and writing not allowed on encoded files"));
1486 return G_IO_STATUS_ERROR;
1488 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1489 if (status != G_IO_STATUS_NORMAL)
1492 channel->ready_to_write = TRUE;
1495 if (channel->is_seekable)
1496 channel->ready_to_read = FALSE;
1498 if (!channel->do_encode)
1500 g_string_append_len (channel->write_buf, buf, count);
1501 *bytes_written = count;
1505 gsize bytes_to_go = count, buf_space, err;
1507 gsize oldlen = channel->write_buf->len;
1511 g_string_set_size (channel->write_buf, channel->write_buf->len + channel->buf_size);
1512 buf_space = channel->buf_size;
1513 outbuf = channel->write_buf->str + channel->write_buf->len - buf_space;
1515 err = g_iconv (channel->write_cd, (gchar**) &buf, &bytes_to_go,
1516 &outbuf, &buf_space);
1518 g_string_truncate (channel->write_buf, channel->write_buf->len - buf_space);
1519 *bytes_written = count - bytes_to_go;
1521 if (err == (size_t) -1)
1526 if (*bytes_written > 0)
1527 return G_IO_STATUS_NORMAL;
1529 return G_IO_STATUS_PARTIAL_CHARS;
1533 if (*bytes_written > 0)
1534 return G_IO_STATUS_NORMAL;
1537 g_set_error (error, G_CONVERT_ERROR,
1538 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1539 _("Invalid byte sequence in conversion input"));
1540 return G_IO_STATUS_ERROR;
1543 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1544 _("Error during conversion: %s"), strerror (errno));
1545 g_string_truncate (channel->write_buf, oldlen);
1547 return G_IO_STATUS_ERROR;
1551 while (bytes_to_go > 0);
1554 if (channel->write_buf->len > channel->buf_size)
1555 return g_io_channel_flush (channel, error);
1557 return G_IO_STATUS_NORMAL;
1561 * g_channel_error_quark:
1563 * Return value: The quark used as %G_IO_CHANNEL_ERROR
1566 g_channel_error_quark (void)
1568 static GQuark q = 0;
1570 q = g_quark_from_static_string ("g-channel-error-quark");