- TODO updated
[public/netxms.git] / tools / mc.pl
CommitLineData
6dde64eb
AK
1#!c:/apps/perl/bin/pelr -w
2
2bdde1f8 3# $Id: mc.pl,v 1.5 2004-09-03 18:17:16 alk Exp $
6dde64eb
AK
4
5###############################################################################
6#
7# VC++ message compiler alternative.
8# read .mc files and generate corresponding source & include
9#
10###############################################################################
11
12use strict;
2bdde1f8 13use Data::Dumper;
6dde64eb
AK
14
15# file names
16my $inFile = shift || die "Usage : mc.pl <input_file>";
17my $outHeader = $inFile;
18$outHeader =~ s/\.[a-z]+$//;
19my $outSource = $outHeader . ".c";
20$outHeader .= ".h";
21
22my $type = "DWORD";
23my $messageId = 0;
24my $comment;
25my $symbolicName = "";
26my %structData;
27
28open(IN, "<$inFile") || die "input file: $!";
29open(OUTH, ">$outHeader") || die "out header: $!";
30open(OUTS, ">$outSource") || die "out source: $!";
31
32while(<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 }
eeb5e39c 93 printf OUTH ("#define %-30s (($type)0x%0" . $size . "x)\n\n", $symbolicName, $messageId);
6dde64eb
AK
94 last;
95 }
96 }
97}
98
99print OUTS "/* autogenerated from $inFile */\n\n";
100print OUTS "char *g_szMessages[] = {\n";
101
2bdde1f8 102my @keys = sort { $a <=> $b } (keys %structData);
6dde64eb 103
2bdde1f8
AK
104my $i;
105for ($i = 0; $i < $keys[$#keys] + 1; $i++) {
6dde64eb
AK
106 if (defined $structData{$i}) {
107 $structData{$i} =~ s/\"/\\\"/g;
108 print OUTS "\t\"" . $structData{$i} . "\"";
109 } else {
110 print OUTS "\t\"\"";
111 }
112
2bdde1f8 113 if ($i == $keys[$#keys]) {
6dde64eb
AK
114 print OUTS "\n";
115 } else {
116 print OUTS ",\n";
117 }
118}
119
3662e9d3 120print OUTS "};\n";
2bdde1f8 121print OUTS "\nunsigned long g_dwNumMessages = $i;\n";
6dde64eb
AK
122
123close IN;
124close OUTH;
125close OUTS;