1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/* A byte source/sink .. it can be a pipe, file descriptor, memory area,
* socket, node.js stream, etc.
*
* J.Cupitt, 19/6/14
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
/**
* VipsConnection:
*
* An abstract base class representing a source or sink of bytes.
*
* It can be connected to a network socket, for example, or perhaps
* a Node.js stream, or to an area of memory. This allows it to support
* operations like JPEG loading, see for example [ctor@Image.jpegload_source].
*
* Subclass to add other input sources. Use [class@SourceCustom] and
* [class@TargetCustom] to make a source or target with action signals.
* These classes provide action signals such as:
*
* - [signal@SourceCustom::read] for reading data from a custom source.
* - [signal@SourceCustom::seek] for seeking within a data stream.
* - [signal@TargetCustom::write] for writing data to a custom target.
*/
G_DEFINE_ABSTRACT_TYPE(VipsConnection, vips_connection, VIPS_TYPE_OBJECT);
static void
vips_connection_finalize(GObject *gobject)
{
VipsConnection *connection = (VipsConnection *) gobject;
#ifdef VIPS_DEBUG
VIPS_DEBUG_MSG("vips_connection_finalize: ");
vips_object_print_name(VIPS_OBJECT(gobject));
VIPS_DEBUG_MSG("\n");
#endif /*VIPS_DEBUG*/
if (connection->tracked_descriptor >= 0) {
VIPS_DEBUG_MSG(" tracked_close()\n");
vips_tracked_close(connection->tracked_descriptor);
connection->tracked_descriptor = -1;
connection->descriptor = -1;
}
if (connection->close_descriptor >= 0) {
VIPS_DEBUG_MSG(" close()\n");
close(connection->close_descriptor);
connection->close_descriptor = -1;
connection->descriptor = -1;
}
VIPS_FREE(connection->filename);
G_OBJECT_CLASS(vips_connection_parent_class)->finalize(gobject);
}
static void
vips_connection_class_init(VipsConnectionClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
gobject_class->finalize = vips_connection_finalize;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
VIPS_ARG_INT(class, "descriptor", 1,
_("Descriptor"),
_("File descriptor for read or write"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsConnection, descriptor),
-1, 1000000000, 0);
VIPS_ARG_STRING(class, "filename", 2,
_("Filename"),
_("Name of file to open"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsConnection, filename),
NULL);
}
static void
vips_connection_init(VipsConnection *connection)
{
connection->descriptor = -1;
connection->tracked_descriptor = -1;
connection->close_descriptor = -1;
}
/**
* vips_connection_filename:
* @connection: connection to operate on
*
* Returns: any filename associated with this connection, or `NULL`.
*/
const char *
vips_connection_filename(VipsConnection *connection)
{
return connection->filename;
}
/**
* vips_connection_nick:
* @connection: connection to operate on
*
* Returns: a string describing this connection which could be displayed to a
* user.
*/
const char *
vips_connection_nick(VipsConnection *connection)
{
return connection->filename ? connection->filename
: VIPS_OBJECT(connection)->nickname;
}
|