1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * Author: Tor Lillqvist <tml@iki.fi>
31 #include <glib/gstdio.h>
33 #include "gwin32outputstream.h"
34 #include "gcancellable.h"
35 #include "gsimpleasyncresult.h"
36 #include "gasynchelper.h"
41 * SECTION:gwin32outputstream
42 * @short_description: Streaming output operations for Windows file handles
43 * @include: gio/gwin32outputstream.h
44 * @see_also: #GOutputStream
46 * #GWin32OutputStream implements #GOutputStream for writing to a
47 * Windows file handle.
49 * Note that <filename><gio/gwin32outputstream.h></filename> belongs
50 * to the Windows-specific GIO interfaces, thus you have to use the
51 * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
60 G_DEFINE_TYPE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM);
63 struct _GWin32OutputStreamPrivate {
65 gboolean close_handle;
68 static void g_win32_output_stream_set_property (GObject *object,
72 static void g_win32_output_stream_get_property (GObject *object,
76 static gssize g_win32_output_stream_write (GOutputStream *stream,
79 GCancellable *cancellable,
81 static gboolean g_win32_output_stream_close (GOutputStream *stream,
82 GCancellable *cancellable,
87 g_win32_output_stream_finalize (GObject *object)
89 G_OBJECT_CLASS (g_win32_output_stream_parent_class)->finalize (object);
93 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
98 g_type_class_add_private (klass, sizeof (GWin32OutputStreamPrivate));
100 gobject_class->get_property = g_win32_output_stream_get_property;
101 gobject_class->set_property = g_win32_output_stream_set_property;
102 gobject_class->finalize = g_win32_output_stream_finalize;
104 stream_class->write_fn = g_win32_output_stream_write;
105 stream_class->close_fn = g_win32_output_stream_close;
108 * GWin32OutputStream:handle:
110 * The file handle that the stream writes to.
114 g_object_class_install_property (gobject_class,
116 g_param_spec_pointer ("handle",
118 P_("The file handle to write to"),
119 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
122 * GWin32OutputStream:close-handle:
124 * Whether to close the file handle when the stream is closed.
128 g_object_class_install_property (gobject_class,
130 g_param_spec_boolean ("close-handle",
131 P_("Close file handle"),
132 P_("Whether to close the file handle when the stream is closed"),
134 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
138 g_win32_output_stream_set_property (GObject *object,
143 GWin32OutputStream *win32_stream;
145 win32_stream = G_WIN32_OUTPUT_STREAM (object);
150 win32_stream->priv->handle = g_value_get_pointer (value);
152 case PROP_CLOSE_HANDLE:
153 win32_stream->priv->close_handle = g_value_get_boolean (value);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 g_win32_output_stream_get_property (GObject *object,
167 GWin32OutputStream *win32_stream;
169 win32_stream = G_WIN32_OUTPUT_STREAM (object);
174 g_value_set_pointer (value, win32_stream->priv->handle);
176 case PROP_CLOSE_HANDLE:
177 g_value_set_boolean (value, win32_stream->priv->close_handle);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
187 win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
188 G_TYPE_WIN32_OUTPUT_STREAM,
189 GWin32OutputStreamPrivate);
191 win32_stream->priv->handle = NULL;
192 win32_stream->priv->close_handle = TRUE;
196 * g_win32_output_stream_new:
197 * @handle: a Win32 file handle
198 * @close_handle: %TRUE to close the handle when done
200 * Creates a new #GWin32OutputStream for the given @handle.
202 * If @close_handle, is %TRUE, the handle will be closed when the
203 * output stream is destroyed.
205 * Returns: a new #GOutputStream
210 g_win32_output_stream_new (void *handle,
211 gboolean close_handle)
213 GWin32OutputStream *stream;
215 g_return_val_if_fail (handle != NULL, NULL);
217 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
219 "close-handle", close_handle,
222 return G_OUTPUT_STREAM (stream);
226 * g_win32_output_stream_set_close_handle:
227 * @stream: a #GWin32OutputStream
228 * @close_handle: %TRUE to close the handle when done
230 * Sets whether the handle of @stream shall be closed when the stream
236 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
237 gboolean close_handle)
239 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
241 close_handle = close_handle != FALSE;
242 if (stream->priv->close_handle != close_handle)
244 stream->priv->close_handle = close_handle;
245 g_object_notify (G_OBJECT (stream), "close-handle");
250 * g_win32_output_stream_get_close_handle:
251 * @stream: a #GWin32OutputStream
253 * Returns whether the handle of @stream will be closed when the
256 * Return value: %TRUE if the handle is closed when done
261 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
263 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
265 return stream->priv->close_handle;
269 * g_win32_output_stream_get_handle:
270 * @stream: a #GWin32OutputStream
272 * Return the Windows handle that the stream writes to.
274 * Return value: The handle descriptor of @stream
279 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
281 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
283 return stream->priv->handle;
287 g_win32_output_stream_write (GOutputStream *stream,
290 GCancellable *cancellable,
293 GWin32OutputStream *win32_stream;
295 DWORD nbytes, nwritten;
297 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
299 if (g_cancellable_set_error_if_cancelled (cancellable, error))
302 if (count > G_MAXINT)
307 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, NULL);
310 int errsv = GetLastError ();
311 gchar *emsg = g_win32_error_message (errsv);
313 if (errsv == ERROR_HANDLE_EOF)
316 g_set_error (error, G_IO_ERROR,
317 g_io_error_from_win32_error (errsv),
318 _("Error writing to handle: %s"),
328 g_win32_output_stream_close (GOutputStream *stream,
329 GCancellable *cancellable,
332 GWin32OutputStream *win32_stream;
335 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
337 if (!win32_stream->priv->close_handle)
340 res = CloseHandle (win32_stream->priv->handle);
343 int errsv = GetLastError ();
344 gchar *emsg = g_win32_error_message (errsv);
346 g_set_error (error, G_IO_ERROR,
347 g_io_error_from_win32_error (errsv),
348 _("Error closing handle: %s"),