Add g_str_is_ascii()
[platform/upstream/glib.git] / gio / gwin32outputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  * Author: Tor Lillqvist <tml@iki.fi>
22  */
23
24 #include "config.h"
25
26 #include <windows.h>
27
28 #include <io.h>
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32 #include "gioerror.h"
33 #include "gwin32outputstream.h"
34 #include "gcancellable.h"
35 #include "gsimpleasyncresult.h"
36 #include "gasynchelper.h"
37 #include "glibintl.h"
38
39
40 /**
41  * SECTION:gwin32outputstream
42  * @short_description: Streaming output operations for Windows file handles
43  * @include: gio/gwin32outputstream.h
44  * @see_also: #GOutputStream
45  *
46  * #GWin32OutputStream implements #GOutputStream for writing to a
47  * Windows file handle.
48  *
49  * Note that <filename>&lt;gio/gwin32outputstream.h&gt;</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.
52  */
53
54 enum {
55   PROP_0,
56   PROP_HANDLE,
57   PROP_CLOSE_HANDLE
58 };
59
60 struct _GWin32OutputStreamPrivate {
61   HANDLE handle;
62   gboolean close_handle;
63 };
64
65 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
66
67 static void     g_win32_output_stream_set_property (GObject              *object,
68                                                     guint                 prop_id,
69                                                     const GValue         *value,
70                                                     GParamSpec           *pspec);
71 static void     g_win32_output_stream_get_property (GObject              *object,
72                                                     guint                 prop_id,
73                                                     GValue               *value,
74                                                     GParamSpec           *pspec);
75 static gssize   g_win32_output_stream_write        (GOutputStream        *stream,
76                                                     const void           *buffer,
77                                                     gsize                 count,
78                                                     GCancellable         *cancellable,
79                                                     GError              **error);
80 static gboolean g_win32_output_stream_close        (GOutputStream        *stream,
81                                                     GCancellable         *cancellable,
82                                                     GError              **error);
83
84
85 static void
86 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
90
91   gobject_class->get_property = g_win32_output_stream_get_property;
92   gobject_class->set_property = g_win32_output_stream_set_property;
93
94   stream_class->write_fn = g_win32_output_stream_write;
95   stream_class->close_fn = g_win32_output_stream_close;
96
97    /**
98    * GWin32OutputStream:handle:
99    *
100    * The file handle that the stream writes to.
101    *
102    * Since: 2.26
103    */
104   g_object_class_install_property (gobject_class,
105                                    PROP_HANDLE,
106                                    g_param_spec_pointer ("handle",
107                                                          P_("File handle"),
108                                                          P_("The file handle to write to"),
109                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
110
111   /**
112    * GWin32OutputStream:close-handle:
113    *
114    * Whether to close the file handle when the stream is closed.
115    *
116    * Since: 2.26
117    */
118   g_object_class_install_property (gobject_class,
119                                    PROP_CLOSE_HANDLE,
120                                    g_param_spec_boolean ("close-handle",
121                                                          P_("Close file handle"),
122                                                          P_("Whether to close the file handle when the stream is closed"),
123                                                          TRUE,
124                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
125 }
126
127 static void
128 g_win32_output_stream_set_property (GObject         *object,
129                                     guint            prop_id,
130                                     const GValue    *value,
131                                     GParamSpec      *pspec)
132 {
133   GWin32OutputStream *win32_stream;
134
135   win32_stream = G_WIN32_OUTPUT_STREAM (object);
136
137   switch (prop_id)
138     {
139     case PROP_HANDLE:
140       win32_stream->priv->handle = g_value_get_pointer (value);
141       break;
142     case PROP_CLOSE_HANDLE:
143       win32_stream->priv->close_handle = g_value_get_boolean (value);
144       break;
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148     }
149 }
150
151 static void
152 g_win32_output_stream_get_property (GObject    *object,
153                                     guint       prop_id,
154                                     GValue     *value,
155                                     GParamSpec *pspec)
156 {
157   GWin32OutputStream *win32_stream;
158
159   win32_stream = G_WIN32_OUTPUT_STREAM (object);
160
161   switch (prop_id)
162     {
163     case PROP_HANDLE:
164       g_value_set_pointer (value, win32_stream->priv->handle);
165       break;
166     case PROP_CLOSE_HANDLE:
167       g_value_set_boolean (value, win32_stream->priv->close_handle);
168       break;
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171     }
172 }
173
174 static void
175 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
176 {
177   win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
178   win32_stream->priv->handle = NULL;
179   win32_stream->priv->close_handle = TRUE;
180 }
181
182 /**
183  * g_win32_output_stream_new:
184  * @handle: a Win32 file handle
185  * @close_handle: %TRUE to close the handle when done
186  *
187  * Creates a new #GWin32OutputStream for the given @handle.
188  *
189  * If @close_handle, is %TRUE, the handle will be closed when the
190  * output stream is destroyed.
191  *
192  * Returns: a new #GOutputStream
193  *
194  * Since: 2.26
195 **/
196 GOutputStream *
197 g_win32_output_stream_new (void    *handle,
198                            gboolean close_handle)
199 {
200   GWin32OutputStream *stream;
201
202   g_return_val_if_fail (handle != NULL, NULL);
203
204   stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
205                          "handle", handle,
206                          "close-handle", close_handle,
207                          NULL);
208
209   return G_OUTPUT_STREAM (stream);
210 }
211
212 /**
213  * g_win32_output_stream_set_close_handle:
214  * @stream: a #GWin32OutputStream
215  * @close_handle: %TRUE to close the handle when done
216  *
217  * Sets whether the handle of @stream shall be closed when the stream
218  * is closed.
219  *
220  * Since: 2.26
221  */
222 void
223 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
224                                         gboolean           close_handle)
225 {
226   g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
227
228   close_handle = close_handle != FALSE;
229   if (stream->priv->close_handle != close_handle)
230     {
231       stream->priv->close_handle = close_handle;
232       g_object_notify (G_OBJECT (stream), "close-handle");
233     }
234 }
235
236 /**
237  * g_win32_output_stream_get_close_handle:
238  * @stream: a #GWin32OutputStream
239  *
240  * Returns whether the handle of @stream will be closed when the
241  * stream is closed.
242  *
243  * Return value: %TRUE if the handle is closed when done
244  *
245  * Since: 2.26
246  */
247 gboolean
248 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
249 {
250   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
251
252   return stream->priv->close_handle;
253 }
254
255 /**
256  * g_win32_output_stream_get_handle:
257  * @stream: a #GWin32OutputStream
258  *
259  * Return the Windows handle that the stream writes to.
260  *
261  * Return value: The handle descriptor of @stream
262  *
263  * Since: 2.26
264  */
265 void *
266 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
267 {
268   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
269
270   return stream->priv->handle;
271 }
272
273 static gssize
274 g_win32_output_stream_write (GOutputStream  *stream,
275                             const void     *buffer,
276                             gsize           count,
277                             GCancellable   *cancellable,
278                             GError        **error)
279 {
280   GWin32OutputStream *win32_stream;
281   BOOL res;
282   DWORD nbytes, nwritten;
283   OVERLAPPED overlap = { 0, };
284   gssize retval = -1;
285
286   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
287
288   if (g_cancellable_set_error_if_cancelled (cancellable, error))
289     return -1;
290
291   if (count > G_MAXINT)
292     nbytes = G_MAXINT;
293   else
294     nbytes = count;
295
296   overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
297   g_return_val_if_fail (overlap.hEvent != NULL, -1);
298
299   res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
300   if (res)
301     retval = nwritten;
302   else
303     {
304       int errsv = GetLastError ();
305
306       if (errsv == ERROR_IO_PENDING &&
307           _g_win32_overlap_wait_result (win32_stream->priv->handle,
308                                         &overlap, &nwritten, cancellable))
309         {
310           retval = nwritten;
311           goto end;
312         }
313
314       if (g_cancellable_set_error_if_cancelled (cancellable, error))
315         goto end;
316
317       errsv = GetLastError ();
318       if (errsv == ERROR_HANDLE_EOF ||
319           errsv == ERROR_BROKEN_PIPE)
320         {
321           retval = 0;
322         }
323       else
324         {
325           gchar *emsg;
326
327           emsg = g_win32_error_message (errsv);
328           g_set_error (error, G_IO_ERROR,
329                        g_io_error_from_win32_error (errsv),
330                        _("Error writing to handle: %s"),
331                        emsg);
332           g_free (emsg);
333         }
334     }
335
336 end:
337   CloseHandle (overlap.hEvent);
338   return retval;
339 }
340
341 static gboolean
342 g_win32_output_stream_close (GOutputStream  *stream,
343                              GCancellable   *cancellable,
344                              GError        **error)
345 {
346   GWin32OutputStream *win32_stream;
347   BOOL res;
348
349   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
350
351   if (!win32_stream->priv->close_handle)
352     return TRUE;
353
354   res = CloseHandle (win32_stream->priv->handle);
355   if (!res)
356     {
357       int errsv = GetLastError ();
358       gchar *emsg = g_win32_error_message (errsv);
359
360       g_set_error (error, G_IO_ERROR,
361                    g_io_error_from_win32_error (errsv),
362                    _("Error closing handle: %s"),
363                    emsg);
364       g_free (emsg);
365       return FALSE;
366     }
367
368   return TRUE;
369 }