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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
26 typedef struct _GIOChannelPrivate GIOChannelPrivate;
28 struct _GIOChannelPrivate {
36 g_io_channel_new (GIOFuncs *funcs,
37 gpointer channel_data)
39 GIOChannelPrivate *result;
42 g_return_val_if_fail (funcs != NULL, NULL);
44 result = g_new (GIOChannelPrivate, 1);
45 channel = (GIOChannel *)result;
47 result->funcs = funcs;
48 result->ref_count = 1;
49 result->closed = FALSE;
51 channel->channel_data = channel_data;
57 g_io_channel_ref (GIOChannel *channel)
59 GIOChannelPrivate *private;
60 g_return_if_fail (channel != NULL);
62 private = (GIOChannelPrivate *)channel;
68 g_io_channel_unref (GIOChannel *channel)
70 GIOChannelPrivate *private;
71 g_return_if_fail (channel != NULL);
73 private = (GIOChannelPrivate *)channel;
76 if (private->ref_count == 0)
78 /* We don't want to close the channel here, because
79 * the channel may just be wrapping a file or socket
80 * that the app is independently manipulating.
82 private->funcs->io_free (channel);
88 g_io_channel_read (GIOChannel *channel,
93 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
95 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
96 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
98 return private->funcs->io_read (channel, buf, count, bytes_read);
102 g_io_channel_write (GIOChannel *channel,
105 guint *bytes_written)
107 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
109 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
110 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
112 return private->funcs->io_write (channel, buf, count, bytes_written);
116 g_io_channel_seek (GIOChannel *channel,
120 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
122 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
123 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
125 return private->funcs->io_seek (channel, offset, type);
129 g_io_channel_close (GIOChannel *channel)
131 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
133 g_return_if_fail (channel != NULL);
134 g_return_if_fail (!private->closed);
136 private->closed = TRUE;
137 private->funcs->io_close (channel);
141 g_io_add_watch_full (GIOChannel *channel,
143 GIOCondition condition,
146 GDestroyNotify notify)
148 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
150 g_return_val_if_fail (channel != NULL, 0);
151 g_return_val_if_fail (!private->closed, 0);
153 return private->funcs->io_add_watch (channel, priority, condition,
154 func, user_data, notify);
158 g_io_add_watch (GIOChannel *channel,
159 GIOCondition condition,
163 return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);