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