aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c
new file mode 100644
index 0000000..aa07cdb
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ldraw/imp_ldraw_library.c
@@ -0,0 +1,175 @@
1/* $Id$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22#include <string.h>
23
24#include <g3d/object.h>
25
26#include "imp_ldraw_types.h"
27#include "imp_ldraw_part.h"
28#include "imp_ldraw_color.h"
29#include "imp_ldraw_misc.h"
30
31static gboolean ldraw_library_add_dir(LDrawLibrary *lib, const gchar *subdir)
32{
33 LDrawPart *part;
34 const gchar *filename;
35 gchar *partdir, *strup;
36 GDir *dir;
37 GError *error;
38 gboolean prefix = FALSE;
39
40 partdir = g_strdup_printf("%s%c%s", lib->libdir, G_DIR_SEPARATOR, subdir);
41 dir = g_dir_open(partdir, 0, &error);
42 if(dir == NULL) {
43 g_warning("LDraw: failed to open directory '%s': %s", partdir,
44 error->message);
45 g_error_free(error);
46 g_free(partdir);
47 return FALSE;
48 }
49
50 if(path_sep(subdir))
51 prefix = TRUE;
52
53 filename = g_dir_read_name(dir);
54 while(filename) {
55 if(g_ascii_strcasecmp(filename + strlen(filename) - 4, ".dat") == 0) {
56#if DEBUG > 3
57 g_debug("LDraw: Library: adding '%s'", filename);
58#endif
59 part = g_new0(LDrawPart, 1);
60 part->filename = g_strdup_printf("%s%c%s",
61 partdir, G_DIR_SEPARATOR, filename);
62 strup = g_ascii_strup(filename, -1);
63 if(prefix)
64 part->name = g_strdup_printf("%s%c%s",
65 path_sep(subdir) + 1,
66 G_DIR_SEPARATOR, strup);
67 else
68 part->name = g_strdup(strup);
69 g_free(strup);
70 g_hash_table_insert(lib->partdb, part->name, part);
71 lib->partlist = g_slist_append(lib->partlist, part);
72 }
73 filename = g_dir_read_name(dir);
74 }
75 g_dir_close(dir);
76 g_free(partdir);
77
78 return TRUE;
79}
80
81LDrawLibrary *ldraw_library_init(void)
82{
83 LDrawLibrary *lib;
84 const gchar *lddir;
85
86 lib = g_new0(LDrawLibrary, 1);
87 lib->partdb = g_hash_table_new(g_str_hash, g_str_equal);
88
89 ldraw_color_init(lib);
90
91 lddir = g_getenv("LDRAWDIR");
92 if(lddir == NULL) /* warning is issued when trying to load a model */
93 return lib;
94
95 lib->libdir = g_strdup(lddir);
96
97 ldraw_library_add_dir(lib, "PARTS");
98 ldraw_library_add_dir(lib, "PARTS" G_DIR_SEPARATOR_S "S");
99 ldraw_library_add_dir(lib, "P");
100 ldraw_library_add_dir(lib, "P" G_DIR_SEPARATOR_S "48");
101
102 return lib;
103}
104
105void ldraw_library_cleanup(LDrawLibrary *lib)
106{
107 GSList *item;
108 LDrawPart *part;
109
110 item = lib->partlist;
111 while(item != NULL) {
112 part = item->data;
113 item = g_slist_remove(item, part);
114 ldraw_part_free(part);
115 }
116 g_hash_table_destroy(lib->partdb);
117 g_free(lib);
118}
119
120void ldraw_library_insert(LDrawLibrary *lib, gchar *name, gpointer data)
121{
122 g_hash_table_insert(lib->partdb, name, data);
123}
124
125G3DObject *ldraw_library_lookup(LDrawLibrary *lib, const gchar *name)
126{
127 LDrawPart *part;
128 gchar *strc;
129
130 part = g_hash_table_lookup(lib->partdb, name);
131 if(part == NULL) {
132 strc = g_ascii_strup(name, -1);
133 g_strdelimit(strc, "/\\", G_DIR_SEPARATOR);
134 part = g_hash_table_lookup(lib->partdb, strc);
135 g_free(strc);
136 }
137#if 0
138 if(part == NULL) {
139 strc = g_ascii_strdown(name, -1);
140 g_strdelimit(strc, "/\\", G_DIR_SEPARATOR);
141 part = g_hash_table_lookup(lib->partdb, strc);
142 g_free(strc);
143 }
144#endif
145 if(part == NULL) {
146 g_warning("LDraw: failed to find '%s' in library", name);
147 return NULL;
148 }
149 if(part->object == NULL) {
150 if(part->stream) {
151 /* MPD loader has a custom stream */
152 part->object = ldraw_part_get_object(part, lib);
153 } else if(part->filename) {
154 /* try to load part from standard library */
155 part->stream = g3d_stream_open_file(part->filename, "r");
156 if(part->stream) {
157 part->object = ldraw_part_get_object(part, lib);
158 g3d_stream_close(part->stream);
159 part->stream = NULL;
160 } else {
161 g_warning("LDraw: failed to open stream for '%s'",
162 part->filename);
163 }
164 } else {
165 g_warning("LDraw: don't know how to open part '%s'", part->name);
166 }
167
168 if(part->object == NULL) {
169 g_warning("LDraw: failed to load part '%s'", part->name);
170 return NULL;
171 }
172 }
173 return g3d_object_duplicate(part->object);
174}
175