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