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