File: g3d-stat.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 (324 lines) | stat: -rw-r--r-- 8,078 bytes parent folder | download | duplicates (8)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* $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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <g3d/g3d.h>
#include <g3d/plugins.h>

typedef struct {
	guint32 n_objects;
	guint32 n_vertices;
	guint32 n_faces;
	guint32 n_textures;
} FileStats;

typedef struct {
	guint32 n_success;
	guint32 n_failure;
	GSList *no_objects;
	GSList *no_faces;
	GSList *no_vertices;
} PluginStats;

typedef struct {
	G3DContext *context;
	gboolean use_color;
	FILE *ftree;
} Config;

static gboolean quit = FALSE;

static void show_help(void);
static Config *get_config(gint *argc, gchar ***argv);
static gboolean dir_stat(Config *config, const char *dirname);
static gboolean file_stat(Config *config, const char *filename,
	gboolean detailed, FileStats *stats, gchar **plugin_used);

static void sighandler(int signal)
{
	quit = TRUE;
	fprintf(stderr, "signal %d received, quitting\n", signal);
}

static void _g_print(const gchar *str)
{
#if 0
	fputs(str, stderr);
#endif
}

#define ANSI_RESET "\033[0m"
#define ANSI_GREEN "\033[32m"
#define ANSI_RED   "\033[31m"

static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
	const gchar *message, gpointer user_data)
{
	Config *config = user_data;

	if(message[0] == '\\') {
		if(config->use_color)
			fprintf(config->ftree, ANSI_GREEN "%s\n" ANSI_RESET, message + 1);
		else
			fprintf(config->ftree, "%s\n", message + 1);
	} else if(message[0] == '|') {
		printf("%s\n", message + 1);
	} else {
		if(config->use_color)
			printf(ANSI_RED "%s\n" ANSI_RESET, message);
		else
			printf("%s\n", message);
	}
}

int main(int argc, char **argv)
{
	Config *config;

	signal(SIGINT, sighandler);
#ifdef SIGQUIT
	signal(SIGQUIT, sighandler);
#endif

	config = get_config(&argc, &argv);

	if(argc < 1) {
		show_help();
		exit(EXIT_FAILURE);
	}

	g_set_print_handler(_g_print);
	g_set_printerr_handler(_g_print);
	g_log_set_handler("LibG3D", G_LOG_LEVEL_MASK, log_handler, config);

	config->context = g3d_context_new();

	if(g_file_test(argv[0], G_FILE_TEST_IS_DIR)) {
		return dir_stat(config, argv[0]);
	}
	else {
		return file_stat(config, argv[0], TRUE, NULL, NULL);
	}
}

static Config *get_config(gint *argc, gchar ***argv)
{
	Config *config;
	guint32 skip = 1; /* skip program name by default */
	gint32 i;
	gchar *opt;

	config = g_new0(Config, 1);
	config->use_color = TRUE;
	config->ftree = stdout;

	for(i = 1; i < *argc; i ++) {
		if(strncmp((*argv)[i], "--", 2) != 0)
			break;
		skip ++;
		opt = (*argv)[i] + 2;
		if(strcmp(opt, "help") == 0) {
			show_help();
			exit(EXIT_FAILURE);
		} else if(strcmp(opt, "nocolor") == 0) {
			config->use_color = FALSE;
		} else if(strncmp(opt, "tree=", 5) == 0) {
			config->use_color = FALSE;
			config->ftree = fopen(opt + 5, "w");
			if(config->ftree == NULL) {
				perror("error opening tree output file");
				exit(EXIT_FAILURE);
			}
		}
	}

	*argc -= skip;
	*argv = &((*argv)[skip]);

	return config;
}

static void show_help(void)
{
	fprintf(stderr,
		"usage: g3d-stat [<option> ...] (file|directory)\n"
		"\n"
		"options:\n"
		"	--help             show this message\n"
		"	--nocolor          disable ANSI color output\n"
		"	--tree=<filename>  redirect tree output to file <filename>\n"
		);
}

