"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf / gsf-output-iochannel.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-output-iochannel.c
4  *
5  * Copyright (C) 2002-2006 Dom Lachowicz (cinamod@hotmail.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 #include <gsf-config.h>
22 #include <gsf/gsf-output-iochannel.h>
23 #include <gsf/gsf-output-impl.h>
24 #include <gsf/gsf-impl-utils.h>
25
26 static GsfOutputClass *parent_class;
27
28 struct _GsfOutputIOChannel {
29         GsfOutput output;
30         GIOChannel * channel;
31 };
32
33 typedef struct {
34         GsfOutputClass output_class;
35 } GsfOutputIOChannelClass;
36
37 /**
38  * gsf_output_iochannel_new :
39  * @channel: A #GIOChannel
40  *
41  * Returns: a new file or NULL.
42  **/
43 GsfOutput *
44 gsf_output_iochannel_new (GIOChannel *channel)
45 {
46         GsfOutputIOChannel *output = NULL;
47
48         g_return_val_if_fail (channel != NULL, NULL);
49
50         output = g_object_new (GSF_OUTPUT_IOCHANNEL_TYPE, NULL);        
51         if (G_UNLIKELY (NULL == output)) return NULL;
52
53         output->channel = channel;
54         return GSF_OUTPUT (output);
55 }
56
57 static gboolean
58 gsf_output_iochannel_close (GsfOutput *output)
59 {
60         g_io_channel_shutdown (GSF_OUTPUT_IOCHANNEL (output)->channel, TRUE, NULL);
61
62         if (parent_class->Close)
63                 parent_class->Close (output);
64         
65         return TRUE;
66 }
67
68 static void
69 gsf_output_iochannel_finalize (GObject *obj)
70 {
71         g_io_channel_unref (GSF_OUTPUT_IOCHANNEL (obj)->channel);
72         G_OBJECT_CLASS (parent_class)->finalize (obj);
73 }
74
75 static gboolean
76 gsf_output_iochannel_seek (GsfOutput *output, gsf_off_t offset,
77                            GSeekType whence)
78 {
79         GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (output);
80         GIOStatus status = G_IO_STATUS_NORMAL;
81
82         status = g_io_channel_seek_position (io->channel, offset, whence, NULL);
83         if (status == G_IO_STATUS_NORMAL)
84                 return TRUE;
85
86         gsf_output_set_error (output, status, " ");
87         return FALSE;
88 }
89
90 static gboolean
91 gsf_output_iochannel_write (GsfOutput *output,
92                             size_t num_bytes,
93                             guint8 const *buffer)
94 {
95         GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (output);
96         GIOStatus status = G_IO_STATUS_NORMAL;
97         size_t bytes_written = 0, total_written = 0;
98
99         g_return_val_if_fail (io != NULL, FALSE);
100
101         while ((status == G_IO_STATUS_NORMAL) && (total_written < num_bytes)) {
102                 status = g_io_channel_write_chars (io->channel, (const gchar *)(buffer + total_written),
103                                                    num_bytes - total_written, &bytes_written, NULL);
104                 total_written += bytes_written;
105         }
106
107         return (status == G_IO_STATUS_NORMAL && total_written == num_bytes);
108 }
109
110 static void
111 gsf_output_iochannel_init (GObject *obj)
112 {
113         GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (obj);
114
115         io->channel   = NULL;
116 }
117
118 static void
119 gsf_output_iochannel_class_init (GObjectClass *gobject_class)
120 {
121         GsfOutputClass *output_class = GSF_OUTPUT_CLASS (gobject_class);
122         
123         gobject_class->finalize = gsf_output_iochannel_finalize;
124         output_class->Close     = gsf_output_iochannel_close;
125         output_class->Seek      = gsf_output_iochannel_seek;
126         output_class->Write     = gsf_output_iochannel_write;
127
128         parent_class = GSF_OUTPUT_CLASS (g_type_class_peek_parent (gobject_class));
129 }
130
131 GSF_CLASS (GsfOutputIOChannel, gsf_output_iochannel,
132            gsf_output_iochannel_class_init, gsf_output_iochannel_init,
133            GSF_OUTPUT_TYPE)