1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * gsf-output-iochannel.c
5 * Copyright (C) 2002-2006 Dom Lachowicz (cinamod@hotmail.com)
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.
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.
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
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>
26 static GsfOutputClass *parent_class;
28 struct _GsfOutputIOChannel {
34 GsfOutputClass output_class;
35 } GsfOutputIOChannelClass;
38 * gsf_output_iochannel_new :
39 * @channel: A #GIOChannel
41 * Returns: a new file or NULL.
44 gsf_output_iochannel_new (GIOChannel *channel)
46 GsfOutputIOChannel *output = NULL;
48 g_return_val_if_fail (channel != NULL, NULL);
50 output = g_object_new (GSF_OUTPUT_IOCHANNEL_TYPE, NULL);
51 if (G_UNLIKELY (NULL == output)) return NULL;
53 output->channel = channel;
54 return GSF_OUTPUT (output);
58 gsf_output_iochannel_close (GsfOutput *output)
60 g_io_channel_shutdown (GSF_OUTPUT_IOCHANNEL (output)->channel, TRUE, NULL);
62 if (parent_class->Close)
63 parent_class->Close (output);
69 gsf_output_iochannel_finalize (GObject *obj)
71 g_io_channel_unref (GSF_OUTPUT_IOCHANNEL (obj)->channel);
72 G_OBJECT_CLASS (parent_class)->finalize (obj);
76 gsf_output_iochannel_seek (GsfOutput *output, gsf_off_t offset,
79 GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (output);
80 GIOStatus status = G_IO_STATUS_NORMAL;
82 status = g_io_channel_seek_position (io->channel, offset, whence, NULL);
83 if (status == G_IO_STATUS_NORMAL)
86 gsf_output_set_error (output, status, " ");
91 gsf_output_iochannel_write (GsfOutput *output,
95 GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (output);
96 GIOStatus status = G_IO_STATUS_NORMAL;
97 size_t bytes_written = 0, total_written = 0;
99 g_return_val_if_fail (io != NULL, FALSE);
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;
107 return (status == G_IO_STATUS_NORMAL && total_written == num_bytes);
111 gsf_output_iochannel_init (GObject *obj)
113 GsfOutputIOChannel *io = GSF_OUTPUT_IOCHANNEL (obj);
119 gsf_output_iochannel_class_init (GObjectClass *gobject_class)
121 GsfOutputClass *output_class = GSF_OUTPUT_CLASS (gobject_class);
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;
128 parent_class = GSF_OUTPUT_CLASS (g_type_class_peek_parent (gobject_class));
131 GSF_CLASS (GsfOutputIOChannel, gsf_output_iochannel,
132 gsf_output_iochannel_class_init, gsf_output_iochannel_init,