Add g_key_file_save_to_file()
[glib.git] / gio / gdataoutputstream.c
blob76575737cbd3d9aadb828e13f1b9bff3a9ffe4b7
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 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>
23 #include "config.h"
24 #include <string.h>
25 #include "gdataoutputstream.h"
26 #include "gseekable.h"
27 #include "gioenumtypes.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
32 /**
33 * SECTION:gdataoutputstream
34 * @short_description: Data Output Stream
35 * @include: gio/gio.h
36 * @see_also: #GOutputStream
38 * Data output stream implements #GOutputStream and includes functions for
39 * writing data directly to an output stream.
41 **/
45 struct _GDataOutputStreamPrivate {
46 GDataStreamByteOrder byte_order;
49 enum {
50 PROP_0,
51 PROP_BYTE_ORDER
54 static void g_data_output_stream_set_property (GObject *object,
55 guint prop_id,
56 const GValue *value,
57 GParamSpec *pspec);
58 static void g_data_output_stream_get_property (GObject *object,
59 guint prop_id,
60 GValue *value,
61 GParamSpec *pspec);
63 static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
64 static goffset g_data_output_stream_tell (GSeekable *seekable);
65 static gboolean g_data_output_stream_can_seek (GSeekable *seekable);
66 static gboolean g_data_output_stream_seek (GSeekable *seekable,
67 goffset offset,
68 GSeekType type,
69 GCancellable *cancellable,
70 GError **error);
71 static gboolean g_data_output_stream_can_truncate (GSeekable *seekable);
72 static gboolean g_data_output_stream_truncate (GSeekable *seekable,
73 goffset offset,
74 GCancellable *cancellable,
75 GError **error);
77 G_DEFINE_TYPE_WITH_CODE (GDataOutputStream,
78 g_data_output_stream,
79 G_TYPE_FILTER_OUTPUT_STREAM,
80 G_ADD_PRIVATE (GDataOutputStream)
81 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
82 g_data_output_stream_seekable_iface_init))
85 static void
86 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
88 GObjectClass *object_class;
90 object_class = G_OBJECT_CLASS (klass);
91 object_class->get_property = g_data_output_stream_get_property;
92 object_class->set_property = g_data_output_stream_set_property;
94 /**
95 * GDataOutputStream:byte-order:
97 * Determines the byte ordering that is used when writing
98 * multi-byte entities (such as integers) to the stream.
100 g_object_class_install_property (object_class,
101 PROP_BYTE_ORDER,
102 g_param_spec_enum ("byte-order",
103 P_("Byte order"),
104 P_("The byte order"),
105 G_TYPE_DATA_STREAM_BYTE_ORDER,
106 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
107 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
111 static void
112 g_data_output_stream_set_property (GObject *object,
113 guint prop_id,
114 const GValue *value,
115 GParamSpec *pspec)
117 GDataOutputStream *dstream;
119 dstream = G_DATA_OUTPUT_STREAM (object);
121 switch (prop_id)
123 case PROP_BYTE_ORDER:
124 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
125 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 break;
133 static void
134 g_data_output_stream_get_property (GObject *object,
135 guint prop_id,
136 GValue *value,
137 GParamSpec *pspec)
139 GDataOutputStreamPrivate *priv;
140 GDataOutputStream *dstream;
142 dstream = G_DATA_OUTPUT_STREAM (object);
143 priv = dstream->priv;
145 switch (prop_id)
147 case PROP_BYTE_ORDER:
148 g_value_set_enum (value, priv->byte_order);
149 break;
151 default:
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 break;
157 static void
158 g_data_output_stream_init (GDataOutputStream *stream)
160 stream->priv = g_data_output_stream_get_instance_private (stream);
161 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
164 static void
165 g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
167 iface->tell = g_data_output_stream_tell;
168 iface->can_seek = g_data_output_stream_can_seek;
169 iface->seek = g_data_output_stream_seek;
170 iface->can_truncate = g_data_output_stream_can_truncate;
171 iface->truncate_fn = g_data_output_stream_truncate;
175 * g_data_output_stream_new:
176 * @base_stream: a #GOutputStream.
178 * Creates a new data output stream for @base_stream.
180 * Returns: #GDataOutputStream.
182 GDataOutputStream *
183 g_data_output_stream_new (GOutputStream *base_stream)
185 GDataOutputStream *stream;
187 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
189 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
190 "base-stream", base_stream,
191 NULL);
193 return stream;
197 * g_data_output_stream_set_byte_order:
198 * @stream: a #GDataOutputStream.
199 * @order: a %GDataStreamByteOrder.
201 * Sets the byte order of the data output stream to @order.
203 void
204 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
205 GDataStreamByteOrder order)
207 GDataOutputStreamPrivate *priv;
208 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
209 priv = stream->priv;
210 if (priv->byte_order != order)
212 priv->byte_order = order;
213 g_object_notify (G_OBJECT (stream), "byte-order");
218 * g_data_output_stream_get_byte_order:
219 * @stream: a #GDataOutputStream.
221 * Gets the byte order for the stream.
223 * Returns: the #GDataStreamByteOrder for the @stream.
225 GDataStreamByteOrder
226 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
228 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
230 return stream->priv->byte_order;
234 * g_data_output_stream_put_byte:
235 * @stream: a #GDataOutputStream.
236 * @data: a #guchar.
237 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
238 * @error: a #GError, %NULL to ignore.
240 * Puts a byte into the output stream.
242 * Returns: %TRUE if @data was successfully added to the @stream.
244 gboolean
245 g_data_output_stream_put_byte (GDataOutputStream *stream,
246 guchar data,
247 GCancellable *cancellable,
248 GError **error)
250 gsize bytes_written;
252 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
254 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
255 &data, 1,
256 &bytes_written,
257 cancellable, error);
261 * g_data_output_stream_put_int16:
262 * @stream: a #GDataOutputStream.
263 * @data: a #gint16.
264 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
265 * @error: a #GError, %NULL to ignore.
267 * Puts a signed 16-bit integer into the output stream.
269 * Returns: %TRUE if @data was successfully added to the @stream.
271 gboolean
272 g_data_output_stream_put_int16 (GDataOutputStream *stream,
273 gint16 data,
274 GCancellable *cancellable,
275 GError **error)
277 gsize bytes_written;
279 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
281 switch (stream->priv->byte_order)
283 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
284 data = GINT16_TO_BE (data);
285 break;
286 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
287 data = GINT16_TO_LE (data);
288 break;
289 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
290 default:
291 break;
294 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
295 &data, 2,
296 &bytes_written,
297 cancellable, error);
301 * g_data_output_stream_put_uint16:
302 * @stream: a #GDataOutputStream.
303 * @data: a #guint16.
304 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
305 * @error: a #GError, %NULL to ignore.
307 * Puts an unsigned 16-bit integer into the output stream.
309 * Returns: %TRUE if @data was successfully added to the @stream.
311 gboolean
312 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
313 guint16 data,
314 GCancellable *cancellable,
315 GError **error)
317 gsize bytes_written;
319 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
321 switch (stream->priv->byte_order)
323 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
324 data = GUINT16_TO_BE (data);
325 break;
326 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
327 data = GUINT16_TO_LE (data);
328 break;
329 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
330 default:
331 break;
334 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
335 &data, 2,
336 &bytes_written,
337 cancellable, error);
341 * g_data_output_stream_put_int32:
342 * @stream: a #GDataOutputStream.
343 * @data: a #gint32.
344 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
345 * @error: a #GError, %NULL to ignore.
347 * Puts a signed 32-bit integer into the output stream.
349 * Returns: %TRUE if @data was successfully added to the @stream.
351 gboolean
352 g_data_output_stream_put_int32 (GDataOutputStream *stream,
353 gint32 data,
354 GCancellable *cancellable,
355 GError **error)
357 gsize bytes_written;
359 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
361 switch (stream->priv->byte_order)
363 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
364 data = GINT32_TO_BE (data);
365 break;
366 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
367 data = GINT32_TO_LE (data);
368 break;
369 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
370 default:
371 break;
374 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
375 &data, 4,
376 &bytes_written,
377 cancellable, error);
381 * g_data_output_stream_put_uint32:
382 * @stream: a #GDataOutputStream.
383 * @data: a #guint32.
384 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
385 * @error: a #GError, %NULL to ignore.
387 * Puts an unsigned 32-bit integer into the stream.
389 * Returns: %TRUE if @data was successfully added to the @stream.
391 gboolean
392 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
393 guint32 data,
394 GCancellable *cancellable,
395 GError **error)
397 gsize bytes_written;
399 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
401 switch (stream->priv->byte_order)
403 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
404 data = GUINT32_TO_BE (data);
405 break;
406 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
407 data = GUINT32_TO_LE (data);
408 break;
409 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
410 default:
411 break;
414 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
415 &data, 4,
416 &bytes_written,
417 cancellable, error);
421 * g_data_output_stream_put_int64:
422 * @stream: a #GDataOutputStream.
423 * @data: a #gint64.
424 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
425 * @error: a #GError, %NULL to ignore.
427 * Puts a signed 64-bit integer into the stream.
429 * Returns: %TRUE if @data was successfully added to the @stream.
431 gboolean
432 g_data_output_stream_put_int64 (GDataOutputStream *stream,
433 gint64 data,
434 GCancellable *cancellable,
435 GError **error)
437 gsize bytes_written;
439 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
441 switch (stream->priv->byte_order)
443 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
444 data = GINT64_TO_BE (data);
445 break;
446 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
447 data = GINT64_TO_LE (data);
448 break;
449 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
450 default:
451 break;
454 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
455 &data, 8,
456 &bytes_written,
457 cancellable, error);
461 * g_data_output_stream_put_uint64:
462 * @stream: a #GDataOutputStream.
463 * @data: a #guint64.
464 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
465 * @error: a #GError, %NULL to ignore.
467 * Puts an unsigned 64-bit integer into the stream.
469 * Returns: %TRUE if @data was successfully added to the @stream.
471 gboolean
472 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
473 guint64 data,
474 GCancellable *cancellable,
475 GError **error)
477 gsize bytes_written;
479 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
481 switch (stream->priv->byte_order)
483 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
484 data = GUINT64_TO_BE (data);
485 break;
486 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
487 data = GUINT64_TO_LE (data);
488 break;
489 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
490 default:
491 break;
494 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
495 &data, 8,
496 &bytes_written,
497 cancellable, error);
501 * g_data_output_stream_put_string:
502 * @stream: a #GDataOutputStream.
503 * @str: a string.
504 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
505 * @error: a #GError, %NULL to ignore.
507 * Puts a string into the output stream.
509 * Returns: %TRUE if @string was successfully added to the @stream.
511 gboolean
512 g_data_output_stream_put_string (GDataOutputStream *stream,
513 const char *str,
514 GCancellable *cancellable,
515 GError **error)
517 gsize bytes_written;
519 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
520 g_return_val_if_fail (str != NULL, FALSE);
522 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
523 str, strlen (str),
524 &bytes_written,
525 cancellable, error);
528 static goffset
529 g_data_output_stream_tell (GSeekable *seekable)
531 GOutputStream *base_stream;
532 GSeekable *base_stream_seekable;
534 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
535 if (!G_IS_SEEKABLE (base_stream))
536 return 0;
537 base_stream_seekable = G_SEEKABLE (base_stream);
538 return g_seekable_tell (base_stream_seekable);
541 static gboolean
542 g_data_output_stream_can_seek (GSeekable *seekable)
544 GOutputStream *base_stream;
546 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
547 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
550 static gboolean
551 g_data_output_stream_seek (GSeekable *seekable,
552 goffset offset,
553 GSeekType type,
554 GCancellable *cancellable,
555 GError **error)
557 GOutputStream *base_stream;
558 GSeekable *base_stream_seekable;
560 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
561 if (!G_IS_SEEKABLE (base_stream))
563 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
564 _("Seek not supported on base stream"));
565 return FALSE;
568 base_stream_seekable = G_SEEKABLE (base_stream);
569 return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
572 static gboolean
573 g_data_output_stream_can_truncate (GSeekable *seekable)
575 GOutputStream *base_stream;
577 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
578 return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
581 static gboolean
582 g_data_output_stream_truncate (GSeekable *seekable,
583 goffset offset,
584 GCancellable *cancellable,
585 GError **error)
587 GOutputStream *base_stream;
588 GSeekable *base_stream_seekable;
590 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
591 if (!G_IS_SEEKABLE (base_stream))
593 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
594 _("Truncate not supported on base stream"));
595 return FALSE;
598 base_stream_seekable = G_SEEKABLE (base_stream);
599 return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);