sync master with lastest vba changes
[ooovba.git] / sc / addin / util / cl2c.pl
blob3115c2cac773e1b16fa1bbf32839144c5bbe429b
1 #!/usr/solar/bin/perl
3 ##########################################################################
5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 #
7 # Copyright 2008 by Sun Microsystems, Inc.
9 # OpenOffice.org - a multi-platform office productivity suite
11 # $RCSfile: cl2c.pl,v $
13 # $Revision: 1.3 $
15 # This file is part of OpenOffice.org.
17 # OpenOffice.org is free software: you can redistribute it and/or modify
18 # it under the terms of the GNU Lesser General Public License version 3
19 # only, as published by the Free Software Foundation.
21 # OpenOffice.org is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU Lesser General Public License version 3 for more details
25 # (a copy is included in the LICENSE file that accompanied this code).
27 # You should have received a copy of the GNU Lesser General Public License
28 # version 3 along with OpenOffice.org. If not, see
29 # <http://www.openoffice.org/license.html>
30 # for a copy of the LGPLv3 License.
32 ##########################################################################
34 if ( $#ARGV != 3 ) {
35 print STDERR "usage: cl2c.pl <file.cl> <file.c> <file.src> <resname>\n";
36 exit -1;
39 $CL=$ARGV[0];
40 $C=$ARGV[1];
41 $SRC=$ARGV[2];
42 $RNAME=$ARGV[3];
44 sub sconv
46 local($s)=@_[0];
47 local($o,$c);
48 $_="";
49 foreach $o ( unpack("C*",$s) ) {
50 $c=chr($o);
51 if ( $o >= 32 && $o < 127 ) {
52 $_ .= $c;
53 } else {
54 $_ .= sprintf("\\%o", $o);
57 return $_;
61 sub makeneutral {
63 print COUT "\n\n/**\n";
64 print COUT " * Get neutral language for specific language.\n";
65 print COUT " * This simplifies the getText switch cases and allows to handle\n";
66 print COUT " * previously unknown language derivates due to foreign installations.\n";
67 print COUT " * If you want to distinguish between some dialects change this function\n";
68 print COUT " * to return the desired nLang before doing the bit masking stuff.\n";
69 print COUT " * See xlang.h for defined LANGUAGE_*\n";
70 print COUT " */\n";
72 # taken from tools/source/intntl/intn.cxx International::GetNeutralLanguage
73 print COUT "static USHORT GetNeutralLanguage( USHORT nLang )\n";
74 print COUT "{\n";
75 print COUT "\tUSHORT nPrimLang;\n";
76 print COUT "\n";
77 print COUT "\t/* ignore LANGUAGE_USER* */\n";
78 print COUT "\tif ( (nLang & 0x03FF) >= 0x0200 )\n";
79 print COUT "\t return nLang;\n";
80 print COUT "\n";
81 print COUT "\tnLang &= 0x03FF;\n";
82 print COUT "\n";
83 print COUT "\tnPrimLang = nLang | 0x0400;\n";
84 print COUT "\n";
85 print COUT "\tswitch ( nPrimLang )\n";
86 print COUT "\t{\n";
87 print COUT "\t\tcase LANGUAGE_CHINESE_TRADITIONAL:\n";
88 print COUT "\t\t\tnLang = LANGUAGE_CHINESE;\n";
89 print COUT "\t\t\tbreak;\n";
90 print COUT "\t\tcase LANGUAGE_ENGLISH_US:\n";
91 print COUT "\t\t\tnLang = LANGUAGE_ENGLISH;\n";
92 print COUT "\t\t\tbreak;\n";
93 print COUT "\t\tcase LANGUAGE_NORWEGIAN_BOKMAL:\n";
94 print COUT "\t\t\tnLang = LANGUAGE_NORWEGIAN;\n";
95 print COUT "\t\t\tbreak;\n";
96 print COUT "\t\tcase LANGUAGE_PORTUGUESE_BRAZILIAN:\n";
97 print COUT "\t\t\tnLang = LANGUAGE_PORTUGUESE;\n";
98 print COUT "\t\t\tbreak;\n";
99 print COUT "\n";
100 print COUT "\t\tdefault:\n";
101 print COUT "\t\t\tnLang = nPrimLang;\n";
102 print COUT "\t\t\tbreak;\n";
103 print COUT "\t}\n";
104 print COUT "\n";
105 print COUT "\treturn nLang;\n";
106 print COUT "}\n";
107 print COUT "\n";
112 sub maketext {
114 print COUT "\n\n/**\n";
115 print COUT " * Get text resource for current language.\n";
116 print COUT " * Remember that 8-bit characters are shown in\n";
117 print COUT " * system dependend code pages!\n";
118 print COUT " * To get correct results you will have to distuinguish\n";
119 print COUT " * for example between UNIX and WIN and OS2 target systems.\n";
120 print COUT " */\n";
122 print COUT "static char* getText( int nResource )\n{\n";
123 print COUT "\tswitch( nResource ) {\n";
125 $resflag=0;
126 $strname="";
127 $cnt=0;
128 $text_english="";
130 while (<SRCIN>) {
131 $resflag=1 if ( /Resource\s$RNAME/ );
133 if ( /\{/ ) {
134 if ( ++$cnt == 2 ) {
135 # start language
136 $text_english="";
137 print COUT "\t\t\tswitch( _nLanguage ) {\n";
138 next;
142 if ( /\}/ ) {
143 if ( --$cnt == 1 ) {
144 # end language
146 if ( $text_english ne "" ) {
147 print COUT "\t\t\t\tcase LANGUAGE_ENGLISH:\n\t\t\t\tdefault:\n";
148 print COUT "\t\t\t\treturn(" . $text_english . ")\;\n";
151 print COUT "\t\t\t}\n\t\t\tbreak;\n";
152 next;
153 } elsif ( $cnt == 0 ) {
154 # end of resource
155 $resflag=0;
156 print COUT "\t\tdefault:\n\t\t\tbreak;\n";
157 print COUT "\t}\n\treturn(\"\");\n}\n";
158 next;
163 if ( $resflag && $cnt == 1) {
164 if ( /\sString\s(([A-Z]|\_|[0-9]|[a-z])*)/ ) {
165 $strname=$1;
166 print COUT "\t\tcase " . $strname . ":\n";
170 if ( $cnt == 2 && /^\s*Text/ ) {
171 $langname="german";
172 ($textdef,@textx)=split(/=/);
173 $text=join("=",@textx);
174 if ( $textdef =~ /\[\s+(.*)\s+\]/ ) {
175 $langname=$1;
177 else {
178 $langname="ENGLISH_US"; # no [...] => not to be translated
181 $langname="LANGUAGE_" . uc($langname);
183 chop($text) while ( $text=~/(\r|\n|\;)$/ );
184 $text=sconv($text);
185 # english_us, not english because it's developer's pigeon
186 if ( $langname eq "LANGUAGE_ENGLISH_US" ) {
187 $text_english=$text;
189 # ISO coded, obtain at least the default
190 elsif ( $langname =~ /^LANGUAGE_EN-US$/ ) {
191 $text_english=$text;
193 # we don't know about USER languages, ENGLISH will be appended later
194 elsif ( ! ( $langname =~ /LANGUAGE_USER/ || $langname =~ /^LANGUAGE_ENGLISH$/ ) ) {
195 # ER 28.04.99: for the moment only German and English are
196 # exported, because we have a problem with non-existing
197 # character code tables for systems other than Windoze.
198 # => Chinese would be definitely mixed up and we don't
199 # want to insult anybody.. others like Spanish would look
200 # very ugly, but we'll have to live with bad German Umlauts.
201 if ( $langname =~ /LANGUAGE_(GERMAN|ENGLISH)/ ) {
202 print COUT "\t\t\t\tcase " . $langname . ":\n";
203 print COUT "\t\t\t\treturn(" . $text . ")\;\n";
210 makeneutral();
214 open(CLIN,"<$CL") || die "can not open $CL\n";
215 open(SRCIN,"<$SRC") || die "can not open $CL\n";
216 open(COUT,">$C") || die "can not open $CL\n";
218 $ccnt=0;
219 $incomment=0;
220 while(<CLIN>) {
221 if ( /^\/\*--(-*)/ ) {
222 $incomment=1;
223 $ccnt++;
226 print COUT $_ if ( $incomment==0 || $ccnt==1 );
228 &maketext() if ( /^static USHORT _nLanguage=/ );
230 if ( /(-*)--\*\/$/ ) {
231 $incomment=0;
236 close(CLIN);
237 close(SRCIN);
238 close(COUT);
240 exit 0;