Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / l10ntools / source / merge.cxx
blob138846016b0289aed8044f100dab0cb259b922ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include <algorithm>
32 #include <fstream>
33 #include <string>
34 #include <vector>
36 #include "export.hxx"
38 namespace
40 static ::rtl::OString lcl_NormalizeFilename(const ::rtl::OString& rFilename)
42 return rFilename.copy(
43 std::max(
44 rFilename.lastIndexOf( "\\" ),
45 rFilename.lastIndexOf( "/" ))+1);
50 // class PFormEntrys
53 sal_Bool PFormEntrys::GetTransex3Text( rtl::OString &rReturn,
54 sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel )
56 sal_Bool rc = GetText( rReturn , nTyp , nLangIndex , bDel );
57 for( sal_Int32 idx = 0; idx < rReturn.getLength(); idx++ )
59 if( rReturn[idx] == '\"' && ( idx >= 1 ) && rReturn[idx-1] == '\\' )
61 rReturn = rReturn.replaceAt( idx-1, 1, rtl::OString() );
64 return rc;
66 /*****************************************************************************/
67 sal_Bool PFormEntrys::GetText( rtl::OString &rReturn,
68 sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel )
71 sal_Bool bReturn=sal_True;
72 switch ( nTyp ) {
73 case STRING_TYP_TEXT :
74 rReturn = sText[ nLangIndex ];
75 if ( bDel )
76 sText[ nLangIndex ] = "";
77 bReturn = bTextFirst[ nLangIndex ];
78 bTextFirst[ nLangIndex ] = sal_False;
79 break;
80 case STRING_TYP_HELPTEXT :
81 rReturn = sHelpText;
82 break;
83 case STRING_TYP_QUICKHELPTEXT :
84 rReturn = sQuickHelpText[ nLangIndex ];
85 if ( bDel )
86 sQuickHelpText[ nLangIndex ] = "";
87 bReturn = bQuickHelpTextFirst[ nLangIndex ];
88 bQuickHelpTextFirst[ nLangIndex ] = sal_False;
89 break;
90 case STRING_TYP_TITLE :
91 rReturn = sTitle[ nLangIndex ];
92 if ( bDel )
93 sTitle[ nLangIndex ] = "";
94 bReturn = bTitleFirst[ nLangIndex ];
95 bTitleFirst[ nLangIndex ] = sal_False;
96 break;
98 return bReturn;
103 // class MergeData
106 MergeData::~MergeData()
110 PFormEntrys* MergeData::GetPFormEntries()
112 if( aMap.find( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) ) != aMap.end() )
113 return aMap[rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK"))];
114 return NULL;
117 void MergeData::Insert(PFormEntrys* pfEntrys )
119 aMap.insert( PFormEntrysHashMap::value_type( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) , pfEntrys ) );
122 PFormEntrys* MergeData::GetPFObject( const rtl::OString& rPFO )
124 if( aMap.find( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) ) != aMap.end() )
125 return aMap[ rPFO ];
126 return NULL;
129 sal_Bool MergeData::operator==( ResData *pData )
131 return pData->sId == sLID && pData->sGId == sGID
132 && pData->sResTyp.equalsIgnoreAsciiCase(sTyp);
136 // class MergeDataFile
139 #define FFORMAT_UNKNOWN 0x0000
140 #define FFORMAT_NEW 0x0001
141 #define FFORMAT_OLD 0x0002
144 MergeDataFile::MergeDataFile(
145 const rtl::OString &rFileName,
146 const rtl::OString &rFile,
147 bool bErrLog,
148 bool bCaseSensitive)
149 : bErrorLog( bErrLog )
151 std::ifstream aInputStream(rFileName.getStr());
152 const ::rtl::OString sHACK(RTL_CONSTASCII_STRINGPARAM("HACK"));
153 const ::rtl::OString sFileNormalized(lcl_NormalizeFilename(rFile));
154 const bool isFileEmpty = !sFileNormalized.isEmpty();
156 if (!aInputStream.is_open())
158 printf("Warning : Can't open %s\n", rFileName.getStr());
159 return;
161 while (!aInputStream.eof())
163 std::string buf;
164 std::getline(aInputStream, buf);
165 rtl::OString sLine(buf.data(), buf.length());
166 sal_Int32 n = 0;
167 // Skip all wrong filenames
168 const ::rtl::OString filename = lcl_NormalizeFilename(sLine.getToken(1, '\t', n)); // token 1
169 if(isFileEmpty || sFileNormalized.equals("") || (!isFileEmpty && filename.equals(sFileNormalized)) )
171 const rtl::OString sTYP = sLine.getToken( 1, '\t', n ); // token 3
172 const rtl::OString sGID = sLine.getToken( 0, '\t', n ); // token 4
173 const rtl::OString sLID = sLine.getToken( 0, '\t', n ); // token 5
174 rtl::OString sPFO = sLine.getToken( 1, '\t', n ); // token 7
175 sPFO = sHACK;
176 rtl::OString nLANG = sLine.getToken( 1, '\t', n ); // token 9
177 nLANG = nLANG.trim();
178 const rtl::OString sTEXT = sLine.getToken( 0, '\t', n ); // token 10
179 const rtl::OString sQHTEXT = sLine.getToken( 1, '\t', n ); // token 12
180 const rtl::OString sTITLE = sLine.getToken( 0, '\t', n ); // token 13
182 if (!nLANG.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")))
184 aLanguageSet.insert(nLANG);
185 InsertEntry( sTYP, sGID, sLID, sPFO, nLANG, sTEXT, sQHTEXT, sTITLE, filename, bCaseSensitive );
189 aInputStream.close();
192 MergeDataFile::~MergeDataFile()
194 for (MergeDataHashMap::iterator aI = aMap.begin(), aEnd = aMap.end(); aI != aEnd; ++aI)
195 delete aI->second;
198 std::vector<rtl::OString> MergeDataFile::GetLanguages()
200 return std::vector<rtl::OString>(aLanguageSet.begin(),aLanguageSet.end());
203 MergeData *MergeDataFile::GetMergeData( ResData *pResData , bool bCaseSensitive )
205 rtl::OString sOldG = pResData->sGId;
206 rtl::OString sOldL = pResData->sId;
207 rtl::OString sGID = pResData->sGId;
208 rtl::OString sLID;
209 if (sGID.isEmpty())
210 sGID = pResData->sId;
211 else
212 sLID = pResData->sId;
213 pResData->sGId = sGID;
214 pResData->sId = sLID;
216 rtl::OString sKey = CreateKey( pResData->sResTyp , pResData->sGId , pResData->sId , pResData->sFilename , bCaseSensitive );
218 if(aMap.find( sKey ) != aMap.end())
220 pResData->sGId = sOldG;
221 pResData->sId = sOldL;
222 return aMap[ sKey ];
224 pResData->sGId = sOldG;
225 pResData->sId = sOldL;
226 return NULL;
230 PFormEntrys *MergeDataFile::GetPFormEntrys( ResData *pResData )
232 // search for requested PFormEntrys
233 MergeData *pData = GetMergeData( pResData );
234 if ( pData )
235 return pData->GetPFormEntries();
236 return NULL;
239 PFormEntrys *MergeDataFile::GetPFormEntrysCaseSensitive( ResData *pResData )
241 // search for requested PFormEntrys
242 MergeData *pData = GetMergeData( pResData , true );
243 if ( pData )
244 return pData->GetPFormEntries();
245 return NULL;
248 void MergeDataFile::InsertEntry(
249 const rtl::OString &rTYP, const rtl::OString &rGID,
250 const rtl::OString &rLID, const rtl::OString &rPFO,
251 const rtl::OString &nLANG, const rtl::OString &rTEXT,
252 const rtl::OString &rQHTEXT, const rtl::OString &rTITLE ,
253 const rtl::OString &rInFilename , bool bCaseSensitive
256 MergeData *pData;
258 // search for MergeData
259 rtl::OString sKey = CreateKey(rTYP , rGID , rLID , rInFilename , bCaseSensitive);
260 MergeDataHashMap::const_iterator mit;
261 mit = aMap.find( sKey );
262 if( mit != aMap.end() )
264 pData = mit->second;
266 else
268 pData = new MergeData( rTYP, rGID, rLID, rInFilename );
269 aMap.insert( MergeDataHashMap::value_type( sKey, pData ) );
272 PFormEntrys *pFEntrys = 0;
274 // search for PFormEntrys
275 pFEntrys = pData->GetPFObject( rPFO );
276 if( !pFEntrys )
278 // create new PFormEntrys, cause no one exists with current properties
279 pFEntrys = new PFormEntrys( rPFO );
280 pData->Insert( pFEntrys );
283 // finaly insert the cur string
284 pFEntrys->InsertEntry( nLANG , rTEXT, rQHTEXT, rTITLE );
287 rtl::OString MergeDataFile::CreateKey(const rtl::OString& rTYP, const rtl::OString& rGID,
288 const rtl::OString& rLID, const rtl::OString& rFilename, bool bCaseSensitive)
290 static const ::rtl::OString sStroke('-');
291 ::rtl::OString sKey( rTYP );
292 sKey += sStroke;
293 sKey += rGID;
294 sKey += sStroke;
295 sKey += rLID;
296 sKey += sStroke;
297 sKey += lcl_NormalizeFilename(rFilename);
298 OSL_TRACE("created key: %s", sKey.getStr());
299 if(bCaseSensitive)
300 return sKey; // officecfg case sensitive identifier
301 return sKey.toAsciiUpperCase();
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */