Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "sal/config.h"
30 : :
31 : : #include <algorithm>
32 : : #include <fstream>
33 : : #include <string>
34 : : #include <vector>
35 : :
36 : : #include "export.hxx"
37 : :
38 : : namespace
39 : : {
40 : 0 : static ::rtl::OString lcl_NormalizeFilename(const ::rtl::OString& rFilename)
41 : : {
42 : : return rFilename.copy(
43 : : std::max(
44 : 0 : rFilename.lastIndexOf( '\\' ),
45 : 0 : rFilename.lastIndexOf( '/' ))+1);
46 : : };
47 : : }
48 : :
49 : : //
50 : : // class PFormEntrys
51 : : //
52 : :
53 : 0 : sal_Bool PFormEntrys::GetTransex3Text( rtl::OString &rReturn,
54 : : sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel )
55 : : {
56 : 0 : sal_Bool rc = GetText( rReturn , nTyp , nLangIndex , bDel );
57 : 0 : for( sal_Int32 idx = 0; idx < rReturn.getLength(); idx++ )
58 : : {
59 : 0 : if( rReturn[idx] == '\"' && ( idx >= 1 ) && rReturn[idx-1] == '\\' )
60 : : {
61 : 0 : rReturn = rReturn.replaceAt( idx-1, 1, rtl::OString() );
62 : : }
63 : : }
64 : 0 : return rc;
65 : : }
66 : : /*****************************************************************************/
67 : 0 : sal_Bool PFormEntrys::GetText( rtl::OString &rReturn,
68 : : sal_uInt16 nTyp, const rtl::OString &nLangIndex, sal_Bool bDel )
69 : : {
70 : :
71 : 0 : sal_Bool bReturn=sal_True;
72 : 0 : switch ( nTyp ) {
73 : : case STRING_TYP_TEXT :
74 : 0 : rReturn = sText[ nLangIndex ];
75 : 0 : if ( bDel )
76 : 0 : sText[ nLangIndex ] = "";
77 : 0 : bReturn = bTextFirst[ nLangIndex ];
78 : 0 : bTextFirst[ nLangIndex ] = sal_False;
79 : 0 : break;
80 : : case STRING_TYP_HELPTEXT :
81 : 0 : rReturn = sHelpText;
82 : 0 : break;
83 : : case STRING_TYP_QUICKHELPTEXT :
84 : 0 : rReturn = sQuickHelpText[ nLangIndex ];
85 : 0 : if ( bDel )
86 : 0 : sQuickHelpText[ nLangIndex ] = "";
87 : 0 : bReturn = bQuickHelpTextFirst[ nLangIndex ];
88 : 0 : bQuickHelpTextFirst[ nLangIndex ] = sal_False;
89 : 0 : break;
90 : : case STRING_TYP_TITLE :
91 : 0 : rReturn = sTitle[ nLangIndex ];
92 : 0 : if ( bDel )
93 : 0 : sTitle[ nLangIndex ] = "";
94 : 0 : bReturn = bTitleFirst[ nLangIndex ];
95 : 0 : bTitleFirst[ nLangIndex ] = sal_False;
96 : 0 : break;
97 : : }
98 : 0 : return bReturn;
99 : : }
100 : :
101 : :
102 : : //
103 : : // class MergeData
104 : : //
105 : :
106 : 0 : MergeData::~MergeData()
107 : : {
108 : 0 : }
109 : :
110 : 0 : PFormEntrys* MergeData::GetPFormEntries()
111 : : {
112 : 0 : if( aMap.find( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) ) != aMap.end() )
113 : 0 : return aMap[rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK"))];
114 : 0 : return NULL;
115 : : }
116 : :
117 : 0 : void MergeData::Insert(PFormEntrys* pfEntrys )
118 : : {
119 : 0 : aMap.insert( PFormEntrysHashMap::value_type( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) , pfEntrys ) );
120 : 0 : }
121 : :
122 : 0 : PFormEntrys* MergeData::GetPFObject( const rtl::OString& rPFO )
123 : : {
124 : 0 : if( aMap.find( rtl::OString(RTL_CONSTASCII_STRINGPARAM("HACK")) ) != aMap.end() )
125 : 0 : return aMap[ rPFO ];
126 : 0 : return NULL;
127 : : }
128 : :
129 : 0 : sal_Bool MergeData::operator==( ResData *pData )
130 : : {
131 : 0 : return pData->sId == sLID && pData->sGId == sGID
132 : 0 : && pData->sResTyp.equalsIgnoreAsciiCase(sTyp);
133 : : }
134 : :
135 : : //
136 : : // class MergeDataFile
137 : : //
138 : :
139 : 0 : MergeDataFile::MergeDataFile(
140 : : const rtl::OString &rFileName,
141 : : const rtl::OString &rFile,
142 : 0 : bool bCaseSensitive)
143 : : {
144 : 0 : std::ifstream aInputStream(rFileName.getStr());
145 : 0 : const ::rtl::OString sHACK(RTL_CONSTASCII_STRINGPARAM("HACK"));
146 : 0 : const ::rtl::OString sFileNormalized(lcl_NormalizeFilename(rFile));
147 : 0 : const bool isFileEmpty = !sFileNormalized.isEmpty();
148 : :
149 : 0 : if (!aInputStream.is_open())
150 : : {
151 : 0 : printf("Warning : Can't open %s\n", rFileName.getStr());
152 : 0 : return;
153 : : }
154 : 0 : while (!aInputStream.eof())
155 : : {
156 : 0 : std::string buf;
157 : 0 : std::getline(aInputStream, buf);
158 : 0 : rtl::OString sLine(buf.data(), buf.length());
159 : 0 : sal_Int32 n = 0;
160 : : // Skip all wrong filenames
161 : 0 : const ::rtl::OString filename = lcl_NormalizeFilename(sLine.getToken(1, '\t', n)); // token 1
162 : 0 : if(isFileEmpty || sFileNormalized.equals("") || (!isFileEmpty && filename.equals(sFileNormalized)) )
163 : : {
164 : 0 : const rtl::OString sTYP = sLine.getToken( 1, '\t', n ); // token 3
165 : 0 : const rtl::OString sGID = sLine.getToken( 0, '\t', n ); // token 4
166 : 0 : const rtl::OString sLID = sLine.getToken( 0, '\t', n ); // token 5
167 : 0 : rtl::OString sPFO = sLine.getToken( 1, '\t', n ); // token 7
168 : 0 : sPFO = sHACK;
169 : 0 : rtl::OString nLANG = sLine.getToken( 1, '\t', n ); // token 9
170 : 0 : nLANG = nLANG.trim();
171 : 0 : const rtl::OString sTEXT = sLine.getToken( 0, '\t', n ); // token 10
172 : 0 : const rtl::OString sQHTEXT = sLine.getToken( 1, '\t', n ); // token 12
173 : 0 : const rtl::OString sTITLE = sLine.getToken( 0, '\t', n ); // token 13
174 : :
175 : 0 : if (!nLANG.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")))
176 : : {
177 : 0 : aLanguageSet.insert(nLANG);
178 : 0 : InsertEntry( sTYP, sGID, sLID, sPFO, nLANG, sTEXT, sQHTEXT, sTITLE, filename, bCaseSensitive );
179 : 0 : }
180 : : }
181 : 0 : }
182 : 0 : aInputStream.close();
183 : : }
184 : :
185 : 0 : MergeDataFile::~MergeDataFile()
186 : : {
187 : 0 : for (MergeDataHashMap::iterator aI = aMap.begin(), aEnd = aMap.end(); aI != aEnd; ++aI)
188 : 0 : delete aI->second;
189 : 0 : }
190 : :
191 : 0 : std::vector<rtl::OString> MergeDataFile::GetLanguages()
192 : : {
193 : 0 : return std::vector<rtl::OString>(aLanguageSet.begin(),aLanguageSet.end());
194 : : }
195 : :
196 : 0 : MergeData *MergeDataFile::GetMergeData( ResData *pResData , bool bCaseSensitive )
197 : : {
198 : 0 : rtl::OString sOldG = pResData->sGId;
199 : 0 : rtl::OString sOldL = pResData->sId;
200 : 0 : rtl::OString sGID = pResData->sGId;
201 : 0 : rtl::OString sLID;
202 : 0 : if (sGID.isEmpty())
203 : 0 : sGID = pResData->sId;
204 : : else
205 : 0 : sLID = pResData->sId;
206 : 0 : pResData->sGId = sGID;
207 : 0 : pResData->sId = sLID;
208 : :
209 : 0 : rtl::OString sKey = CreateKey( pResData->sResTyp , pResData->sGId , pResData->sId , pResData->sFilename , bCaseSensitive );
210 : :
211 : 0 : if(aMap.find( sKey ) != aMap.end())
212 : : {
213 : 0 : pResData->sGId = sOldG;
214 : 0 : pResData->sId = sOldL;
215 : 0 : return aMap[ sKey ];
216 : : }
217 : 0 : pResData->sGId = sOldG;
218 : 0 : pResData->sId = sOldL;
219 : 0 : return NULL;
220 : : }
221 : :
222 : :
223 : 0 : PFormEntrys *MergeDataFile::GetPFormEntrys( ResData *pResData )
224 : : {
225 : : // search for requested PFormEntrys
226 : 0 : MergeData *pData = GetMergeData( pResData );
227 : 0 : if ( pData )
228 : 0 : return pData->GetPFormEntries();
229 : 0 : return NULL;
230 : : }
231 : :
232 : 0 : PFormEntrys *MergeDataFile::GetPFormEntrysCaseSensitive( ResData *pResData )
233 : : {
234 : : // search for requested PFormEntrys
235 : 0 : MergeData *pData = GetMergeData( pResData , true );
236 : 0 : if ( pData )
237 : 0 : return pData->GetPFormEntries();
238 : 0 : return NULL;
239 : : }
240 : :
241 : 0 : void MergeDataFile::InsertEntry(
242 : : const rtl::OString &rTYP, const rtl::OString &rGID,
243 : : const rtl::OString &rLID, const rtl::OString &rPFO,
244 : : const rtl::OString &nLANG, const rtl::OString &rTEXT,
245 : : const rtl::OString &rQHTEXT, const rtl::OString &rTITLE ,
246 : : const rtl::OString &rInFilename , bool bCaseSensitive
247 : : )
248 : : {
249 : : MergeData *pData;
250 : :
251 : : // search for MergeData
252 : 0 : rtl::OString sKey = CreateKey(rTYP , rGID , rLID , rInFilename , bCaseSensitive);
253 : 0 : MergeDataHashMap::const_iterator mit;
254 : 0 : mit = aMap.find( sKey );
255 : 0 : if( mit != aMap.end() )
256 : : {
257 : 0 : pData = mit->second;
258 : : }
259 : : else
260 : : {
261 : 0 : pData = new MergeData( rTYP, rGID, rLID, rInFilename );
262 : 0 : aMap.insert( MergeDataHashMap::value_type( sKey, pData ) );
263 : : }
264 : :
265 : 0 : PFormEntrys *pFEntrys = 0;
266 : :
267 : : // search for PFormEntrys
268 : 0 : pFEntrys = pData->GetPFObject( rPFO );
269 : 0 : if( !pFEntrys )
270 : : {
271 : : // create new PFormEntrys, cause no one exists with current properties
272 : 0 : pFEntrys = new PFormEntrys( rPFO );
273 : 0 : pData->Insert( pFEntrys );
274 : : }
275 : :
276 : : // finaly insert the cur string
277 : 0 : pFEntrys->InsertEntry( nLANG , rTEXT, rQHTEXT, rTITLE );
278 : 0 : }
279 : :
280 : 0 : rtl::OString MergeDataFile::CreateKey(const rtl::OString& rTYP, const rtl::OString& rGID,
281 : : const rtl::OString& rLID, const rtl::OString& rFilename, bool bCaseSensitive)
282 : : {
283 : 0 : static const ::rtl::OString sStroke('-');
284 : 0 : ::rtl::OString sKey( rTYP );
285 : 0 : sKey += sStroke;
286 : 0 : sKey += rGID;
287 : 0 : sKey += sStroke;
288 : 0 : sKey += rLID;
289 : 0 : sKey += sStroke;
290 : 0 : sKey += lcl_NormalizeFilename(rFilename);
291 : : OSL_TRACE("created key: %s", sKey.getStr());
292 : 0 : if(bCaseSensitive)
293 : 0 : return sKey; // officecfg case sensitive identifier
294 : 0 : return sKey.toAsciiUpperCase();
295 : : }
296 : :
297 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|