Doc fixes
[platform/upstream/glib.git] / gio / gdatainputstream.c
index 095428a..9d3abef 100644 (file)
 
 #include <config.h>
 #include "gdatainputstream.h"
+#include "gioenumtypes.h"
 #include "glibintl.h"
 
+#include "gioalias.h"
+
 /**
  * SECTION:gdatainputstream
  * @short_description: Data Input Stream
+ * @include: gio/gio.h
  * @see_also: #GInputStream
  * 
  * Data input stream implements #GInputStream and includes functions for 
- * reading data directly from an input stream.
+ * reading structured data directly from a binary input stream.
  *
  **/
 
@@ -41,7 +45,9 @@ struct _GDataInputStreamPrivate {
 };
 
 enum {
-  PROP_0
+  PROP_0,
+  PROP_BYTE_ORDER,
+  PROP_NEWLINE_TYPE
 };
 
 static void g_data_input_stream_set_property (GObject      *object,
@@ -68,13 +74,44 @@ g_data_input_stream_class_init (GDataInputStreamClass *klass)
   object_class = G_OBJECT_CLASS (klass);
   object_class->get_property = g_data_input_stream_get_property;
   object_class->set_property = g_data_input_stream_set_property;
+
+  /**
+   * GDataStream:byte-order:
+   *
+   * The ::byte-order property determines the byte ordering that
+   * is used when reading multi-byte entities (such as integers)
+   * from the stream.
+   */ 
+  g_object_class_install_property (object_class,
+                                   PROP_BYTE_ORDER,
+                                   g_param_spec_enum ("byte-order",
+                                                      P_("Byte order"),
+                                                      P_("The byte order"),
+                                                      G_TYPE_DATA_STREAM_BYTE_ORDER,
+                                                      G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
+                                                      G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
+
+  /**
+   * GDataStream:newline-type:
+   *
+   * The :newline-type property determines what is considered
+   * as a line ending when reading complete lines from the stream.
+   */ 
+  g_object_class_install_property (object_class,
+                                   PROP_NEWLINE_TYPE,
+                                   g_param_spec_enum ("newline-type",
+                                                      P_("Newline type"),
+                                                      P_("The accepted types of line ending"),
+                                                      G_TYPE_DATA_STREAM_NEWLINE_TYPE,
+                                                      G_DATA_STREAM_NEWLINE_TYPE_LF,
+                                                      G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
 }
 
 static void
-g_data_input_stream_set_property (GObject         *object,
-                                 guint            prop_id,
-                                 const GValue    *value,
-                                 GParamSpec      *pspec)
+g_data_input_stream_set_property (GObject      *object,
+                                 guint         prop_id,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
 {
   GDataInputStreamPrivate *priv;
   GDataInputStream        *dstream;
@@ -82,8 +119,15 @@ g_data_input_stream_set_property (GObject         *object,
   dstream = G_DATA_INPUT_STREAM (object);
   priv = dstream->priv;
 
-  switch (prop_id) 
+   switch (prop_id) 
     {
+    case PROP_BYTE_ORDER:
+      g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
+      break;
+
+    case PROP_NEWLINE_TYPE:
+      g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
+      break;
 
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -94,9 +138,9 @@ g_data_input_stream_set_property (GObject         *object,
 
 static void
 g_data_input_stream_get_property (GObject    *object,
-                                      guint       prop_id,
-                                      GValue     *value,
-                                      GParamSpec *pspec)
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
 {
   GDataInputStreamPrivate *priv;
   GDataInputStream        *dstream;
@@ -106,7 +150,14 @@ g_data_input_stream_get_property (GObject    *object,
 
   switch (prop_id)
     { 
-  
+    case PROP_BYTE_ORDER:
+      g_value_set_enum (value, priv->byte_order);
+      break;
+
+    case PROP_NEWLINE_TYPE:
+      g_value_set_enum (value, priv->newline_type);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -156,12 +207,21 @@ g_data_input_stream_new (GInputStream *base_stream)
  *  
  **/
 void
-g_data_input_stream_set_byte_order (GDataInputStream *stream,
-                                   GDataStreamByteOrder order)
+g_data_input_stream_set_byte_order (GDataInputStream     *stream,
+                                   GDataStreamByteOrder  order)
 {
+  GDataInputStreamPrivate *priv;
+
   g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
 
-  stream->priv->byte_order = order;
+  priv = stream->priv;
+
+  if (priv->byte_order != order)
+    {
+      priv->byte_order = order;
+      
+      g_object_notify (G_OBJECT (stream), "byte-order");
+    }
 }
 
 /**
@@ -187,17 +247,27 @@ g_data_input_stream_get_byte_order (GDataInputStream *stream)
  * 
  * Sets the newline type for the @stream.
  * 
- * TODO: is it valid to set this to G_DATA_STREAM_NEWLINE_TYPE_ANY, or
- * should it always be set to {_LF, _CR, _CR_LF}
+ * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
+ * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
+ * "CR LF", and this might block if there is no more data availible.
  *  
  **/
 void
-g_data_input_stream_set_newline_type (GDataInputStream        *stream,
-                                     GDataStreamNewlineType   type)
+g_data_input_stream_set_newline_type (GDataInputStream       *stream,
+                                     GDataStreamNewlineType  type)
 {
+  GDataInputStreamPrivate *priv;
+
   g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
 
-  stream->priv->newline_type = type;
+  priv = stream->priv;
+  
+  if (priv->newline_type != type)
+    {
+      priv->newline_type = type;
+
+      g_object_notify (G_OBJECT (stream), "newline-type");
+    }
 }
 
 /**
@@ -217,11 +287,11 @@ g_data_input_stream_get_newline_type (GDataInputStream *stream)
 }
 
 static gboolean
-read_data (GDataInputStream *stream,
-         void *buffer,
-         gsize size,
-         GCancellable *cancellable,
-         GError **error)
+read_data (GDataInputStream  *stream,
+           void              *buffer,
+           gsize              size,
+           GCancellable      *cancellable,
+           GError           **error)
 {
   gsize available;
   gssize res;
@@ -245,7 +315,7 @@ read_data (GDataInputStream *stream,
   res = g_input_stream_read (G_INPUT_STREAM (stream),
                             buffer, size,
                             NULL, NULL);
-  g_assert (res == size);
+  g_warn_if_fail (res == size);
   return TRUE;
 }
 
@@ -259,12 +329,12 @@ read_data (GDataInputStream *stream,
  * Reads an unsigned 8-bit/1-byte value from @stream.
  *
  * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0 
- * if an error occured.
+ * if an error occurred.
  **/
 guchar
-g_data_input_stream_read_byte (GDataInputStream        *stream,
-                             GCancellable            *cancellable,
-                             GError                 **error)
+g_data_input_stream_read_byte (GDataInputStream  *stream,
+                              GCancellable       *cancellable,
+                              GError            **error)
 {
   guchar c;
   
@@ -289,12 +359,12 @@ g_data_input_stream_read_byte (GDataInputStream        *stream,
  * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order().
  * 
  * Returns: a signed 16-bit/2-byte value read from @stream or %0 if 
- * an error occured.
+ * an error occurred.
  **/
 gint16
-g_data_input_stream_read_int16 (GDataInputStream        *stream,
-                              GCancellable            *cancellable,
-                              GError                 **error)
+g_data_input_stream_read_int16 (GDataInputStream  *stream,
+                              GCancellable       *cancellable,
+                              GError            **error)
 {
   gint16 v;
   
@@ -333,12 +403,12 @@ g_data_input_stream_read_int16 (GDataInputStream        *stream,
  * see g_data_stream_get_byte_order() and g_data_stream_set_byte_order(). 
  * 
  * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if 
- * an error occured. 
+ * an error occurred. 
  **/
 guint16
-g_data_input_stream_read_uint16 (GDataInputStream        *stream,
-                               GCancellable            *cancellable,
-                               GError                 **error)
+g_data_input_stream_read_uint16 (GDataInputStream  *stream,
+                                GCancellable       *cancellable,
+                                GError            **error)
 {
   guint16 v;
   
@@ -381,12 +451,12 @@ g_data_input_stream_read_uint16 (GDataInputStream        *stream,
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
  *   
  * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if 
- * an error occured. 
+ * an error occurred. 
  **/
 gint32
-g_data_input_stream_read_int32 (GDataInputStream        *stream,
-                              GCancellable            *cancellable,
-                              GError                 **error)
+g_data_input_stream_read_int32 (GDataInputStream  *stream,
+                               GCancellable       *cancellable,
+                               GError            **error)
 {
   gint32 v;
   
@@ -429,12 +499,12 @@ g_data_input_stream_read_int32 (GDataInputStream        *stream,
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
  * 
  * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if 
- * an error occured. 
+ * an error occurred. 
  **/
 guint32
-g_data_input_stream_read_uint32 (GDataInputStream        *stream,
-                               GCancellable            *cancellable,
-                               GError                 **error)
+g_data_input_stream_read_uint32 (GDataInputStream  *stream,
+                                GCancellable       *cancellable,
+                                GError            **error)
 {
   guint32 v;
   
@@ -477,12 +547,12 @@ g_data_input_stream_read_uint32 (GDataInputStream        *stream,
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
  * 
  * Returns: a signed 64-bit/8-byte value read from @stream or %0 if 
- * an error occured.  
+ * an error occurred.  
  **/
 gint64
-g_data_input_stream_read_int64 (GDataInputStream        *stream,
-                              GCancellable            *cancellable,
-                              GError                 **error)
+g_data_input_stream_read_int64 (GDataInputStream  *stream,
+                              GCancellable       *cancellable,
+                              GError            **error)
 {
   gint64 v;
   
@@ -525,12 +595,12 @@ g_data_input_stream_read_int64 (GDataInputStream        *stream,
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
  * 
  * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if 
- * an error occured. 
+ * an error occurred. 
  **/
 guint64
-g_data_input_stream_read_uint64 (GDataInputStream        *stream,
-                               GCancellable            *cancellable,
-                               GError                 **error)
+g_data_input_stream_read_uint64 (GDataInputStream  *stream,
+                               GCancellable       *cancellable,
+                               GError            **error)
 {
   guint64 v;
   
@@ -558,9 +628,9 @@ g_data_input_stream_read_uint64 (GDataInputStream        *stream,
 
 static gssize
 scan_for_newline (GDataInputStream *stream,
-                 gsize *checked_out,
-                 gboolean *last_saw_cr_out,
-                 int *newline_len_out)
+                 gsize            *checked_out,
+                 gboolean         *last_saw_cr_out,
+                 int              *newline_len_out)
 {
   GBufferedInputStream *bstream;
   GDataInputStreamPrivate *priv;
@@ -582,7 +652,7 @@ scan_for_newline (GDataInputStream *stream,
   newline_len = 0;
   
   start = checked;
-  buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
+  buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
   end = available;
   peeked = end - start;
 
@@ -668,14 +738,14 @@ scan_for_newline (GDataInputStream *stream,
  * triggering the cancellable object from another thread. If the operation
  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
  * 
- * Returns: a string with the line that was read in. Set @length to 
- * a #gsize to get the length of the read line. Returns %NULL on an error.
+ * Returns: a string with the line that was read in (including the newlines).
+ * Set @length to a #gsize to get the length of the read line. Returns %NULL on an error.
  **/
 char *
-g_data_input_stream_read_line (GDataInputStream        *stream,
-                             gsize                   *length,
-                             GCancellable            *cancellable,
-                             GError                 **error)
+g_data_input_stream_read_line (GDataInputStream  *stream,
+                              gsize             *length,
+                              GCancellable      *cancellable,
+                              GError           **error)
 {
   GBufferedInputStream *bstream;
   gsize checked;
@@ -729,7 +799,7 @@ g_data_input_stream_read_line (GDataInputStream        *stream,
                             NULL, NULL);
   if (length)
     *length = (gsize)found_pos;
-  g_assert (res == found_pos + newline_len);
+  g_warn_if_fail (res == found_pos + newline_len);
   line[found_pos] = 0;
   
   return line;
@@ -738,8 +808,8 @@ g_data_input_stream_read_line (GDataInputStream        *stream,
 
 static gssize
 scan_for_chars (GDataInputStream *stream,
-               gsize *checked_out,
-               const char *stop_chars)
+               gsize            *checked_out,
+               const char       *stop_chars)
 {
   GBufferedInputStream *bstream;
   GDataInputStreamPrivate *priv;
@@ -758,7 +828,7 @@ scan_for_chars (GDataInputStream *stream,
   found_pos = -1;
   
   start = checked;
-  buffer = g_buffered_input_stream_peek_buffer (bstream, &available) + start;
+  buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
   end = available;
   peeked = end - start;
 
@@ -793,11 +863,11 @@ scan_for_chars (GDataInputStream *stream,
  * of the string. This function will return %NULL on an error.
  **/
 char *
-g_data_input_stream_read_until (GDataInputStream        *stream,
-                              const gchar             *stop_chars,
-                              gsize                   *length,
-                              GCancellable            *cancellable,
-                              GError                 **error)
+g_data_input_stream_read_until (GDataInputStream  *stream,
+                              const gchar        *stop_chars,
+                              gsize              *length,
+                              GCancellable       *cancellable,
+                              GError            **error)
 {
   GBufferedInputStream *bstream;
   gsize checked;
@@ -849,8 +919,11 @@ g_data_input_stream_read_until (GDataInputStream        *stream,
                             NULL, NULL);
   if (length)
     *length = (gsize)found_pos;
-  g_assert (res == found_pos + stop_char_len);
+  g_warn_if_fail (res == found_pos + stop_char_len);
   data_until[found_pos] = 0;
   
   return data_until;
 }
+
+#define __G_DATA_INPUT_STREAM_C__
+#include "gioaliasdef.c"