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