static gboolean dir_stat_1(Config *config, const char *dirname,
	GHashTable *plugins)
{
	GDir *dir;
	FileStats *fstats;
	PluginStats *pstats;
	const gchar *fname;
	gchar *filename, *plugin_used;
	gboolean retval;

	dir = g_dir_open(dirname, 0, NULL);
	g_return_val_if_fail(dir != NULL, FALSE);
	while((fname = g_dir_read_name(dir)) != NULL) {
		filename = g_strdup_printf("%s%c%s",
			dirname, G_DIR_SEPARATOR, fname);
		if(quit)
			return FALSE;
		if(g_file_test(filename, G_FILE_TEST_IS_DIR))
			dir_stat_1(config, filename, plugins);
		else {
			fstats = g_new0(FileStats, 1);
			plugin_used = NULL;
			fprintf(stderr, "*** DEBUG: %s\n", filename);
			retval = file_stat(config, filename, FALSE, fstats, &plugin_used);
			if(plugin_used) {
				pstats = g_hash_table_lookup(plugins, plugin_used);
				if(pstats)
					g_free(plugin_used);
				else {
					pstats = g_new0(PluginStats, 1);
					g_hash_table_insert(plugins, plugin_used, pstats);
				}

				/* update pstats for plugin */
				if(retval)
					pstats->n_success ++;
				else
					pstats->n_failure ++;

				if(fstats->n_objects == 0)
					pstats->no_objects = g_slist_append(pstats->no_objects,
						g_strdup(filename));
				else if(fstats->n_vertices == 0)
					pstats->no_vertices = g_slist_append(pstats->no_vertices,
						g_strdup(filename));
				else if(fstats->n_faces == 0)
					pstats->no_faces = g_slist_append(pstats->no_faces,
						g_strdup(filename));
			} /* plugin_used */
			g_free(fstats);
		} /* !directory */
		g_free(filename);
	}
	g_dir_close(dir);
	return TRUE;
}

static void output_plugin_stats(gpointer key, gpointer value, gpointer data)
{
	gchar *name = (gchar *)key;
	PluginStats *pstats = (PluginStats *)value;
	GSList *item;

	printf("%s:\n"
		"  num success: %d\n"
		"  num failure: %d\n"
		"  num no objects: %d\n",
		name, pstats->n_success, pstats->n_failure,
		g_slist_length(pstats->no_objects));
	for(item = pstats->no_objects; item != NULL; item = item->next)
		printf("    %s\n", (gchar *)item->data);
	printf("  num no vertices: %d\n", g_slist_length(pstats->no_vertices));
	for(item = pstats->no_vertices; item != NULL; item = item->next)
		printf("    %s\n", (gchar *)item->data);
	printf("  num no faces: %d\n", g_slist_length(pstats->no_faces));
	for(item = pstats->no_faces; item != NULL; item = item->next)
		printf("    %s\n", (gchar *)item->data);
}

static gboolean dir_stat(Config *config, const char *dirname)
{
	GHashTable *plugins;

	plugins = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);

	dir_stat_1(config, dirname, plugins);

	/* output statistics */
	g_hash_table_foreach(plugins, output_plugin_stats, NULL);

	g_hash_table_destroy(plugins);

	return TRUE;
}

static gboolean objects_stat(GSList *objects, gboolean detailed,
	FileStats *stats, guint32 indent)
{
	GSList *item;
	G3DObject *object;
	gchar *istr;

	if(stats)
		stats->n_objects += g_slist_length(objects);

	for(item = objects; item != NULL; item = item->next) {
		object = (G3DObject *)item->data;
		g_return_val_if_fail(object != NULL, FALSE);

		if(stats) {
			stats->n_vertices += object->vertex_count;
			stats->n_faces += g_slist_length(object->faces);
		}

		if(detailed) {
			istr = g_strnfill(indent * 2, ' ');
			printf("%sobject name: %s\n"
				"%sobject num vertices: %d\n"
				"%sobject num faces: %d\n"
				"%sobject num subobjects: %d\n",
				istr, object->name,
				istr, object->vertex_count,
				istr, g_slist_length(object->faces),
				istr, g_slist_length(object->objects));
			g_free(istr);
		}

		objects_stat(object->objects, detailed, stats, indent + 1);
	}

	return TRUE;
}

static gboolean file_stat(Config *config, const char *filename,
	gboolean detailed, FileStats *stats, gchar **plugin_used)
{
	G3DModel *model;

	model = g3d_model_load(config->context, filename);
	if(model == NULL) {
		return FALSE;
	}

	objects_stat(model->objects, detailed, stats, 0);
	if(plugin_used && model->plugin)
		*plugin_used = g_strdup(model->plugin->name);

	g3d_model_free(model);

	return TRUE;
}