File: imp_dae_library.c

package info (click to toggle)
libg3d 0.0.8-26
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,700 kB
  • sloc: ansic: 27,295; sh: 9,288; xml: 4,123; makefile: 865; cpp: 13
file content (168 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download | duplicates (5)
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
/* $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 <libxml/tree.h>

#include "imp_dae_xml.h"
#include "imp_dae_library.h"

struct _DaeLibrary {
	GHashTable *ids;
	GSList *libs;
};

typedef struct {
	GHashTable *ids;
	GSList *nodes;
} DaeLibraryNodes;

static gchar *dae_library_names[][2] = {
	{ "library_animations",     "animation"     },
	{ "library_cameras",        "camera"        },
	{ "library_controllers",    "controller"    },
	{ "library_effects",        "effect"        },
	{ "library_geometries",     "geometry"      },
	{ "library_images",         "image"         },
	{ "library_lights",         "light"         },
	{ "library_materials",      "material"      },
	{ "library_nodes",          "node"          },
	{ "library_physics_scenes", "physics_scene" },
	{ "library_visual_scenes",  "visual_scene"  },
	{ NULL, NULL }
};

static gboolean dae_add_library(DaeLibrary *lib, xmlNodePtr libnode,
	guint32 entryid)
{
	xmlNodePtr node;
	DaeLibraryNodes *nodelib;
	gchar *id;

	nodelib = g_new0(DaeLibraryNodes, 1);
	nodelib->ids = g_hash_table_new_full(
		g_str_hash, g_str_equal, g_free, NULL);
	g_hash_table_insert(lib->ids, g_strdup(dae_library_names[entryid][1]),
		nodelib);
	lib->libs = g_slist_append(lib->libs, nodelib);

	node = libnode->children;
	while(node != NULL) {
		if((node->type == XML_ELEMENT_NODE) &&
			(xmlStrcmp(node->name,
				(const xmlChar *)dae_library_names[entryid][1]) == 0)) {
			/* found library entry */
			id = dae_xml_get_attr(node, "id");
			if(id != NULL) {
#if DEBUG > 2
				g_debug("\t%s id=\"%s\"", dae_library_names[entryid][1], id);
#endif
				g_hash_table_insert(nodelib->ids, id, node);
				nodelib->nodes = g_slist_append(nodelib->nodes, node);
			}
		}
		node = node->next;
	}
	return TRUE;
}

gboolean dae_library_add(DaeLibrary *lib, const gchar *libname,
	const gchar *id, xmlNodePtr node)
{
	DaeLibraryNodes *nodelib;

	g_return_val_if_fail(lib != NULL, FALSE);

	nodelib = g_hash_table_lookup(lib->ids, libname);
	if(nodelib == NULL) {
		nodelib = g_new0(DaeLibraryNodes, 1);
		nodelib->ids = g_hash_table_new_full(
			g_str_hash, g_str_equal, g_free, NULL);
		g_hash_table_insert(lib->ids, g_strdup(libname), nodelib);
		lib->libs = g_slist_append(lib->libs, nodelib);
	}
	g_hash_table_insert(nodelib->ids, g_strdup(id), node);
	nodelib->nodes = g_slist_append(nodelib->nodes, node);
	return TRUE;
}

DaeLibrary *dae_library_load(xmlDocPtr xmldoc)
{
	DaeLibrary *lib;
	gint i = 0;
	xmlNodePtr rootnode, node;

	lib = g_new0(DaeLibrary, 1);
	lib->ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	rootnode = xmlDocGetRootElement(xmldoc);
	g_return_val_if_fail(rootnode != NULL, NULL);

	while(dae_library_names[i][0] != NULL) {
#if DEBUG > 2
		g_debug("loading library %s", dae_library_names[i][0]);
#endif
		node = rootnode->children;
		while(node != NULL) {
			if((node->type == XML_ELEMENT_NODE) &&
				(xmlStrcmp(node->name,
					(const xmlChar *)dae_library_names[i][0]) == 0)) {
				dae_add_library(lib, node, i);
				break;
			}
			node = node->next;
		}
		i ++;
	}
	return lib;
}

xmlNodePtr dae_library_lookup(DaeLibrary *library, const gchar *tagname,
	const gchar *id)
{
	DaeLibraryNodes *nodelib;

	nodelib = g_hash_table_lookup(library->ids, tagname);
	if(nodelib == NULL) {
		g_warning("DAE: failed to lookup library for '%s'", tagname);
		return NULL;
	}
	return (xmlNodePtr)g_hash_table_lookup(nodelib->ids, id);
}

void dae_library_cleanup(DaeLibrary *library)
{
	DaeLibraryNodes *nodelib;
	GSList *item;

	for(item = library->libs; item != NULL; item = item->next) {
		nodelib = (DaeLibraryNodes *)item->data;
		g_hash_table_destroy(nodelib->ids);
		g_slist_free(nodelib->nodes);
		g_free(nodelib);
	}

	g_hash_table_destroy(library->ids);
	g_slist_free(library->libs);
	g_free(library);
}