This commit merges the glib-threads branch into the main
[platform/upstream/glib.git] / glib / giochannel.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * giochannel.c: IO Channel abstraction
5  * Copyright 1998 Owen Taylor
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include "glib.h"
28 #include <unistd.h>
29
30 typedef struct _GIOChannelPrivate GIOChannelPrivate;
31
32 struct _GIOChannelPrivate {
33   GIOChannel channel;
34   GIOFuncs *funcs;
35   guint ref_count;
36   gboolean closed;
37 };
38
39 GIOChannel *
40 g_io_channel_new (GIOFuncs *funcs,
41                   gpointer  channel_data)
42 {
43   GIOChannelPrivate *result;
44   GIOChannel *channel;
45   
46   g_return_val_if_fail (funcs != NULL, NULL);
47   
48   result = g_new (GIOChannelPrivate, 1);
49   channel = (GIOChannel *)result;
50   
51   result->funcs = funcs;
52   result->ref_count = 1;
53   result->closed = FALSE;
54
55   channel->channel_data = channel_data;
56   return channel;
57 }
58
59
60 void 
61 g_io_channel_ref (GIOChannel *channel)
62 {
63   GIOChannelPrivate *private;
64   g_return_if_fail (channel != NULL);
65
66   private = (GIOChannelPrivate *)channel;
67
68   private->ref_count++;
69 }
70
71 void 
72 g_io_channel_unref (GIOChannel *channel)
73 {
74   GIOChannelPrivate *private;
75   g_return_if_fail (channel != NULL);
76
77   private = (GIOChannelPrivate *)channel;
78
79   private->ref_count--;
80   if (private->ref_count == 0)
81     {
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.
85        */
86       private->funcs->io_free (channel);
87       g_free (private);
88     }
89 }
90
91 GIOError 
92 g_io_channel_read (GIOChannel *channel, 
93                    gchar      *buf, 
94                    guint       count,
95                    guint      *bytes_read)
96 {
97   GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
98
99   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
100   g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
101
102   return private->funcs->io_read (channel, buf, count, bytes_read);
103 }
104
105 GIOError 
106 g_io_channel_write (GIOChannel *channel, 
107                     gchar      *buf, 
108                     guint       count,
109                     guint      *bytes_written)
110 {
111   GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
112
113   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
114   g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
115
116   return private->funcs->io_write (channel, buf, count, bytes_written);
117 }
118
119 GIOError 
120 g_io_channel_seek  (GIOChannel   *channel,
121                     gint        offset, 
122                     GSeekType   type)
123 {
124   GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
125
126   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
127   g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
128
129   return private->funcs->io_seek (channel, offset, type);
130 }
131      
132 void
133 g_io_channel_close (GIOChannel *channel)
134 {
135   GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
136
137   g_return_if_fail (channel != NULL);
138   g_return_if_fail (!private->closed);
139
140   private->closed = TRUE;
141   private->funcs->io_close (channel);
142 }
143
144 guint 
145 g_io_add_watch_full (GIOChannel      *channel,
146                      gint           priority,
147                      GIOCondition   condition,
148                      GIOFunc        func,
149                      gpointer       user_data,
150                      GDestroyNotify notify)
151 {
152   GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
153
154   g_return_val_if_fail (channel != NULL, 0);
155   g_return_val_if_fail (!private->closed, 0);
156
157   return private->funcs->io_add_watch (channel, priority, condition,
158                                        func, user_data, notify);
159 }
160
161 guint 
162 g_io_add_watch (GIOChannel      *channel,
163                 GIOCondition   condition,
164                 GIOFunc        func,
165                 gpointer       user_data)
166 {
167   return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);
168 }