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>
32 #include "gsimpleasyncresult.h"
33 #include "gwin32inputstream.h"
34 #include "gcancellable.h"
35 #include "gasynchelper.h"
41 * SECTION:gwin32inputstream
42 * @short_description: Streaming input operations for Windows file handles
43 * @include: gio/gwin32inputstream.h
44 * @see_also: #GInputStream
46 * #GWin32InputStream implements #GInputStream for reading from a
47 * Windows file handle.
49 * Note that <filename><gio/gwin32inputstream.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 (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM);
62 struct _GWin32InputStreamPrivate {
64 gboolean close_handle;
67 static void g_win32_input_stream_set_property (GObject *object,
71 static void g_win32_input_stream_get_property (GObject *object,
75 static gssize g_win32_input_stream_read (GInputStream *stream,
78 GCancellable *cancellable,
80 static gboolean g_win32_input_stream_close (GInputStream *stream,
81 GCancellable *cancellable,
85 g_win32_input_stream_finalize (GObject *object)
87 G_OBJECT_CLASS (g_win32_input_stream_parent_class)->finalize (object);
91 g_win32_input_stream_class_init (GWin32InputStreamClass *klass)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
96 g_type_class_add_private (klass, sizeof (GWin32InputStreamPrivate));
98 gobject_class->get_property = g_win32_input_stream_get_property;
99 gobject_class->set_property = g_win32_input_stream_set_property;
100 gobject_class->finalize = g_win32_input_stream_finalize;
102 stream_class->read_fn = g_win32_input_stream_read;
103 stream_class->close_fn = g_win32_input_stream_close;
106 * GWin32InputStream:handle:
108 * The handle that the stream reads from.
112 g_object_class_install_property (gobject_class,
114 g_param_spec_pointer ("handle",
116 P_("The file handle to read from"),
117 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
120 * GWin32InputStream:close-handle:
122 * Whether to close the file handle when the stream is closed.
126 g_object_class_install_property (gobject_class,
128 g_param_spec_boolean ("close-handle",
129 P_("Close file handle"),
130 P_("Whether to close the file handle when the stream is closed"),
132 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
136 g_win32_input_stream_set_property (GObject *object,
141 GWin32InputStream *win32_stream;
143 win32_stream = G_WIN32_INPUT_STREAM (object);
148 win32_stream->priv->handle = g_value_get_pointer (value);
150 case PROP_CLOSE_HANDLE:
151 win32_stream->priv->close_handle = g_value_get_boolean (value);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 g_win32_input_stream_get_property (GObject *object,
165 GWin32InputStream *win32_stream;
167 win32_stream = G_WIN32_INPUT_STREAM (object);
172 g_value_set_pointer (value, win32_stream->priv->handle);
174 case PROP_CLOSE_HANDLE:
175 g_value_set_boolean (value, win32_stream->priv->close_handle);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 g_win32_input_stream_init (GWin32InputStream *win32_stream)
185 win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
186 G_TYPE_WIN32_INPUT_STREAM,
187 GWin32InputStreamPrivate);
189 win32_stream->priv->handle = NULL;
190 win32_stream->priv->close_handle = TRUE;
194 * g_win32_input_stream_new:
195 * @handle: a Win32 file handle
196 * @close_fd: %TRUE to close the handle when done
198 * Creates a new #GWin32InputStream for the given @fd.
200 * If @close_handle is %TRUE, the handle will be closed
201 * when the stream is closed.
203 * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
204 * as used in the Windows C libraries.
206 * Returns: a new #GWin32InputStream
209 g_win32_input_stream_new (void *handle,
210 gboolean close_handle)
212 GWin32InputStream *stream;
214 g_return_val_if_fail (handle != NULL, NULL);
216 stream = g_object_new (G_TYPE_WIN32_INPUT_STREAM,
218 "close-handle", close_handle,
221 return G_INPUT_STREAM (stream);
225 * g_win32_input_stream_set_close_handle:
226 * @stream: a #GWin32InputStream
227 * @close_handle: %TRUE to close the handle when done
229 * Sets whether the handle of @stream shall be closed
230 * when the stream is closed.
235 g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
236 gboolean close_handle)
238 g_return_if_fail (G_IS_WIN32_INPUT_STREAM (stream));
240 close_handle = close_handle != FALSE;
241 if (stream->priv->close_handle != close_handle)
243 stream->priv->close_handle = close_handle;
244 g_object_notify (G_OBJECT (stream), "close-handle");
249 * g_win32_input_stream_get_close_handle:
250 * @stream: a #GWin32InputStream
252 * Returns whether the handle of @stream will be
253 * closed when the stream is closed.
255 * Return value: %TRUE if the handle is closed when done
260 g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
262 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), FALSE);
264 return stream->priv->close_handle;
268 * g_win32_input_stream_get_handle:
269 * @stream: a #GWin32InputStream
271 * Return the Windows file handle that the stream reads from.
273 * Return value: The file handle of @stream
278 g_win32_input_stream_get_handle (GWin32InputStream *stream)
280 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), NULL);
282 return stream->priv->handle;
286 g_win32_input_stream_read (GInputStream *stream,
289 GCancellable *cancellable,
292 GWin32InputStream *win32_stream;
296 win32_stream = G_WIN32_INPUT_STREAM (stream);
298 if (g_cancellable_set_error_if_cancelled (cancellable, error))
301 if (count > G_MAXINT)
306 res = ReadFile (win32_stream->priv->handle, buffer, nbytes, &nread, NULL);
309 int errsv = GetLastError ();
312 if (errsv == ERROR_HANDLE_EOF ||
313 errsv == ERROR_BROKEN_PIPE)
316 emsg = g_win32_error_message (errsv);
317 g_set_error (error, G_IO_ERROR,
318 g_io_error_from_win32_error (errsv),
319 _("Error reading from handle: %s"),
329 g_win32_input_stream_close (GInputStream *stream,
330 GCancellable *cancellable,
333 GWin32InputStream *win32_stream;
336 win32_stream = G_WIN32_INPUT_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_INPUT_STREAM_C__
359 #include "gioaliasdef.c"