]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/geninclude.pl.in
Avoid perl warnings
[~andy/gtk] / demos / gtk-demo / geninclude.pl.in
1 #!@PERL@ -w
2
3 print <<EOT;
4 typedef GtkWidget *(*GDoDemoFunc) (GtkWidget *do_widget);
5
6 typedef struct _Demo Demo;
7
8 struct _Demo 
9 {
10   gchar *name;
11   gchar *title;
12   gchar *filename;
13   GDoDemoFunc func;
14   Demo *children;
15 };
16
17 EOT
18
19 for $file (@ARGV) {
20     my %demo;
21     
22     ($basename = $file) =~ s/\.c$//;
23
24     open INFO_FILE, $file or die "Cannot open '$file'\n";
25     $title = <INFO_FILE>;
26     $title =~ s@^\s*/\*\s*@@;
27     $title =~ s@\s*$@@;
28
29     close INFO_FILE;
30
31     print "GtkWidget *do_$basename (GtkWidget *do_widget);\n";
32
33     push @demos, {"name" => $basename, "title" => $title, "file" => $file,
34                   "func"  => "do_$basename"};
35 }
36
37 # generate a list of 'parent names'
38 foreach $href (@demos) {
39     if ($href->{"title"} =~ m|^([-\w\s]+)/[-\w\s]+$|) {
40         my $parent_name = $1;
41         my $do_next = 0;
42
43         # parent detected
44         if (@parents) {
45             foreach $foo (@parents) {
46                 if ($foo eq $parent_name) {
47                     $do_next = 1;
48                 }
49             }
50             
51             if ($do_next) {
52                 next;
53             }
54         }
55
56         push @parents, $parent_name;
57
58         $tmp = (@child_arrays)?($#child_arrays + 1):0;
59         push @child_arrays, "child$tmp";
60
61         push @demos, {"name" => "NULL", "title" => $parent_name, "file" => "NULL",
62                       "func" => "NULL"};
63     }
64 }
65
66 if (@parents) {
67     $i = 0;
68     for ($i = 0; $i <= $#parents; $i++) {
69         $first = 1;
70         
71         print "\nDemo ", $child_arrays[$i], "[] = {\n";
72         
73         $j = 0;
74         for ($j = 0; $j <= $#demos; $j++) {
75             $href = $demos[$j];
76             
77             if (!$demos[$j]) {
78                 next;
79             }
80             
81             if ($demos[$j]{"title"} =~ m|^$parents[$i]/([-\w\s]+)$|) {
82                 if ($first) {
83                     $first = 0;
84                 } else {
85                     print ",\n";
86                 }
87                 
88                 print qq (  { "$demos[$j]{name}", "$1", "$demos[$j]{file}", $demos[$j]{func}, NULL });
89
90                 # hack ... ugly
91                 $demos[$j]{"title"} = "foo";
92             }
93         }
94
95         print ",\n";
96         print qq (  { NULL } );
97         print "\n};\n";
98     }   
99 }
100
101 # sort @demos
102 @demos_old = @demos;
103
104 @demos = sort {
105     $a->{"title"} cmp $b->{"title"};
106 } @demos_old;
107
108 # sort the child arrays
109 if (@child_arrays) {
110     for ($i = 0; $i <= $#child_arrays; $i++) {
111         @foo_old = @{$child_arrays[$i]};
112
113         @{$child_arrays[$i]} = sort {
114             $a->{"title"} cmp $b->{"title"};
115         } @foo_old;
116     }
117 }
118
119 # toplevel
120 print "\nDemo gtk_demos[] = {\n";
121
122 $first = 1;
123 foreach $href (@demos) {
124     $handled = 0;
125
126     # ugly evil hack
127     if ($href->{title} eq "foo") {
128         next;
129     }
130
131     if ($first) {
132         $first = 0;
133     } else {
134         print ", \n";
135     }
136
137     if (@parents) {
138         for ($i = 0; $i <= $#parents; $i++) {
139             if ($parents[$i] eq $href->{title}) {
140
141                 if ($href->{file} eq 'NULL') {
142                     print qq (  { NULL, "$href->{title}", NULL, $href->{func}, $child_arrays[$i] });
143                 } else {
144                     print qq (  { "$href->{name}", "$href->{title}", "$href->{file}", $href->{func}, $child_arrays[$i] });
145                 }
146                 
147                 $handled = 1;
148                 last;
149             }
150         }
151     }
152     
153     if ($handled) {
154         next;
155     }
156     
157     print qq (  { "$href->{name}", "$href->{title}", "$href->{file}", $href->{func}, NULL });
158 }
159
160 print ",\n";
161 print qq (  { NULL } );
162 print "\n};\n";
163
164 exit 0;