- TODO updated
[public/netxms.git] / tools / mc.pl
1 #!c:/apps/perl/bin/pelr -w
2
3 # $Id: mc.pl,v 1.5 2004-09-03 18:17:16 alk Exp $
4
5 ###############################################################################
6 #
7 # VC++ message compiler alternative.
8 # read .mc files and generate corresponding source & include
9 #
10 ###############################################################################
11
12 use strict;
13 use Data::Dumper;
14
15 # file names
16 my $inFile = shift || die "Usage : mc.pl <input_file>";
17 my $outHeader = $inFile;
18 $outHeader =~ s/\.[a-z]+$//;
19 my $outSource = $outHeader . ".c";
20 $outHeader .= ".h";
21
22 my $type = "DWORD";
23 my $messageId = 0;
24 my $comment;
25 my $symbolicName = "";
26 my %structData;
27
28 open(IN, "<$inFile") || die "input file: $!";
29 open(OUTH, ">$outHeader") || die "out header: $!";
30 open(OUTS, ">$outSource") || die "out source: $!";
31
32 while(<IN>) {
33 chomp;
34 my $in = $_;
35 my $text = "";
36
37 if ($in eq "") {
38 next;
39 }
40
41 if ($in =~ /^[;#]+.*$/) {
42 print OUTH substr($in, 1) . "\n";
43 next; # comment -> skip
44 }
45
46 if ($in =~ /^MessageIdTypedef=([A-Z]+)$/i) {
47 # typedef
48 $type = $1;
49
50 next;
51 }
52
53 if ($in =~ /^MessageId=([0-9]*)$/i) {
54 if ($1 eq "") {
55 $messageId++;
56 } else {
57 $messageId = $1 + 0;
58 }
59
60 next;
61 }
62 if ($in =~ /^SymbolicName=(.+)$/i) {
63 $symbolicName = $1;
64
65 next;
66 }
67 if ($in =~ /^Language=(.*)$/i) {
68 # ignore language
69 next;
70 }
71
72 # multiline text
73 $text = $in;
74 while(<IN>) {
75 chomp;
76 if ($_ ne ".") {
77 $text .= "$_\n";
78 # end
79 } else {
80 if ($symbolicName eq "") {
81 last;
82 }
83 print OUTH "//\n// MessageId: $symbolicName\n//\n";
84 print OUTH "// MessageText:\n//\n// ";
85 $structData{$messageId} = $text;
86 $text =~ s/\n/\n\/\//gm;
87 print OUTH $text . "\n//\n";
88
89 my $size = 8;
90 if ($type =~ /^(WORD|short)$/i) {
91 $size = 4;
92 }
93 printf OUTH ("#define %-30s (($type)0x%0" . $size . "x)\n\n", $symbolicName, $messageId);
94 last;
95 }
96 }
97 }
98
99 print OUTS "/* autogenerated from $inFile */\n\n";
100 print OUTS "char *g_szMessages[] = {\n";
101
102 my @keys = sort { $a <=> $b } (keys %structData);
103
104 my $i;
105 for ($i = 0; $i < $keys[$#keys] + 1; $i++) {
106 if (defined $structData{$i}) {
107 $structData{$i} =~ s/\"/\\\"/g;
108 print OUTS "\t\"" . $structData{$i} . "\"";
109 } else {
110 print OUTS "\t\"\"";
111 }
112
113 if ($i == $keys[$#keys]) {
114 print OUTS "\n";
115 } else {
116 print OUTS ",\n";
117 }
118 }
119
120 print OUTS "};\n";
121 print OUTS "\nunsigned long g_dwNumMessages = $i;\n";
122
123 close IN;
124 close OUTH;
125 close OUTS;