File: imp_ldraw_library.c

package info (click to toggle)
libg3d 0.0.8-20
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 5,656 kB
  • ctags: 2,803
  • sloc: ansic: 27,295; sh: 9,288; xml: 4,123; makefile: 864; cpp: 13
file content (176 lines) | stat: -rw-r--r-- 4,624 bytes parent folder | download | duplicates (6)
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
/* $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 <string.h>

#include <g3d/object.h>

#include "imp_ldraw_types.h"
#include "imp_ldraw_part.h"
#include "imp_ldraw_color.h"
#include "imp_ldraw_misc.h"

static gboolean ldraw_library_add_dir(LDrawLibrary *lib, const gchar *subdir)
{
	LDrawPart *part;
	const gchar *filename;
	gchar *partdir, *strup;
	GDir *dir;
	GError *error;
	gboolean prefix = FALSE;

	partdir = g_strdup_printf("%s%c%s", lib->libdir, G_DIR_SEPARATOR, subdir);
	dir = g_dir_open(partdir, 0, &error);
	if(dir == NULL) {
		g_warning("LDraw: failed to open directory '%s': %s", partdir,
			error->message);
		g_error_free(error);
		g_free(partdir);
		return FALSE;
	}

	if(path_sep(subdir))
		prefix = TRUE;

	filename = g_dir_read_name(dir);
	while(filename) {
		if(g_ascii_strcasecmp(filename + strlen(filename) - 4, ".dat") == 0) {
#if DEBUG > 3
			g_debug("LDraw: Library: adding '%s'", filename);
#endif
			part = g_new0(LDrawPart, 1);
			part->filename = g_strdup_printf("%s%c%s",
				partdir, G_DIR_SEPARATOR, filename);
			strup = g_ascii_strup(filename, -1);
			if(prefix)
				part->name = g_strdup_printf("%s%c%s",
					path_sep(subdir) + 1,
					G_DIR_SEPARATOR, strup);
			else
				part->name = g_strdup(strup);
			g_free(strup);
			g_hash_table_insert(lib->partdb, part->name, part);
			lib->partlist = g_slist_append(lib->partlist, part);
		}
		filename = g_dir_read_name(dir);
	}
	g_dir_close(dir);
	g_free(partdir);

	return TRUE;
}

LDrawLibrary *ldraw_library_init(void)
{
	LDrawLibrary *lib;
	const gchar *lddir;

	lib = g_new0(LDrawLibrary, 1);
	lib->partdb = g_hash_table_new(g_str_hash, g_str_equal);

	ldraw_color_init(lib);

	lddir = g_getenv("LDRAWDIR");
	if(lddir == NULL) /* warning is issued when trying to load a model */
		return lib;

	lib->libdir = g_strdup(lddir);

	ldraw_library_add_dir(lib, "PARTS");
	ldraw_library_add_dir(lib, "PARTS" G_DIR_SEPARATOR_S "S");
	ldraw_library_add_dir(lib, "P");
	ldraw_library_add_dir(lib, "P" G_DIR_SEPARATOR_S "48");

	return lib;
}

void ldraw_library_cleanup(LDrawLibrary *lib)
{
	GSList *item;
	LDrawPart *part;

	item = lib->partlist;
	while(item != NULL) {
		part = item->data;
		item = g_slist_remove(item, part);
		ldraw_part_free(part);
	}
	g_hash_table_destroy(lib->partdb);
	ldraw_color_cleanup(lib);
	g_free(lib);
}

void ldraw_library_insert(LDrawLibrary *lib, gchar *name, gpointer data)
{
	g_hash_table_insert(lib->partdb, name, data);
}

G3DObject *ldraw_library_lookup(LDrawLibrary *lib, const gchar *name)
{
	LDrawPart *part;
	gchar *strc;

	part = g_hash_table_lookup(lib->partdb, name);
	if(part == NULL) {
		strc = g_ascii_strup(name, -1);
		g_strdelimit(strc, "/\\", G_DIR_SEPARATOR);
		part = g_hash_table_lookup(lib->partdb, strc);
		g_free(strc);
	}
#if 0
	if(part == NULL) {
		strc = g_ascii_strdown(name, -1);
		g_strdelimit(strc, "/\\", G_DIR_SEPARATOR);
		part = g_hash_table_lookup(lib->partdb, strc);
		g_free(strc);
	}
#endif
	if(part == NULL) {
		g_warning("LDraw: failed to find '%s' in library", name);
		return NULL;
	}
	if(part->object == NULL) {
		if(part->stream) {
			/* MPD loader has a custom stream */
			part->object = ldraw_part_get_object(part, lib);
		} else if(part->filename) {
			/* try to load part from standard library */
			part->stream = g3d_stream_open_file(part->filename, "r");
			if(part->stream) {
				part->object = ldraw_part_get_object(part, lib);
				g3d_stream_close(part->stream);
				part->stream = NULL;
			} else {
				g_warning("LDraw: failed to open stream for '%s'",
					part->filename);
			}
		} else {
			g_warning("LDraw: don't know how to open part '%s'", part->name);
		}

		if(part->object == NULL) {
			g_warning("LDraw: failed to load part '%s'", part->name);
			return NULL;
		}
	}
	return g3d_object_duplicate(part->object);
}