summaryrefslogtreecommitdiff
path: root/encode_converter.c
blob: e11335186ad0b2540a9046a3b3e7e47d09da5e24 (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
#include "encode_converter.h"

#include <gio/gio.h>

static void encode_converter_interface_init(GConverterIface *iface);
static void encode_converter_finalize(GObject *object);

static GConverterResult encoder_converter_convert(GConverter *converter,
		const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size,
		GConverterFlags flags, gsize *bytes_read, gsize *bytes_written,
		GError **error);
static void encode_converter_reset(GConverter *converter);

static GObjectClass *parent_class = NULL;

G_DEFINE_TYPE_WITH_CODE(EncodeConverter, encode_converter, G_TYPE_OBJECT,
		G_IMPLEMENT_INTERFACE(G_TYPE_CONVERTER,
			encode_converter_interface_init));

static void encode_converter_class_init(EncodeConverterClass *klass) {
	GObjectClass *object_class;

	parent_class = (GObjectClass*)g_type_class_peek_parent(klass);
	object_class = (GObjectClass*)klass;
	object_class->finalize = encode_converter_finalize;
}

static void encode_converter_interface_init(GConverterIface *iface) {
	iface->convert = encoder_converter_convert;
	iface->reset = encode_converter_reset;
}

static void encode_converter_init(EncodeConverter *self) {
}

static void encode_converter_finalize(GObject *object) {
	EncodeConverter *dc;

	g_assert(CONVERTER_IS_ENCODE(object));

	dc = ENCODE_CONVERTER(object);

	dc->encoder.encoder->close(dc->encoder.data);
}

static GConverterResult encoder_converter_convert(GConverter *converter,
		const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size,
		GConverterFlags flags, gsize *bytes_read, gsize *bytes_written,
		GError **error) {
	EncodeConverter *dc;

	dc = (EncodeConverter*)converter;

	gssize size = dc->encoder.encoder->encode(dc->encoder.data, inbuf, inbuf_size, outbuf, outbuf_size);

	if(size < 0) {
		*error = g_error_new(g_quark_from_string("EncodeConverter"),
				size, "encode() returned %ld", size);
		return G_CONVERTER_ERROR;
	} else if(size == 0) {
		return G_CONVERTER_FINISHED;
	}
	*bytes_read = inbuf_size;
	*bytes_written = size;
	return G_CONVERTER_CONVERTED;
}

static void encode_converter_reset(GConverter *converter) {
	EncodeConverter *ec;

	ec = (EncodeConverter*)converter;

	ec->encoder.encoder->close(&ec->encoder.data);
}

EncodeConverter *encode_converter_new(const struct encoder_plugin *encoder_plugin) {
	EncodeConverter *dc;

	dc = (EncodeConverter*)g_object_new(CONVERTER_TYPE_ENCODE, NULL);

	g_assert(dc != NULL);

	dc->encoder.encoder = encoder_plugin;
	dc->encoder.encoder->init(&dc->encoder.data);

	return dc;
}