Subversion Repositories HomeAutomation

Rev

Rev 947 | Rev 965 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #!/usr/bin/perl
  2.  
  3. # Written by Mattias Runge 2008
  4.  
  5. use XML::Twig;
  6. use Getopt::Long;
  7.  
  8. $binaryname = "makeCanId.pl";
  9. if (@ARGV > 0)
  10. {
  11.     GetOptions( "xmlfile=s"     => \$xmlfile,
  12.             "cfile=s"   => \$cfile);
  13. }
  14. else
  15. {
  16.     $help = 1;
  17. }
  18.  
  19. if ($help)
  20. {
  21.     print "Usage ./$binaryname [options]\n";
  22.     print "Options:\n";
  23.     print "  -x <xmlfile>           Choose xmlfile to parse\n";
  24.     print "  -c <headerfile>        Choose header file to write\n";
  25.     print "  -h                 Shows this usage\n";
  26.     print "\n";
  27.     print "example: ./$binaryname -x data.xml -c moduleid.h\n";
  28.  
  29.     exit 0;
  30. }
  31.  
  32. my $now = localtime time;
  33. my $twig = XML::Twig->new();
  34.  
  35. $twig->parsefile($xmlfile);
  36.  
  37. my $root = $twig->root;
  38.  
  39. open (MYFILE, '>' . $cfile);
  40.  
  41. print MYFILE "// Built on $now by $binaryname\n";
  42. print MYFILE "// DO NOT CHANGE MANUALLY\n\n";
  43.  
  44. print MYFILE "//------------------ //\n";
  45. print MYFILE "// Class definitions //\n";
  46. print MYFILE "//------------------ //\n";
  47. print MYFILE "\n";
  48.  
  49. foreach my $class ($root->first_child('classes')->children('class'))
  50. {
  51.     my $className = $class->att('name');
  52.     $className =~ tr/a-z/A-Z/;
  53.     print MYFILE "#define CAN_MODULE_CLASS_$className " . $class->att('id') . "\n";
  54. }
  55.  
  56. print MYFILE "\n";
  57. print MYFILE "//-------------------- //\n";
  58. print MYFILE "// Command definitions //\n";
  59. print MYFILE "//-------------------- //\n";
  60. print MYFILE "\n";
  61.  
  62. foreach my $command ($root->first_child('commands')->children('command'))
  63. {
  64.     my $commandName = $command->att('name');
  65.     $commandName =~ tr/a-z/A-Z/;
  66.     my $commandType = $command->att('type');
  67.     $commandType =~ tr/a-z/A-Z/;
  68.    
  69.     if ($commandType eq "SPECIFIC")
  70.     {
  71.         $commandType = $command->att('module');
  72.         $commandType =~ tr/a-z/A-Z/;
  73.     }
  74.    
  75.     print MYFILE "#define CAN_MODULE_CMD_" . $commandType . "_" . $commandName . " " . $command->att('id') . "\n";
  76. }
  77.  
  78. print MYFILE "\n";
  79. print MYFILE "//------------------- //\n";
  80. print MYFILE "// Module definitions //\n";
  81. print MYFILE "//------------------- //\n";
  82. print MYFILE "\n";
  83.  
  84. foreach my $module ($root->first_child('modules')->children('module'))
  85. {
  86.     my $className = $module->att('class');
  87.     $className =~ tr/a-z/A-Z/;
  88.     my $moduleName = $module->att('name');
  89.     $moduleName =~ tr/a-z/A-Z/;
  90.     print MYFILE "#define CAN_MODULE_TYPE_" . $className . "_" . $moduleName . " " . $module->att('id') . "\n";
  91. }
  92.  
  93. print MYFILE "\n";
  94. print MYFILE "//----------------- //\n";
  95. print MYFILE "// Misc definitions //\n";
  96. print MYFILE "//----------------- //\n";
  97. print MYFILE "\n";
  98.  
  99. foreach my $define ($root->first_child('defines')->children('define'))
  100. {
  101.     my $groupName = $define->att('group');
  102.     $groupName =~ tr/a-z/A-Z/;
  103.     my $defineName = $define->att('name');
  104.     $defineName =~ tr/a-z/A-Z/;
  105.     print MYFILE "#define " . $groupName . "_" . $defineName . " " . $define->att('id') . "\n";
  106. }
  107.  
  108. print MYFILE "\n";
  109.  
  110. close (MYFILE);
  111.  
  112. print "Successfully wrote " . $cfile . "\n";
  113.  
  114.