"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf-win32 / gsf-output-win32.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-output-win32.c:
4  *
5  * Copyright (C) 2003-2004 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
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>
26 #include <string.h>
27 #include <objidl.h>
28
29 struct _GsfOutputIStream {
30         GsfOutput output;
31         IStream * stream;
32 };
33
34 typedef struct {
35         GsfOutputClass output_class;
36 } GsfOutputIStreamClass;
37
38 #define NEED_ISTREAM_MACROS
39
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)
45 #endif
46
47 /* declared in gsf-input-win32.c */
48 extern gchar * gsf_win32_hresult_to_utf8 (HRESULT hr);
49
50 static gboolean
51 gsf_output_istream_set_error (GsfOutput * output, HRESULT hr)
52 {
53         if (!SUCCEEDED (hr)) {
54                 gchar * msg;
55
56                 msg = gsf_win32_hresult_to_utf8 (hr);
57                 if (msg) {
58                         gsf_output_set_error (output, 0, msg);
59                         g_free (msg);
60                 } /* "else" case should never happen */
61
62                 return FALSE;
63         }
64
65         return TRUE;
66 }
67
68 /**
69  * gsf_output_istream_new :
70  * @stream   : IStream stream
71  *
72  * Returns: a new output object or %NULL.
73  **/
74 GsfOutput *
75 gsf_output_istream_new (IStream * stream)
76 {
77         GsfOutputIStream *output;
78
79         g_return_val_if_fail (stream != NULL, NULL);
80
81         output = g_object_new (GSF_OUTPUT_ISTREAM_TYPE, NULL);
82         if (G_UNLIKELY (NULL == output)) return NULL;
83
84         output->stream = stream;
85         IStream_AddRef (output->stream);
86
87         return GSF_OUTPUT(output);
88 }
89
90 static gboolean
91 gsf_output_istream_close (GsfOutput *output)
92 {
93         GsfOutputIStream *istream = GSF_OUTPUT_ISTREAM (output);
94         gboolean res = FALSE;
95
96         if (istream->stream != NULL) {
97                 IStream_Release (istream->stream);
98                 istream->stream = NULL;
99                 res = TRUE;
100         }
101
102         return res;
103 }
104
105 static void
106 gsf_output_istream_finalize (GObject *obj)
107 {
108         GObjectClass *parent_class;
109         GsfOutputIStream *output = (GsfOutputIStream *)obj;
110
111         gsf_output_istream_close (GSF_OUTPUT(output));
112
113         parent_class = g_type_class_peek (GSF_OUTPUT_TYPE);
114         if (parent_class && parent_class->finalize)
115                 parent_class->finalize (obj);
116 }
117
118 static gboolean
119 gsf_output_istream_write (GsfOutput *output,
120                           size_t num_bytes,
121                           guint8 const *buffer)
122 {
123         GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (output);
124         HRESULT hr;
125         ULONG nwritten, total_written = 0;
126
127         g_return_val_if_fail (istm != NULL, FALSE);
128         g_return_val_if_fail (istm->stream != NULL, FALSE);
129
130         while (1) {
131                 hr = IStream_Write (istm->stream, (guint8 *)(buffer + total_written), (ULONG)(num_bytes - total_written), &nwritten);
132
133                 if (SUCCEEDED (hr)) {
134                         total_written += nwritten;
135                         if ((size_t)total_written == num_bytes)
136                                 return TRUE;
137                 } else {
138                         return gsf_output_istream_set_error (output, hr);
139                 }
140         }
141
142         return TRUE;
143 }
144
145 static gboolean
146 gsf_output_istream_seek (GsfOutput *output, gsf_off_t offset, GSeekType whence)
147 {
148         GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (output);
149         DWORD dwhence = STREAM_SEEK_SET;
150         HRESULT hr;
151
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"));
154
155         switch (whence) {
156         case G_SEEK_SET :
157                 dwhence = STREAM_SEEK_SET;
158                 break;
159         case G_SEEK_CUR :
160                 dwhence = STREAM_SEEK_CUR;
161                 break;
162         case G_SEEK_END :
163                 dwhence = STREAM_SEEK_END;
164                 break;
165         default:
166                 break; /* checked in parent wrapper */
167         }
168
169         hr = IStream_Seek (istm->stream, *(LARGE_INTEGER *) &offset, dwhence, NULL);
170
171         if (SUCCEEDED (hr))
172                 return TRUE;
173
174         return gsf_output_istream_set_error (output, hr);
175 }
176
177 static void
178 gsf_output_istream_init (GObject *obj)
179 {
180         GsfOutputIStream *istm = GSF_OUTPUT_ISTREAM (obj);
181
182         istm->stream = NULL;
183 }
184
185 static void
186 gsf_output_istream_class_init (GObjectClass *gobject_class)
187 {
188         GsfOutputClass *output_class = GSF_OUTPUT_CLASS (gobject_class);
189
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;
194 }
195
196 GSF_CLASS (GsfOutputIStream, gsf_output_istream,
197            gsf_output_istream_class_init, gsf_output_istream_init, GSF_OUTPUT_TYPE)
198
199 /***************************************************************************/
200 /***************************************************************************/