xref: /aoo4110/main/sc/addin/util/cl2c.pl (revision b1cdbd2c)
1*b1cdbd2cSJim Jagielski#!/usr/solar/bin/perl
2*b1cdbd2cSJim Jagielski
3*b1cdbd2cSJim Jagielski#**************************************************************
4*b1cdbd2cSJim Jagielski#
5*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
6*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
7*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
8*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
9*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
10*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
11*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
12*b1cdbd2cSJim Jagielski#
13*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
14*b1cdbd2cSJim Jagielski#
15*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
16*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
17*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
19*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
20*b1cdbd2cSJim Jagielski#  under the License.
21*b1cdbd2cSJim Jagielski#
22*b1cdbd2cSJim Jagielski#**************************************************************
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielskiif ( $#ARGV != 3 ) {
25*b1cdbd2cSJim Jagielski	print STDERR "usage: cl2c.pl <file.cl> <file.c> <file.src> <resname>\n";
26*b1cdbd2cSJim Jagielski	exit -1;
27*b1cdbd2cSJim Jagielski}
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski$CL=$ARGV[0];
30*b1cdbd2cSJim Jagielski$C=$ARGV[1];
31*b1cdbd2cSJim Jagielski$SRC=$ARGV[2];
32*b1cdbd2cSJim Jagielski$RNAME=$ARGV[3];
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielskisub sconv
35*b1cdbd2cSJim Jagielski{
36*b1cdbd2cSJim Jagielski	local($s)=@_[0];
37*b1cdbd2cSJim Jagielski	local($o,$c);
38*b1cdbd2cSJim Jagielski	$_="";
39*b1cdbd2cSJim Jagielski	foreach $o ( unpack("C*",$s) ) {
40*b1cdbd2cSJim Jagielski		$c=chr($o);
41*b1cdbd2cSJim Jagielski		if ( $o >= 32 && $o < 127 ) {
42*b1cdbd2cSJim Jagielski			$_ .= $c;
43*b1cdbd2cSJim Jagielski		} else {
44*b1cdbd2cSJim Jagielski			$_ .= sprintf("\\%o", $o);
45*b1cdbd2cSJim Jagielski		}
46*b1cdbd2cSJim Jagielski	}
47*b1cdbd2cSJim Jagielski	return $_;
48*b1cdbd2cSJim Jagielski}
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski
51*b1cdbd2cSJim Jagielskisub makeneutral {
52*b1cdbd2cSJim Jagielski
53*b1cdbd2cSJim Jagielski	print COUT "\n\n/**\n";
54*b1cdbd2cSJim Jagielski	print COUT " * Get neutral language for specific language.\n";
55*b1cdbd2cSJim Jagielski	print COUT " * This simplifies the getText switch cases and allows to handle\n";
56*b1cdbd2cSJim Jagielski	print COUT " * previously unknown language derivates due to foreign installations.\n";
57*b1cdbd2cSJim Jagielski	print COUT " * If you want to distinguish between some dialects change this function\n";
58*b1cdbd2cSJim Jagielski	print COUT " * to return the desired nLang before doing the bit masking stuff.\n";
59*b1cdbd2cSJim Jagielski	print COUT " * See xlang.h for defined LANGUAGE_*\n";
60*b1cdbd2cSJim Jagielski	print COUT " */\n";
61*b1cdbd2cSJim Jagielski
62*b1cdbd2cSJim Jagielski	# taken from tools/source/intntl/intn.cxx International::GetNeutralLanguage
63*b1cdbd2cSJim Jagielski	print COUT "static USHORT GetNeutralLanguage( USHORT nLang )\n";
64*b1cdbd2cSJim Jagielski	print COUT "{\n";
65*b1cdbd2cSJim Jagielski	print COUT "\tUSHORT nPrimLang;\n";
66*b1cdbd2cSJim Jagielski	print COUT "\n";
67*b1cdbd2cSJim Jagielski	print COUT "\t/* ignore LANGUAGE_USER* */\n";
68*b1cdbd2cSJim Jagielski	print COUT "\tif ( (nLang & 0x03FF) >= 0x0200 )\n";
69*b1cdbd2cSJim Jagielski	print COUT "\t	return nLang;\n";
70*b1cdbd2cSJim Jagielski	print COUT "\n";
71*b1cdbd2cSJim Jagielski	print COUT "\tnLang &= 0x03FF;\n";
72*b1cdbd2cSJim Jagielski	print COUT "\n";
73*b1cdbd2cSJim Jagielski	print COUT "\tnPrimLang = nLang | 0x0400;\n";
74*b1cdbd2cSJim Jagielski	print COUT "\n";
75*b1cdbd2cSJim Jagielski	print COUT "\tswitch ( nPrimLang )\n";
76*b1cdbd2cSJim Jagielski	print COUT "\t{\n";
77*b1cdbd2cSJim Jagielski	print COUT "\t\tcase LANGUAGE_CHINESE_TRADITIONAL:\n";
78*b1cdbd2cSJim Jagielski	print COUT "\t\t\tnLang = LANGUAGE_CHINESE;\n";
79*b1cdbd2cSJim Jagielski	print COUT "\t\t\tbreak;\n";
80*b1cdbd2cSJim Jagielski	print COUT "\t\tcase LANGUAGE_ENGLISH_US:\n";
81*b1cdbd2cSJim Jagielski	print COUT "\t\t\tnLang = LANGUAGE_ENGLISH;\n";
82*b1cdbd2cSJim Jagielski	print COUT "\t\t\tbreak;\n";
83*b1cdbd2cSJim Jagielski	print COUT "\t\tcase LANGUAGE_NORWEGIAN_BOKMAL:\n";
84*b1cdbd2cSJim Jagielski	print COUT "\t\t\tnLang = LANGUAGE_NORWEGIAN;\n";
85*b1cdbd2cSJim Jagielski	print COUT "\t\t\tbreak;\n";
86*b1cdbd2cSJim Jagielski	print COUT "\t\tcase LANGUAGE_PORTUGUESE_BRAZILIAN:\n";
87*b1cdbd2cSJim Jagielski	print COUT "\t\t\tnLang = LANGUAGE_PORTUGUESE;\n";
88*b1cdbd2cSJim Jagielski	print COUT "\t\t\tbreak;\n";
89*b1cdbd2cSJim Jagielski	print COUT "\n";
90*b1cdbd2cSJim Jagielski	print COUT "\t\tdefault:\n";
91*b1cdbd2cSJim Jagielski	print COUT "\t\t\tnLang = nPrimLang;\n";
92*b1cdbd2cSJim Jagielski	print COUT "\t\t\tbreak;\n";
93*b1cdbd2cSJim Jagielski	print COUT "\t}\n";
94*b1cdbd2cSJim Jagielski	print COUT "\n";
95*b1cdbd2cSJim Jagielski	print COUT "\treturn nLang;\n";
96*b1cdbd2cSJim Jagielski	print COUT "}\n";
97*b1cdbd2cSJim Jagielski	print COUT "\n";
98*b1cdbd2cSJim Jagielski
99*b1cdbd2cSJim Jagielski}
100*b1cdbd2cSJim Jagielski
101*b1cdbd2cSJim Jagielski
102*b1cdbd2cSJim Jagielskisub maketext {
103*b1cdbd2cSJim Jagielski
104*b1cdbd2cSJim Jagielski	print COUT "\n\n/**\n";
105*b1cdbd2cSJim Jagielski	print COUT " * Get text resource for current language.\n";
106*b1cdbd2cSJim Jagielski	print COUT " * Remember that 8-bit characters are shown in\n";
107*b1cdbd2cSJim Jagielski	print COUT " * system dependend code pages!\n";
108*b1cdbd2cSJim Jagielski	print COUT " * To get correct results you will have to distuinguish\n";
109*b1cdbd2cSJim Jagielski	print COUT " * for example between UNIX and WIN and OS2 target systems.\n";
110*b1cdbd2cSJim Jagielski	print COUT " */\n";
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski	print COUT "static char* getText( int nResource )\n{\n";
113*b1cdbd2cSJim Jagielski	print COUT "\tswitch( nResource ) {\n";
114*b1cdbd2cSJim Jagielski
115*b1cdbd2cSJim Jagielski	$resflag=0;
116*b1cdbd2cSJim Jagielski	$strname="";
117*b1cdbd2cSJim Jagielski	$cnt=0;
118*b1cdbd2cSJim Jagielski	$text_english="";
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim Jagielski	while (<SRCIN>) {
121*b1cdbd2cSJim Jagielski		$resflag=1 if ( /Resource\s$RNAME/ );
122*b1cdbd2cSJim Jagielski
123*b1cdbd2cSJim Jagielski		if ( /\{/ ) {
124*b1cdbd2cSJim Jagielski			if ( ++$cnt == 2 ) {
125*b1cdbd2cSJim Jagielski				# start language
126*b1cdbd2cSJim Jagielski				$text_english="";
127*b1cdbd2cSJim Jagielski				print COUT "\t\t\tswitch( _nLanguage ) {\n";
128*b1cdbd2cSJim Jagielski				next;
129*b1cdbd2cSJim Jagielski			}
130*b1cdbd2cSJim Jagielski		}
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski		if ( /\}/ ) {
133*b1cdbd2cSJim Jagielski			if ( --$cnt == 1 ) {
134*b1cdbd2cSJim Jagielski				# end language
135*b1cdbd2cSJim Jagielski
136*b1cdbd2cSJim Jagielski				if ( $text_english ne "" ) {
137*b1cdbd2cSJim Jagielski					print COUT "\t\t\t\tcase LANGUAGE_ENGLISH:\n\t\t\t\tdefault:\n";
138*b1cdbd2cSJim Jagielski					print COUT "\t\t\t\treturn(" . $text_english . ")\;\n";
139*b1cdbd2cSJim Jagielski				}
140*b1cdbd2cSJim Jagielski
141*b1cdbd2cSJim Jagielski				print COUT "\t\t\t}\n\t\t\tbreak;\n";
142*b1cdbd2cSJim Jagielski				next;
143*b1cdbd2cSJim Jagielski			} elsif ( $cnt == 0 ) {
144*b1cdbd2cSJim Jagielski				# end of resource
145*b1cdbd2cSJim Jagielski				$resflag=0;
146*b1cdbd2cSJim Jagielski				print COUT "\t\tdefault:\n\t\t\tbreak;\n";
147*b1cdbd2cSJim Jagielski				print COUT "\t}\n\treturn(\"\");\n}\n";
148*b1cdbd2cSJim Jagielski				next;
149*b1cdbd2cSJim Jagielski			}
150*b1cdbd2cSJim Jagielski
151*b1cdbd2cSJim Jagielski		}
152*b1cdbd2cSJim Jagielski
153*b1cdbd2cSJim Jagielski		if ( $resflag && $cnt == 1) {
154*b1cdbd2cSJim Jagielski			if ( /\sString\s(([A-Z]|\_|[0-9]|[a-z])*)/ ) {
155*b1cdbd2cSJim Jagielski				$strname=$1;
156*b1cdbd2cSJim Jagielski				print COUT "\t\tcase " . $strname . ":\n";
157*b1cdbd2cSJim Jagielski			}
158*b1cdbd2cSJim Jagielski		}
159*b1cdbd2cSJim Jagielski
160*b1cdbd2cSJim Jagielski		if ( $cnt == 2 && /^\s*Text/ ) {
161*b1cdbd2cSJim Jagielski			$langname="german";
162*b1cdbd2cSJim Jagielski			($textdef,@textx)=split(/=/);
163*b1cdbd2cSJim Jagielski			$text=join("=",@textx);
164*b1cdbd2cSJim Jagielski			if ( $textdef =~ /\[\s+(.*)\s+\]/ ) {
165*b1cdbd2cSJim Jagielski				$langname=$1;
166*b1cdbd2cSJim Jagielski			}
167*b1cdbd2cSJim Jagielski            else {
168*b1cdbd2cSJim Jagielski                $langname="ENGLISH_US";     # no [...] => not to be translated
169*b1cdbd2cSJim Jagielski            }
170*b1cdbd2cSJim Jagielski
171*b1cdbd2cSJim Jagielski			$langname="LANGUAGE_" . uc($langname);
172*b1cdbd2cSJim Jagielski
173*b1cdbd2cSJim Jagielski			chop($text) while ( $text=~/(\r|\n|\;)$/ );
174*b1cdbd2cSJim Jagielski			$text=sconv($text);
175*b1cdbd2cSJim Jagielski			# english_us, not english because it's developer's pigeon
176*b1cdbd2cSJim Jagielski			if ( $langname eq "LANGUAGE_ENGLISH_US" ) {
177*b1cdbd2cSJim Jagielski				$text_english=$text;
178*b1cdbd2cSJim Jagielski			}
179*b1cdbd2cSJim Jagielski            # ISO coded, obtain at least the default
180*b1cdbd2cSJim Jagielski            elsif ( $langname =~ /^LANGUAGE_EN-US$/ ) {
181*b1cdbd2cSJim Jagielski				$text_english=$text;
182*b1cdbd2cSJim Jagielski            }
183*b1cdbd2cSJim Jagielski			# we don't know about USER languages, ENGLISH will be appended later
184*b1cdbd2cSJim Jagielski			elsif ( ! ( $langname =~ /LANGUAGE_USER/ || $langname =~ /^LANGUAGE_ENGLISH$/ ) ) {
185*b1cdbd2cSJim Jagielski				# ER 28.04.99: for the moment only German and English are
186*b1cdbd2cSJim Jagielski				# exported, because we have a problem with non-existing
187*b1cdbd2cSJim Jagielski				# character code tables for systems other than Windoze.
188*b1cdbd2cSJim Jagielski				# => Chinese would be definitely mixed up and we don't
189*b1cdbd2cSJim Jagielski				# want to insult anybody.. others like Spanish would look
190*b1cdbd2cSJim Jagielski				# very ugly, but we'll have to live with bad German Umlauts.
191*b1cdbd2cSJim Jagielski				if ( $langname =~ /LANGUAGE_(GERMAN|ENGLISH)/ ) {
192*b1cdbd2cSJim Jagielski					print COUT "\t\t\t\tcase " . $langname . ":\n";
193*b1cdbd2cSJim Jagielski					print COUT "\t\t\t\treturn(" . $text . ")\;\n";
194*b1cdbd2cSJim Jagielski				}
195*b1cdbd2cSJim Jagielski			}
196*b1cdbd2cSJim Jagielski
197*b1cdbd2cSJim Jagielski		}
198*b1cdbd2cSJim Jagielski	}
199*b1cdbd2cSJim Jagielski
200*b1cdbd2cSJim Jagielski	makeneutral();
201*b1cdbd2cSJim Jagielski
202*b1cdbd2cSJim Jagielski}
203*b1cdbd2cSJim Jagielski
204*b1cdbd2cSJim Jagielskiopen(CLIN,"<$CL") || die "can not open $CL\n";
205*b1cdbd2cSJim Jagielskiopen(SRCIN,"<$SRC") || die "can not open $CL\n";
206*b1cdbd2cSJim Jagielskiopen(COUT,">$C") || die "can not open $CL\n";
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski$ccnt=0;
209*b1cdbd2cSJim Jagielski$incomment=0;
210*b1cdbd2cSJim Jagielskiwhile(<CLIN>) {
211*b1cdbd2cSJim Jagielski	if ( /^\/\*--(-*)/ ) {
212*b1cdbd2cSJim Jagielski		$incomment=1;
213*b1cdbd2cSJim Jagielski		$ccnt++;
214*b1cdbd2cSJim Jagielski	}
215*b1cdbd2cSJim Jagielski
216*b1cdbd2cSJim Jagielski	print COUT $_ if ( $incomment==0 || $ccnt==1 );
217*b1cdbd2cSJim Jagielski
218*b1cdbd2cSJim Jagielski	&maketext() if ( /^static USHORT _nLanguage=/ );
219*b1cdbd2cSJim Jagielski
220*b1cdbd2cSJim Jagielski	if ( /(-*)--\*\/$/ ) {
221*b1cdbd2cSJim Jagielski		$incomment=0;
222*b1cdbd2cSJim Jagielski	}
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski}
225*b1cdbd2cSJim Jagielski
226*b1cdbd2cSJim Jagielskiclose(CLIN);
227*b1cdbd2cSJim Jagielskiclose(SRCIN);
228*b1cdbd2cSJim Jagielskiclose(COUT);
229*b1cdbd2cSJim Jagielski
230*b1cdbd2cSJim Jagielskiexit 0;
231*b1cdbd2cSJim Jagielski
232*b1cdbd2cSJim Jagielski
233