LCOV - code coverage report
Current view: top level - glib/gio - gsimpleiostream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 40 56 71.4 %
Date: 2024-03-26 05:16:46 Functions: 10 11 90.9 %
Branches: 9 16 56.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright © 2014 NICE s.r.l.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General
      17                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  *
      19                 :            :  * Authors: Ignacio Casal Quinteiro <ignacio.casal@nice-software.com>
      20                 :            :  */
      21                 :            : 
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : #include <glib.h>
      25                 :            : #include "glibintl.h"
      26                 :            : 
      27                 :            : #include "gsimpleiostream.h"
      28                 :            : #include "gtask.h"
      29                 :            : 
      30                 :            : /**
      31                 :            :  * GSimpleIOStream:
      32                 :            :  *
      33                 :            :  * `GSimpleIOStream` creates a [class@Gio.IOStream] from an arbitrary
      34                 :            :  * [class@Gio.InputStream] and [class@Gio.OutputStream]. This allows any pair of
      35                 :            :  * input and output streams to be used with [class@Gio.IOStream] methods.
      36                 :            :  *
      37                 :            :  * This is useful when you obtained a [class@Gio.InputStream] and a
      38                 :            :  * [class@Gio.OutputStream] by other means, for instance creating them with
      39                 :            :  * platform specific methods as
      40                 :            :  * [`g_unix_input_stream_new()`](../gio-unix/ctor.UnixInputStream.new.html)
      41                 :            :  * (from `gio-unix-2.0.pc` / `GioUnix-2.0`), and you want to
      42                 :            :  * take advantage of the methods provided by [class@Gio.IOStream].
      43                 :            :  *
      44                 :            :  * Since: 2.44
      45                 :            :  */
      46                 :            : struct _GSimpleIOStream
      47                 :            : {
      48                 :            :   GIOStream parent;
      49                 :            : 
      50                 :            :   GInputStream *input_stream;
      51                 :            :   GOutputStream *output_stream;
      52                 :            : };
      53                 :            : 
      54                 :            : struct _GSimpleIOStreamClass
      55                 :            : {
      56                 :            :   GIOStreamClass parent;
      57                 :            : };
      58                 :            : 
      59                 :            : typedef struct _GSimpleIOStreamClass GSimpleIOStreamClass;
      60                 :            : 
      61                 :            : enum
      62                 :            : {
      63                 :            :   PROP_0,
      64                 :            :   PROP_INPUT_STREAM,
      65                 :            :   PROP_OUTPUT_STREAM
      66                 :            : };
      67                 :            : 
      68   [ +  +  +  -  :        158 : G_DEFINE_TYPE (GSimpleIOStream, g_simple_io_stream, G_TYPE_IO_STREAM)
                   +  + ]
      69                 :            : 
      70                 :            : static void
      71                 :          4 : g_simple_io_stream_finalize (GObject *object)
      72                 :            : {
      73                 :          4 :   GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
      74                 :            : 
      75         [ +  - ]:          4 :   if (stream->input_stream)
      76                 :          4 :     g_object_unref (stream->input_stream);
      77                 :            : 
      78         [ +  - ]:          4 :   if (stream->output_stream)
      79                 :          4 :     g_object_unref (stream->output_stream);
      80                 :            : 
      81                 :          4 :   G_OBJECT_CLASS (g_simple_io_stream_parent_class)->finalize (object);
      82                 :          4 : }
      83                 :            : 
      84                 :            : static void
      85                 :          8 : g_simple_io_stream_set_property (GObject      *object,
      86                 :            :                                  guint         prop_id,
      87                 :            :                                  const GValue *value,
      88                 :            :                                  GParamSpec   *pspec)
      89                 :            : {
      90                 :          8 :   GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
      91                 :            : 
      92      [ +  +  - ]:          8 :   switch (prop_id)
      93                 :            :     {
      94                 :          4 :     case PROP_INPUT_STREAM:
      95                 :          4 :       stream->input_stream = g_value_dup_object (value);
      96                 :          4 :       break;
      97                 :            : 
      98                 :          4 :     case PROP_OUTPUT_STREAM:
      99                 :          4 :       stream->output_stream = g_value_dup_object (value);
     100                 :          4 :       break;
     101                 :            : 
     102                 :          0 :     default:
     103                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     104                 :          0 :       break;
     105                 :            :     }
     106                 :          8 : }
     107                 :            : 
     108                 :            : static void
     109                 :          0 : g_simple_io_stream_get_property (GObject    *object,
     110                 :            :                                  guint       prop_id,
     111                 :            :                                  GValue     *value,
     112                 :            :                                  GParamSpec *pspec)
     113                 :            : {
     114                 :          0 :   GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
     115                 :            : 
     116      [ #  #  # ]:          0 :   switch (prop_id)
     117                 :            :     {
     118                 :          0 :     case PROP_INPUT_STREAM:
     119                 :          0 :       g_value_set_object (value, stream->input_stream);
     120                 :          0 :       break;
     121                 :            : 
     122                 :          0 :     case PROP_OUTPUT_STREAM:
     123                 :          0 :       g_value_set_object (value, stream->output_stream);
     124                 :          0 :       break;
     125                 :            : 
     126                 :          0 :     default:
     127                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     128                 :          0 :       break;
     129                 :            :     }
     130                 :          0 : }
     131                 :            : 
     132                 :            : static GInputStream *
     133                 :          7 : g_simple_io_stream_get_input_stream (GIOStream *stream)
     134                 :            : {
     135                 :          7 :   GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
     136                 :            : 
     137                 :          7 :   return simple_stream->input_stream;
     138                 :            : }
     139                 :            : 
     140                 :            : static GOutputStream *
     141                 :          9 : g_simple_io_stream_get_output_stream (GIOStream *stream)
     142                 :            : {
     143                 :          9 :   GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
     144                 :            : 
     145                 :          9 :   return simple_stream->output_stream;
     146                 :            : }
     147                 :            : 
     148                 :            : static void
     149                 :          2 : g_simple_io_stream_class_init (GSimpleIOStreamClass *class)
     150                 :            : {
     151                 :          2 :   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
     152                 :          2 :   GIOStreamClass *io_class = G_IO_STREAM_CLASS (class);
     153                 :            : 
     154                 :          2 :   gobject_class->finalize = g_simple_io_stream_finalize;
     155                 :          2 :   gobject_class->get_property = g_simple_io_stream_get_property;
     156                 :          2 :   gobject_class->set_property = g_simple_io_stream_set_property;
     157                 :            : 
     158                 :          2 :   io_class->get_input_stream = g_simple_io_stream_get_input_stream;
     159                 :          2 :   io_class->get_output_stream = g_simple_io_stream_get_output_stream;
     160                 :            : 
     161                 :            :   /**
     162                 :            :    * GSimpleIOStream:input-stream:
     163                 :            :    *
     164                 :            :    * The [class@Gio.InputStream] to read from.
     165                 :            :    *
     166                 :            :    * Since: 2.44
     167                 :            :    */
     168                 :          2 :   g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
     169                 :            :                                    g_param_spec_object ("input-stream", NULL, NULL,
     170                 :            :                                                         G_TYPE_INPUT_STREAM,
     171                 :            :                                                         G_PARAM_READWRITE |
     172                 :            :                                                         G_PARAM_STATIC_STRINGS |
     173                 :            :                                                         G_PARAM_CONSTRUCT_ONLY));
     174                 :            : 
     175                 :            :   /**
     176                 :            :    * GSimpleIOStream:output-stream:
     177                 :            :    *
     178                 :            :    * The [class@Gio.OutputStream] to write to.
     179                 :            :    *
     180                 :            :    * Since: 2.44
     181                 :            :    */
     182                 :          2 :   g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
     183                 :            :                                    g_param_spec_object ("output-stream", NULL, NULL,
     184                 :            :                                                         G_TYPE_OUTPUT_STREAM,
     185                 :            :                                                         G_PARAM_READWRITE |
     186                 :            :                                                         G_PARAM_STATIC_STRINGS |
     187                 :            :                                                         G_PARAM_CONSTRUCT_ONLY));
     188                 :          2 : }
     189                 :            : 
     190                 :            : static void
     191                 :          4 : g_simple_io_stream_init (GSimpleIOStream *stream)
     192                 :            : {
     193                 :          4 : }
     194                 :            : 
     195                 :            : /**
     196                 :            :  * g_simple_io_stream_new:
     197                 :            :  * @input_stream: a #GInputStream.
     198                 :            :  * @output_stream: a #GOutputStream.
     199                 :            :  *
     200                 :            :  * Creates a new #GSimpleIOStream wrapping @input_stream and @output_stream.
     201                 :            :  * See also #GIOStream.
     202                 :            :  *
     203                 :            :  * Returns: a new #GSimpleIOStream instance.
     204                 :            :  *
     205                 :            :  * Since: 2.44
     206                 :            :  */
     207                 :            : GIOStream *
     208                 :          4 : g_simple_io_stream_new (GInputStream  *input_stream,
     209                 :            :                         GOutputStream *output_stream)
     210                 :            : {
     211                 :          4 :   return g_object_new (G_TYPE_SIMPLE_IO_STREAM,
     212                 :            :                        "input-stream", input_stream,
     213                 :            :                        "output-stream", output_stream,
     214                 :            :                        NULL);
     215                 :            : }

Generated by: LCOV version 1.14