File: sourceginput.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (266 lines) | stat: -rw-r--r-- 6,619 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* A Source subclass which wraps a ginputstream.
 */

/*

    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>

#include "vipsmarshal.h"

G_DEFINE_TYPE( VipsSourceGInputStream, vips_source_g_input_stream, 
	VIPS_TYPE_SOURCE );

/* TODO:
 * 	- some more docs
 */

/* This will only be useful for memory and pipe-style streams. It's not
 * possible to get filename or filenos from GInputStream objects. 
 * Without those two bits of information, important VipsSource features like
 * mmap and openslide load will not work.
 *
 * For sources which are files on local disc, you should use
 * vips_source_new_from_file() instead.
 */

static int
vips_source_g_input_stream_build( VipsObject *object )
{
	VipsSource *source = VIPS_SOURCE( object );
	VipsSourceGInputStream *source_ginput = 
		VIPS_SOURCE_G_INPUT_STREAM( source );
	GError *error = NULL;

	VIPS_DEBUG_MSG( "vips_source_g_input_stream_build: %p\n", source );

	if( VIPS_OBJECT_CLASS( vips_source_g_input_stream_parent_class )->
		build( object ) )
		return( -1 );

	if( G_IS_FILE_INPUT_STREAM( source_ginput->stream ) ) {
		const char *name;

		/* It's unclear if this will ever produce useful output.
		 */
		if( !(source_ginput->info = g_file_input_stream_query_info( 
			G_FILE_INPUT_STREAM( source_ginput->stream ), 
			G_FILE_ATTRIBUTE_STANDARD_NAME,
			NULL, &error )) ) {
			vips_g_error( &error );
			return( -1 );
		}

#ifdef VIPS_DEBUG
{
		char **attributes;
		int i;

		/* Swap G_FILE_ATTRIBUTE_STANDARD_NAME above for "*" to get a
		 * list of all available attributes.
		 */
		attributes = g_file_info_list_attributes( 
			source_ginput->info, NULL );
		printf( "stream attributes:\n" );
		for( i = 0; attributes[i]; i++ ) {
			char *name = attributes[i];
			char *value;

			value = g_file_info_get_attribute_as_string( 
				source_ginput->info, name );
			printf( "\t%s = %s\n", name, value );
			g_free( value );
		}
		g_strfreev( attributes );
}
#endif /*VIPS_DEBUG*/

		if( (name = g_file_info_get_name( source_ginput->info )) ) 
			g_object_set( object,
				"filename", name,
				NULL );
	}	

	if( G_IS_SEEKABLE( source_ginput->stream ) &&
		g_seekable_can_seek( G_SEEKABLE( source_ginput->stream ) ) ) 
		source_ginput->seekable = G_SEEKABLE( source_ginput->stream );

	return( 0 );
}

static gint64
vips_source_g_input_stream_read( VipsSource *source, 
	void *buffer, size_t length )
{
	VipsSourceGInputStream *source_ginput = 
		VIPS_SOURCE_G_INPUT_STREAM( source );
	GError *error = NULL;

	gint64 bytes_read;

	VIPS_DEBUG_MSG( "vips_source_g_input_stream_read: %zd bytes\n", 
		length );

	/* Do we need to loop on this call? The docs are unclear.
	 */
	if( (bytes_read = g_input_stream_read( source_ginput->stream, 
		buffer, length, NULL, &error )) < 0 ) {
		VIPS_DEBUG_MSG( "    %s\n", error->message );
		vips_g_error( &error );
		return( -1 );
	}

	VIPS_DEBUG_MSG( "    (returned %zd bytes)\n", bytes_read );

	return( bytes_read );
}

static GSeekType
lseek_to_seek_type( int whence )
{
	switch( whence ) {
	default:
	case SEEK_CUR:
		return( G_SEEK_CUR );
	case SEEK_SET:
		return( G_SEEK_SET );
	case SEEK_END:
		return( G_SEEK_END );
	}
}

static gint64
vips_source_g_input_stream_seek( VipsSource *source, gint64 offset, int whence )
{
	VipsSourceGInputStream *source_ginput = 
		VIPS_SOURCE_G_INPUT_STREAM( source );
	GSeekType type = lseek_to_seek_type( whence );
	GError *error = NULL;

	gint64 new_position;

	VIPS_DEBUG_MSG( "vips_source_g_input_stream_seek: "
		"offset = %zd, whence = %d\n", offset, whence );

	if( source_ginput->seekable ) {
		if( !g_seekable_seek( source_ginput->seekable, 
			offset, type, NULL, &error ) ) {
			vips_g_error( &error );
			return( -1 );
		}

		new_position = g_seekable_tell( source_ginput->seekable );
	}
	else
		new_position = -1;

	VIPS_DEBUG_MSG( "  (new position = %zd)\n", new_position );

	return( new_position );
}

static void
vips_source_g_input_stream_class_init( VipsSourceGInputStreamClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
	VipsSourceClass *source_class = VIPS_SOURCE_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	object_class->nickname = "source_g_input_stream";
	object_class->description = _( "GInputStream source" );

	object_class->build = vips_source_g_input_stream_build;

	source_class->read = vips_source_g_input_stream_read;
	source_class->seek = vips_source_g_input_stream_seek;

	VIPS_ARG_OBJECT( class, "stream", 3, 
		_( "Stream" ),
		_( "GInputStream to read from" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsSourceGInputStream, stream ),
		G_TYPE_INPUT_STREAM );

}

static void
vips_source_g_input_stream_init( VipsSourceGInputStream *source )
{
}

/**
 * vips_source_g_input_stream_new:
 * @stream: read from this stream
 *
 * Create a #VipsSourceGInputStream which wraps @stream.
 *
 * Returns: the new source.
 */
VipsSourceGInputStream *
vips_source_g_input_stream_new( GInputStream *stream )
{
	VipsSourceGInputStream *source;

	VIPS_DEBUG_MSG( "vips_source_g_input_stream_new:\n" );

	source = VIPS_SOURCE_G_INPUT_STREAM( 
		g_object_new( VIPS_TYPE_SOURCE_G_INPUT_STREAM, 
			"stream", stream,
			NULL ) );

	if( vips_object_build( VIPS_OBJECT( source ) ) ) {
		VIPS_UNREF( source );
		return( NULL );
	}

	return( source ); 
}