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