Update ooo320-m1
[ooovba.git] / transex3 / source / localize.cxx
blob625b08b03632dd64148c0a7568fb0ffc1ea4287b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localize.cxx,v $
10 * $Revision: 1.50 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_transex3.hxx"
34 #include "srciter.hxx"
35 #include "export.hxx"
36 #include <stdio.h>
37 #include "tools/errcode.hxx"
38 #include "tools/fsys.hxx"
40 #ifndef TRANSEX_FILE_HXX
41 #define TRANSEX_FILE_HXX
42 #include <transex3/file.hxx>
43 #endif
47 // SourceTreeLocalizer
50 const char *ExeTable[][5] = {
51 { "src", "transex3", " -UTF8 -e", "negative", "noiso" },
52 { "hrc", "transex3", " -UTF8 -e", "positive", "noiso" },
53 //{ "src", "transex3", "-UTF8 -e", "negative", "noiso" },
54 //{ "hrc", "transex3", "-UTF8 -e", "positive", "noiso" },
56 //{ "lng", "lngex", "-UTF8 -e", "negative", "noiso" },
57 { "ulf", "ulfex", " -e", "negative", "noiso" },
58 { "xrb", "xmlex", "-UTF8 -e", "negative", "iso" },
59 { "xxl", "xmlex", "-UTF8 -e", "negative", "iso" },
60 { "xgf", "xmlex", "-UTF8 -e -t:xgf", "negative", "iso" },
61 { "xcd", "cfgex", "-UTF8 -e", "negative", "iso" },
62 { "xcu", "cfgex", "-UTF8 -e", "negative", "iso" },
63 { "xcs", "cfgex", "-UTF8 -e -f", "negative", "iso" },
64 { "xrm", "xrmex", "-UTF8 -e", "negative", "iso" },
65 { "xhp", "helpex", " -e", "negative", "noiso" },
66 { "NULL", "NULL", "NULL", "NULL", "NULL" }
69 const char *NegativeList[] = {
70 "officecfg/data/org.openoffice.Office.Labels.xcd",
71 "officecfg/data/org/openoffice/Office/Labels.xcd",
72 "officecfg/data/org/openoffice/Office/SFX.xcd",
73 "officecfg/data/org/openoffice/Office/Accelerators.xcu",
74 "hidother.src",
75 "NULL"
78 const char *PositiveList[] = {
79 "svx/inc/globlmn_tmpl.hrc",
80 "sw/source/ui/inc/swmn_tmpl.hrc",
81 "sw/source/ui/inc/swacc_tmpl.hrc",
82 "sw/source/ui/inc/toolbox_tmpl.hrc",
83 "offmgr/inc/offmenu_tmpl.hrc",
84 "offmgr/source/offapp/intro/intro_tmpl.hrc",
85 "dbaccess/source/ui/inc/toolbox_tmpl.hrc",
86 "svx/source/intro/intro_tmpl.hrc",
87 "dbaccess/source/ui/dlg/AutoControls_tmpl.hrc",
88 "svx/source/unodialogs/textconversiondlgs/chinese_direction_tmpl.hrc",
89 "chart2/source/controller/dialogs/res_DataLabel_tmpl.hrc",
90 "chart2/source/controller/dialogs/res_LegendPosition_tmpl.hrc",
91 "chart2/source/controller/dialogs/res_Statistic_tmpl.hrc",
92 "chart2/source/controller/dialogs/res_Titlesx_tmpl.hrc",
93 "chart2/source/controller/dialogs/res_SecondaryAxisCheckBoxes_tmpl.hrc",
94 "chart2/source/controller/menu/MenuItems_tmpl.hrc",
95 "chart2/source/controller/dialogs/res_ErrorBar_tmpl.hrc",
96 "chart2/source/controller/dialogs/res_Trendline_tmpl.hrc",
97 "svx.link/inc/globlmn_tmpl.hrc",
98 "sw.link/source/ui/inc/swmn_tmpl.hrc",
99 "sw.link/source/ui/inc/swacc_tmpl.hrc",
100 "sw.link/source/ui/inc/toolbox_tmpl.hrc",
101 "offmgr.link/inc/offmenu_tmpl.hrc",
102 "offmgr.link/source/offapp/intro/intro_tmpl.hrc",
103 "dbaccess.link/source/ui/inc/toolbox_tmpl.hrc",
104 "svx.link/source/intro/intro_tmpl.hrc",
105 "dbaccess.link/source/ui/dlg/AutoControls_tmpl.hrc",
106 "svx.link/source/unodialogs/textconversiondlgs/chinese_direction_tmpl.hrc",
107 "chart2.link/source/controller/dialogs/res_DataLabel_tmpl.hrc",
108 "chart2.link/source/controller/dialogs/res_LegendPosition_tmpl.hrc",
109 "chart2.link/source/controller/dialogs/res_Statistic_tmpl.hrc",
110 "chart2.link/source/controller/dialogs/res_Titlesx_tmpl.hrc",
111 "chart2.link/source/controller/dialogs/res_SecondaryAxisCheckBoxes_tmpl.hrc",
112 "chart2.link/source/controller/menu/MenuItems_tmpl.hrc",
113 "chart2.link/source/controller/dialogs/res_ErrorBar_tmpl.hrc",
114 "chart2.link/source/controller/dialogs/res_Trendline_tmpl.hrc",
115 "NULL"
119 const char PRJ_DIR_NAME[] = "prj";
120 const char DLIST_NAME[] = "d.lst";
122 #define LOCALIZE_NONE 0x0000
123 #define LOCALIZE_EXTRACT 0x0001
124 #define LOCALIZE_MERGE 0x0002
126 class SourceTreeLocalizer : public SourceTreeIterator
128 private:
129 SvFileStream aSDF;
130 USHORT nMode;
132 ByteString sLanguageRestriction;
134 ByteString sIsoCode99;
135 ByteString sOutputFile;
136 bool bQuiet2;
138 int nFileCnt;
140 const ByteString GetProjectName( BOOL bAbs = FALSE );
141 const ByteString GetProjectRootRel();
144 BOOL CheckNegativeList( const ByteString &rFileName );
145 BOOL CheckPositiveList( const ByteString &rFileName );
147 void WorkOnFile(
148 const ByteString &rFileName,
149 const ByteString &rExecutable,
150 const ByteString &rParameter,
151 const ByteString &rIso
154 void WorkOnFileType(
155 const ByteString &rDirectory,
156 const ByteString &rExtension,
157 const ByteString &rExecutable,
158 const ByteString &rParameter,
159 const ByteString &rCollectMode,
160 const ByteString &rIso
162 void WorkOnDirectory( const ByteString &rDirectory );
163 BOOL ExecuteMerge();
164 BOOL MergeSingleFile(
165 const ByteString &rPrj,
166 const ByteString &rFile,
167 const ByteString &rSDFFile
170 public:
171 SourceTreeLocalizer( const ByteString &rRoot, const ByteString &rVersion , bool bLocal , bool bQuiet2_in , bool skip_links );
172 ~SourceTreeLocalizer();
174 ByteString getSourceLanguages( ByteString sLanguageRestriction , ByteString sCommand );
176 void SetLanguageRestriction( const ByteString& rRestrictions )
177 { sLanguageRestriction = rRestrictions; }
178 void SetIsoCode99( const ByteString& rIsoCode )
179 { sIsoCode99 = rIsoCode; }
180 int getFileCnt();
181 BOOL Extract( const ByteString &rDestinationFile );
182 BOOL Merge( const ByteString &rSourceFile , const ByteString &rOutput );
183 int GetFileCnt();
184 virtual void OnExecuteDirectory( const rtl::OUString &rDirectory );
187 /*****************************************************************************/
188 SourceTreeLocalizer::SourceTreeLocalizer(
189 const ByteString &rRoot, const ByteString &rVersion, bool bLocal_in , bool bQuiet2_in , bool skip_links )
190 /*****************************************************************************/
191 : SourceTreeIterator( rRoot, rVersion , bLocal_in ),
192 nMode( LOCALIZE_NONE ),
193 bQuiet2( bQuiet2_in ),
194 nFileCnt( 0 )
196 bSkipLinks = skip_links ;
199 /*****************************************************************************/
200 SourceTreeLocalizer::~SourceTreeLocalizer()
201 /*****************************************************************************/
205 /*****************************************************************************/
206 const ByteString SourceTreeLocalizer::GetProjectName( BOOL bAbs )
207 /*****************************************************************************/
209 BOOL bFound = FALSE;
210 DirEntry aCur;
211 aCur.ToAbs();
213 for ( ; ! bFound && aCur.Level() > 1; aCur.CutName() )
215 DirEntry aTest = aCur + DirEntry(PRJ_DIR_NAME) + DirEntry(DLIST_NAME);
216 if ( aTest.Exists() )
218 // HACK !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
219 if (( ByteString( aCur.GetName(), RTL_TEXTENCODING_ASCII_US ).Equals("webinstall") ) ||
220 ( ByteString( aCur.GetName(), RTL_TEXTENCODING_ASCII_US ).Equals("portal") ) ||
221 ( ByteString( aCur.GetName(), RTL_TEXTENCODING_ASCII_US ).Equals("xulclient") ) ||
222 ( ByteString( aCur.GetName(), RTL_TEXTENCODING_ASCII_US ).Search( "wdk_" ) == 0 ))
223 return "";
224 // end HACK !!!!!!!!!!!!!!!!!!!!!!!!!
228 if ( bAbs )
229 return ByteString( aCur.GetFull(), RTL_TEXTENCODING_ASCII_US );
230 else
231 return ByteString( aCur.GetName(), RTL_TEXTENCODING_ASCII_US );
235 return "";
237 /*****************************************************************************/
238 int SourceTreeLocalizer::GetFileCnt(){
239 /*****************************************************************************/
240 return nFileCnt;
243 /*****************************************************************************/
244 const ByteString SourceTreeLocalizer::GetProjectRootRel()
245 /*****************************************************************************/
247 ByteString sProjectRoot( GetProjectName( TRUE ));
248 DirEntry aCur;
249 aCur.ToAbs();
250 ByteString sCur( aCur.GetFull(), RTL_TEXTENCODING_ASCII_US );
252 if( sCur.SearchAndReplace( sProjectRoot, "" ) == STRING_NOTFOUND )
253 return "";
255 ByteString sDelimiter(
256 DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
258 sCur.SearchAndReplaceAll( sDelimiter, "/" );
259 sCur.EraseLeadingChars( '/' );
260 ULONG nCount = sCur.GetTokenCount( '/' );
262 ByteString sProjectRootRel;
263 for ( ULONG i = 0; i < nCount; i++ ) {
264 if ( sProjectRootRel.Len())
265 sProjectRootRel += sDelimiter;
266 sProjectRootRel += "..";
268 if ( sProjectRootRel.Len())
269 return sProjectRootRel;
271 return ".";
274 bool skipProject( ByteString sPrj )
276 static const ByteString READLICENSE( "readlicense" );
277 return sPrj.EqualsIgnoreCaseAscii( READLICENSE );
280 /*****************************************************************************/
281 void SourceTreeLocalizer::WorkOnFile(
282 const ByteString &rFileName, const ByteString &rExecutable,
283 const ByteString &rParameter, const ByteString &rIso )
284 /*****************************************************************************/
286 (void) rIso; // Remove me ;)
287 String sFull( rFileName, RTL_TEXTENCODING_ASCII_US );
288 DirEntry aEntry( sFull );
289 ByteString sFileName( aEntry.GetName(), RTL_TEXTENCODING_ASCII_US );
291 // set current working directory
292 DirEntry aPath( aEntry.GetPath());
293 DirEntry aOldCWD;
294 aPath.SetCWD();
296 ByteString sPrj( GetProjectName());
297 //printf ("prj = %s , exe = %s\n", sPrj.GetBuffer() , rExecutable.GetBuffer() );
298 // printf("Skip %s = %d \n",sPrj.GetBuffer() , skipProject( sPrj ) );
299 //printf("prj = %s\n",sPrj.GetBuffer());
300 if ( sPrj.Len() && !skipProject( sPrj ) )
302 ByteString sRoot( GetProjectRootRel());
304 // get temp file
305 DirEntry aTemp( Export::GetTempFile());
306 ByteString sTempFile( aTemp.GetFull(), RTL_TEXTENCODING_ASCII_US );
308 ByteString sDel;
309 #if defined(WNT) || defined(OS2)
310 sDel=ByteString("\\");
311 #else
312 sDel=ByteString("/");
313 #endif
314 ByteString sPath1( Export::GetEnv("SOLARVER") );
315 ByteString sPath2( Export::GetEnv("INPATH") );
316 ByteString sPath3( "bin" );
317 ByteString sPath4( Export::GetEnv("UPDMINOREXT") );
318 ByteString sExecutable( sPath1 );
319 sExecutable += sDel ;
320 sExecutable += sPath2 ;
321 sExecutable += sDel;
322 sExecutable += sPath3 ;
323 sExecutable += sPath4 ;
324 sExecutable += sDel ;
325 sExecutable += rExecutable ;
328 ByteString sCommand( sExecutable );
329 sCommand += " ";
330 sCommand += rParameter;
331 sCommand += " -p ";
332 sCommand += sPrj;
333 sCommand += " -r ";
334 sCommand += sRoot;
335 sCommand += " -i ";
336 sCommand += sFileName;
337 sCommand += " -o ";
338 sCommand += sTempFile;
339 if ( sLanguageRestriction.Len()) {
340 sCommand += " -l ";
341 sCommand += getSourceLanguages( sLanguageRestriction , sCommand );
344 if( bQuiet2 ){
345 sCommand +=" -QQ ";
347 //printf("DBG: %s\n",sCommand.GetBuffer());
348 system( sCommand.GetBuffer());
349 nFileCnt++;
350 printf(".");
351 fflush( stdout );
353 SvFileStream aSDFIn( aTemp.GetFull(), STREAM_READ );
354 ByteString sLine;
355 while ( aSDFIn.IsOpen() && !aSDFIn.IsEof()) {
356 aSDFIn.ReadLine( sLine );
357 if ( sLine.Len()) {
358 aSDF.WriteLine( sLine );
361 aSDFIn.Close();
363 aTemp.Kill();
366 // reset current working directory
367 aOldCWD.SetCWD();
370 ByteString SourceTreeLocalizer::getSourceLanguages( ByteString sLanguageRestriction_inout , ByteString sCommand )
372 // Source languages in helpcontent2 and macromigration en-US only!
373 if( sCommand.Search("helpex") != STRING_NOTFOUND ) {
374 sLanguageRestriction_inout.Assign( ByteString("en-US") );
376 else if( sCommand.Search("xmlex") != STRING_NOTFOUND ){
377 sLanguageRestriction_inout.Assign( ByteString("en-US") );
379 return sLanguageRestriction_inout;
382 /*****************************************************************************/
383 BOOL SourceTreeLocalizer::CheckNegativeList( const ByteString &rFileName )
384 /*****************************************************************************/
386 ULONG nIndex = 0;
387 BOOL bReturn = TRUE;
389 ByteString sDelimiter(
390 DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
392 ByteString sFileName( rFileName );
393 sFileName.ToLowerAscii();
395 ByteString sNegative( NegativeList[ nIndex ] );
396 while( !sNegative.Equals( "NULL" ) && bReturn ) {
397 sNegative.SearchAndReplaceAll( "\\", sDelimiter );
398 sNegative.SearchAndReplaceAll( "/", sDelimiter );
399 sNegative.ToLowerAscii();
401 if( sFileName.Search( sNegative ) == sFileName.Len() - sNegative.Len())
402 bReturn = FALSE;
404 nIndex++;
405 sNegative = NegativeList[ nIndex ];
408 return bReturn;
411 /*****************************************************************************/
412 BOOL SourceTreeLocalizer::CheckPositiveList( const ByteString &rFileName )
413 /*****************************************************************************/
415 ULONG nIndex = 0;
416 BOOL bReturn = FALSE;
418 ByteString sDelimiter(
419 DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
421 ByteString sFileName( rFileName );
422 sFileName.ToLowerAscii();
424 ByteString sNegative( PositiveList[ nIndex ] );
425 while( !sNegative.Equals( "NULL" ) && !bReturn ) {
426 sNegative.SearchAndReplaceAll( "\\", sDelimiter );
427 sNegative.SearchAndReplaceAll( "/", sDelimiter );
428 sNegative.ToLowerAscii();
430 if( sFileName.Search( sNegative ) == sFileName.Len() - sNegative.Len())
431 bReturn = TRUE;
433 nIndex++;
434 sNegative = PositiveList[ nIndex ];
437 return bReturn;
440 /*****************************************************************************/
441 void SourceTreeLocalizer::WorkOnFileType(
442 const ByteString &rDirectory, const ByteString &rExtension,
443 const ByteString &rExecutable, const ByteString &rParameter,
444 const ByteString &rCollectMode, const ByteString &rIso
446 /*****************************************************************************/
448 String sWild( rDirectory, RTL_TEXTENCODING_ASCII_US );
449 sWild += DirEntry::GetAccessDelimiter();
450 sWild += String::CreateFromAscii( "*." );
451 sWild += String( rExtension, RTL_TEXTENCODING_ASCII_US );
453 DirEntry aEntry( sWild );
454 Dir aDir( sWild, FSYS_KIND_FILE );
456 for ( USHORT i = 0; i < aDir.Count(); i++ ) {
457 DirEntry aFile( aDir[ i ] );
458 ByteString sFile( aFile.GetFull(), RTL_TEXTENCODING_ASCII_US );
460 BOOL bAllowed = TRUE;
462 if ( rCollectMode.Equals( "negative" ))
463 bAllowed = CheckNegativeList( sFile );
464 else if ( rCollectMode.Equals( "positive" ))
465 bAllowed = CheckPositiveList( sFile );
467 if ( bAllowed )
468 WorkOnFile( sFile, rExecutable, rParameter, rIso );
472 /*****************************************************************************/
473 void SourceTreeLocalizer::WorkOnDirectory( const ByteString &rDirectory )
474 /*****************************************************************************/
476 //printf("Working on Directory %s\n",rDirectory.GetBuffer());
477 ULONG nIndex = 0;
478 ByteString sExtension( ExeTable[ nIndex ][ 0 ] );
479 ByteString sExecutable( ExeTable[ nIndex ][ 1 ] );
480 ByteString sParameter( ExeTable[ nIndex ][ 2 ] );
481 ByteString sCollectMode( ExeTable[ nIndex ][ 3 ] );
482 ByteString sIso( ExeTable[ nIndex ][ 4 ] );
484 while( !sExtension.Equals( "NULL" )) {
485 WorkOnFileType(
486 rDirectory,
487 sExtension,
488 sExecutable,
489 sParameter,
490 sCollectMode,
491 sIso
494 nIndex++;
496 sExtension = ExeTable[ nIndex ][ 0 ];
497 sExecutable = ExeTable[ nIndex ][ 1 ];
498 sParameter = ExeTable[ nIndex ][ 2 ];
499 sCollectMode = ExeTable[ nIndex ][ 3 ];
500 sIso = ExeTable[ nIndex ][ 4 ];
504 void SourceTreeLocalizer::OnExecuteDirectory( const rtl::OUString &aDirectory )
506 ByteString rDirectory( rtl::OUStringToOString( aDirectory , RTL_TEXTENCODING_UTF8 , aDirectory.getLength() ) ) ;
507 if ( nMode == LOCALIZE_NONE ){
508 if( !bQuiet2 ) fprintf( stdout, "%s\n", rDirectory.GetBuffer());
510 else
511 WorkOnDirectory( rDirectory );
514 /*****************************************************************************/
515 BOOL SourceTreeLocalizer::Extract( const ByteString &rDestinationFile )
516 /*****************************************************************************/
518 nMode = LOCALIZE_EXTRACT;
519 aSDF.Open( String( rDestinationFile, RTL_TEXTENCODING_ASCII_US ),
520 STREAM_STD_WRITE | STREAM_TRUNC );
521 aSDF.SetLineDelimiter( LINEEND_CRLF );
523 BOOL bReturn = aSDF.IsOpen();
524 if ( bReturn ) {
525 bReturn = StartExecute();
526 aSDF.Close();
528 else{
529 printf("ERROR: Can't create file %s\n", rDestinationFile.GetBuffer() );
531 nMode = LOCALIZE_NONE;
532 return bReturn;
535 /*****************************************************************************/
536 BOOL SourceTreeLocalizer::MergeSingleFile(
537 const ByteString &rPrj,
538 const ByteString &rFile,
539 const ByteString &rSDFFile
541 /*****************************************************************************/
543 //printf("MergeSingleFile(%s,%s,%s)",rPrj.GetBuffer(),rFile.GetBuffer(),rSDFFile.GetBuffer());
544 if ( !rFile.Len())
545 return TRUE;
547 ByteString sRoot( Export::GetEnv( "SRC_ROOT" ));
548 DirEntry aEntry( String( sRoot, RTL_TEXTENCODING_ASCII_US ));
549 aEntry += DirEntry( String( rPrj, RTL_TEXTENCODING_ASCII_US ));
551 ByteString sDelimiter(
552 DirEntry::GetAccessDelimiter(), RTL_TEXTENCODING_ASCII_US );
554 ByteString sCur( rFile );
555 sCur.SearchAndReplaceAll( "\\", sDelimiter );
556 sCur.SearchAndReplaceAll( "/", sDelimiter );
558 aEntry += DirEntry( String( sCur, RTL_TEXTENCODING_ASCII_US ));
559 ByteString sFile( aEntry.GetFull(), RTL_TEXTENCODING_ASCII_US );
561 ByteString sBCur( aEntry.GetFull(), RTL_TEXTENCODING_ASCII_US );
562 if( !bQuiet2 ) fprintf( stdout, "##### %s #####\n", sBCur.GetBuffer());
564 ULONG nIndex = 0;
565 ByteString sExtension( aEntry.GetExtension(), RTL_TEXTENCODING_ASCII_US );
566 ByteString sCandidate( ExeTable[ nIndex ][ 0 ] );
568 while( !sCandidate.Equals ("NULL") && !sCandidate.Equals(sExtension) )
569 sCandidate = ExeTable[ ++nIndex ][ 0 ];
571 ByteString sIso( ExeTable[ nIndex ][ 4 ] );
573 if ( !sCandidate.Equals( "NULL" ) ) {
574 if( !aEntry.Exists()) {
575 DirEntryKind theDir=FSYS_KIND_FILE;
576 Dir myDir( aEntry.GetPath(), theDir);
577 DirEntry current;
578 BOOL found=FALSE;
579 for( USHORT x=0; x < myDir.Count() && !found;){
580 current=myDir[x++];
581 StringCompare result=current.GetName().CompareIgnoreCaseToAscii( aEntry.GetName() );
582 if( result==COMPARE_EQUAL ){
583 fprintf(stderr,"WARNING: %s not found\n", ByteString(aEntry.GetFull(),RTL_TEXTENCODING_ASCII_US).GetBuffer() );
584 fprintf(stderr,"but use %s instead \n" , ByteString(current.GetFull(), RTL_TEXTENCODING_ASCII_US).GetBuffer() );
585 aEntry=current;
586 found=TRUE;
589 if(!found) return TRUE;
593 DirEntry aOut( Export::GetTempFile() );
594 ByteString sOutput;
595 if( sOutputFile.Len() == 0 )
596 sOutput = ByteString ( aOut.GetFull(), RTL_TEXTENCODING_ASCII_US );
597 else
598 sOutput = sOutputFile;
599 ByteString sCommand( ExeTable[ nIndex ][ 1 ] );
600 sCommand += " -i ";
601 sCommand += ByteString( aEntry.GetName(), RTL_TEXTENCODING_ASCII_US );
602 sCommand += " -m ";
603 sCommand += rSDFFile;
604 sCommand += " -o ";
605 sCommand += sOutput;
606 sCommand += " ";
607 sCommand += ByteString( ExeTable[ nIndex ][ 2 ] );
608 if ( sIso.Equals( "iso" ) && sIsoCode99.Len()) {
609 sCommand += " -ISO99 ";
610 sCommand += sIsoCode99;
612 if ( sLanguageRestriction.Len()) {
613 sCommand += " -l ";
614 sCommand += sLanguageRestriction;
616 if( bQuiet2 ){
617 sCommand +=" -QQ ";
620 DirEntry aPath( aEntry.GetPath());
621 DirEntry aOldCWD;
622 aPath.SetCWD();
624 system( sCommand.GetBuffer());
625 nFileCnt++;
626 printf(".");
627 //if( bQuiet2 ){ printf("."); }
628 SvFileStream aInStream( aOut.GetFull(), STREAM_READ );
629 if ( !aInStream.IsOpen()) {
630 fprintf( stderr,
631 "ERROR: Unable to open file %s for reading!\n",
632 sOutput.GetBuffer());
634 else {
635 FileStat::SetReadOnlyFlag( aEntry, FALSE );
636 String myStr2(aEntry.GetFull());
637 String aTemp22 = String::CreateFromAscii("_tmp");
638 myStr2.Append(aTemp22);
640 ByteString test(myStr2,RTL_TEXTENCODING_ASCII_US);
641 SvFileStream aOutStream( myStr2, STREAM_STD_WRITE | STREAM_TRUNC );
642 if ( !aOutStream.IsOpen()) {
643 ByteString test2(myStr2,RTL_TEXTENCODING_ASCII_US);
644 fprintf( stderr,"ERROR: Unable to open file %s for modification!\n", test2.GetBuffer());
645 aInStream.Close();
648 else {
649 ByteString sLine;
650 aOutStream.SetLineDelimiter( LINEEND_LF );
652 aInStream.ReadLine( sLine );
653 while ( !aInStream.IsEof()) {
654 aOutStream.WriteLine( sLine );
655 aInStream.ReadLine( sLine );
657 aInStream.Close();
658 aOutStream.Close();
661 DirEntry myTempFile(ByteString(myStr2,RTL_TEXTENCODING_ASCII_US)); // xxx_tmp ->
662 DirEntry myFile(ByteString(aEntry.GetFull(),RTL_TEXTENCODING_ASCII_US));// xxx
664 DirEntry oldFile(ByteString(aEntry.GetFull(),RTL_TEXTENCODING_ASCII_US));
666 if(oldFile.Kill()==ERRCODE_NONE){
667 if(myTempFile.MoveTo(myFile)!=ERRCODE_NONE){
668 fprintf( stderr, "ERROR: Can't rename file %s\n",ByteString(myStr2,RTL_TEXTENCODING_ASCII_US).GetBuffer());
671 else{
672 fprintf( stderr, "ERROR: Can't remove file %s\n",ByteString(aEntry.GetFull(),RTL_TEXTENCODING_ASCII_US).GetBuffer());
674 } // else
676 aOldCWD.SetCWD();
677 aOut.Kill();
678 } // else
680 return TRUE;
682 /*****************************************************************************/
683 BOOL SourceTreeLocalizer::ExecuteMerge( )
684 /*****************************************************************************/
686 DirEntry aEntry( Export::GetTempFile());
687 BOOL bReturn = TRUE;
688 bool bMerged = false;
690 ByteString sFileName;
691 ByteString sCurFile;
692 ByteString sLine;
693 ByteString sFileKey;
695 SvFileStream aFile;
697 ByteString sOutputFileName = sOutputFile;
698 ByteString sInpath(".");
699 sInpath += Export::GetEnv("INPATH");
700 ByteString sBlank("");
702 sOutputFileName.SearchAndReplaceAll( sInpath , sBlank );
704 String sDel = DirEntry::GetAccessDelimiter();
705 ByteString sBDel( sDel.GetBuffer() , sDel.Len() , RTL_TEXTENCODING_UTF8 );
706 if( bLocal ){
707 xub_StrLen nPos = sOutputFileName.SearchBackward( sBDel.GetChar(0) );
708 //if( nPos >= 0 )
709 sOutputFileName = sOutputFileName.Copy( nPos+1 , sOutputFileName.Len()-nPos-1 );
711 ByteStringBoolHashMap aFileHM;
712 // Read all possible files
713 while ( !aSDF.IsEof()) {
714 aSDF.ReadLine( sLine );
715 sFileName = sLine.GetToken( 0, '\t' );
716 sFileName += "#";
717 sFileName += sLine.GetToken( 1, '\t' );
718 aFileHM[sFileName]=true;
721 // RECODE THIS !!!!!!!!!!!!!!!!!!!!!
722 for( ByteStringBoolHashMap::iterator iter = aFileHM.begin(); iter != aFileHM.end(); ++iter ){
723 sFileKey = iter->first;
724 aSDF.Seek( 0 );
725 aFile.Open( aEntry.GetFull(), STREAM_STD_WRITE |STREAM_TRUNC );
727 while ( !aSDF.IsEof()) {
728 aSDF.ReadLine( sLine );
729 sFileName = sLine.GetToken( 0, '\t' );
730 sFileName += "#";
731 sFileName += sLine.GetToken( 1, '\t' );
732 if( sFileName.Len() && ( sFileName.CompareTo(sFileKey) == COMPARE_EQUAL ) ){
733 if ( aFile.IsOpen() && sLine.Len())
734 aFile.WriteLine( sLine );
737 if ( aFile.IsOpen())
738 aFile.Close();
740 ByteString sPrj( sFileKey.GetToken( 0, '#' ));
741 ByteString sFile( sFileKey.GetToken( 1, '#' ));
742 ByteString sSDFFile( aFile.GetFileName(), RTL_TEXTENCODING_ASCII_US );
744 //printf("localize test sPrj = %s , sFile = %s , sSDFFile = %s sOutputFileName = %s\n",sPrj.GetBuffer(), sFile.GetBuffer() , sSDFFile.GetBuffer() , sOutputFileName.GetBuffer() );
746 // Test
747 bLocal = true;
748 // Test
750 if( bLocal ){
751 USHORT nPos = sFile.SearchBackward( '\\' );
752 ByteString sTmp = sFile.Copy( nPos+1 , sFile.Len()-nPos-1 );
753 //printf("'%s'='%s'\n",sTmp.GetBuffer(), sOutputFileName.GetBuffer());
754 if( sTmp.CompareTo(sOutputFileName) == COMPARE_EQUAL ){
755 bMerged = true;
756 if ( !MergeSingleFile( sPrj, sFile, sSDFFile ))
757 bReturn = FALSE;
759 }else{
760 bMerged = true;
761 //printf("MergeSingleFile('%s','%s','%s')\n",sPrj.GetBuffer(),sFile.GetBuffer(),sSDFFile.GetBuffer());
762 if ( !MergeSingleFile( sPrj, sFile, sSDFFile ))
763 bReturn = FALSE;
768 aEntry.Kill();
769 // If Outputfile not included in the SDF file copy it without merge
771 if( bLocal && !bMerged ){
772 DirEntry aSourceFile( sOutputFileName.GetBuffer() );
773 FSysError aErr = aSourceFile.CopyTo( DirEntry ( sOutputFile.GetBuffer() ) , FSYS_ACTION_COPYFILE );
774 if( aErr != FSYS_ERR_OK ){
775 printf("ERROR: Can't copy file '%s' to '%s' %d\n",sOutputFileName.GetBuffer(),sOutputFile.GetBuffer(),sal::static_int_cast<int>(aErr));
778 return bReturn;
782 /*****************************************************************************/
783 BOOL SourceTreeLocalizer::Merge( const ByteString &rSourceFile , const ByteString &rOutput )
784 /*****************************************************************************/
786 sOutputFile = rOutput;
787 nMode = LOCALIZE_MERGE;
788 aSDF.Open( String( rSourceFile, RTL_TEXTENCODING_ASCII_US ),
789 STREAM_STD_READ );
791 BOOL bReturn = aSDF.IsOpen();
792 if ( bReturn ) {
793 bReturn = ExecuteMerge();
794 aSDF.Close();
797 nMode = LOCALIZE_NONE;
798 return bReturn;
802 #define STATE_NONE 0x0000
803 #define STATE_EXPORT 0x0001
804 #define STATE_MERGE 0x0002
805 #define STATE_ISOCODE 0x0003
806 #define STATE_LANGUAGES 0x0004
807 #define STATE_FILENAME 0x0005
808 #define STATE_OUTPUT 0x0006
810 /*****************************************************************************/
811 void Help()
812 /*****************************************************************************/
814 fprintf( stdout,
815 "localize (c)2001 by Sun Microsystems\n"
816 "====================================\n" );
817 fprintf( stdout,
818 "As part of the L10N framework, localize extracts and merges translations\n"
819 "out of and into the whole source tree.\n\n"
820 "Syntax: localize -e|-m -l l1[=f1][,l2[=f2]][...] -f FileName [-QQ][-skip_links]\n"
821 "Parameter:\n"
822 "\t-e: Extract mode\n"
823 "\t-m: Merge mode\n"
824 "\tFileName: Output file when extract mode, input file when merge mode\n"
825 "\tl1...ln: supported languages (\"all\" for all languages).\n"
826 "\tf1...fn: fallback languages for supported languages\n"
827 "\tQQ: quiet output)"
830 fprintf( stdout,
831 "Valid language codes for l1...ln and f1...fn are:\n" );
832 fprintf( stdout,
833 "\nExample 1:\n"
834 "==========\n"
835 "localize -e -l en-US,de -f MyFile\n\n"
836 "All strings will be extracted for language de and language en-US.\n"
838 fprintf( stdout,
839 "\nExample 2:\n"
840 "==========\n"
841 "localize -m -l es -f MyFile\n\n"
842 "All strings in MyFile will be merged into language es in the\n"
843 "source code.\n"
847 /*****************************************************************************/
848 int Error()
849 /*****************************************************************************/
851 Help();
852 return 1;
855 /*****************************************************************************/
856 BOOL CheckLanguages( ByteString &rLanguages )
857 /*****************************************************************************/
859 ByteString sTmp( rLanguages );
860 return true;
863 /*****************************************************************************/
864 #if defined(UNX) || defined(OS2)
865 int main( int argc, char *argv[] )
866 #else
867 int _cdecl main( int argc, char *argv[] )
868 #endif
869 /*****************************************************************************/
871 String sTempBase( String::CreateFromAscii( "loc" ));
872 DirEntry::SetTempNameBase( sTempBase );
873 USHORT nState = STATE_NONE;
875 BOOL bExport = FALSE;
876 BOOL bMerge = FALSE;
877 bool bQuiet = false;
878 bool bQuiet2 = false;
879 bool bSkipLinks = false;
881 ByteString sIsoCode;
882 ByteString sLanguages;
883 ByteString sFileName;
884 ByteString sOutput;
886 for( int i = 1; i < argc; i++ ) {
887 ByteString sSwitch( argv[ i ] );
888 sSwitch.ToUpperAscii();
890 if ( sSwitch.Equals( "-E" )) {
891 nState = STATE_EXPORT;
892 if ( bMerge )
893 return Error();
894 bExport = TRUE;
896 else if ( sSwitch.Equals( "-M" )) {
897 nState = STATE_MERGE;
898 if ( bExport )
899 return Error();
900 bMerge = TRUE;
902 else if( sSwitch.Equals( "-Q" )) {
903 bQuiet = true;
905 else if ( sSwitch.Equals( "-I" ) )
906 nState = STATE_ISOCODE;
907 else if ( sSwitch.Equals( "-L" ) )
908 nState = STATE_LANGUAGES;
909 else if ( sSwitch.Equals( "-F" ) )
910 nState = STATE_FILENAME;
911 else if ( sSwitch.Equals( "-QQ" ))
912 bQuiet2 = true;
913 // else if ( ByteString( argv[ i ]).ToUpperAscii().Equals( "-SKIP_LINKS" ))
914 // bSkipLinks = true;
915 else if ( ByteString( argv[ i ]).ToUpperAscii().Equals( "-O" ) )
916 nState = STATE_OUTPUT;
917 else {
918 switch ( nState ) {
919 case STATE_NONE:
920 return Error();
921 case STATE_ISOCODE:
922 if ( sIsoCode.Len())
923 return Error();
924 sIsoCode = ByteString( argv[ i ] );
925 nState = STATE_NONE;
926 break;
927 case STATE_OUTPUT:
928 if ( sOutput.Len())
929 return Error();
930 sOutput = ByteString( argv[ i ] );
931 nState = STATE_NONE;
932 break;
933 case STATE_LANGUAGES:
934 if ( sLanguages.Len())
935 return Error();
936 sLanguages = ByteString( argv[ i ] );
937 nState = STATE_NONE;
938 break;
939 case STATE_FILENAME:
940 if ( sFileName.Len())
941 return Error();
942 sFileName = ByteString( argv[ i ] );
943 nState = STATE_NONE;
944 break;
945 default:
946 return Error();
950 if ( !bMerge && !bExport ) {
951 Help();
952 return 1;
955 ByteString sRoot( Export::GetEnv( "SRC_ROOT" ));
956 DirEntry aRoot( String( sRoot, RTL_TEXTENCODING_ASCII_US ));
957 sRoot = ByteString( aRoot.GetFull(), RTL_TEXTENCODING_ASCII_US );
958 ByteString sVersion( Export::GetEnv( "WORK_STAMP" ));
960 if ( !sRoot.Len() || !sVersion.Len()) {
961 fprintf( stderr, "ERROR: No environment set!\n" );
962 return 1;
965 if ( !CheckLanguages( sLanguages ))
966 return 2;
968 if ( !sFileName.Len()) {
969 fprintf( stderr, "ERROR: No filename given\n" );
970 return 3;
973 ByteString sMode( "merge" );
974 if ( bExport )
975 sMode = "extract";
977 ByteString sICode( sIsoCode );
978 if ( !sICode.Len())
979 sICode = "not given, support for language 99 disabled";
980 if(!bQuiet && !bQuiet2 ){
981 fprintf( stdout,
982 "\n"
983 "============================================================\n"
984 "Current settings:\n"
985 "============================================================\n"
986 "Mode: %s\n"
987 "Workspace: %s\n"
988 "Source tree: %s\n"
989 "Languages: %s\n"
990 "ISO code (99): %s\n"
991 "Filename: %s\n"
992 "Outputfile %s\n"
993 "============================================================\n"
994 "\n"
996 sMode.GetBuffer(),
997 sVersion.GetBuffer(),
998 sRoot.GetBuffer(),
999 sLanguages.GetBuffer(),
1000 sICode.GetBuffer(),
1001 sFileName.GetBuffer(),
1002 sOutput.GetBuffer()
1005 SourceTreeLocalizer aIter( sRoot, sVersion , (sOutput.Len() > 0) , bQuiet2 , bSkipLinks );
1007 aIter.SetLanguageRestriction( sLanguages );
1008 aIter.SetIsoCode99( sIsoCode );
1009 if ( bExport ){
1010 if( bQuiet2 ){ /*printf("");*/fflush( stdout );}
1011 aIter.Extract( sFileName );
1012 if( bQuiet2 ){ printf("\n %d files found!\n",aIter.GetFileCnt());}
1014 else {
1016 DirEntry aEntry( String( sFileName, RTL_TEXTENCODING_ASCII_US ));
1017 if ( !aEntry.Exists())
1018 return FALSE;
1019 printf("%s\n",sFileName.GetBuffer());
1020 aIter.Merge( sFileName , sOutput );
1023 return 0;