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->buf_size = G_IO_NICE_BUF_SIZE;
80 channel->read_cd = (GIConv) -1;
81 channel->write_cd = (GIConv) -1;
82 channel->read_buf = NULL; /* Lazy allocate buffers */
83 channel->encoded_read_buf = NULL;
84 channel->write_buf = NULL;
85 channel->partial_write_buf[0] = '\0';
86 channel->use_buffer = TRUE;
87 channel->do_encode = FALSE;
88 channel->close_on_unref = FALSE;
92 g_io_channel_ref (GIOChannel *channel)
94 g_return_if_fail (channel != NULL);
100 g_io_channel_unref (GIOChannel *channel)
102 g_return_if_fail (channel != NULL);
104 channel->ref_count--;
105 if (channel->ref_count == 0)
107 if (channel->close_on_unref)
108 g_io_channel_shutdown (channel, TRUE, NULL);
110 g_io_channel_purge (channel);
111 g_free (channel->encoding);
112 if (channel->read_cd != (GIConv) -1)
113 g_iconv_close (channel->read_cd);
114 if (channel->write_cd != (GIConv) -1)
115 g_iconv_close (channel->write_cd);
116 g_free (channel->line_term);
117 if (channel->read_buf)
118 g_string_free (channel->read_buf, TRUE);
119 if (channel->write_buf)
120 g_string_free (channel->write_buf, TRUE);
121 if (channel->encoded_read_buf)
122 g_string_free (channel->encoded_read_buf, TRUE);
123 channel->funcs->io_free (channel);
128 g_io_error_get_from_g_error (GIOStatus status,
133 case G_IO_STATUS_NORMAL:
134 case G_IO_STATUS_EOF:
135 return G_IO_ERROR_NONE;
136 case G_IO_STATUS_AGAIN:
137 return G_IO_ERROR_AGAIN;
138 case G_IO_STATUS_ERROR:
139 if (err->domain != G_IO_CHANNEL_ERROR)
140 return G_IO_ERROR_UNKNOWN;
143 case G_IO_CHANNEL_ERROR_INVAL:
144 return G_IO_ERROR_INVAL;
146 return G_IO_ERROR_UNKNOWN;
149 g_assert_not_reached ();
150 return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
156 * @channel: a #GIOChannel.
157 * @buf: a buffer to read the data into (which should be at least count bytes long).
158 * @count: the number of bytes to read from the #GIOChannel.
159 * @bytes_read: returns the number of bytes actually read.
161 * Reads data from a #GIOChannel. This function is depricated. New code should
162 * use g_io_channel_read_chars() instead.
164 * Return value: %G_IO_ERROR_NONE if the operation was successful.
167 g_io_channel_read (GIOChannel *channel,
176 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
177 g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
179 status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
181 error = g_io_error_get_from_g_error (status, err);
190 * g_io_channel_write:
191 * @channel: a #GIOChannel.
192 * @buf: the buffer containing the data to write.
193 * @count: the number of bytes to write.
194 * @bytes_written: the number of bytes actually written.
196 * Writes data to a #GIOChannel. This function is depricated. New code should
197 * use g_io_channel_write_chars() instead.
199 * Return value: %G_IO_ERROR_NONE if the operation was successful.
202 g_io_channel_write (GIOChannel *channel,
205 gsize *bytes_written)
211 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
212 g_return_val_if_fail (bytes_written != NULL, G_IO_ERROR_UNKNOWN);
214 status = channel->funcs->io_write (channel, buf, count, bytes_written, &err);
216 error = g_io_error_get_from_g_error (status, err);
226 * @channel: a #GIOChannel.
227 * @offset: an offset, in bytes, which is added to the position specified by @type
228 * @type: the position in the file, which can be %G_SEEK_CUR (the current
229 * position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
232 * Sets the current position in the #GIOChannel, similar to the standard library
233 * function fseek(). This function is depricated. New code should
234 * use g_io_channel_seek_position() instead.
236 * Return value: %G_IO_ERROR_NONE if the operation was successful.
239 g_io_channel_seek (GIOChannel *channel,
247 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
248 g_return_val_if_fail (channel->is_seekable, G_IO_ERROR_UNKNOWN);
257 g_warning ("g_io_channel_seek: unknown seek type");
258 return G_IO_ERROR_UNKNOWN;
261 status = channel->funcs->io_seek (channel, offset, type, &err);
263 error = g_io_error_get_from_g_error (status, err);
271 /* The function g_io_channel_new_file() is prototyped in both
272 * giounix.c and giowin32.c, so we stick its documentation here.
276 * g_io_channel_new_file:
277 * @filename: A string containing the name of a file.
278 * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
279 * the same meaning as in fopen().
280 * @error: A location to return an error of type %G_IO_FILE_ERROR.
282 * Open a file @filename as a #GIOChannel using mode @mode. This
283 * channel will be closed when the last reference to it is dropped,
284 * so there is no need to call g_io_channel_close() (though doing
285 * so will not cause problems, as long as no attempt is made to
286 * access the channel after it is closed).
288 * Return value: A #GIOChannel on success, %NULL on failure.
292 * g_io_channel_close:
293 * @channel: A #GIOChannel
295 * Close an IO channel. Any pending data to be written will be
296 * flushed, ignoring errors. The channel will not be freed until the
297 * last reference is dropped using g_io_channel_unref(). This
298 * function is deprecated: you should use g_io_channel_shutdown()
302 g_io_channel_close (GIOChannel *channel)
306 g_return_if_fail (channel != NULL);
308 g_io_channel_purge (channel);
310 channel->funcs->io_close (channel, &err);
313 { /* No way to return the error */
314 g_warning ("Error closing channel: %s", err->message);
318 channel->close_on_unref = FALSE; /* Because we already did */
319 channel->is_readable = FALSE;
320 channel->is_writeable = FALSE;
321 channel->is_seekable = FALSE;
325 * g_io_channel_shutdown:
326 * @channel: a #GIOChannel
327 * @flush: if %TRUE, flush pending
328 * @err: location to store a #GIOChannelError
330 * Close an IO channel. Any pending data to be written will be
331 * flushed. The channel will not be freed until the
332 * last reference is dropped using g_io_channel_unref().
337 g_io_channel_shutdown (GIOChannel *channel,
341 GIOStatus status, result;
342 GError *tmperr = NULL;
344 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
345 g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
347 if (flush && channel->write_buf && channel->write_buf->len > 0)
351 /* Set the channel to blocking, to avoid a busy loop
353 flags = g_io_channel_get_flags (channel);
354 /* Ignore any errors here, they're irrelevant */
355 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
357 result = g_io_channel_flush (channel, &tmperr);
359 if (channel->partial_write_buf[0] != '\0')
361 g_warning ("Partial character at end of write buffer not flushed.\n");
362 channel->partial_write_buf[0] = '\0';
366 result = G_IO_STATUS_NORMAL;
368 status = channel->funcs->io_close (channel, err);
370 channel->close_on_unref = FALSE; /* Because we already did */
371 channel->is_readable = FALSE;
372 channel->is_writeable = FALSE;
373 channel->is_seekable = FALSE;
375 if (status != G_IO_STATUS_NORMAL)
377 g_clear_error (&tmperr);
380 else if (result != G_IO_STATUS_NORMAL)
382 g_propagate_error (err, tmperr);
386 return G_IO_STATUS_NORMAL;
389 /* This function is used for the final flush on close or unref */
391 g_io_channel_purge (GIOChannel *channel)
396 g_return_if_fail (channel != NULL);
398 if (channel->write_buf && channel->write_buf->len > 0)
402 /* Set the channel to blocking, to avoid a busy loop
404 flags = g_io_channel_get_flags (channel);
405 g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
407 status = g_io_channel_flush (channel, &err);
410 { /* No way to return the error */
411 g_warning ("Error flushing string: %s", err->message);
416 /* Flush these in case anyone tries to close without unrefing */
418 if (channel->read_buf)
419 g_string_truncate (channel->read_buf, 0);
420 if (channel->write_buf)
421 g_string_truncate (channel->write_buf, 0);
422 if (channel->encoding)
424 if (channel->encoded_read_buf)
425 g_string_truncate (channel->encoded_read_buf, 0);
427 if (channel->partial_write_buf[0] != '\0')
429 g_warning ("Partial character at end of write buffer not flushed.\n");
430 channel->partial_write_buf[0] = '\0';
436 g_io_create_watch (GIOChannel *channel,
437 GIOCondition condition)
439 g_return_val_if_fail (channel != NULL, NULL);
441 return channel->funcs->io_create_watch (channel, condition);
445 g_io_add_watch_full (GIOChannel *channel,
447 GIOCondition condition,
450 GDestroyNotify notify)
455 g_return_val_if_fail (channel != NULL, 0);
457 source = g_io_create_watch (channel, condition);
459 if (priority != G_PRIORITY_DEFAULT)
460 g_source_set_priority (source, priority);
461 g_source_set_callback (source, (GSourceFunc)func, user_data, notify);
463 id = g_source_attach (source, NULL);
464 g_source_unref (source);
470 g_io_add_watch (GIOChannel *channel,
471 GIOCondition condition,
475 return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
479 * g_io_channel_get_buffer_condition:
480 * @channel: A #GIOChannel
482 * This function returns a #GIOCondition depending on the status of the
483 * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
484 * %G_IO_OUT will be set.
486 * Return value: A #GIOCondition
489 g_io_channel_get_buffer_condition (GIOChannel *channel)
491 GIOCondition condition = 0;
493 if (channel->encoding)
495 if (channel->encoded_read_buf && (channel->encoded_read_buf->len > 0))
496 condition |= G_IO_IN; /* Only return if we have full characters */
500 if (channel->read_buf && (channel->read_buf->len > 0))
501 condition |= G_IO_IN;
504 if (channel->write_buf && (channel->write_buf->len < channel->buf_size))
505 condition |= G_IO_OUT;
511 * g_io_channel_error_from_errno:
512 * @en: An errno error number, e.g. EINVAL
514 * Return value: A #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL
517 g_io_channel_error_from_errno (gint en)
520 g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
523 g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
530 g_warning("Invalid file descriptor.\n");
531 return G_IO_CHANNEL_ERROR_FAILED;
536 g_warning("File descriptor outside valid address space.\n");
537 return G_IO_CHANNEL_ERROR_FAILED;
542 return G_IO_CHANNEL_ERROR_FBIG;
547 return G_IO_CHANNEL_ERROR_INVAL;
552 return G_IO_CHANNEL_ERROR_IO;
557 return G_IO_CHANNEL_ERROR_ISDIR;
562 return G_IO_CHANNEL_ERROR_NOSPC;
567 return G_IO_CHANNEL_ERROR_NXIO;
572 return G_IO_CHANNEL_ERROR_OVERFLOW;
577 return G_IO_CHANNEL_ERROR_PIPE;
581 return G_IO_CHANNEL_ERROR_FAILED;
586 * g_io_channel_set_buffer_size:
587 * @channel: a #GIOChannel
588 * @size: the size of the buffer. 0 == pick a good size
590 * Set the buffer size.
593 g_io_channel_set_buffer_size (GIOChannel *channel,
596 g_return_if_fail (channel != NULL);
599 size = G_IO_NICE_BUF_SIZE;
601 if (size < MAX_CHAR_SIZE)
602 size = MAX_CHAR_SIZE;
604 channel->buf_size = size;
608 * g_io_channel_get_buffer_size:
609 * @channel: a #GIOChannel
611 * Get the buffer size.
613 * Return value: the size of the buffer.
616 g_io_channel_get_buffer_size (GIOChannel *channel)
618 g_return_val_if_fail (channel != NULL, 0);
620 return channel->buf_size;
624 * g_io_channel_set_line_term:
625 * @channel: a #GIOChannel
626 * @line_term: The line termination string. Use %NULL for auto detect.
628 * This sets the string that #GIOChannel uses to determine
629 * where in the file a line break occurs.
632 g_io_channel_set_line_term (GIOChannel *channel,
633 const gchar *line_term)
635 g_return_if_fail (channel != NULL);
636 g_return_if_fail (!line_term || line_term[0]); /* Disallow "" */
637 g_return_if_fail (!line_term || g_utf8_validate (line_term, -1, NULL));
638 /* Require valid UTF-8 */
640 g_free (channel->line_term);
641 channel->line_term = g_strdup (line_term);
645 * g_io_channel_get_line_term:
646 * @channel: a #GIOChannel
648 * This returns the string that #GIOChannel uses to determine
649 * where in the file a line break occurs. A value of %NULL
650 * indicates auto detection.
652 * Return value: The line termination string. This value
653 * is owned by GLib and must not be freed.
655 G_CONST_RETURN gchar*
656 g_io_channel_get_line_term (GIOChannel *channel)
658 g_return_val_if_fail (channel != NULL, 0);
660 return channel->line_term;
664 * g_io_channel_set_flags:
665 * @channel: a #GIOChannel
666 * @flags: the flags to set on the channel
667 * @error: A location to return an error of type #GIOChannelError
672 g_io_channel_set_flags (GIOChannel *channel,
676 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
677 g_return_val_if_fail ((error == NULL) || (*error == NULL),
680 return (* channel->funcs->io_set_flags)(channel,
681 flags & G_IO_FLAG_SET_MASK,
686 * g_io_channel_get_flags:
687 * @channel: a #GIOChannel
689 * Gets the current flags for a #GIOChannel, including read-only
690 * flags such as %G_IO_FLAG_IS_READABLE.
692 * Return value: the flags which are set on the channel
695 g_io_channel_get_flags (GIOChannel *channel)
699 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
701 flags = (* channel->funcs->io_get_flags) (channel);
703 /* Cross implementation code */
705 if (channel->is_seekable)
706 flags |= G_IO_FLAG_IS_SEEKABLE;
707 if (channel->is_readable)
708 flags |= G_IO_FLAG_IS_READABLE;
709 if (channel->is_writeable)
710 flags |= G_IO_FLAG_IS_WRITEABLE;
716 * g_io_channel_seek_position:
717 * @channel: a #GIOChannel
718 * @offset: The offset in bytes from the position specified by @type
719 * @type: a #GSeekType. The type %G_SEEK_CUR is only allowed if
720 * the channel has the default encoding or the
721 * encoding %G_IO_CHANNEL_ENCODE_RAW for raw file access.
722 * @error: A location to return an error of type #GIOChannelError
724 * Replacement for g_io_channel_seek() with the new API.
729 g_io_channel_seek_position (GIOChannel* channel,
736 /* For files, only one of the read and write buffers can contain data.
737 * For sockets, both can contain data.
740 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
741 g_return_val_if_fail ((error == NULL) || (*error == NULL),
743 g_return_val_if_fail (channel->is_seekable, G_IO_STATUS_ERROR);
747 case G_SEEK_CUR: /* The user is seeking relative to the head of the buffer */
748 if (channel->use_buffer)
750 if (channel->do_encode && channel->encoded_read_buf
751 && channel->encoded_read_buf->len > 0)
753 g_warning ("Seek type G_SEEK_CUR not allowed for this"
754 " channel's encoding.\n");
755 return G_IO_STATUS_ERROR;
757 if (channel->read_buf)
758 offset -= channel->read_buf->len;
759 if (channel->encoded_read_buf)
761 g_assert (channel->encoded_read_buf->len == 0 && !channel->do_encode);
763 /* If there's anything here, it's because the encoding is UTF-8,
764 * so we can just subtract the buffer length, the same as for
765 * the unencoded data.
768 offset -= channel->encoded_read_buf->len;
776 g_warning ("g_io_channel_seek_position: unknown seek type");
777 return G_IO_STATUS_ERROR;
780 if (channel->use_buffer)
782 status = g_io_channel_flush (channel, error);
783 if (status != G_IO_STATUS_NORMAL)
787 status = channel->funcs->io_seek (channel, offset, type, error);
789 if ((status == G_IO_STATUS_NORMAL) && (channel->use_buffer))
791 if (channel->read_buf)
792 g_string_truncate (channel->read_buf, 0);
794 /* Conversion state no longer matches position in file */
795 if (channel->read_cd != (GIConv) -1)
796 g_iconv (channel->read_cd, NULL, NULL, NULL, NULL);
797 if (channel->write_cd != (GIConv) -1)
798 g_iconv (channel->write_cd, NULL, NULL, NULL, NULL);
800 if (channel->encoded_read_buf)
802 g_assert (channel->encoded_read_buf->len == 0 || !channel->do_encode);
803 g_string_truncate (channel->encoded_read_buf, 0);
806 if (channel->partial_write_buf[0] != '\0')
808 g_warning ("Partial character at end of write buffer not flushed.\n");
809 channel->partial_write_buf[0] = '\0';
817 * g_io_channel_flush:
818 * @channel: a #GIOChannel
819 * @error: location to store an error of type #GIOChannelError
821 * Flush the write buffer for the GIOChannel.
823 * Return value: the status of the operation: One of
824 * G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
825 * G_IO_CHANNEL_ERROR.
828 g_io_channel_flush (GIOChannel *channel,
832 gsize this_time = 1, bytes_written = 0;
834 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
835 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
837 if (channel->write_buf == NULL || channel->write_buf->len == 0)
838 return G_IO_STATUS_NORMAL;
842 g_assert (this_time > 0);
844 status = channel->funcs->io_write (channel,
845 channel->write_buf->str + bytes_written,
846 channel->write_buf->len - bytes_written,
848 bytes_written += this_time;
850 while ((bytes_written < channel->write_buf->len)
851 && (status == G_IO_STATUS_NORMAL));
853 g_string_erase (channel->write_buf, 0, bytes_written);
859 * g_io_channel_set_buffered:
860 * @channel: a #GIOChannel
861 * @buffered: whether to set the channel buffered or unbuffered
863 * The buffering state can only be set if the channel's encoding
864 * is %NULL. For any other encoding, the channel must be buffered.
866 * The default state of the channel is buffered.
869 g_io_channel_set_buffered (GIOChannel *channel,
872 g_return_if_fail (channel != NULL);
874 if (channel->encoding != NULL)
876 g_warning ("Need to have NULL encoding to set the buffering state of the "
881 g_return_if_fail (!channel->read_buf || channel->read_buf->len == 0);
882 g_return_if_fail (!channel->write_buf || channel->write_buf->len == 0);
884 channel->use_buffer = buffered;
888 * g_io_channel_get_buffered:
889 * @channel: a #GIOChannel
891 * Return Value: the buffering state of the channel
894 g_io_channel_get_buffered (GIOChannel *channel)
896 g_return_val_if_fail (channel != NULL, FALSE);
898 return channel->use_buffer;
902 * g_io_channel_set_encoding:
903 * @channel: a #GIOChannel
904 * @encoding: the encoding type
905 * @error: location to store an error of type #GConvertError.
907 * Set the encoding for the input/output of the channel. The internal
908 * encoding is always UTF-8. The default encoding for the
909 * external file is UTF-8.
911 * The encoding %NULL is safe to use with binary data.
912 * Encodings other than %NULL must use a buffered channel.
913 * Encodings other than %NULL and UTF-8 cannot
914 * use g_io_channel_seek_position() with seek type %G_SEEK_CUR,
915 * and cannot mix reading and writing if the channel is
916 * a file without first doing a seek of type %G_SEEK_SET or
919 * The encoding can only be set under three conditions:
921 * 1. The channel was just created, and has not been written to
924 * 2. The channel is a file, and the file pointer was just
925 * repositioned by a call to g_io_channel_seek_position().
926 * (This flushes all the internal buffers.)
928 * 3. The current encoding is %NULL or UTF-8.
930 * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
933 g_io_channel_set_encoding (GIOChannel *channel,
934 const gchar *encoding,
937 GIConv read_cd, write_cd;
940 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
941 g_return_val_if_fail ((error == NULL) || (*error == NULL), G_IO_STATUS_ERROR);
943 /* Make sure the encoded buffers are empty */
945 g_return_val_if_fail (!channel->do_encode || !channel->encoded_read_buf ||
946 channel->encoded_read_buf->len == 0, G_IO_STATUS_ERROR);
948 if (!channel->use_buffer)
950 g_warning ("Need to set the channel buffered before setting the encoding.\n");
951 g_warning ("Assuming this is what you meant and acting accordingly.\n");
953 channel->use_buffer = TRUE;
956 if (channel->partial_write_buf[0] != '\0')
958 g_warning ("Partial character at end of write buffer not flushed.\n");
959 channel->partial_write_buf[0] = '\0';
962 did_encode = channel->do_encode;
964 if (!encoding || strcmp (encoding, "UTF8") == 0 || strcmp (encoding, "UTF-8") == 0)
966 channel->do_encode = FALSE;
967 read_cd = write_cd = (GIConv) -1;
972 const gchar *from_enc = NULL, *to_enc = NULL;
974 if (channel->is_readable)
976 read_cd = g_iconv_open ("UTF-8", encoding);
978 if (read_cd == (GIConv) -1)
986 read_cd = (GIConv) -1;
988 if (channel->is_writeable && err == 0)
990 write_cd = g_iconv_open (encoding, "UTF-8");
992 if (write_cd == (GIConv) -1)
1000 write_cd = (GIConv) -1;
1004 g_assert (from_enc);
1008 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
1009 _("Conversion from character set `%s' to `%s' is not supported"),
1012 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1013 _("Could not open converter from `%s' to `%s': %s"),
1014 from_enc, to_enc, strerror (errno));
1016 if (read_cd != (GIConv) -1)
1017 g_iconv_close (read_cd);
1018 if (write_cd != (GIConv) -1)
1019 g_iconv_close (write_cd);
1021 return G_IO_STATUS_ERROR;
1024 channel->do_encode = TRUE;
1027 /* The encoding is ok, so set the fields in channel */
1029 if (channel->read_cd != (GIConv) -1)
1030 g_iconv_close (channel->read_cd);
1031 if (channel->write_cd != (GIConv) -1)
1032 g_iconv_close (channel->write_cd);
1034 if (channel->encoded_read_buf && channel->encoded_read_buf->len > 0)
1036 g_assert (!did_encode); /* Encoding UTF-8, NULL doesn't use encoded_read_buf */
1038 /* This is just validated UTF-8, so we can copy it back into read_buf
1039 * so it can be encoded in whatever the new encoding is.
1042 g_string_prepend_len (channel->read_buf, channel->encoded_read_buf->str,
1043 channel->encoded_read_buf->len);
1044 g_string_truncate (channel->encoded_read_buf, 0);
1047 channel->read_cd = read_cd;
1048 channel->write_cd = write_cd;
1050 g_free (channel->encoding);
1051 channel->encoding = g_strdup (encoding);
1053 return G_IO_STATUS_NORMAL;
1057 * g_io_channel_get_encoding:
1058 * @channel: a #GIOChannel
1060 * Get the encoding for the input/output of the channel. The internal
1061 * encoding is always UTF-8. The encoding %G_IO_CHANNEL_ENCODE_RAW
1062 * disables encoding and turns off internal buffering. Both
1063 * %G_IO_CHANNEL_ENCODE_RAW and the default (no encoding, but buffered)
1064 * are safe to use with binary data.
1066 * Return value: A string containing the encoding, this string is
1067 * owned by GLib and must not be freed.
1069 G_CONST_RETURN gchar*
1070 g_io_channel_get_encoding (GIOChannel *channel)
1072 g_return_val_if_fail (channel != NULL, NULL);
1074 return channel->encoding;
1078 g_io_channel_fill_buffer (GIOChannel *channel,
1081 gsize read_size, cur_len, oldlen;
1084 if (channel->is_seekable && channel->write_buf && channel->write_buf->len > 0)
1086 status = g_io_channel_flush (channel, err);
1087 if (status != G_IO_STATUS_NORMAL)
1090 if (channel->is_seekable && channel->partial_write_buf[0] != '\0')
1092 g_warning ("Partial character at end of write buffer not flushed.\n");
1093 channel->partial_write_buf[0] = '\0';
1096 if (!channel->read_buf)
1097 channel->read_buf = g_string_sized_new (channel->buf_size);
1099 cur_len = channel->read_buf->len;
1101 g_string_set_size (channel->read_buf, channel->read_buf->len + channel->buf_size);
1103 status = channel->funcs->io_read (channel, channel->read_buf->str + cur_len,
1104 channel->buf_size, &read_size, err);
1106 g_assert ((status == G_IO_STATUS_NORMAL) || (read_size == 0));
1108 g_string_truncate (channel->read_buf, read_size + cur_len);
1110 if ((status != G_IO_STATUS_NORMAL)
1111 && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
1114 g_assert (channel->read_buf->len > 0);
1116 if (channel->encoded_read_buf)
1117 oldlen = channel->encoded_read_buf->len;
1121 if (channel->encoding)
1122 channel->encoded_read_buf = g_string_sized_new (channel->buf_size);
1125 if (channel->do_encode)
1127 size_t errnum, inbytes_left, outbytes_left;
1128 gchar *inbuf, *outbuf;
1130 g_assert (channel->encoded_read_buf);
1134 inbytes_left = channel->read_buf->len;
1135 outbytes_left = MAX (6, MAX (channel->read_buf->len,
1136 channel->encoded_read_buf->allocated_len
1137 - channel->encoded_read_buf->len));
1139 inbuf = channel->read_buf->str;
1140 outbuf = channel->encoded_read_buf->str + channel->encoded_read_buf->len;
1142 g_string_set_size (channel->encoded_read_buf,
1143 channel->encoded_read_buf->len + outbytes_left);
1145 errnum = g_iconv (channel->read_cd, &inbuf, &inbytes_left,
1146 &outbuf, &outbytes_left);
1148 g_string_erase (channel->read_buf, 0,
1149 channel->read_buf->len - inbytes_left);
1150 g_string_truncate (channel->encoded_read_buf,
1151 channel->encoded_read_buf->len - outbytes_left);
1153 if (errnum == (size_t) -1)
1158 if ((oldlen == channel->encoded_read_buf->len)
1159 && (status == G_IO_STATUS_EOF))
1160 status = G_IO_STATUS_EOF;
1162 status = G_IO_STATUS_NORMAL;
1165 /* Buffer size at least 6, wrote at least on character */
1166 g_assert (inbuf != channel->read_buf->str);
1169 if (oldlen < channel->encoded_read_buf->len)
1170 status = G_IO_STATUS_NORMAL;
1173 g_set_error (err, G_CONVERT_ERROR,
1174 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1175 _("Invalid byte sequence in conversion input"));
1176 return G_IO_STATUS_ERROR;
1180 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1181 _("Error during conversion: %s"), strerror (errno));
1182 return G_IO_STATUS_ERROR;
1185 g_assert ((status != G_IO_STATUS_NORMAL)
1186 || (channel->encoded_read_buf->len > 0));
1188 else if (channel->encoding) /* UTF-8 */
1190 gchar *nextchar, *lastchar;
1192 g_assert (channel->encoded_read_buf);
1194 nextchar = channel->read_buf->str;
1195 lastchar = channel->read_buf->str + channel->read_buf->len;
1197 while (nextchar < lastchar)
1201 val_char = g_utf8_get_char_validated (nextchar, lastchar - nextchar);
1206 /* stop, leave partial character in buffer */
1207 lastchar = nextchar;
1210 if (oldlen < channel->encoded_read_buf->len)
1211 status = G_IO_STATUS_NORMAL;
1214 g_set_error (err, G_CONVERT_ERROR,
1215 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1216 _("Invalid byte sequence in conversion input"));
1217 status = G_IO_STATUS_ERROR;
1219 lastchar = nextchar;
1222 nextchar = g_utf8_next_char (nextchar);
1227 if (lastchar > channel->read_buf->str)
1229 gint copy_len = lastchar - channel->read_buf->str;
1231 g_string_append_len (channel->encoded_read_buf, channel->read_buf->str,
1233 g_string_erase (channel->read_buf, 0, copy_len);
1241 * g_io_channel_read_line:
1242 * @channel: a #GIOChannel
1243 * @str_return: The line read from the #GIOChannel, not including the
1244 * line terminator. This data should be freed with g_free()
1245 * when no longer needed. This
1246 * is a null terminated string. If a @length of zero is
1247 * returned, this will be %NULL instead.
1248 * @length: location to store length of the read data, or %NULL
1249 * @terminator_pos: location to store position of line terminator, or %NULL
1250 * @error: A location to return an error of type #GConvertError
1251 * or #GIOChannelError
1253 * Read a line, not including the terminating character(s),
1254 * from a #GIOChannel into a newly allocated string.
1255 * @length will contain allocated memory if the return
1256 * is %G_IO_STATUS_NORMAL.
1258 * Return value: a newly allocated string. Free this string
1259 * with g_free() when you are done with it.
1262 g_io_channel_read_line (GIOChannel *channel,
1265 gsize *terminator_pos,
1271 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1272 g_return_val_if_fail (str_return != NULL, G_IO_STATUS_ERROR);
1273 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1275 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1277 status = g_io_channel_read_line_backend (channel, &got_length, terminator_pos, error);
1280 *length = got_length;
1282 if (status == G_IO_STATUS_NORMAL)
1284 g_assert (USE_BUF (channel));
1285 *str_return = g_strndup (USE_BUF (channel)->str, got_length);
1286 g_string_erase (USE_BUF (channel), 0, got_length);
1295 * g_io_channel_read_line_string:
1296 * @channel: a #GIOChannel
1297 * @buffer: a #GString into which the line will be written.
1298 * If @buffer already contains data, the new data will
1299 * be appended to it.
1300 * @terminator_pos: location to store position of line terminator, or %NULL
1301 * @error: a location to store an error of type #GConvertError
1302 * or #GIOChannelError
1304 * Read a line from a #GIOChannel, using a #GString as a buffer.
1309 g_io_channel_read_line_string (GIOChannel *channel,
1311 gsize *terminator_pos,
1317 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1318 g_return_val_if_fail (buffer != NULL, G_IO_STATUS_ERROR);
1319 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1321 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1323 if (buffer->len > 0)
1324 g_string_truncate (buffer, 0); /* clear out the buffer */
1326 status = g_io_channel_read_line_backend (channel, &length, terminator_pos, error);
1328 if (status == G_IO_STATUS_NORMAL)
1330 g_assert (USE_BUF (channel));
1331 g_string_append_len (buffer, USE_BUF (channel)->str, length);
1332 g_string_erase (USE_BUF (channel), 0, length);
1340 g_io_channel_read_line_backend (GIOChannel *channel,
1342 gsize *terminator_pos,
1346 gsize checked_to, line_term_len, line_length, got_term_len;
1347 gboolean first_time = TRUE;
1349 if (!channel->use_buffer)
1351 /* Can't do a raw read in read_line */
1352 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1353 _("Can't do a raw read in g_io_channel_read_line_string"));
1354 return G_IO_STATUS_ERROR;
1357 status = G_IO_STATUS_NORMAL;
1359 if (channel->line_term)
1360 line_term_len = strlen (channel->line_term);
1363 /* This value used for setting checked_to, it's the longest of the four
1364 * we autodetect for.
1371 gchar *nextchar, *lastchar;
1374 if (!first_time || (BUF_LEN (USE_BUF (channel)) == 0))
1377 status = g_io_channel_fill_buffer (channel, error);
1380 case G_IO_STATUS_NORMAL:
1381 if (BUF_LEN (USE_BUF (channel)) == 0)
1382 /* Can happen when using conversion and only read
1383 * part of a character
1390 case G_IO_STATUS_EOF:
1391 if (BUF_LEN (USE_BUF (channel)) == 0)
1396 if (channel->encoding && channel->read_buf->len != 0)
1398 g_set_error (error, G_CONVERT_ERROR,
1399 G_CONVERT_ERROR_PARTIAL_INPUT,
1400 "Leftover unconverted data in read buffer");
1401 return G_IO_STATUS_ERROR;
1404 return G_IO_STATUS_EOF;
1414 g_assert (BUF_LEN (USE_BUF (channel)) != 0);
1416 use_buf = USE_BUF (channel); /* The buffer has been created by this point */
1420 lastchar = use_buf->str + strlen (use_buf->str);
1422 for (nextchar = use_buf->str + checked_to; nextchar < lastchar;
1423 channel->encoding ? nextchar = g_utf8_next_char (nextchar) : nextchar++)
1425 if (channel->line_term)
1427 if (strncmp (channel->line_term, nextchar, line_term_len) == 0)
1429 line_length = nextchar - use_buf->str;
1430 got_term_len = line_term_len;
1434 else /* auto detect */
1438 case '\n': /* unix */
1439 line_length = nextchar - use_buf->str;
1442 case '\r': /* Warning: do not use with sockets */
1443 line_length = nextchar - use_buf->str;
1444 if ((nextchar == lastchar - 1) && (status != G_IO_STATUS_EOF)
1445 && (lastchar == use_buf->str + use_buf->len))
1446 goto read_again; /* Try to read more data */
1447 if ((nextchar < lastchar - 1) && (*(nextchar + 1) == '\n')) /* dos */
1452 case '\xe2': /* Unicode paragraph separator */
1453 if (strncmp ("\xe2\x80\xa9", nextchar, 3) == 0)
1455 line_length = nextchar - use_buf->str;
1460 default: /* no match */
1466 g_assert (nextchar == lastchar); /* Valid UTF-8, didn't overshoot */
1468 /* Also terminate on '\0' */
1470 line_length = lastchar - use_buf->str;
1471 if (line_length < use_buf->len)
1479 if (status == G_IO_STATUS_EOF)
1481 if (channel->encoding && channel->read_buf->len > 0)
1483 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1484 "Channel terminates in a partial character");
1485 return G_IO_STATUS_ERROR;
1487 line_length = use_buf->len;
1492 if (use_buf->len > line_term_len - 1)
1493 checked_to = use_buf->len - (line_term_len - 1);
1501 *terminator_pos = line_length;
1504 *length = line_length + got_term_len;
1506 return G_IO_STATUS_NORMAL;
1510 * g_io_channel_read_to_end:
1511 * @channel: a #GIOChannel
1512 * @str_return: Location to store a pointer to a string holding
1513 * the remaining data in the #GIOChannel. This data should
1514 * be freed with g_free() when no longer needed. This
1515 * data is terminated by an extra null, but there may be other
1516 * nulls in the intervening data.
1517 * @length: Location to store length of the data
1518 * @error: A location to return an error of type #GConvertError
1519 * or #GIOChannelError
1521 * Read all the remaining data from the file. Parameters as
1522 * for g_io_channel_read_line.
1524 * Return value: One of #G_IO_STATUS_EOF or #G_IO_STATUS_PARTIAL_CHARS
1528 g_io_channel_read_to_end (GIOChannel *channel,
1535 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1536 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1538 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1545 if (!channel->use_buffer)
1547 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
1548 _("Can't do a raw read in g_io_channel_read_to_end"));
1549 return G_IO_STATUS_ERROR;
1553 status = g_io_channel_fill_buffer (channel, error);
1554 while (status == G_IO_STATUS_NORMAL);
1556 if (status != G_IO_STATUS_EOF)
1559 if (channel->encoding && channel->read_buf->len > 0)
1561 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1562 "Channel terminates in a partial character");
1563 return G_IO_STATUS_ERROR;
1566 if (USE_BUF (channel) == NULL)
1568 /* length is already set to zero */
1570 *str_return = g_strdup ("");
1575 *length = USE_BUF (channel)->len;
1578 *str_return = g_string_free (USE_BUF (channel), FALSE);
1580 g_string_free (USE_BUF (channel), TRUE);
1582 /* This only works because USE_BUF () is a macro */
1583 USE_BUF (channel) = NULL;
1586 return G_IO_STATUS_NORMAL;
1590 * g_io_channel_read_chars:
1591 * @channel: a #GIOChannel
1592 * @buf: a buffer to read data into
1593 * @count: the size of the buffer. Note that the buffer may
1594 * not be complelely filled even if there is data
1595 * in the buffer if the remaining data is not a
1596 * complete character.
1597 * @bytes_read: The number of bytes read.
1598 * @error: A location to return an error of type #GConvertError
1599 * or #GIOChannelError.
1601 * Replacement for g_io_channel_read() with the new API.
1606 g_io_channel_read_chars (GIOChannel *channel,
1615 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1616 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1618 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1623 return G_IO_STATUS_NORMAL;
1625 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1627 if (!channel->use_buffer)
1629 g_assert (!channel->read_buf || channel->read_buf->len == 0);
1631 return channel->funcs->io_read (channel, buf, count, bytes_read, error);
1634 status = G_IO_STATUS_NORMAL;
1636 while (BUF_LEN (USE_BUF (channel)) < count && status == G_IO_STATUS_NORMAL)
1637 status = g_io_channel_fill_buffer (channel, error);
1639 /* Only return an error if we have no data */
1641 if (BUF_LEN (USE_BUF (channel)) == 0)
1643 g_assert (status != G_IO_STATUS_NORMAL);
1645 if (status == G_IO_STATUS_EOF && channel->encoding
1646 && BUF_LEN (channel->read_buf) > 0)
1648 g_set_error (error, G_CONVERT_ERROR,
1649 G_CONVERT_ERROR_PARTIAL_INPUT,
1650 "Leftover unconverted data in read buffer");
1651 status = G_IO_STATUS_ERROR;
1660 if (status == G_IO_STATUS_ERROR)
1661 g_clear_error (error);
1663 got_bytes = MIN (count, BUF_LEN (USE_BUF (channel)));
1665 g_assert (got_bytes > 0);
1667 if (channel->encoding)
1668 /* Don't validate for NULL encoding, binary safe */
1670 gchar *nextchar, *prevchar;
1672 g_assert (USE_BUF (channel) == channel->encoded_read_buf);
1674 nextchar = channel->encoded_read_buf->str;
1678 prevchar = nextchar;
1679 nextchar = g_utf8_next_char (nextchar);
1680 g_assert (nextchar != prevchar); /* Posibile for *prevchar of -1 or -2 */
1682 while (nextchar < channel->encoded_read_buf->str + got_bytes);
1684 if (nextchar > channel->encoded_read_buf->str + got_bytes)
1685 got_bytes = prevchar - channel->encoded_read_buf->str;
1687 g_assert (got_bytes > 0 || count < 6);
1690 memcpy (buf, USE_BUF (channel)->str, got_bytes);
1691 g_string_erase (USE_BUF (channel), 0, got_bytes);
1694 *bytes_read = got_bytes;
1696 return G_IO_STATUS_NORMAL;
1700 * g_io_channel_read_unichar:
1701 * @channel: a #GIOChannel
1702 * @thechar: a location to return a character
1703 * @error: A location to return an error of type #GConvertError
1704 * or #GIOChannelError
1706 * This function cannot be called on a channel with %NULL encoding.
1708 * Return value: a #GIOStatus
1711 g_io_channel_read_unichar (GIOChannel *channel,
1715 GIOStatus status = G_IO_STATUS_NORMAL;
1717 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1718 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
1719 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1721 g_return_val_if_fail (channel->is_readable, G_IO_STATUS_ERROR);
1723 while (BUF_LEN (channel->encoded_read_buf) == 0 && status == G_IO_STATUS_NORMAL)
1724 status = g_io_channel_fill_buffer (channel, error);
1726 /* Only return an error if we have no data */
1728 if (BUF_LEN (USE_BUF (channel)) == 0)
1730 g_assert (status != G_IO_STATUS_NORMAL);
1732 if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
1734 g_set_error (error, G_CONVERT_ERROR,
1735 G_CONVERT_ERROR_PARTIAL_INPUT,
1736 "Leftover unconverted data in read buffer");
1737 status = G_IO_STATUS_ERROR;
1741 *thechar = (gunichar) -1;
1746 if (status == G_IO_STATUS_ERROR)
1747 g_clear_error (error);
1750 *thechar = g_utf8_get_char (channel->encoded_read_buf->str);
1752 g_string_erase (channel->encoded_read_buf, 0,
1753 g_utf8_next_char (channel->encoded_read_buf->str)
1754 - channel->encoded_read_buf->str);
1756 return G_IO_STATUS_NORMAL;
1760 * g_io_channel_write_chars:
1761 * @channel: a #GIOChannel
1762 * @buf: a buffer to write data from
1763 * @count: the size of the buffer. If -1, the buffer
1764 * is taken to be a nul terminated string.
1765 * @bytes_written: The number of bytes written. This can be nonzero
1766 * even if the return value is not %G_IO_STATUS_NORMAL.
1767 * @error: A location to return an error of type #GConvertError
1768 * or #GIOChannelError
1770 * Replacement for g_io_channel_write() with the new API.
1775 g_io_channel_write_chars (GIOChannel *channel,
1778 gsize *bytes_written,
1782 gssize wrote_bytes = 0;
1784 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
1785 g_return_val_if_fail ((error == NULL) || (*error == NULL),
1787 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
1789 if ((count < 0) && buf)
1790 count = strlen (buf);
1793 return G_IO_STATUS_NORMAL;
1795 g_return_val_if_fail (buf != NULL, G_IO_STATUS_ERROR);
1796 g_return_val_if_fail (count > 0, G_IO_STATUS_ERROR);
1798 /* Raw write case */
1800 if (!channel->use_buffer)
1802 g_assert (!channel->write_buf || channel->write_buf->len == 0);
1803 g_assert (channel->partial_write_buf[0] == '\0');
1804 return channel->funcs->io_write (channel, buf, count, bytes_written, error);
1809 if (channel->is_seekable && (( BUF_LEN (channel->read_buf) > 0)
1810 || (BUF_LEN (channel->encoded_read_buf) > 0)))
1812 if (channel->do_encode && BUF_LEN (channel->encoded_read_buf) > 0)
1814 g_warning("Mixed reading and writing not allowed on encoded files");
1815 return G_IO_STATUS_ERROR;
1817 status = g_io_channel_seek_position (channel, 0, G_SEEK_CUR, error);
1818 if (status != G_IO_STATUS_NORMAL)
1822 if (!channel->write_buf)
1823 channel->write_buf = g_string_sized_new (channel->buf_size);
1825 while (wrote_bytes < count)
1829 /* If the buffer is full, try a write immediately. In
1830 * the nonblocking case, this prevents the user from
1831 * writing just a little bit to the buffer every time
1832 * and never receiving an EAGAIN.
1835 if (channel->write_buf->len >= channel->buf_size)
1837 gsize did_write = 0, this_time;
1841 status = channel->funcs->io_write (channel, channel->write_buf->str
1842 + did_write, channel->write_buf->len
1843 - did_write, &this_time, error);
1844 did_write += this_time;
1846 while (status == G_IO_STATUS_NORMAL &&
1847 did_write < MIN (channel->write_buf->len, MAX_CHAR_SIZE));
1849 g_string_erase (channel->write_buf, 0, did_write);
1851 if (status != G_IO_STATUS_NORMAL)
1853 if (status == G_IO_STATUS_AGAIN && wrote_bytes > 0)
1854 status = G_IO_STATUS_NORMAL;
1856 *bytes_written = wrote_bytes;
1861 space_in_buf = MAX (channel->buf_size, channel->write_buf->allocated_len)
1862 - channel->write_buf->len;
1864 /* This is only true because g_io_channel_set_buffer_size ()
1865 * ensures that channel->buf_size >= MAX_CHAR_SIZE.
1867 g_assert (space_in_buf >= MAX_CHAR_SIZE);
1869 if (!channel->encoding)
1871 gssize write_this = MIN (space_in_buf, count - wrote_bytes);
1873 g_string_append_len (channel->write_buf, buf, write_this);
1875 wrote_bytes += write_this;
1879 const gchar *from_buf;
1880 gsize from_buf_len, from_buf_old_len, left_len;
1884 if (channel->partial_write_buf[0] != '\0')
1886 g_assert (wrote_bytes == 0);
1888 from_buf = channel->partial_write_buf;
1889 from_buf_old_len = strlen (channel->partial_write_buf);
1890 g_assert (from_buf_old_len > 0);
1891 from_buf_len = MIN (6, from_buf_old_len + count);
1893 memcpy (channel->partial_write_buf + from_buf_old_len, buf,
1894 from_buf_len - from_buf_old_len);
1899 from_buf_len = count - wrote_bytes;
1900 from_buf_old_len = 0;
1905 if (!channel->do_encode)
1907 const gchar *badchar;
1909 /* UTF-8, just validate, emulate g_iconv */
1911 if (!g_utf8_validate (from_buf, from_buf_len, &badchar))
1915 left_len = from_buf + from_buf_len - badchar;
1917 try_char = g_utf8_get_char_validated (badchar, left_len);
1922 g_assert (left_len < 6);
1929 g_assert_not_reached ();
1930 errnum = 0; /* Don't confunse the compiler */
1941 g_string_append_len (channel->write_buf, from_buf,
1942 from_buf_len - left_len);
1943 from_buf += from_buf_len - left_len;
1949 left_len = from_buf_len;
1950 outbuf = channel->write_buf->str + channel->write_buf->len;
1951 g_string_set_size (channel->write_buf, channel->write_buf->len
1953 err = g_iconv (channel->write_cd, (gchar **) &from_buf, &left_len,
1954 &outbuf, &space_in_buf);
1956 g_string_truncate (channel->write_buf, channel->write_buf->len
1960 if (err == (size_t) -1)
1965 g_assert (left_len < 6);
1967 if (from_buf_old_len == 0)
1969 /* Not from partial_write_buf */
1971 memcpy (channel->partial_write_buf, from_buf, left_len);
1972 channel->partial_write_buf[left_len] = '\0';
1973 return G_IO_STATUS_NORMAL;
1976 /* Working in partial_write_buf */
1978 if (left_len == from_buf_len)
1980 /* Didn't convert anything, must still have
1981 * less than a full character
1984 g_assert (count == from_buf_len - from_buf_old_len);
1986 channel->partial_write_buf[from_buf_len] = '\0';
1989 *bytes_written = count;
1991 return G_IO_STATUS_NORMAL;
1994 g_assert (from_buf_len - left_len >= from_buf_old_len);
1996 /* We converted all the old data. This is fine */
2000 if (from_buf_len == left_len)
2002 /* Nothing was written, add enough space for
2003 * at least one character.
2005 space_in_buf += MAX_CHAR_SIZE;
2010 g_set_error (error, G_CONVERT_ERROR,
2011 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
2012 _("Invalid byte sequence in conversion input"));
2013 if (from_buf_old_len > 0 && from_buf_len == left_len)
2014 g_warning ("Illegal sequence due to partial character "
2015 "at the end of a previous write.\n");
2017 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2019 *bytes_written = wrote_bytes;
2020 channel->partial_write_buf[0] = '\0';
2021 return G_IO_STATUS_ERROR;
2023 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
2024 _("Error during conversion: %s"), strerror (errno));
2025 if (from_buf_len >= left_len + from_buf_old_len)
2026 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2028 *bytes_written = wrote_bytes;
2029 channel->partial_write_buf[0] = '\0';
2030 return G_IO_STATUS_ERROR;
2034 g_assert (from_buf_len - left_len >= from_buf_old_len);
2036 wrote_bytes += from_buf_len - left_len - from_buf_old_len;
2038 if (from_buf_old_len > 0)
2040 /* We were working in partial_write_buf */
2042 buf += from_buf_len - left_len - from_buf_old_len;
2043 channel->partial_write_buf[0] = '\0';
2051 *bytes_written = wrote_bytes;
2053 return G_IO_STATUS_NORMAL;
2057 * g_io_channel_write_unichar:
2058 * @channel: a #GIOChannel
2059 * @thechar: a character
2060 * @error: A location to return an error of type #GConvertError
2061 * or #GIOChannelError
2063 * This function cannot be called on a channel with %NULL encoding.
2065 * Return value: a #GIOStatus
2068 g_io_channel_write_unichar (GIOChannel *channel,
2073 gchar static_buf[6];
2074 gsize char_len, wrote_len;
2076 g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
2077 g_return_val_if_fail (channel->encoding != NULL, G_IO_STATUS_ERROR);
2078 g_return_val_if_fail ((error == NULL) || (*error == NULL),
2080 g_return_val_if_fail (channel->is_writeable, G_IO_STATUS_ERROR);
2082 char_len = g_unichar_to_utf8 (thechar, static_buf);
2084 if (channel->partial_write_buf[0] != '\0')
2086 g_warning ("Partial charater written before writing unichar.\n");
2087 channel->partial_write_buf[0] = '\0';
2090 status = g_io_channel_write_chars (channel, static_buf,
2091 char_len, &wrote_len, error);
2093 /* We validate UTF-8, so we can't get a partial write */
2095 g_assert (wrote_len == char_len || status != G_IO_STATUS_NORMAL);
2101 * g_io_channel_error_quark:
2103 * Return value: The quark used as %G_IO_CHANNEL_ERROR
2106 g_io_channel_error_quark (void)
2108 static GQuark q = 0;
2110 q = g_quark_from_static_string ("g-io-channel-error-quark");