aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_def.c
blob: bf310b1d1912671c9a333889be7408bd62e3cb2c (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
/* $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 <glib.h>

#include "imp_acf_def.h"

#define ACF_DEBUG_TYPES 2

AcfFile *acf_def_read(G3DStream *stream, const AcfDef *def,
	gboolean bigendian)
{
	AcfFile *acf;
	AcfValue *value;
	gint32 i, j;

	acf = g_new0(AcfFile, 1);
	acf->db = g_hash_table_new(g_str_hash, g_str_equal);

	for(i = 0; def[i].type != XEOF; i ++) {
		value = g_new0(AcfValue, 1);
		value->name = g_strdup(def[i].description);
		value->type = def[i].type;
		value->num = def[i].num;
		switch(value->type) {
			case XCHR:
				value->xchr = g_new0(gchar, value->num + 1);
				g3d_stream_read(stream, value->xchr, value->num);
#if DEBUG > ACF_DEBUG_TYPES
				g_debug("ACF: XCHR: %s = %s", value->name, value->xchr);
#endif
				break;
			case XINT:
				value->xint = g_new0(gint32, value->num);
				for(j = 0; j < value->num; j ++)
					value->xint[j] = bigendian ?
						g3d_stream_read_int32_be(stream) :
						g3d_stream_read_int32_le(stream);
#if DEBUG > ACF_DEBUG_TYPES
				g_debug("ACF: XINT: %s(1/%d) = %i", value->name, value->num,
					value->xint[0]);
#endif
				break;
			case XFLT:
				value->xflt = g_new0(G3DFloat, value->num);
				for(j = 0; j < value->num; j ++)
					value->xflt[j] = bigendian ?
						g3d_stream_read_float_be(stream) :
						g3d_stream_read_float_le(stream);
#if DEBUG > ACF_DEBUG_TYPES
				g_debug("ACF: XFLT: %s(1/%d) = %f", value->name, value->num,
					value->xflt[0]);
#endif
				break;
			case XEOF:
				/* should never happen, just make compiler happy */
				break;
		}
		g_hash_table_insert(acf->db, def[i].description, value);
	}

	return acf;
}

static gboolean acf_def_remove_value_cb(gpointer key, gpointer hashvalue,
	gpointer data)
{
	AcfValue *value = hashvalue;

	if(value->xchr)
		g_free(value->xchr);
	if(value->xint)
		g_free(value->xint);
	if(value->xflt)
		g_free(value->xflt);
	g_free(value->name);
	g_free(value);
	return TRUE;
}

void acf_def_free(AcfFile *acf)
{
	g_hash_table_foreach_remove(acf->db, acf_def_remove_value_cb, NULL);
	g_free(acf);
}

AcfValue *acf_def_lookup(AcfFile *acf, const gchar *name)
{
	return g_hash_table_lookup(acf->db, name);
}

void acf_def_dump(AcfValue *value)
{
	gint32 i;

	if(value->type == XCHR) {
		g_debug("ACF: %s: %s", value->name, value->xchr);
		return;
	}

	g_debug("ACF: %s: dumping %d %s items", value->name, value->num,
		(value->type == XINT) ? "XINT" : "XFLT");
	for(i = 0; i < value->num; i ++) {
		if(value->type == XINT)
			g_debug("\tXINT: %s[%i] = %i", value->name, i, value->xint[i]);
		else if(value->type == XFLT)
			g_debug("\tXFLT: %s[%i] = %f", value->name, i, value->xflt[i]);
	}
}