Moved the check for MT save function variants after the determination of
[platform/upstream/glib.git] / 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 "config.h"
28
29 #include "glib.h"
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 void
35 g_io_channel_init (GIOChannel *channel)
36 {
37   channel->channel_flags = 0;
38   channel->ref_count = 1;
39 }
40
41
42 void 
43 g_io_channel_ref (GIOChannel *channel)
44 {
45   g_return_if_fail (channel != NULL);
46
47   channel->ref_count++;
48 }
49
50 void 
51 g_io_channel_unref (GIOChannel *channel)
52 {
53   g_return_if_fail (channel != NULL);
54
55   channel->ref_count--;
56   if (channel->ref_count == 0)
57     channel->funcs->io_free (channel);
58 }
59
60 GIOError 
61 g_io_channel_read (GIOChannel *channel, 
62                    gchar      *buf, 
63                    guint       count,
64                    guint      *bytes_read)
65 {
66   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
67
68   return channel->funcs->io_read (channel, buf, count, bytes_read);
69 }
70
71 GIOError 
72 g_io_channel_write (GIOChannel *channel, 
73                     gchar      *buf, 
74                     guint       count,
75                     guint      *bytes_written)
76 {
77   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
78
79   return channel->funcs->io_write (channel, buf, count, bytes_written);
80 }
81
82 GIOError 
83 g_io_channel_seek  (GIOChannel   *channel,
84                     gint        offset, 
85                     GSeekType   type)
86 {
87   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
88
89   return channel->funcs->io_seek (channel, offset, type);
90 }
91      
92 void
93 g_io_channel_close (GIOChannel *channel)
94 {
95   g_return_if_fail (channel != NULL);
96
97   channel->funcs->io_close (channel);
98 }
99
100 guint 
101 g_io_add_watch_full (GIOChannel    *channel,
102                      gint           priority,
103                      GIOCondition   condition,
104                      GIOFunc        func,
105                      gpointer       user_data,
106                      GDestroyNotify notify)
107 {
108   g_return_val_if_fail (channel != NULL, 0);
109
110   return channel->funcs->io_add_watch (channel, priority, condition,
111                                        func, user_data, notify);
112 }
113
114 guint 
115 g_io_add_watch (GIOChannel    *channel,
116                 GIOCondition   condition,
117                 GIOFunc        func,
118                 gpointer       user_data)
119 {
120   return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);
121 }