1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2003-2004 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
22 #include <gsf-config.h>
23 #include <gsf-win32/gsf-output-win32.h>
24 #include <gsf/gsf-output-impl.h>
25 #include <gsf/gsf-impl-utils.h>
29 struct _GsfOutputIStream {
35 GsfOutputClass output_class;
36 } GsfOutputIStreamClass;
38 #define NEED_ISTREAM_MACROS
40 #ifdef NEED_ISTREAM_MACROS
41 #define IStream_AddRef(This) (This)->lpVtbl->AddRef(This)
42 #define IStream_Write(This,pv,cb,pcbRead) (This)->lpVtbl->Read(This,pv,cb,pcbRead)
43 #define IStream_Release(This) (This)->lpVtbl->Release(This)
44 #define IStream_Seek(This,dlibMove,dwOrigin,plibNewPosition) (This)->lpVtbl->Seek(This,dlibMove,dwOrigin,plibNewPosition)
47 /* declared in gsf-input-win32.c */
48 extern gchar * gsf_win32_hresult_to_utf8 (HRESULT hr);
51 gsf_output_istream_set_error (GsfOutput * output, HRESULT hr)
53 if (!SUCCEEDED (hr)) {
56 msg = gsf_win32_hresult_to_utf8 (hr);
58 gsf_output_set_error (output, 0, msg);
60 } /* "else" case should never happen */
69 * gsf_output_istream_new :
70 * @stream : IStream stream
72 * Returns: a new output object or %NULL.
75 gsf_output_istream_new (IStream * stream)
77 GsfOutputIStream *output;
79 g_return_val_if_fail (stream != NULL, NULL);
81 output = g_object_new (GSF_OUTPUT_ISTREAM_TYPE, NULL);
82 if (G_UNLIKELY (NULL == output)) return NULL;
84 output->stream = stream;
85 IStream_AddRef (output->stream);
87 return GSF_OUTPUT(output);
91 gsf_output_istream_close (GsfOutput *output)
93 GsfOutputIStream *istream = GSF_OUTPUT_ISTREAM (output);
96 if (istream->stream != NULL) {
97 IStream_Release (istream->stream);
98 istream->stream = NULL;
106 gsf_output_istream_finalize (GObject *obj)
108 GObjectClass *parent_class;
109 GsfOutputIStream *output = (GsfOutputIStream *)obj;
111 gsf_output_istream_close (GSF_OUTPUT(output));
113 parent_class = g_type_class_peek (GSF_OUTPUT_TYPE);
114 if (parent_class && parent_class->finalize)
115 parent_class->finalize (obj);
119 gsf_output_istream_write (GsfOutput *output,
121 guint8 const *buffer)
123 GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (output);
125 ULONG nwritten, total_written = 0;
127 g_return_val_if_fail (istm != NULL, FALSE);
128 g_return_val_if_fail (istm->stream != NULL, FALSE);
131 hr = IStream_Write (istm->stream, (guint8 *)(buffer + total_written), (ULONG)(num_bytes - total_written), &nwritten);
133 if (SUCCEEDED (hr)) {
134 total_written += nwritten;
135 if ((size_t)total_written == num_bytes)
138 return gsf_output_istream_set_error (output, hr);
146 gsf_output_istream_seek (GsfOutput *output, gsf_off_t offset, GSeekType whence)
148 GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (output);
149 DWORD dwhence = STREAM_SEEK_SET;
152 g_return_val_if_fail (istm != NULL, gsf_output_set_error (output, 0, "missing handle"));
153 g_return_val_if_fail (istm->stream != NULL, gsf_output_set_error (output, 0, "missing handle"));
157 dwhence = STREAM_SEEK_SET;
160 dwhence = STREAM_SEEK_CUR;
163 dwhence = STREAM_SEEK_END;
166 break; /* checked in parent wrapper */
169 hr = IStream_Seek (istm->stream, *(LARGE_INTEGER *) &offset, dwhence, NULL);
174 return gsf_output_istream_set_error (output, hr);
178 gsf_output_istream_init (GObject *obj)
180 GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (obj);
186 gsf_output_istream_class_init (GObjectClass *gobject_class)
188 GsfOutputClass *output_class = GSF_OUTPUT_CLASS (gobject_class);
190 gobject_class->finalize = gsf_output_istream_finalize;
191 output_class->Close = gsf_output_istream_close;
192 output_class->Write = gsf_output_istream_write;
193 output_class->Seek = gsf_output_istream_seek;
196 GSF_CLASS (GsfOutputIStream, gsf_output_istream,
197 gsf_output_istream_class_init, gsf_output_istream_init, GSF_OUTPUT_TYPE)
199 /***************************************************************************/
200 /***************************************************************************/