aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/stream_gsf_class.c
blob: ff127bf540793a650625092ea756f6d0117073ba (plain)
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
/* $Id:$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library 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.1 of the License, or (at your option) any later version.

    This library 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 library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <gsf/gsf-infile.h>
#include <gsf/gsf-infile-impl.h>
#include <gsf/gsf-impl-utils.h>

#include <g3d/types.h>
#include <g3d/stream.h>

#include "stream_gsf_class.h"

static GObjectClass *parent_class;

struct _G3DGsfInputStream {
	GsfInput input;
	G3DStream *stream;
	guint8 *buf;
	gsize buf_size;
};

typedef struct {
	GsfInputClass input_class;
} G3DGsfInputStreamClass;

EAPI
GsfInput *g3d_gsf_input_stream_new(G3DStream *stream)
{
	G3DGsfInputStream *g3dsf;

	g3dsf = g_object_new(G3D_GSF_INPUT_STREAM_TYPE, NULL);
	g3dsf->stream = stream;

	gsf_input_set_size(GSF_INPUT(g3dsf), g3d_stream_size(stream));
	gsf_input_set_name_from_filename(GSF_INPUT(g3dsf), stream->uri);

	return GSF_INPUT(g3dsf);
}

EAPI G3DStream *g3d_gsf_input_stream_get_stream(GsfInput *input)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(input);

	return g3dsf->stream;
}

static void g3d_gsf_input_stream_finalize(GObject *obj)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(obj);

	if(g3dsf->buf)
		g_free(g3dsf->buf);

	parent_class->finalize(obj);
}

static GsfInput *g3d_gsf_input_stream_dup(GsfInput *src_input, GError **err)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(src_input);

	return g3d_gsf_input_stream_new(g3dsf->stream);
}

static const guint8 *g3d_gsf_input_stream_read(GsfInput *input,
	size_t num_bytes, guint8 *buffer)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(input);

	if(buffer == NULL) {
		if(g3dsf->buf_size < num_bytes) {
			g3dsf->buf_size = num_bytes;
			if(g3dsf->buf)
				g_free(g3dsf->buf);
			g3dsf->buf = g_new0(guint8, num_bytes);
		}
		buffer = g3dsf->buf;
	}

	g3d_stream_read(g3dsf->stream, buffer, num_bytes);
	return buffer;
}

static gboolean g3d_gsf_input_stream_seek(GsfInput *input, gsf_off_t offset,
	GSeekType whence)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(input);

	return g3d_stream_seek(g3dsf->stream, offset, whence);
}

static void g3d_gsf_input_stream_init(GObject *obj)
{
	G3DGsfInputStream *g3dsf = G3D_GSF_INPUT_STREAM(obj);

	g3dsf->stream = NULL;
	g3dsf->buf = NULL;
	g3dsf->buf_size = 0;
}

static void g3d_gsf_input_stream_class_init(GObjectClass *gobject_class)
{
	GsfInputClass *input_class = GSF_INPUT_CLASS(gobject_class);

	gobject_class->finalize = g3d_gsf_input_stream_finalize;
	input_class->Dup  = g3d_gsf_input_stream_dup;
	input_class->Read = g3d_gsf_input_stream_read;
	input_class->Seek = g3d_gsf_input_stream_seek;

	parent_class = g_type_class_peek_parent(gobject_class);
}

GSF_CLASS(G3DGsfInputStream,
	g3d_gsf_input_stream,
	g3d_gsf_input_stream_class_init,
	g3d_gsf_input_stream_init,
	GSF_INPUT_TYPE)