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