kusano 7d535a
##
kusano 7d535a
## Copyright (C) 2002-2008, Marcelo E. Magallon <mmagallo[]debian org=""></mmagallo[]debian>
kusano 7d535a
## Copyright (C) 2002-2008, Milan Ikits <milan ikits[]ieee="" org=""></milan>
kusano 7d535a
##
kusano 7d535a
## This program is distributed under the terms and conditions of the GNU
kusano 7d535a
## General Public License Version 2 as published by the Free Software
kusano 7d535a
## Foundation or, at your option, any later version.
kusano 7d535a
kusano 7d535a
my %regex = (
kusano 7d535a
    extname  => qr/^[A-Z][A-Za-z0-9_]+$/,
kusano 7d535a
    exturl   => qr/^http.+$/,
kusano 7d535a
    function => qr/^(.+) ([a-z][a-z0-9_]*) \((.+)\)$/i, 
kusano 7d535a
    token    => qr/^([A-Z][A-Z0-9_x]*)\s+((?:0x)?[0-9A-Fa-f]+|[A-Z][A-Z0-9_]*)$/,
kusano 7d535a
    type     => qr/^typedef\s+(.+)$/,
kusano 7d535a
    exact    => qr/.*;$/,
kusano 7d535a
);
kusano 7d535a
kusano 7d535a
# prefix function name with glew
kusano 7d535a
sub prefixname($)
kusano 7d535a
{
kusano 7d535a
    my $name = $_[0];
kusano 7d535a
    $name =~ s/^(.*?)gl/__$1glew/;
kusano 7d535a
    return $name;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
# prefix function name with glew
kusano 7d535a
sub prefix_varname($)
kusano 7d535a
{
kusano 7d535a
    my $name = $_[0];
kusano 7d535a
    $name =~ s/^(.*?)GL(X*?)EW/__$1GL$2EW/;
kusano 7d535a
    return $name;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#---------------------------------------------------------------------------------------
kusano 7d535a
kusano 7d535a
sub make_exact($)
kusano 7d535a
{
kusano 7d535a
	my $exact = $_[0];
kusano 7d535a
	$exact =~ s/(; |{)/$1\n/g;
kusano 7d535a
    return $exact;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
sub make_separator($)
kusano 7d535a
{
kusano 7d535a
    my $extname = $_[0];
kusano 7d535a
    my $l = length $extname;
kusano 7d535a
    my $s = (71 - $l)/2;
kusano 7d535a
    print "/* ";
kusano 7d535a
    my $j = 3;
kusano 7d535a
    for (my $i = 0; $i < $s; $i++)
kusano 7d535a
    {
kusano 7d535a
	print "-";
kusano 7d535a
	$j++;
kusano 7d535a
    }
kusano 7d535a
    print " $_[0] ";
kusano 7d535a
    $j += $l + 2;
kusano 7d535a
    while ($j < 76)
kusano 7d535a
    {
kusano 7d535a
	print "-";
kusano 7d535a
	$j++;
kusano 7d535a
    }
kusano 7d535a
    print " */\n\n";
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#---------------------------------------------------------------------------------------
kusano 7d535a
kusano 7d535a
sub parse_ext($)
kusano 7d535a
{
kusano 7d535a
    my $filename = shift;
kusano 7d535a
    my %functions = ();
kusano 7d535a
    my %tokens = ();
kusano 7d535a
    my @types = ();
kusano 7d535a
    my @exacts = ();
kusano 7d535a
    my $extname = "";    # Full extension name GL_FOO_extension
kusano 7d535a
    my $exturl = "";     # Info URL
kusano 7d535a
    my $extstring = "";  # Relevant extension string 
kusano 7d535a
kusano 7d535a
    open EXT, "<$filename" or return;
kusano 7d535a
kusano 7d535a
    # As of GLEW 1.5.3 the first three lines _must_ be
kusano 7d535a
    # the extension name, the URL and the GL extension
kusano 7d535a
    # string (which might be different to the name)
kusano 7d535a
    #
kusano 7d535a
    # For example GL_NV_geometry_program4 is available
kusano 7d535a
    # iff GL_NV_gpu_program4 appears in the extension
kusano 7d535a
    # string.
kusano 7d535a
    #
kusano 7d535a
    # For core OpenGL versions, the third line should
kusano 7d535a
    # be blank.
kusano 7d535a
    #
kusano 7d535a
    # If the URL is unknown, the second line should be
kusano 7d535a
    # blank.
kusano 7d535a
   
kusano 7d535a
    $extname   = readline(*EXT);
kusano 7d535a
    $exturl    = readline(*EXT);
kusano 7d535a
    $extstring = readline(*EXT);
kusano 7d535a
kusano 7d535a
    chomp($extname);
kusano 7d535a
    chomp($exturl);
kusano 7d535a
    chomp($extstring);
kusano 7d535a
kusano 7d535a
    while(<ext>)</ext>
kusano 7d535a
    {
kusano 7d535a
        chomp;
kusano 7d535a
        if (s/^\s+//)
kusano 7d535a
        {
kusano 7d535a
            if (/$regex{exact}/)
kusano 7d535a
            {
kusano 7d535a
                push @exacts, $_;
kusano 7d535a
            }
kusano 7d535a
            elsif (/$regex{type}/)
kusano 7d535a
            {
kusano 7d535a
                push @types, $_;
kusano 7d535a
            }
kusano 7d535a
            elsif (/$regex{token}/)
kusano 7d535a
            {
kusano 7d535a
                my ($name, $value) = ($1, $2);
kusano 7d535a
                $tokens{$name} = $value;
kusano 7d535a
            }
kusano 7d535a
            elsif (/$regex{function}/)
kusano 7d535a
            {
kusano 7d535a
                my ($return, $name, $parms) = ($1, $2, $3);
kusano 7d535a
                $functions{$name} = {
kusano 7d535a
		    rtype => $return,
kusano 7d535a
		    parms => $parms,
kusano 7d535a
		};
kusano 7d535a
            } else {
kusano 7d535a
                print STDERR "'$_' matched no regex.\n";
kusano 7d535a
            }
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    close EXT;
kusano 7d535a
kusano 7d535a
    return ($extname, $exturl, $extstring, \@types, \%tokens, \%functions, \@exacts);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
sub output_tokens($$)
kusano 7d535a
{
kusano 7d535a
    my ($tbl, $fnc) = @_;
kusano 7d535a
    if (keys %{$tbl})
kusano 7d535a
    {
kusano 7d535a
        local $, = "\n";
kusano 7d535a
        print "\n";
kusano 7d535a
        print map { &{$fnc}($_, $tbl->{$_}) } sort { hex ${$tbl}{$a} <=> hex ${$tbl}{$b} } keys %{$tbl};
kusano 7d535a
        print "\n";
kusano 7d535a
    } else {
kusano 7d535a
        print STDERR "no keys in table!\n";
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
sub output_types($$)
kusano 7d535a
{
kusano 7d535a
    my ($tbl, $fnc) = @_;
kusano 7d535a
    if (scalar @{$tbl})
kusano 7d535a
    {
kusano 7d535a
        local $, = "\n";
kusano 7d535a
        print "\n";
kusano 7d535a
        print map { &{$fnc}($_) } sort @{$tbl};
kusano 7d535a
        print "\n";
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
sub output_decls($$)
kusano 7d535a
{
kusano 7d535a
    my ($tbl, $fnc) = @_;
kusano 7d535a
    if (keys %{$tbl})
kusano 7d535a
    {
kusano 7d535a
        local $, = "\n";
kusano 7d535a
        print "\n";
kusano 7d535a
        print map { &{$fnc}($_, $tbl->{$_}) } sort keys %{$tbl};
kusano 7d535a
        print "\n";
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
sub output_exacts($$)
kusano 7d535a
{
kusano 7d535a
    my ($tbl, $fnc) = @_;
kusano 7d535a
    if (scalar @{$tbl})
kusano 7d535a
    {
kusano 7d535a
        local $, = "\n";
kusano 7d535a
        print "\n";
kusano 7d535a
        print map { &{$fnc}($_) } sort @{$tbl};
kusano 7d535a
        print "\n";
kusano 7d535a
    }
kusano 7d535a
}
kusano 7d535a