/* $Id:$ */ /* libg3d - 3D object loading library Copyright (C) 2005-2009 Markus Dahms 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 #include #include #include #include #include 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 [