glib/gobject/gobject-query.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GObject - GLib Type, Object, Parameter and Signal Library
       
     2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
       
     3  * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Lesser General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General
       
    15  * Public License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 #include "config.h"
       
    21 
       
    22 #include <stdlib.h>
       
    23 #include <string.h>
       
    24 #ifdef HAVE_UNISTD_H
       
    25 #include <unistd.h>
       
    26 #endif
       
    27 #include <sys/stat.h>
       
    28 #include <fcntl.h>
       
    29 
       
    30 #include <glib-object.h>
       
    31 #include <glib/gprintf.h>
       
    32 #ifdef __SYMBIAN32__
       
    33 #include "mrt2_glib2_test.h"
       
    34 #endif /*__SYMBIAN32__*/
       
    35 
       
    36 static gchar *indent_inc = NULL;
       
    37 static guint spacing = 1;
       
    38 static FILE *f_out = NULL;
       
    39 static GType root = 0;
       
    40 static gboolean recursion = TRUE;
       
    41 
       
    42 #if 0
       
    43 #  define	O_SPACE	"\\as"
       
    44 #  define	O_ESPACE " "
       
    45 #  define	O_BRANCH "\\aE"
       
    46 #  define	O_VLINE "\\al"
       
    47 #  define	O_LLEAF	"\\aL"
       
    48 #  define	O_KEY_FILL "_"
       
    49 #else
       
    50 #  define	O_SPACE	" "
       
    51 #  define	O_ESPACE ""
       
    52 #  define	O_BRANCH "+"
       
    53 #  define	O_VLINE "|"
       
    54 #  define	O_LLEAF	"`"
       
    55 #  define	O_KEY_FILL "_"
       
    56 #endif
       
    57 
       
    58 static void
       
    59 show_nodes (GType        type,
       
    60 	    GType        sibling,
       
    61 	    const gchar *indent)
       
    62 {
       
    63   GType   *children;
       
    64   guint i;
       
    65   
       
    66   if (!type)
       
    67     return;
       
    68   
       
    69   children = g_type_children (type, NULL);
       
    70   
       
    71   if (type != root)
       
    72     for (i = 0; i < spacing; i++)
       
    73       g_fprintf (f_out, "%s%s\n", indent, O_VLINE);
       
    74   
       
    75   g_fprintf (f_out, "%s%s%s%s",
       
    76 	   indent,
       
    77 	   sibling ? O_BRANCH : (type != root ? O_LLEAF : O_SPACE),
       
    78 	   O_ESPACE,
       
    79 	   g_type_name (type));
       
    80   
       
    81   for (i = strlen (g_type_name (type)); i <= strlen (indent_inc); i++)
       
    82     fputs (O_KEY_FILL, f_out);
       
    83   
       
    84   fputc ('\n', f_out);
       
    85   
       
    86   if (children && recursion)
       
    87     {
       
    88       gchar *new_indent;
       
    89       GType   *child;
       
    90       
       
    91       if (sibling)
       
    92 	new_indent = g_strconcat (indent, O_VLINE, indent_inc, NULL);
       
    93       else
       
    94 	new_indent = g_strconcat (indent, O_SPACE, indent_inc, NULL);
       
    95       
       
    96       for (child = children; *child; child++)
       
    97 	show_nodes (child[0], child[1], new_indent);
       
    98       
       
    99       g_free (new_indent);
       
   100     }
       
   101   
       
   102   g_free (children);
       
   103 }
       
   104 
       
   105 static gint
       
   106 help (gchar *arg)
       
   107 {
       
   108   g_fprintf (stderr, "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
       
   109   g_fprintf (stderr, "       -r       specifiy root type\n");
       
   110   g_fprintf (stderr, "       -n       don't descend type tree\n");
       
   111   g_fprintf (stderr, "       -h       guess what ;)\n");
       
   112   g_fprintf (stderr, "       -b       specify indent string\n");
       
   113   g_fprintf (stderr, "       -i       specify incremental indent string\n");
       
   114   g_fprintf (stderr, "       -s       specify line spacing\n");
       
   115   g_fprintf (stderr, "qualifiers:\n");
       
   116   g_fprintf (stderr, "       froots   iterate over fundamental roots\n");
       
   117   g_fprintf (stderr, "       tree     print type tree\n");
       
   118   
       
   119   return arg != NULL;
       
   120 }
       
   121 
       
   122 int
       
   123 main (gint   argc,
       
   124       gchar *argv[])
       
   125 {
       
   126   GLogLevelFlags fatal_mask;
       
   127   gboolean gen_froots = 0;
       
   128   gboolean gen_tree = 0;
       
   129   gint i;
       
   130   gchar *iindent = "";
       
   131 #ifdef __SYMBIAN32__
       
   132   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   133   g_set_print_handler(mrtPrintHandler);
       
   134 #endif /*__SYMBIAN32__*/
       
   135   f_out = stdout;
       
   136   
       
   137   fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
       
   138   fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
       
   139   g_log_set_always_fatal (fatal_mask);
       
   140   
       
   141   root = G_TYPE_OBJECT;
       
   142 
       
   143   g_type_init ();
       
   144   
       
   145   for (i = 1; i < argc; i++)
       
   146     {
       
   147       if (strcmp ("-s", argv[i]) == 0)
       
   148 	{
       
   149 	  i++;
       
   150 	  if (i < argc)
       
   151 	    spacing = atoi (argv[i]);
       
   152 	}
       
   153       else if (strcmp ("-i", argv[i]) == 0)
       
   154 	{
       
   155 	  i++;
       
   156 	  if (i < argc)
       
   157 	    {
       
   158 	      char *p;
       
   159 	      guint n;
       
   160 	      
       
   161 	      p = argv[i];
       
   162 	      while (*p)
       
   163 		p++;
       
   164 	      n = p - argv[i];
       
   165 	      indent_inc = g_new (gchar, n * strlen (O_SPACE) + 1);
       
   166 	      *indent_inc = 0;
       
   167 	      while (n)
       
   168 		{
       
   169 		  n--;
       
   170 		  strcpy (indent_inc, O_SPACE);
       
   171 		}
       
   172 	    }
       
   173 	}
       
   174       else if (strcmp ("-b", argv[i]) == 0)
       
   175 	{
       
   176 	  i++;
       
   177 	  if (i < argc)
       
   178 	    iindent = argv[i];
       
   179 	}
       
   180       else if (strcmp ("-r", argv[i]) == 0)
       
   181 	{
       
   182 	  i++;
       
   183 	  if (i < argc)
       
   184 	    root = g_type_from_name (argv[i]);
       
   185 	}
       
   186       else if (strcmp ("-n", argv[i]) == 0)
       
   187 	{
       
   188 	  recursion = FALSE;
       
   189 	}
       
   190       else if (strcmp ("froots", argv[i]) == 0)
       
   191 	{
       
   192 	  gen_froots = 1;
       
   193 	}
       
   194       else if (strcmp ("tree", argv[i]) == 0)
       
   195 	{
       
   196 	  gen_tree = 1;
       
   197 	}
       
   198       else if (strcmp ("-h", argv[i]) == 0)
       
   199 	{
       
   200 	  return help (NULL);
       
   201 	}
       
   202       else if (strcmp ("--help", argv[i]) == 0)
       
   203 	{
       
   204 	  return help (NULL);
       
   205 	}
       
   206       else
       
   207 	return help (argv[i]);
       
   208     }
       
   209   
       
   210   if (!gen_froots && !gen_tree)
       
   211     return help (argv[i-1]);
       
   212   
       
   213   if (!indent_inc)
       
   214     {
       
   215       indent_inc = g_new (gchar, strlen (O_SPACE) + 1);
       
   216       *indent_inc = 0;
       
   217       strcpy (indent_inc, O_SPACE);
       
   218     }
       
   219   
       
   220   if (gen_tree)
       
   221     show_nodes (root, 0, iindent);
       
   222   if (gen_froots)
       
   223     {
       
   224       root = ~0;
       
   225       for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1))
       
   226 	{
       
   227 	  const gchar *name = g_type_name (i);
       
   228 	  
       
   229 	  if (name)
       
   230 	    show_nodes (i, 0, iindent);
       
   231 	}
       
   232     }
       
   233 #ifdef __SYMBIAN32__
       
   234   testResultXml("gobject-querry");
       
   235 #endif /* EMULATOR */  
       
   236   return 0;
       
   237 }