gobject.py: Fix indentation
[platform/upstream/glib.git] / gobject / gobject-query.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 #include <glib-object.h>
28 #include <glib/gprintf.h>
29
30
31 static gchar *indent_inc = NULL;
32 static guint spacing = 1;
33 static FILE *f_out = NULL;
34 static GType root = 0;
35 static gboolean recursion = TRUE;
36
37 #if 0
38 #  define       O_SPACE "\\as"
39 #  define       O_ESPACE " "
40 #  define       O_BRANCH "\\aE"
41 #  define       O_VLINE "\\al"
42 #  define       O_LLEAF "\\aL"
43 #  define       O_KEY_FILL "_"
44 #else
45 #  define       O_SPACE " "
46 #  define       O_ESPACE ""
47 #  define       O_BRANCH "+"
48 #  define       O_VLINE "|"
49 #  define       O_LLEAF "`"
50 #  define       O_KEY_FILL "_"
51 #endif
52
53 static void
54 show_nodes (GType        type,
55             GType        sibling,
56             const gchar *indent)
57 {
58   GType   *children;
59   guint i;
60   
61   if (!type)
62     return;
63   
64   children = g_type_children (type, NULL);
65   
66   if (type != root)
67     for (i = 0; i < spacing; i++)
68       g_fprintf (f_out, "%s%s\n", indent, O_VLINE);
69   
70   g_fprintf (f_out, "%s%s%s%s",
71            indent,
72            sibling ? O_BRANCH : (type != root ? O_LLEAF : O_SPACE),
73            O_ESPACE,
74            g_type_name (type));
75   
76   for (i = strlen (g_type_name (type)); i <= strlen (indent_inc); i++)
77     fputs (O_KEY_FILL, f_out);
78   
79   fputc ('\n', f_out);
80   
81   if (children && recursion)
82     {
83       gchar *new_indent;
84       GType   *child;
85       
86       if (sibling)
87         new_indent = g_strconcat (indent, O_VLINE, indent_inc, NULL);
88       else
89         new_indent = g_strconcat (indent, O_SPACE, indent_inc, NULL);
90       
91       for (child = children; *child; child++)
92         show_nodes (child[0], child[1], new_indent);
93       
94       g_free (new_indent);
95     }
96   
97   g_free (children);
98 }
99
100 static gint
101 help (gchar *arg)
102 {
103   g_fprintf (stderr, "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
104   g_fprintf (stderr, "       -r       specifiy root type\n");
105   g_fprintf (stderr, "       -n       don't descend type tree\n");
106   g_fprintf (stderr, "       -h       guess what ;)\n");
107   g_fprintf (stderr, "       -b       specify indent string\n");
108   g_fprintf (stderr, "       -i       specify incremental indent string\n");
109   g_fprintf (stderr, "       -s       specify line spacing\n");
110   g_fprintf (stderr, "qualifiers:\n");
111   g_fprintf (stderr, "       froots   iterate over fundamental roots\n");
112   g_fprintf (stderr, "       tree     print type tree\n");
113   
114   return arg != NULL;
115 }
116
117 int
118 main (gint   argc,
119       gchar *argv[])
120 {
121   GLogLevelFlags fatal_mask;
122   gboolean gen_froots = 0;
123   gboolean gen_tree = 0;
124   gint i;
125   gchar *iindent = "";
126
127   f_out = stdout;
128   
129   fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
130   fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
131   g_log_set_always_fatal (fatal_mask);
132   
133   root = G_TYPE_OBJECT;
134
135   for (i = 1; i < argc; i++)
136     {
137       if (strcmp ("-s", argv[i]) == 0)
138         {
139           i++;
140           if (i < argc)
141             spacing = atoi (argv[i]);
142         }
143       else if (strcmp ("-i", argv[i]) == 0)
144         {
145           i++;
146           if (i < argc)
147             {
148               char *p;
149               guint n;
150               
151               p = argv[i];
152               while (*p)
153                 p++;
154               n = p - argv[i];
155               indent_inc = g_new (gchar, n * strlen (O_SPACE) + 1);
156               *indent_inc = 0;
157               while (n)
158                 {
159                   n--;
160                   strcpy (indent_inc, O_SPACE);
161                 }
162             }
163         }
164       else if (strcmp ("-b", argv[i]) == 0)
165         {
166           i++;
167           if (i < argc)
168             iindent = argv[i];
169         }
170       else if (strcmp ("-r", argv[i]) == 0)
171         {
172           i++;
173           if (i < argc)
174             root = g_type_from_name (argv[i]);
175         }
176       else if (strcmp ("-n", argv[i]) == 0)
177         {
178           recursion = FALSE;
179         }
180       else if (strcmp ("froots", argv[i]) == 0)
181         {
182           gen_froots = 1;
183         }
184       else if (strcmp ("tree", argv[i]) == 0)
185         {
186           gen_tree = 1;
187         }
188       else if (strcmp ("-h", argv[i]) == 0)
189         {
190           return help (NULL);
191         }
192       else if (strcmp ("--help", argv[i]) == 0)
193         {
194           return help (NULL);
195         }
196       else
197         return help (argv[i]);
198     }
199   
200   if (!gen_froots && !gen_tree)
201     return help (argv[i-1]);
202   
203   if (!indent_inc)
204     {
205       indent_inc = g_new (gchar, strlen (O_SPACE) + 1);
206       *indent_inc = 0;
207       strcpy (indent_inc, O_SPACE);
208     }
209   
210   if (gen_tree)
211     show_nodes (root, 0, iindent);
212   if (gen_froots)
213     {
214       root = ~0;
215       for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1))
216         {
217           const gchar *name = g_type_name (i);
218           
219           if (name)
220             show_nodes (i, 0, iindent);
221         }
222     }
223   
224   return 0;
225 }