merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / merge.cxx
blob4de82ed9bfb3a72a26da1c2b5ff9992103eb6379
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: 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"
33 #include <stdio.h>
34 #include <tools/fsys.hxx>
35 #include "export.hxx"
36 #include "utf8conv.hxx"
37 #include <iostream>
39 using namespace std;
41 extern void ConvertHalfwitdhToFullwidth( String& rString );
44 // class PFormEntrys
47 ByteString PFormEntrys::Dump(){
48 ByteString sRet( "PFormEntrys\n" );
49 //sRet.Append( Export::DumpMap( ByteString("sText") , sText ) );
50 //sRet.Append("\n");
51 ByteString a("sText");
52 if ( sText.size() ) Export::DumpMap( a , sText );
53 return sRet;
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";
69 std::cout << "\n\n";
70 std::cout << "String sText[ nLangIndex ] = " << sText[ nLangIndex ].GetBuffer() << "\n";
71 // DEBUG******************
74 BOOL bReturn=TRUE;
75 switch ( nTyp ) {
76 case STRING_TYP_TEXT :
77 rReturn = sText[ nLangIndex ];
78 if ( bDel )
79 sText[ nLangIndex ] = "";
80 bReturn = bTextFirst[ nLangIndex ];
81 bTextFirst[ nLangIndex ] = FALSE;
82 break;
83 case STRING_TYP_HELPTEXT :
84 rReturn = sHelpText;
85 break;
86 case STRING_TYP_QUICKHELPTEXT :
87 rReturn = sQuickHelpText[ nLangIndex ];
88 if ( bDel )
89 sQuickHelpText[ nLangIndex ] = "";
90 bReturn = bQuickHelpTextFirst[ nLangIndex ];
91 bQuickHelpTextFirst[ nLangIndex ] = FALSE;
92 break;
93 case STRING_TYP_TITLE :
94 rReturn = sTitle[ nLangIndex ];
95 if ( bDel )
96 sTitle[ nLangIndex ] = "";
97 bReturn = bTitleFirst[ nLangIndex ];
98 bTitleFirst[ nLangIndex ] = FALSE;
99 break;
101 //printf("Returning '%s'\n",rReturn.GetBuffer());
102 return bReturn;
107 // class MergeData
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") ];
125 else{
126 return 0;
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();
144 printf("\n") ;
146 printf("\n") ;
147 return sRet;
150 PFormEntrys* MergeData::GetPFObject( const ByteString& rPFO ){
151 if( aMap.find( ByteString("HACK") ) != aMap.end() ){
152 return aMap[ rPFO ];
154 else{
155 return 0;
160 /*****************************************************************************/
161 PFormEntrys *MergeData::InsertEntry( const ByteString &rPForm )
162 /*****************************************************************************/
164 PFormEntrys* pFEntrys = new PFormEntrys( rPForm );
165 aMap.insert( PFormEntrysHashMap::value_type( rPForm , pFEntrys ) );
166 return 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 );
203 ByteString sLine;
204 // printf("\nReading localize.sdf ...\n");
205 ByteString sTYP;
206 ByteString sGID;
207 ByteString sLID;
208 ByteString sPFO;
209 ByteString nLANG;
210 ByteString sTEXT;
211 ByteString sQHTEXT;
212 ByteString sTITLE;
213 ByteString sHACK("HACK");
215 const ByteString sEmpty("");
217 if( !aInputStream.IsOpen() ) {
218 printf("Warning : Can't open %s\n", rFileName.GetBuffer());
219 //exit( -1 );
220 return;
222 while ( !aInputStream.IsEof()) {
223 xub_StrLen nToks;
224 aInputStream.ReadLine( sLine );
225 sLine = sLine.Convert( RTL_TEXTENCODING_MS_1252, aCharSet );
227 nToks = sLine.GetTokenCount( '\t' );
228 if ( nToks == 15 ) {
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 ) ) )
235 xub_StrLen rIdx = 0;
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
240 sPFO = sHACK;
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
250 if( true ){
251 #else
252 if ( !nLANG.EqualsIgnoreCaseAscii("en-US") ){
253 #endif
254 ByteStringHashMap::const_iterator lit;
255 lit = aLanguageMap.find (nLANG);
256 ByteString aLANG;
257 if (lit == aLanguageMap.end()) {
258 aLANG = nLANG;
259 aLanguageMap.insert( ByteStringHashMap::value_type( aLANG, aLANG ) );
260 // Remember read languages for -l all switch
261 aLanguageList.push_back( nLANG );
262 } else
263 aLANG = lit->first;
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 /*****************************************************************************/
285 // DEAD
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() );
300 sRet.Append("\n");*/
302 printf("aMap[ %s ] = ",idbg->first.GetBuffer());
303 ((MergeData*) (idbg->second))->Dump();
304 printf("\n");
306 printf("\n");
307 //sRet.Append("\n");
308 return sRet;
311 /*****************************************************************************/
312 void MergeDataFile::WriteError( const ByteString &rLine )
313 /*****************************************************************************/
315 if ( bErrorLog ) {
316 if ( !aErrLog.IsOpen())
317 aErrLog.Open( String( sErrorLog, RTL_TEXTENCODING_ASCII_US ), STREAM_STD_WRITE | STREAM_TRUNC );
318 aErrLog.WriteLine( rLine );
320 else
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;
334 ByteString sLID;
335 if ( !sGID.Len())
336 sGID = pResData->sId;
337 else
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());
349 return aMap[ sKey ];
351 pResData->sGId = sOldG;
352 pResData->sId = sOldL;
353 //printf("DBG: Found[%s]\n",sKey.GetBuffer());
354 return NULL;
358 /*****************************************************************************/
359 PFormEntrys *MergeDataFile::GetPFormEntrys( ResData *pResData )
360 /*****************************************************************************/
362 // search for requested PFormEntrys
363 MergeData *pData = GetMergeData( pResData );
364 if ( pData )
365 return pData->GetPFormEntrys( pResData );
366 return NULL;
369 /*****************************************************************************/
370 PFormEntrys *MergeDataFile::GetPFormEntrysCaseSensitive( ResData *pResData )
371 /*****************************************************************************/
373 // search for requested PFormEntrys
374 MergeData *pData = GetMergeData( pResData , true );
375 if ( pData )
376 return pData->GetPFormEntrys( pResData );
377 return NULL;
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 /*****************************************************************************/
389 MergeData *pData;
390 BOOL bFound = FALSE;
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));
398 } else
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() ){
407 pData = mit->second;
408 }else{
409 pData = new MergeData( rTYP, rGID, rLID, aFilename );
410 aMap.insert( MergeDataHashMap::value_type( sKey, pData ) );
413 bFound = FALSE;
414 PFormEntrys *pFEntrys = 0;
416 // search for PFormEntrys
418 pFEntrys = pData->GetPFObject( rPFO );
419 if( !pFEntrys ){
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 );
434 sKey.Append( '-' );
435 sKey.Append( rGID );
436 sKey.Append( '-' );
437 sKey.Append( rLID );
438 sKey.Append( '-' );
439 sKey.Append( rFilename );
441 if( bCaseSensitive ) return sKey; // officecfg case sensitive identifier
442 else return sKey.ToUpperAscii();