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