not very beautiful but should work
Tobias Leich [Wed, 11 Nov 2009 23:32:33 +0000 (00:32 +0100)]
scripts/auto_constants.pl

index dceffbe..c4b3fc7 100644 (file)
@@ -8,11 +8,12 @@ my $head_loc = `sdl-config --cflags`;
 print "# Getting header constants from $head_loc\n";
 
 
-#my @header = <$head_loc/*>;
-my @header = <$head_loc/SDL_events.h>;
+my @header = <$head_loc/*>;
 
 my $is_enum  = 0;
+my $is_comment  = 0;
 my $enum_val = -1;
+my $line     = '';
 
 foreach (@header)
 {
@@ -20,8 +21,54 @@ foreach (@header)
        open FH, $_;
        while(<FH>)
        {
+               $_ =~ s/\/\*.*\*\///g;
+               $_ =~ s/\/\/.*$//g;
+               
+               if($_ =~ /\/\*/)
+               {
+                       $line .= $_;
+                       $line =~ s/\/\*.*//;
+                       $is_comment = 1;
+               }
+               
+               if($is_comment && $_ !~ /\*\//)
+               {
+                       next;
+               }
+       
+               if($is_comment && $_ =~ /\*\//)
+               {
+                       $line .= $_;
+                       $line =~ s/.*\*\///;
+                       $is_comment = 0;
+               }
+       
+               # if we are inside an enum, and there is an linebreak in value
+               if($is_enum && $line !~ /,.*$/ && $_ !~ /,.*$/ && $_ !~ /^\s*}\s+(\w+)\s*;\s*$/ && $line !~ /^typedef\s+enum\s*{\s*$/)
+               {
+                       $line .= $_;
+                       $line =~ s/\s+//g;
+                       
+                       next;
+               }
+               elsif($is_enum && $line !~ /,.*$/ && $_ =~ /,\s*(.*){0,1}$/ && $_ !~ /^\s*}\s+(\w+)\s*;\s*$/ && $line !~ /^typedef\s+enum\s*{\s*$/)
+               {
+                       $line .= $_;
+                       $line =~ s/\s+//g;
+               }
+               elsif($is_enum && $line !~ /,.*$/ && $_ =~ /^\s*}\s+(\w+)\s*;\s*$/)
+               {
+                       $line = $_;
+               }
+               else
+               {
+                       $line = $_;
+               }
+               
+               #print("\n$line\n") if ($is_enum);
+       
                # found an enum
-               if($_ =~ /^typedef\s+enum\s*{\s*$/)
+               if($line =~ /^typedef\s+enum\s*{\s*$/)
                {
                        $is_enum  = 1;
                        $enum_val = -1;
@@ -29,26 +76,27 @@ foreach (@header)
                }
                
                # closed enum
-               if($is_enum && $_ =~ /^\s*}\s+(\w+)\s*;\s*$/)
+               if($is_enum && $line =~ /^\s*}\s+(\w+)\s*;\s*$/)
                {
                        $is_enum = 0;
                        printf("#} enum %s\n\n", $1);
                }
-       
+               
                # inside an enum (without value)
-               printf("sub %s{ return %s; }\n", $1, ++$enum_val)    if($is_enum && $_ =~ /^\s*(\w+)\s*,{0,1}\s*(\/.*){0,1}$/);
+               printf("sub %s{ return %s; }\n", $1, ++$enum_val)    if($is_enum && $line =~ /^\s*(\w+)\s*,{0,1}\s*$/);
                
                # inside an enum (decimal)
-               printf("sub %s{ return %s; }\n", $1, $enum_val = $2) if($is_enum && $_ =~ /^\s*(\w+)\s*=\s*(\d+)\s*,{0,1}.*$/i);
+               printf("sub %s{ return %s; }\n", $1, $enum_val = $2) if($is_enum && $line =~ /^\s*(\w+)\s*=\s*(\d+)\s*,{0,1}.*$/i);
                
                # inside an enum (hex)
-               printf("sub %s{ return %s; }\n", $1, $2)             if($is_enum && $_ =~ /^\s*(\w+)\s*=\s*(0x[\dA-F]+)\s*,{0,1}.*$/i);
+               printf("sub %s{ return %s; }\n", $1, $2)             if($is_enum && $line =~ /^\s*(\w+)\s*=\s*(0x[\dA-F]+)\s*,{0,1}.*$/i);
                
                # inside an enum (function)
-               printf("sub %s{ return %s; }\n", $1, $2)             if($is_enum && $_ =~ /^\s*(\w+)\s*=\s*([A-Z_\(\)\|]+)\s*,{0,1}.*$/i);
+               printf("sub %s{ return %s; }\n", $1, $2)             if($is_enum && $line =~ /^\s*(\w+)\s*=\s*([A-Z_\(\)\|]+)\s*,{0,1}.*$/i);
                
                # pattern: "#define SDL_RELEASED 0" (decimal or hex)
-               printf("sub %s{ return %s; }\n", $1, $2)             if($_ =~ /^#define\s+([^_]\w+)\s+(\d+|0x[\dA-F]+)\s*$/i);
+               printf("sub %s{ return %s; }\n", $1, $2)             if($line =~ /^#define\s+([^_]\w+)\s+(\d+|0x[\dA-F]+)\s*$/i);
        }
+       
        close FH;
 }