Blame gio/gsimpleiostream.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2014 NICE s.r.l.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Ignacio Casal Quinteiro <ignacio.casal@nice-software.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <glib.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include "gsimpleiostream.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gsimpleiostream
Packit ae235b
 * @short_description: A wrapper around an input and an output stream.
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GIOStream
Packit ae235b
 *
Packit ae235b
 * GSimpleIOStream creates a #GIOStream from an arbitrary #GInputStream and
Packit ae235b
 * #GOutputStream. This allows any pair of input and output streams to be used
Packit ae235b
 * with #GIOStream methods.
Packit ae235b
 *
Packit ae235b
 * This is useful when you obtained a #GInputStream and a #GOutputStream
Packit ae235b
 * by other means, for instance creating them with platform specific methods as
Packit ae235b
 * g_unix_input_stream_new() or g_win32_input_stream_new(), and you want
Packit ae235b
 * to take advantage of the methods provided by #GIOStream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GSimpleIOStream:
Packit ae235b
 *
Packit ae235b
 * A wrapper around a #GInputStream and a #GOutputStream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
struct _GSimpleIOStream
Packit ae235b
{
Packit ae235b
  GIOStream parent;
Packit ae235b
Packit ae235b
  GInputStream *input_stream;
Packit ae235b
  GOutputStream *output_stream;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GSimpleIOStreamClass
Packit ae235b
{
Packit ae235b
  GIOStreamClass parent;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef struct _GSimpleIOStreamClass GSimpleIOStreamClass;
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_INPUT_STREAM,
Packit ae235b
  PROP_OUTPUT_STREAM
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GSimpleIOStream, g_simple_io_stream, G_TYPE_IO_STREAM)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_io_stream_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
Packit ae235b
Packit ae235b
  if (stream->input_stream)
Packit ae235b
    g_object_unref (stream->input_stream);
Packit ae235b
Packit ae235b
  if (stream->output_stream)
Packit ae235b
    g_object_unref (stream->output_stream);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_simple_io_stream_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_io_stream_set_property (GObject      *object,
Packit ae235b
                                 guint         prop_id,
Packit ae235b
                                 const GValue *value,
Packit ae235b
                                 GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_INPUT_STREAM:
Packit ae235b
      stream->input_stream = g_value_dup_object (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_OUTPUT_STREAM:
Packit ae235b
      stream->output_stream = g_value_dup_object (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_io_stream_get_property (GObject    *object,
Packit ae235b
                                 guint       prop_id,
Packit ae235b
                                 GValue     *value,
Packit ae235b
                                 GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_INPUT_STREAM:
Packit ae235b
      g_value_set_object (value, stream->input_stream);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_OUTPUT_STREAM:
Packit ae235b
      g_value_set_object (value, stream->output_stream);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GInputStream *
Packit ae235b
g_simple_io_stream_get_input_stream (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
Packit ae235b
Packit ae235b
  return simple_stream->input_stream;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GOutputStream *
Packit ae235b
g_simple_io_stream_get_output_stream (GIOStream *stream)
Packit ae235b
{
Packit ae235b
  GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
Packit ae235b
Packit ae235b
  return simple_stream->output_stream;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_io_stream_class_init (GSimpleIOStreamClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Packit ae235b
  GIOStreamClass *io_class = G_IO_STREAM_CLASS (class);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_simple_io_stream_finalize;
Packit ae235b
  gobject_class->get_property = g_simple_io_stream_get_property;
Packit ae235b
  gobject_class->set_property = g_simple_io_stream_set_property;
Packit ae235b
Packit ae235b
  io_class->get_input_stream = g_simple_io_stream_get_input_stream;
Packit ae235b
  io_class->get_output_stream = g_simple_io_stream_get_output_stream;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleIOStream:input-stream:
Packit ae235b
   *
Packit ae235b
   * Since: 2.44
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
Packit ae235b
                                   g_param_spec_object ("input-stream",
Packit ae235b
                                                        P_("Input stream"),
Packit ae235b
                                                        P_("The GInputStream to read from"),
Packit ae235b
                                                        G_TYPE_INPUT_STREAM,
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS |
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GSimpleIOStream:output-stream:
Packit ae235b
   *
Packit ae235b
   * Since: 2.44
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
Packit ae235b
                                   g_param_spec_object ("output-stream",
Packit ae235b
                                                        P_("Output stream"),
Packit ae235b
                                                        P_("The GOutputStream to write to"),
Packit ae235b
                                                        G_TYPE_OUTPUT_STREAM,
Packit ae235b
                                                        G_PARAM_READWRITE |
Packit ae235b
                                                        G_PARAM_STATIC_STRINGS |
Packit ae235b
                                                        G_PARAM_CONSTRUCT_ONLY));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_io_stream_init (GSimpleIOStream *stream)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_io_stream_new:
Packit ae235b
 * @input_stream: a #GInputStream.
Packit ae235b
 * @output_stream: a #GOutputStream.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GSimpleIOStream wrapping @input_stream and @output_stream.
Packit ae235b
 * See also #GIOStream.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GSimpleIOStream instance.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_simple_io_stream_new (GInputStream  *input_stream,
Packit ae235b
                        GOutputStream *output_stream)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_SIMPLE_IO_STREAM,
Packit ae235b
                       "input-stream", input_stream,
Packit ae235b
                       "output-stream", output_stream,
Packit ae235b
                       NULL);
Packit ae235b
}