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.
30 typedef struct _GIOChannelPrivate GIOChannelPrivate;
32 struct _GIOChannelPrivate {
40 g_io_channel_new (GIOFuncs *funcs,
41 gpointer channel_data)
43 GIOChannelPrivate *result;
46 g_return_val_if_fail (funcs != NULL, NULL);
48 result = g_new (GIOChannelPrivate, 1);
49 channel = (GIOChannel *)result;
51 result->funcs = funcs;
52 result->ref_count = 1;
53 result->closed = FALSE;
55 channel->channel_data = channel_data;
61 g_io_channel_ref (GIOChannel *channel)
63 GIOChannelPrivate *private;
64 g_return_if_fail (channel != NULL);
66 private = (GIOChannelPrivate *)channel;
72 g_io_channel_unref (GIOChannel *channel)
74 GIOChannelPrivate *private;
75 g_return_if_fail (channel != NULL);
77 private = (GIOChannelPrivate *)channel;
80 if (private->ref_count == 0)
82 /* We don't want to close the channel here, because
83 * the channel may just be wrapping a file or socket
84 * that the app is independently manipulating.
86 private->funcs->io_free (channel);
92 g_io_channel_read (GIOChannel *channel,
97 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
99 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
100 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
102 return private->funcs->io_read (channel, buf, count, bytes_read);
106 g_io_channel_write (GIOChannel *channel,
109 guint *bytes_written)
111 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
113 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
114 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
116 return private->funcs->io_write (channel, buf, count, bytes_written);
120 g_io_channel_seek (GIOChannel *channel,
124 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
126 g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
127 g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
129 return private->funcs->io_seek (channel, offset, type);
133 g_io_channel_close (GIOChannel *channel)
135 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
137 g_return_if_fail (channel != NULL);
138 g_return_if_fail (!private->closed);
140 private->closed = TRUE;
141 private->funcs->io_close (channel);
145 g_io_add_watch_full (GIOChannel *channel,
147 GIOCondition condition,
150 GDestroyNotify notify)
152 GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
154 g_return_val_if_fail (channel != NULL, 0);
155 g_return_val_if_fail (!private->closed, 0);
157 return private->funcs->io_add_watch (channel, priority, condition,
158 func, user_data, notify);
162 g_io_add_watch (GIOChannel *channel,
163 GIOCondition condition,
167 return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);