Add Friulian translation
[glib.git] / gio / gwin32outputstream.c
blob083746c1584c5dd2b2712fa3846153a08a9d5bfa
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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
19 * Author: Tor Lillqvist <tml@iki.fi>
22 #include "config.h"
24 #include <windows.h>
26 #include <io.h>
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include "gioerror.h"
31 #include "gwin32outputstream.h"
32 #include "giowin32-priv.h"
33 #include "gcancellable.h"
34 #include "gasynchelper.h"
35 #include "glibintl.h"
37 /**
38 * SECTION:gwin32outputstream
39 * @short_description: Streaming output operations for Windows file handles
40 * @include: gio/gwin32outputstream.h
41 * @see_also: #GOutputStream
43 * #GWin32OutputStream implements #GOutputStream for writing to a
44 * Windows file handle.
46 * Note that `<gio/gwin32outputstream.h>` belongs to the Windows-specific GIO
47 * interfaces, thus you have to use the `gio-windows-2.0.pc` pkg-config file
48 * when using it.
51 struct _GWin32OutputStreamPrivate {
52 HANDLE handle;
53 gboolean close_handle;
54 gint fd;
57 enum {
58 PROP_0,
59 PROP_HANDLE,
60 PROP_CLOSE_HANDLE,
61 LAST_PROP
64 static GParamSpec *props[LAST_PROP];
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
68 static void
69 g_win32_output_stream_set_property (GObject *object,
70 guint prop_id,
71 const GValue *value,
72 GParamSpec *pspec)
74 GWin32OutputStream *win32_stream;
76 win32_stream = G_WIN32_OUTPUT_STREAM (object);
78 switch (prop_id)
80 case PROP_HANDLE:
81 win32_stream->priv->handle = g_value_get_pointer (value);
82 break;
83 case PROP_CLOSE_HANDLE:
84 win32_stream->priv->close_handle = g_value_get_boolean (value);
85 break;
86 default:
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 break;
92 static void
93 g_win32_output_stream_get_property (GObject *object,
94 guint prop_id,
95 GValue *value,
96 GParamSpec *pspec)
98 GWin32OutputStream *win32_stream;
100 win32_stream = G_WIN32_OUTPUT_STREAM (object);
102 switch (prop_id)
104 case PROP_HANDLE:
105 g_value_set_pointer (value, win32_stream->priv->handle);
106 break;
107 case PROP_CLOSE_HANDLE:
108 g_value_set_boolean (value, win32_stream->priv->close_handle);
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115 static gssize
116 g_win32_output_stream_write (GOutputStream *stream,
117 const void *buffer,
118 gsize count,
119 GCancellable *cancellable,
120 GError **error)
122 GWin32OutputStream *win32_stream;
123 BOOL res;
124 DWORD nbytes, nwritten;
125 OVERLAPPED overlap = { 0, };
126 gssize retval = -1;
128 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
130 if (g_cancellable_set_error_if_cancelled (cancellable, error))
131 return -1;
133 if (count > G_MAXINT)
134 nbytes = G_MAXINT;
135 else
136 nbytes = count;
138 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
139 g_return_val_if_fail (overlap.hEvent != NULL, -1);
141 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
142 if (res)
143 retval = nwritten;
144 else
146 int errsv = GetLastError ();
148 if (errsv == ERROR_IO_PENDING &&
149 _g_win32_overlap_wait_result (win32_stream->priv->handle,
150 &overlap, &nwritten, cancellable))
152 retval = nwritten;
153 goto end;
156 if (g_cancellable_set_error_if_cancelled (cancellable, error))
157 goto end;
159 errsv = GetLastError ();
160 if (errsv == ERROR_HANDLE_EOF ||
161 errsv == ERROR_BROKEN_PIPE)
163 retval = 0;
165 else
167 gchar *emsg;
169 emsg = g_win32_error_message (errsv);
170 g_set_error (error, G_IO_ERROR,
171 g_io_error_from_win32_error (errsv),
172 _("Error writing to handle: %s"),
173 emsg);
174 g_free (emsg);
178 end:
179 CloseHandle (overlap.hEvent);
180 return retval;
183 static gboolean
184 g_win32_output_stream_close (GOutputStream *stream,
185 GCancellable *cancellable,
186 GError **error)
188 GWin32OutputStream *win32_stream;
189 BOOL res;
191 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
193 if (!win32_stream->priv->close_handle)
194 return TRUE;
196 if (win32_stream->priv->fd != -1)
198 if (close (win32_stream->priv->fd) < 0)
200 int errsv = errno;
202 g_set_error (error, G_IO_ERROR,
203 g_io_error_from_errno (errsv),
204 _("Error closing file descriptor: %s"),
205 g_strerror (errsv));
206 return FALSE;
209 else
211 res = CloseHandle (win32_stream->priv->handle);
212 if (!res)
214 int errsv = GetLastError ();
215 gchar *emsg = g_win32_error_message (errsv);
217 g_set_error (error, G_IO_ERROR,
218 g_io_error_from_win32_error (errsv),
219 _("Error closing handle: %s"),
220 emsg);
221 g_free (emsg);
222 return FALSE;
226 return TRUE;
229 static void
230 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
232 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
233 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
235 gobject_class->get_property = g_win32_output_stream_get_property;
236 gobject_class->set_property = g_win32_output_stream_set_property;
238 stream_class->write_fn = g_win32_output_stream_write;
239 stream_class->close_fn = g_win32_output_stream_close;
242 * GWin32OutputStream:handle:
244 * The file handle that the stream writes to.
246 * Since: 2.26
248 props[PROP_HANDLE] =
249 g_param_spec_pointer ("handle",
250 P_("File handle"),
251 P_("The file handle to write to"),
252 G_PARAM_READABLE |
253 G_PARAM_WRITABLE |
254 G_PARAM_CONSTRUCT_ONLY |
255 G_PARAM_STATIC_STRINGS);
258 * GWin32OutputStream:close-handle:
260 * Whether to close the file handle when the stream is closed.
262 * Since: 2.26
264 props[PROP_CLOSE_HANDLE] =
265 g_param_spec_boolean ("close-handle",
266 P_("Close file handle"),
267 P_("Whether to close the file handle when the stream is closed"),
268 TRUE,
269 G_PARAM_READABLE |
270 G_PARAM_WRITABLE |
271 G_PARAM_STATIC_STRINGS);
273 g_object_class_install_properties (gobject_class, LAST_PROP, props);
276 static void
277 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
279 win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
280 win32_stream->priv->handle = NULL;
281 win32_stream->priv->close_handle = TRUE;
282 win32_stream->priv->fd = -1;
286 * g_win32_output_stream_new:
287 * @handle: a Win32 file handle
288 * @close_handle: %TRUE to close the handle when done
290 * Creates a new #GWin32OutputStream for the given @handle.
292 * If @close_handle, is %TRUE, the handle will be closed when the
293 * output stream is destroyed.
295 * Returns: a new #GOutputStream
297 * Since: 2.26
299 GOutputStream *
300 g_win32_output_stream_new (void *handle,
301 gboolean close_handle)
303 GWin32OutputStream *stream;
305 g_return_val_if_fail (handle != NULL, NULL);
307 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
308 "handle", handle,
309 "close-handle", close_handle,
310 NULL);
312 return G_OUTPUT_STREAM (stream);
316 * g_win32_output_stream_set_close_handle:
317 * @stream: a #GWin32OutputStream
318 * @close_handle: %TRUE to close the handle when done
320 * Sets whether the handle of @stream shall be closed when the stream
321 * is closed.
323 * Since: 2.26
325 void
326 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
327 gboolean close_handle)
329 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
331 close_handle = close_handle != FALSE;
332 if (stream->priv->close_handle != close_handle)
334 stream->priv->close_handle = close_handle;
335 g_object_notify (G_OBJECT (stream), "close-handle");
340 * g_win32_output_stream_get_close_handle:
341 * @stream: a #GWin32OutputStream
343 * Returns whether the handle of @stream will be closed when the
344 * stream is closed.
346 * Returns: %TRUE if the handle is closed when done
348 * Since: 2.26
350 gboolean
351 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
353 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
355 return stream->priv->close_handle;
359 * g_win32_output_stream_get_handle:
360 * @stream: a #GWin32OutputStream
362 * Return the Windows handle that the stream writes to.
364 * Returns: The handle descriptor of @stream
366 * Since: 2.26
368 void *
369 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
371 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
373 return stream->priv->handle;
376 GOutputStream *
377 g_win32_output_stream_new_from_fd (gint fd,
378 gboolean close_fd)
380 GWin32OutputStream *win32_stream;
382 win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
383 win32_stream->priv->fd = fd;
385 return (GOutputStream*)win32_stream;