genUsageTableEnum.pl
author StephaneLenclud
Sun, 15 Feb 2015 13:30:54 +0100
changeset 59 89dfad9633b2
parent 33 ddfd2ddf10e1
permissions -rw-r--r--
Win32Hid: Fixing boolean bug and improving value caps union.
     1 #!/usr/local/bin/perl
     2 #Developed by Stéphane Lenclud
     3 #Generate C# enumeration from parsing Hid Usage Table
     4 #See ../data/gl.h
     5 #Usage example
     6 #perl -S genUsageTableEnum.pl ../data/gl.h
     7 
     8 use strict;
     9 use warnings;	
    10 
    11 
    12 my $inputFile = $ARGV[0];
    13 
    14 #my $dummy="lala";
    15 #$dummy=~s/(^\w)/uc($1)/e;
    16 #print "$dummy";
    17 #exit(0);
    18 
    19 #Open input file
    20 open INPUT, "< $inputFile" or die "Can't read $inputFile\n";
    21 my @lines = <INPUT>;
    22 close INPUT;
    23 
    24 
    25 my %hash = ();
    26 
    27 my $count=0;
    28 foreach my $line(@lines)
    29 	{
    30 	#if ($line=~ /^([a-fA-FxX\d]+)(.+)\s+\w+\s+15\..*$/)	
    31 	if ($line=~ /^([a-fA-FxX\d]+)(.+)\s+\w+\s+\d+\..*$/)
    32 		{
    33 		my $string=$2;
    34 		my $value=$1;
    35 		
    36 		my $varName=FormatVarName($string);	
    37 
    38 		
    39 		$hash{$string}=$value;
    40 		
    41 		print "$varName = 0x$value,\n";			
    42 		}
    43 	else
    44 		{
    45 		#print "NO MATCH $line\n";
    46 		}
    47 	}
    48 
    49 exit(0);	
    50 	
    51 #Output in sorted order	
    52 for my $string ( sort keys %hash )
    53 	{	
    54     #print "_S8(\"$string\"),$hash{$string}, //$count\n";
    55     print "_S8(\"$string\"),$string, //$count\n";						
    56     $count++;	
    57     }	
    58 	
    59 	
    60 print "$count const found.\n";	
    61 	
    62 exit(0);
    63 
    64 #	
    65 
    66 sub FormatVarName
    67 	{
    68 	my $text=$_[0];	
    69 	my $varName="";
    70 	Trim($text);
    71 	#Make sure AC ends up as AppCtrl
    72 	$text=~s/(^AC)/App Ctrl/;
    73 	#Make sure AL ends up as AppLaunch
    74 	$text=~s/(^AL)/App Launch/;		
    75 	#Replace / by white-space
    76 	$text=~s/\// /g;
    77 	#Replace + with Plus
    78 	$text=~s/\+/Plus/g;
    79 	#Replace - with white-space
    80 	$text=~s/-/ /g;
    81 
    82 	
    83 	$text=lc($text);
    84 	while ($text=~/(\w+)\s+(.+)/)
    85 		{		
    86 		my $word=$1;
    87 		$text=$2;
    88 		#upper case the first letter
    89 		$word=~s/(^\w)/uc($1)/e;	
    90 		$varName.=$word;		
    91 		}
    92 	
    93 	$text=~s/(^\w)/uc($1)/e;					
    94 	$varName.=$text;		
    95 	#get ride of -			
    96 	$varName=~s/-(\w)/uc($1)/e;
    97 
    98 	return $varName;
    99 	}
   100 	
   101 sub Trim
   102 	{
   103 	$_[0] =~ s/^\s+//; #Trim leading space and line return char
   104 	$_[0] =~ s/\s+$//; #Trim trailling space and line return char
   105 	}	
   106