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"
42 * SECTION:gwin32outputstream
43 * @short_description: Streaming output operations for Windows file handles
44 * @include: gio/gwin32outputstream.h
45 * @see_also: #GOutputStream
47 * #GWin32OutputStream implements #GOutputStream for writing to a
48 * Windows file handle.
50 * Note that <filename><gio/gwin32outputstream.h></filename> belongs
51 * to the Windows-specific GIO interfaces, thus you have to use the
52 * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
61 G_DEFINE_TYPE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM);
64 struct _GWin32OutputStreamPrivate {
66 gboolean close_handle;
69 static void g_win32_output_stream_set_property (GObject *object,
73 static void g_win32_output_stream_get_property (GObject *object,
77 static gssize g_win32_output_stream_write (GOutputStream *stream,
80 GCancellable *cancellable,
82 static gboolean g_win32_output_stream_close (GOutputStream *stream,
83 GCancellable *cancellable,
88 g_win32_output_stream_finalize (GObject *object)
90 G_OBJECT_CLASS (g_win32_output_stream_parent_class)->finalize (object);
94 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
96 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
99 g_type_class_add_private (klass, sizeof (GWin32OutputStreamPrivate));
101 gobject_class->get_property = g_win32_output_stream_get_property;
102 gobject_class->set_property = g_win32_output_stream_set_property;
103 gobject_class->finalize = g_win32_output_stream_finalize;
105 stream_class->write_fn = g_win32_output_stream_write;
106 stream_class->close_fn = g_win32_output_stream_close;
109 * GWin32OutputStream:handle:
111 * The file handle that the stream writes to.
115 g_object_class_install_property (gobject_class,
117 g_param_spec_pointer ("handle",
119 P_("The file handle to write to"),
120 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
123 * GWin32OutputStream:close-handle:
125 * Whether to close the file handle when the stream is closed.
129 g_object_class_install_property (gobject_class,
131 g_param_spec_boolean ("close-handle",
132 P_("Close file handle"),
133 P_("Whether to close the file handle when the stream is closed"),
135 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
139 g_win32_output_stream_set_property (GObject *object,
144 GWin32OutputStream *win32_stream;
146 win32_stream = G_WIN32_OUTPUT_STREAM (object);
151 win32_stream->priv->handle = g_value_get_pointer (value);
153 case PROP_CLOSE_HANDLE:
154 win32_stream->priv->close_handle = g_value_get_boolean (value);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163 g_win32_output_stream_get_property (GObject *object,
168 GWin32OutputStream *win32_stream;
170 win32_stream = G_WIN32_OUTPUT_STREAM (object);
175 g_value_set_pointer (value, win32_stream->priv->handle);
177 case PROP_CLOSE_HANDLE:
178 g_value_set_boolean (value, win32_stream->priv->close_handle);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
188 win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
189 G_TYPE_WIN32_OUTPUT_STREAM,
190 GWin32OutputStreamPrivate);
192 win32_stream->priv->handle = NULL;
193 win32_stream->priv->close_handle = TRUE;
197 * g_win32_output_stream_new:
198 * @handle: a Win32 file handle
199 * @close_handle: %TRUE to close the handle when done
201 * Creates a new #GWin32OutputStream for the given @handle.
203 * If @close_handle, is %TRUE, the handle will be closed when the
204 * output stream is destroyed.
206 * Returns: a new #GOutputStream
211 g_win32_output_stream_new (void *handle,
212 gboolean close_handle)
214 GWin32OutputStream *stream;
216 g_return_val_if_fail (handle != NULL, NULL);
218 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
220 "close-handle", close_handle,
223 return G_OUTPUT_STREAM (stream);
227 * g_win32_output_stream_set_close_handle:
228 * @stream: a #GWin32OutputStream
229 * @close_handle: %TRUE to close the handle when done
231 * Sets whether the handle of @stream shall be closed when the stream
237 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
238 gboolean close_handle)
240 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
242 close_handle = close_handle != FALSE;
243 if (stream->priv->close_handle != close_handle)
245 stream->priv->close_handle = close_handle;
246 g_object_notify (G_OBJECT (stream), "close-handle");
251 * g_win32_output_stream_get_close_handle:
252 * @stream: a #GWin32OutputStream
254 * Returns whether the handle of @stream will be closed when the
257 * Return value: %TRUE if the handle is closed when done
262 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
264 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
266 return stream->priv->close_handle;
270 * g_win32_output_stream_get_handle:
271 * @stream: a #GWin32OutputStream
273 * Return the Windows handle that the stream writes to.
275 * Return value: The handle descriptor of @stream
280 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
282 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
284 return stream->priv->handle;
288 g_win32_output_stream_write (GOutputStream *stream,
291 GCancellable *cancellable,
294 GWin32OutputStream *win32_stream;
296 DWORD nbytes, nwritten;
298 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
303 if (count > G_MAXINT)
308 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, NULL);
311 int errsv = GetLastError ();
312 gchar *emsg = g_win32_error_message (errsv);
314 if (errsv == ERROR_HANDLE_EOF)
317 g_set_error (error, G_IO_ERROR,
318 g_io_error_from_win32_error (errsv),
319 _("Error writing to handle: %s"),
329 g_win32_output_stream_close (GOutputStream *stream,
330 GCancellable *cancellable,
333 GWin32OutputStream *win32_stream;
336 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
338 if (!win32_stream->priv->close_handle)
341 res = CloseHandle (win32_stream->priv->handle);
344 int errsv = GetLastError ();
345 gchar *emsg = g_win32_error_message (errsv);
347 g_set_error (error, G_IO_ERROR,
348 g_io_error_from_win32_error (errsv),
349 _("Error closing handle: %s"),
358 #define __G_WIN32_OUTPUT_STREAM_C__
359 #include "gioaliasdef.c"