geninclude.pl.in 2.98 KB
Newer Older
1
#!@PERL@ -w
2 3

print <<EOT;
4
typedef	GtkWidget *(*GDoDemoFunc) (GtkWidget *do_widget);
5

6 7 8 9 10 11
typedef struct _Demo Demo;

struct _Demo 
{
  gchar *title;
  gchar *filename;
12
  GDoDemoFunc func;
13
  Demo *children;
14 15 16 17 18
};

EOT

for $file (@ARGV) {
19
    my %demo;
20 21 22 23 24 25
    
    ($basename = $file) =~ s/\.c$//;

    open INFO_FILE, $file or die "Cannot open '$file'\n";
    $title = <INFO_FILE>;
    $title =~ s@^\s*/\*\s*@@;
Matthias Clasen's avatar
Matthias Clasen committed
26 27 28 29 30
    $extra = "";
    if ($title =~ /^(.*)::(.*)$/) {
        $title = $1;
        $extra = " $2";
    }
31
    $title =~ s@\s*$@@;
Matthias Clasen's avatar
Matthias Clasen committed
32 33
    $extra =~ s@^\s*@@;
    $extra =~ s@\s*$@@;
34 35 36

    close INFO_FILE;

37
    print "GtkWidget *do_$basename (GtkWidget *do_widget);\n";
38

Matthias Clasen's avatar
Matthias Clasen committed
39
    push @demos, {"title" => $title, "file" => "$file $extra",
40 41 42 43 44
		  "func"  => "do_$basename"};
}

# generate a list of 'parent names'
foreach $href (@demos) {
45
    if ($href->{"title"} =~ m|^([-\w\s]+)/[-\w\s]+$|) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	my $parent_name = $1;
	my $do_next = 0;

	# parent detected
	if (defined @parents) {
	    foreach $foo (@parents) {
		if ($foo eq $parent_name) {
		    $do_next = 1;
		}
	    }
	    
	    if ($do_next) {
		next;
	    }
	}

	push @parents, $parent_name;

	$tmp = (defined @child_arrays)?($#child_arrays + 1):0;
	push @child_arrays, "child$tmp";

	push @demos, {"title" => $parent_name, "file" => "NULL",
		      "func" => "NULL"};
    }
}

if (defined @parents) {
    $i = 0;
    for ($i = 0; $i <= $#parents; $i++) {
	$first = 1;
	
	print "\nDemo ", $child_arrays[$i], "[] = {\n";
	
	$j = 0;
	for ($j = 0; $j <= $#demos; $j++) {
	    $href = $demos[$j];
	    
	    if (!defined $demos[$j]) {
		next;
	    }
	    
87
	    if ($demos[$j]{"title"} =~ m|^$parents[$i]/([-\w\s]+)$|) {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		if ($first) {
		    $first = 0;
		} else {
		    print ",\n";
		}
		
		print qq (  { "$1", "$demos[$j]{file}", $demos[$j]{func}, NULL });

		# hack ... ugly
		$demos[$j]{"title"} = "foo";
	    }
	}

	print ",\n";
	print qq (  { NULL } );
	print "\n};\n";
    }   
}

# sort @demos
@demos_old = @demos;

@demos = sort {
    $a->{"title"} cmp $b->{"title"};
} @demos_old;

# sort the child arrays
if (defined @child_arrays) {
    for ($i = 0; $i <= $#child_arrays; $i++) {
	@foo_old = @{$child_arrays[$i]};

	@{$child_arrays[$i]} = sort {
	    $a->{"title"} cmp $b->{"title"};
	} @foo_old;
    }
123 124
}

125
# toplevel
Alexander Larsson's avatar
Alexander Larsson committed
126
print "\nDemo gtk_demos[] = {\n";
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

$first = 1;
foreach $href (@demos) {
    $handled = 0;

    # ugly evil hack
    if ($href->{title} eq "foo") {
	next;
    }

    if ($first) {
	$first = 0;
    } else {
	print ", \n";
    }

    if (defined @parents) {
	for ($i = 0; $i <= $#parents; $i++) {
	    if ($parents[$i] eq $href->{title}) {

		if ($href->{file} eq 'NULL') {
		    print qq (  { "$href->{title}", NULL, $href->{func}, $child_arrays[$i] });
		} else {
		    print qq (  { "$href->{title}", "$href->{file}", $href->{func}, $child_arrays[$i] });
		}
		
		$handled = 1;
		last;
	    }
	}
    }
    
    if ($handled) {
	next;
    }
    
    print qq (  { "$href->{title}", "$href->{file}", $href->{func}, NULL });
}

print ",\n";
print qq (  { NULL } );
168
print "\n};\n";
169 170

exit 0;