GSettings: deprecate 'schema' property
[platform/upstream/glib.git] / gio / gdataoutputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 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  */
22
23 #include "config.h"
24 #include <string.h>
25 #include "gdataoutputstream.h"
26 #include "gioenumtypes.h"
27 #include "glibintl.h"
28
29
30 /**
31  * SECTION:gdataoutputstream
32  * @short_description: Data Output Stream
33  * @include: gio/gio.h
34  * @see_also: #GOutputStream
35  * 
36  * Data output stream implements #GOutputStream and includes functions for 
37  * writing data directly to an output stream.
38  *
39  **/
40
41
42
43 struct _GDataOutputStreamPrivate {
44   GDataStreamByteOrder byte_order;
45 };
46
47 enum {
48   PROP_0,
49   PROP_BYTE_ORDER
50 };
51
52 static void g_data_output_stream_set_property (GObject      *object,
53                                                guint         prop_id,
54                                                const GValue *value,
55                                                GParamSpec   *pspec);
56 static void g_data_output_stream_get_property (GObject      *object,
57                                                guint         prop_id,
58                                                GValue       *value,
59                                                GParamSpec   *pspec);
60
61 G_DEFINE_TYPE (GDataOutputStream,
62                g_data_output_stream,
63                G_TYPE_FILTER_OUTPUT_STREAM)
64
65
66 static void
67 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
68 {
69   GObjectClass *object_class;
70
71   g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
72
73   object_class = G_OBJECT_CLASS (klass);
74   object_class->get_property = g_data_output_stream_get_property;
75   object_class->set_property = g_data_output_stream_set_property;
76
77   /**
78    * GDataOutputStream:byte-order:
79    *
80    * Determines the byte ordering that is used when writing 
81    * multi-byte entities (such as integers) to the stream.
82    */
83   g_object_class_install_property (object_class,
84                                    PROP_BYTE_ORDER,
85                                    g_param_spec_enum ("byte-order",
86                                                       P_("Byte order"),
87                                                       P_("The byte order"),
88                                                       G_TYPE_DATA_STREAM_BYTE_ORDER,
89                                                       G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
90                                                       G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
91
92 }
93
94 static void
95 g_data_output_stream_set_property (GObject     *object,
96                                   guint         prop_id,
97                                   const GValue *value,
98                                   GParamSpec   *pspec)
99 {
100   GDataOutputStream *dstream;
101
102   dstream = G_DATA_OUTPUT_STREAM (object);
103
104   switch (prop_id) 
105     {
106     case PROP_BYTE_ORDER:
107       g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
108       break;
109
110     default:
111       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112       break;
113     }
114 }
115
116 static void
117 g_data_output_stream_get_property (GObject    *object,
118                                    guint       prop_id,
119                                    GValue     *value,
120                                    GParamSpec *pspec)
121 {
122   GDataOutputStreamPrivate *priv;
123   GDataOutputStream        *dstream;
124
125   dstream = G_DATA_OUTPUT_STREAM (object);
126   priv = dstream->priv;
127
128   switch (prop_id)
129     {
130     case PROP_BYTE_ORDER:
131       g_value_set_enum (value, priv->byte_order);
132       break;
133
134     default:
135       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136       break;
137     }
138 }
139
140 static void
141 g_data_output_stream_init (GDataOutputStream *stream)
142 {
143   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
144                                               G_TYPE_DATA_OUTPUT_STREAM,
145                                               GDataOutputStreamPrivate);
146
147   stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
148 }
149
150 /**
151  * g_data_output_stream_new:
152  * @base_stream: a #GOutputStream.
153  * 
154  * Creates a new data output stream for @base_stream.
155  * 
156  * Returns: #GDataOutputStream.
157  **/
158 GDataOutputStream *
159 g_data_output_stream_new (GOutputStream *base_stream)
160 {
161   GDataOutputStream *stream;
162
163   g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
164
165   stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
166                          "base-stream", base_stream,
167                          NULL);
168
169   return stream;
170 }
171
172 /**
173  * g_data_output_stream_set_byte_order:
174  * @stream: a #GDataOutputStream.
175  * @order: a %GDataStreamByteOrder.
176  * 
177  * Sets the byte order of the data output stream to @order.
178  **/
179 void
180 g_data_output_stream_set_byte_order (GDataOutputStream    *stream,
181                                      GDataStreamByteOrder  order)
182 {
183   GDataOutputStreamPrivate *priv;
184   g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
185   priv = stream->priv;
186   if (priv->byte_order != order)
187     {
188       priv->byte_order = order;
189       g_object_notify (G_OBJECT (stream), "byte-order");
190     }
191 }
192
193 /**
194  * g_data_output_stream_get_byte_order:
195  * @stream: a #GDataOutputStream.
196  * 
197  * Gets the byte order for the stream.
198  * 
199  * Returns: the #GDataStreamByteOrder for the @stream.
200  **/
201 GDataStreamByteOrder
202 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
203 {
204   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
205
206   return stream->priv->byte_order;
207 }
208
209 /**
210  * g_data_output_stream_put_byte:
211  * @stream: a #GDataOutputStream.
212  * @data: a #guchar.
213  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
214  * @error: a #GError, %NULL to ignore.
215  * 
216  * Puts a byte into the output stream.
217  * 
218  * Returns: %TRUE if @data was successfully added to the @stream.
219  **/
220 gboolean
221 g_data_output_stream_put_byte (GDataOutputStream  *stream,
222                                guchar              data,
223                                GCancellable       *cancellable,
224                                GError            **error)
225 {
226   gsize bytes_written;
227   
228   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
229
230   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
231                                     &data, 1,
232                                     &bytes_written,
233                                     cancellable, error);
234 }
235
236 /**
237  * g_data_output_stream_put_int16:
238  * @stream: a #GDataOutputStream.
239  * @data: a #gint16.
240  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
241  * @error: a #GError, %NULL to ignore.
242  * 
243  * Puts a signed 16-bit integer into the output stream.
244  * 
245  * Returns: %TRUE if @data was successfully added to the @stream.
246  **/
247 gboolean
248 g_data_output_stream_put_int16 (GDataOutputStream  *stream,
249                                 gint16              data,
250                                 GCancellable       *cancellable,
251                                 GError            **error)
252 {
253   gsize bytes_written;
254   
255   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
256
257   switch (stream->priv->byte_order)
258     {
259     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
260       data = GINT16_TO_BE (data);
261       break;
262     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
263       data = GINT16_TO_LE (data);
264       break;
265     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
266     default:
267       break;
268     }
269   
270   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
271                                     &data, 2,
272                                     &bytes_written,
273                                     cancellable, error);
274 }
275
276 /**
277  * g_data_output_stream_put_uint16:
278  * @stream: a #GDataOutputStream.
279  * @data: a #guint16.
280  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
281  * @error: a #GError, %NULL to ignore.
282  * 
283  * Puts an unsigned 16-bit integer into the output stream.
284  * 
285  * Returns: %TRUE if @data was successfully added to the @stream.
286  **/
287 gboolean
288 g_data_output_stream_put_uint16 (GDataOutputStream  *stream,
289                                  guint16             data,
290                                  GCancellable       *cancellable,
291                                  GError            **error)
292 {
293   gsize bytes_written;
294   
295   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
296
297   switch (stream->priv->byte_order)
298     {
299     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
300       data = GUINT16_TO_BE (data);
301       break;
302     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
303       data = GUINT16_TO_LE (data);
304       break;
305     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
306     default:
307       break;
308     }
309   
310   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
311                                     &data, 2,
312                                     &bytes_written,
313                                     cancellable, error);
314 }
315
316 /**
317  * g_data_output_stream_put_int32:
318  * @stream: a #GDataOutputStream.
319  * @data: a #gint32.
320  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
321  * @error: a #GError, %NULL to ignore.
322  * 
323  * Puts a signed 32-bit integer into the output stream.
324  * 
325  * Returns: %TRUE if @data was successfully added to the @stream.
326  **/
327 gboolean
328 g_data_output_stream_put_int32 (GDataOutputStream  *stream,
329                                 gint32              data,
330                                 GCancellable       *cancellable,
331                                 GError            **error)
332 {
333   gsize bytes_written;
334   
335   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
336
337   switch (stream->priv->byte_order)
338     {
339     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
340       data = GINT32_TO_BE (data);
341       break;
342     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
343       data = GINT32_TO_LE (data);
344       break;
345     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
346     default:
347       break;
348     }
349   
350   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
351                                     &data, 4,
352                                     &bytes_written,
353                                     cancellable, error);
354 }
355
356 /**
357  * g_data_output_stream_put_uint32:
358  * @stream: a #GDataOutputStream.
359  * @data: a #guint32.
360  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
361  * @error: a #GError, %NULL to ignore.
362  * 
363  * Puts an unsigned 32-bit integer into the stream.
364  * 
365  * Returns: %TRUE if @data was successfully added to the @stream.
366  **/
367 gboolean
368 g_data_output_stream_put_uint32 (GDataOutputStream  *stream,
369                                  guint32             data,
370                                  GCancellable       *cancellable,
371                                  GError            **error)
372 {
373   gsize bytes_written;
374   
375   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
376
377   switch (stream->priv->byte_order)
378     {
379     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
380       data = GUINT32_TO_BE (data);
381       break;
382     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
383       data = GUINT32_TO_LE (data);
384       break;
385     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
386     default:
387       break;
388     }
389   
390   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
391                                     &data, 4,
392                                     &bytes_written,
393                                     cancellable, error);
394 }
395
396 /**
397  * g_data_output_stream_put_int64:
398  * @stream: a #GDataOutputStream.
399  * @data: a #gint64.
400  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
401  * @error: a #GError, %NULL to ignore.
402  * 
403  * Puts a signed 64-bit integer into the stream.
404  * 
405  * Returns: %TRUE if @data was successfully added to the @stream.
406  **/
407 gboolean
408 g_data_output_stream_put_int64 (GDataOutputStream  *stream,
409                                 gint64              data,
410                                 GCancellable       *cancellable,
411                                 GError            **error)
412 {
413   gsize bytes_written;
414   
415   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
416
417   switch (stream->priv->byte_order)
418     {
419     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
420       data = GINT64_TO_BE (data);
421       break;
422     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
423       data = GINT64_TO_LE (data);
424       break;
425     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
426     default:
427       break;
428     }
429   
430   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
431                                     &data, 8,
432                                     &bytes_written,
433                                     cancellable, error);
434 }
435
436 /**
437  * g_data_output_stream_put_uint64:
438  * @stream: a #GDataOutputStream.
439  * @data: a #guint64.
440  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
441  * @error: a #GError, %NULL to ignore.
442  * 
443  * Puts an unsigned 64-bit integer into the stream.
444  * 
445  * Returns: %TRUE if @data was successfully added to the @stream.
446  **/
447 gboolean
448 g_data_output_stream_put_uint64 (GDataOutputStream  *stream,
449                                  guint64             data,
450                                  GCancellable       *cancellable,
451                                  GError            **error)
452 {
453   gsize bytes_written;
454   
455   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
456
457   switch (stream->priv->byte_order)
458     {
459     case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
460       data = GUINT64_TO_BE (data);
461       break;
462     case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
463       data = GUINT64_TO_LE (data);
464       break;
465     case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
466     default:
467       break;
468     }
469   
470   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
471                                     &data, 8,
472                                     &bytes_written,
473                                     cancellable, error);
474 }
475
476 /**
477  * g_data_output_stream_put_string:
478  * @stream: a #GDataOutputStream.
479  * @str: a string.
480  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
481  * @error: a #GError, %NULL to ignore.
482  * 
483  * Puts a string into the output stream. 
484  * 
485  * Returns: %TRUE if @string was successfully added to the @stream.
486  **/
487 gboolean
488 g_data_output_stream_put_string (GDataOutputStream  *stream,
489                                  const char         *str,
490                                  GCancellable       *cancellable,
491                                  GError            **error)
492 {
493   gsize bytes_written;
494   
495   g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
496   g_return_val_if_fail (str != NULL, FALSE);
497
498   return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
499                                     str, strlen (str),
500                                     &bytes_written,
501                                     cancellable, error);
502 }