1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: merge.cxx,v $
10 * $Revision: 1.27.36.3 $
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 <tools/fsys.hxx>
36 #include "utf8conv.hxx"
41 extern void ConvertHalfwitdhToFullwidth( String
& rString
);
47 ByteString
PFormEntrys::Dump(){
48 ByteString
sRet( "PFormEntrys\n" );
49 //sRet.Append( Export::DumpMap( ByteString("sText") , sText ) );
51 ByteString
a("sText");
52 if ( sText
.size() ) Export::DumpMap( a
, sText
);
56 /*****************************************************************************/
57 BOOL
PFormEntrys::GetText( ByteString
&rReturn
,
58 USHORT nTyp
, const ByteString
&nLangIndex
, BOOL bDel
)
59 /*****************************************************************************/
62 /*printf("DBG: PFormEntrys::GetText(nId=%s)\n",nLangIndex.GetBuffer() );
64 // DEBUG******************
65 ByteStringHashMap::const_iterator idbg;
66 std::cout << "HASHKEYS : \n";
67 for( idbg = sText.begin() ; idbg != sText.end(); ++idbg )
68 std::cout << (idbg->first).GetBuffer() << "\n";
70 std::cout << "String sText[ nLangIndex ] = " << sText[ nLangIndex ].GetBuffer() << "\n";
71 // DEBUG******************
76 case STRING_TYP_TEXT
:
77 rReturn
= sText
[ nLangIndex
];
79 sText
[ nLangIndex
] = "";
80 bReturn
= bTextFirst
[ nLangIndex
];
81 bTextFirst
[ nLangIndex
] = FALSE
;
83 case STRING_TYP_HELPTEXT
:
86 case STRING_TYP_QUICKHELPTEXT
:
87 rReturn
= sQuickHelpText
[ nLangIndex
];
89 sQuickHelpText
[ nLangIndex
] = "";
90 bReturn
= bQuickHelpTextFirst
[ nLangIndex
];
91 bQuickHelpTextFirst
[ nLangIndex
] = FALSE
;
93 case STRING_TYP_TITLE
:
94 rReturn
= sTitle
[ nLangIndex
];
96 sTitle
[ nLangIndex
] = "";
97 bReturn
= bTitleFirst
[ nLangIndex
];
98 bTitleFirst
[ nLangIndex
] = FALSE
;
101 //printf("Returning '%s'\n",rReturn.GetBuffer());
110 /*****************************************************************************/
111 MergeData::~MergeData()
112 /*****************************************************************************/
116 /*****************************************************************************/
117 PFormEntrys
* MergeData::GetPFormEntrys( ResData
*pResData
)
118 /*****************************************************************************/
121 (void) pResData
; // FIXME
122 if( aMap
.find( ByteString("HACK") ) != aMap
.end() ){
123 return aMap
[ ByteString("HACK") ];
130 void MergeData::Insert( const ByteString
& rPFO
, PFormEntrys
* pfEntrys
){
131 (void) rPFO
; // FIXME
132 aMap
.insert( PFormEntrysHashMap::value_type( ByteString("HACK") , pfEntrys
) );
135 ByteString
MergeData::Dump(){
136 ByteString
sRet( "MergeData\n" );
138 printf("MergeData sTyp = %s , sGid = %s , sLid =%s , sFilename = %s\n",sTyp
.GetBuffer(),sGID
.GetBuffer(),sLID
.GetBuffer(), sFilename
.GetBuffer() );
140 PFormEntrysHashMap::const_iterator idbg
;
141 for( idbg
= aMap
.begin() ; idbg
!= aMap
.end(); ++idbg
){
142 printf("aMap[ %s ] = " ,idbg
->first
.GetBuffer());
143 ( (PFormEntrys
*)(idbg
->second
) )->Dump();
150 PFormEntrys
* MergeData::GetPFObject( const ByteString
& rPFO
){
151 if( aMap
.find( ByteString("HACK") ) != aMap
.end() ){
160 /*****************************************************************************/
161 PFormEntrys
*MergeData::InsertEntry( const ByteString
&rPForm
)
162 /*****************************************************************************/
164 PFormEntrys
* pFEntrys
= new PFormEntrys( rPForm
);
165 aMap
.insert( PFormEntrysHashMap::value_type( rPForm
, pFEntrys
) );
169 /*****************************************************************************/
170 BOOL
MergeData::operator==( ResData
*pData
)
171 /*****************************************************************************/
173 ByteString
sResTyp_upper( pData
->sResTyp
);
174 sResTyp_upper
.ToUpperAscii();
175 ByteString
sTyp_upper( sTyp
);
176 sTyp_upper
.ToUpperAscii();
178 return (( pData
->sId
== sLID
) &&
179 ( pData
->sGId
== sGID
) &&
180 ( sResTyp_upper
== sTyp_upper
)
185 // class MergeDataFile
188 #define FFORMAT_UNKNOWN 0x0000
189 #define FFORMAT_NEW 0x0001
190 #define FFORMAT_OLD 0x0002
192 /*****************************************************************************/
193 MergeDataFile::MergeDataFile( const ByteString
&rFileName
, const ByteString
& sFile
,BOOL bErrLog
,
194 // CharSet aCharSet, BOOL bUTF8 , bool bCaseSensitive )
195 CharSet aCharSet
, bool bCaseSensitive
)
197 /*****************************************************************************/
198 : bErrorLog( bErrLog
)
201 SvFileStream
aInputStream( String( rFileName
, RTL_TEXTENCODING_ASCII_US
), STREAM_STD_READ
);
202 aInputStream
.SetStreamCharSet( aCharSet
);
204 // printf("\nReading localize.sdf ...\n");
213 ByteString
sHACK("HACK");
215 const ByteString
sEmpty("");
217 if( !aInputStream
.IsOpen() ) {
218 printf("Warning : Can't open %s\n", rFileName
.GetBuffer());
222 while ( !aInputStream
.IsEof()) {
224 aInputStream
.ReadLine( sLine
);
225 sLine
= sLine
.Convert( RTL_TEXTENCODING_MS_1252
, aCharSet
);
227 nToks
= sLine
.GetTokenCount( '\t' );
229 // Skip all wrong filenames
230 ByteString filename
= sLine
.GetToken( 1 , '\t' );
231 filename
= filename
.Copy( filename
.SearchCharBackward( "\\" )+1 , filename
.Len() );
233 if( sFile
.Equals( sEmpty
) || ( !sFile
.Equals( sEmpty
) && filename
.Equals( sFile
) ) )
236 sTYP
= sLine
.GetToken( 3, '\t', rIdx
);
237 sGID
= sLine
.GetToken( 0, '\t', rIdx
); // 4
238 sLID
= sLine
.GetToken( 0, '\t', rIdx
); // 5
239 sPFO
= sLine
.GetToken( 1, '\t', rIdx
); // 7
241 nLANG
= sLine
.GetToken( 1, '\t', rIdx
); // 9
242 sTEXT
= sLine
.GetToken( 0, '\t', rIdx
); // 10
244 sQHTEXT
= sLine
.GetToken( 1, '\t', rIdx
); // 12
245 sTITLE
= sLine
.GetToken( 0, '\t', rIdx
); // 13
247 nLANG
.EraseLeadingAndTrailingChars();
249 #ifdef MERGE_SOURCE_LANGUAGES
252 if ( !nLANG
.EqualsIgnoreCaseAscii("en-US") ){
254 ByteStringHashMap::const_iterator lit
;
255 lit
= aLanguageMap
.find (nLANG
);
257 if (lit
== aLanguageMap
.end()) {
259 aLanguageMap
.insert( ByteStringHashMap::value_type( aLANG
, aLANG
) );
260 // Remember read languages for -l all switch
261 aLanguageList
.push_back( nLANG
);
265 InsertEntry( sTYP
, sGID
, sLID
, sPFO
, aLANG
, sTEXT
, sQHTEXT
, sTITLE
, filename
, bCaseSensitive
);
269 else if ( nToks
== 10 ) {
270 printf("ERROR: File format is obsolete and no longer supported!\n");
273 aInputStream
.Close();
275 /*****************************************************************************/
276 MergeDataFile::~MergeDataFile()
277 /*****************************************************************************/
281 /*****************************************************************************/
282 //void MergeDataFile::WriteErrorLog( const ByteString &rFileName )
283 /*****************************************************************************/
288 ByteString
MergeDataFile::Dump(){
289 ByteString
sRet( "MergeDataFile\n" );
291 //sRet.Append( Export::DumpMap( "aLanguageSet" , aLanguageSet ) );
292 //sRet.Append( Export::DumpMap( "aLanguageList" , aLanguageList ) );
293 printf("MergeDataFile\n");
294 MergeDataHashMap::const_iterator idbg
;
295 for( idbg
= aMap
.begin() ; idbg
!= aMap
.end(); ++idbg
){
296 /*sRet.Append( "aMap[" );
297 sRet.Append( idbg->first );
298 sRet.Append( "]= " );
299 sRet.Append( ((MergeData*) (idbg->second))->Dump() );
302 printf("aMap[ %s ] = ",idbg
->first
.GetBuffer());
303 ((MergeData
*) (idbg
->second
))->Dump();
311 /*****************************************************************************/
312 void MergeDataFile::WriteError( const ByteString
&rLine
)
313 /*****************************************************************************/
316 if ( !aErrLog
.IsOpen())
317 aErrLog
.Open( String( sErrorLog
, RTL_TEXTENCODING_ASCII_US
), STREAM_STD_WRITE
| STREAM_TRUNC
);
318 aErrLog
.WriteLine( rLine
);
321 fprintf( stderr
, "%s\n", rLine
.GetBuffer());
323 std::vector
<ByteString
> MergeDataFile::GetLanguages(){
324 return aLanguageList
;
327 /*****************************************************************************/
328 MergeData
*MergeDataFile::GetMergeData( ResData
*pResData
, bool bCaseSensitive
)
329 /*****************************************************************************/
331 ByteString sOldG
= pResData
->sGId
;
332 ByteString sOldL
= pResData
->sId
;
333 ByteString sGID
= pResData
->sGId
;
336 sGID
= pResData
->sId
;
338 sLID
= pResData
->sId
;
339 pResData
->sGId
= sGID
;
340 pResData
->sId
= sLID
;
342 ByteString sKey
= CreateKey( pResData
->sResTyp
, pResData
->sGId
, pResData
->sId
, pResData
->sFilename
, bCaseSensitive
);
344 //printf("DBG: Searching [%s]\n",sKey.GetBuffer());
345 if( aMap
.find( sKey
) != aMap
.end() ){
346 pResData
->sGId
= sOldG
;
347 pResData
->sId
= sOldL
;
348 //printf("DBG: Found[%s]\n",sKey.GetBuffer());
351 pResData
->sGId
= sOldG
;
352 pResData
->sId
= sOldL
;
353 //printf("DBG: Found[%s]\n",sKey.GetBuffer());
358 /*****************************************************************************/
359 PFormEntrys
*MergeDataFile::GetPFormEntrys( ResData
*pResData
)
360 /*****************************************************************************/
362 // search for requested PFormEntrys
363 MergeData
*pData
= GetMergeData( pResData
);
365 return pData
->GetPFormEntrys( pResData
);
369 /*****************************************************************************/
370 PFormEntrys
*MergeDataFile::GetPFormEntrysCaseSensitive( ResData
*pResData
)
371 /*****************************************************************************/
373 // search for requested PFormEntrys
374 MergeData
*pData
= GetMergeData( pResData
, true );
376 return pData
->GetPFormEntrys( pResData
);
379 /*****************************************************************************/
380 void MergeDataFile::InsertEntry(
381 const ByteString
&rTYP
, const ByteString
&rGID
,
382 const ByteString
&rLID
, const ByteString
&rPFO
,
383 const ByteString
&nLANG
, const ByteString
&rTEXT
,
384 const ByteString
&rQHTEXT
, const ByteString
&rTITLE
,
385 const ByteString
&rInFilename
, bool bCaseSensitive
387 /*****************************************************************************/
392 // uniquify the filename to save memory.
393 ByteStringHashMap::const_iterator fit
= aFilenames
.find (rInFilename
);
394 ByteString aFilename
;
395 if (fit
== aFilenames
.end()) {
396 aFilename
= rInFilename
;
397 aFilenames
.insert (ByteStringHashMap::value_type (aFilename
, aFilename
));
399 aFilename
= fit
->first
;
401 // search for MergeData
403 ByteString sKey
= CreateKey( rTYP
, rGID
, rLID
, aFilename
, bCaseSensitive
);
404 MergeDataHashMap::const_iterator mit
;
405 mit
= aMap
.find( sKey
);
406 if( mit
!= aMap
.end() ){
409 pData
= new MergeData( rTYP
, rGID
, rLID
, aFilename
);
410 aMap
.insert( MergeDataHashMap::value_type( sKey
, pData
) );
414 PFormEntrys
*pFEntrys
= 0;
416 // search for PFormEntrys
418 pFEntrys
= pData
->GetPFObject( rPFO
);
420 // create new PFormEntrys, cause no one exists with current properties
421 pFEntrys
= new PFormEntrys( rPFO
);
422 pData
->Insert( rPFO
, pFEntrys
);
425 // finaly insert the cur string
427 pFEntrys
->InsertEntry( nLANG
, rTEXT
, rQHTEXT
, rTITLE
);
429 //printf("DBG: MergeDataFile::Insert[]=( sKey=%s,nLang=%s,rTEXT=%s)\n",sKey2.GetBuffer(),nLANG.GetBuffer(),rTEXT.GetBuffer());
431 ByteString
MergeDataFile::CreateKey( const ByteString
& rTYP
, const ByteString
& rGID
, const ByteString
& rLID
, const ByteString
& rFilename
, bool bCaseSensitive
){
433 ByteString
sKey( rTYP
);
439 sKey
.Append( rFilename
);
441 if( bCaseSensitive
) return sKey
; // officecfg case sensitive identifier
442 else return sKey
.ToUpperAscii();