Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _EXPORT_HXX
21 : #define _EXPORT_HXX
22 :
23 : #include "sal/config.h"
24 : #include "po.hxx"
25 :
26 : #include <cstddef>
27 : #include <fstream>
28 :
29 : #include <osl/file.hxx>
30 : #include <osl/file.h>
31 :
32 : #include <boost/unordered_map.hpp>
33 : #include <iterator>
34 : #include <set>
35 : #include <vector>
36 : #include <queue>
37 : #include <string>
38 :
39 : #ifdef WNT
40 : #include <direct.h>
41 : #else
42 : #include <unistd.h>
43 : #endif
44 :
45 : #define NO_TRANSLATE_ISO "x-no-translate"
46 :
47 : class MergeEntrys;
48 :
49 : typedef boost::unordered_map<OString, OString, OStringHash>
50 : OStringHashMap;
51 :
52 : typedef boost::unordered_map<OString, bool, OStringHash>
53 : OStringBoolHashMap;
54 :
55 : #define SOURCE_LANGUAGE "en-US"
56 : #define X_COMMENT "x-comment"
57 : #define LIST_REFID "LIST_REFID"
58 :
59 : //
60 : // class ExportList
61 : //
62 :
63 : typedef OStringHashMap ExportListEntry;
64 :
65 :
66 : /// Container for parse different types of string lists
67 2 : class ExportList
68 : {
69 : private:
70 : std::vector< ExportListEntry* > maList;
71 : std::size_t nSourceLanguageListEntryCount;
72 :
73 : public:
74 2 : ExportList() { nSourceLanguageListEntryCount = 0; }
75 0 : std::size_t GetSourceLanguageListEntryCount() { return nSourceLanguageListEntryCount; }
76 17 : void NewSourceLanguageListEntry() { nSourceLanguageListEntryCount++; }
77 36 : size_t size() const { return maList.size(); }
78 17 : void push_back( ExportListEntry* item ) { maList.push_back( item ); }
79 :
80 34 : ExportListEntry* operator [] ( size_t i )
81 : {
82 34 : return ( i < maList.size() ) ? maList[ i ] : NULL;
83 : }
84 : };
85 :
86 : #define REFID_NONE 0xFFFF
87 :
88 : //
89 : // class ResData
90 : //
91 :
92 : #define ID_LEVEL_NULL 0x0000
93 : #define ID_LEVEL_AUTOID 0x0001
94 : #define ID_LEVEL_TEXT 0x0002
95 : #define ID_LEVEL_FIELDNAME 0x0003
96 : #define ID_LEVEL_ACCESSPATH 0x0004
97 : #define ID_LEVEL_IDENTIFIER 0x0005
98 : #define ID_LEVEL_LISTINDEX 0x0006
99 :
100 :
101 : /// Purpose: holds mandatory data to export a single res (used with ResStack)
102 : class ResData
103 : {
104 : public:
105 : ResData( const OString &rGId );
106 : ResData( const OString &rGId , const OString &rFilename );
107 : ~ResData();
108 : sal_Bool SetId(const OString &rId, sal_uInt16 nLevel);
109 :
110 : sal_uInt16 nIdLevel;
111 : sal_Bool bChild;
112 : sal_Bool bChildWithText;
113 :
114 : sal_Bool bText;
115 : sal_Bool bHelpText;
116 : sal_Bool bQuickHelpText;
117 : sal_Bool bTitle;
118 : sal_Bool bList;
119 :
120 : OString sResTyp;
121 : OString sId;
122 : OString sGId;
123 : OString sHelpId;
124 : OString sFilename;
125 :
126 : OStringHashMap sText;
127 :
128 : OStringHashMap sHelpText;
129 :
130 : OStringHashMap sQuickHelpText;
131 :
132 : OStringHashMap sTitle;
133 :
134 : OString sTextTyp;
135 :
136 : ExportList *pStringList;
137 : ExportList *pUIEntries;
138 : ExportList *pItemList;
139 : ExportList *pFilterList;
140 : ExportList *pPairedList;
141 : };
142 :
143 :
144 : //
145 : // class Export
146 : //
147 :
148 : #define LIST_NON 0x0000
149 : #define LIST_STRING 0x0001
150 : #define LIST_FILTER 0x0002
151 : #define LIST_ITEM 0x0004
152 : #define LIST_PAIRED 0x0005
153 : #define LIST_UIENTRIES 0x0008
154 : #define STRING_TYP_TEXT 0x0010
155 : #define STRING_TYP_HELPTEXT 0x0020
156 : #define STRING_TYP_QUICKHELPTEXT 0x0040
157 : #define STRING_TYP_TITLE 0x0080
158 :
159 : #define MERGE_MODE_NORMAL 0x0000
160 : #define MERGE_MODE_LIST 0x0001
161 :
162 : typedef ::std::vector< ResData* > ResStack;
163 : class ParserQueue;
164 :
165 : /// Purpose: syntax check and export of *.src, called from lexer
166 : class Export
167 : {
168 : private:
169 : union
170 : {
171 : std::ofstream* mSimple;
172 : PoOfstream* mPo;
173 :
174 : } aOutput;
175 :
176 : ResStack aResStack; ///< stack for parsing recursive
177 :
178 : sal_Bool bDefine; // cur. res. in a define?
179 : sal_Bool bNextMustBeDefineEOL; ///< define but no \ at lineend
180 : std::size_t nLevel; // res. recursiv? how deep?
181 : sal_uInt16 nList; ///< cur. res. is List
182 : OString m_sListLang;
183 : std::size_t nListIndex;
184 : std::size_t nListLevel;
185 : bool bSkipFile;
186 : sal_Bool bMergeMode;
187 : OString sMergeSrc;
188 : sal_Bool bError; // any errors while export?
189 : sal_Bool bReadOver;
190 : sal_Bool bDontWriteOutput;
191 : OString sLastTextTyp;
192 : OString sFilename;
193 : OString sLanguages;
194 :
195 : std::vector<OString> aLanguages;
196 :
197 : sal_Bool WriteData( ResData *pResData, sal_Bool bCreateNew = sal_False ); ///< called befor dest. cur ResData
198 : sal_Bool WriteExportList( ResData *pResData, ExportList *pExportList,
199 : const OString &rTyp, sal_Bool bCreateNew = sal_False );
200 :
201 : OString MergePairedList( OString const & sLine , OString const & sText );
202 :
203 : OString FullId(); ///< creates cur. GID
204 :
205 : OString GetPairedListID(const OString & rText);
206 : OString GetPairedListString(const OString& rText);
207 : OString StripList(const OString& rText);
208 :
209 : void InsertListEntry(const OString &rText, const OString &rLine);
210 : void CleanValue( OString &rValue );
211 : OString GetText(const OString &rSource, int nToken);
212 :
213 : /**
214 : Get all MergeEntrys for the ExportList identified by pResData
215 : Check whether list can merge and load all needed MergeEntry from DataBase.
216 : */
217 : bool GetAllMergeEntrysOfList(ResData *pResData, std::vector<MergeEntrys*>& o_vMergeEntrys, ExportList*& o_pList);
218 :
219 : void ResData2Output( MergeEntrys *pEntry, sal_uInt16 nType, const OString& rTextType );
220 : void MergeRest( ResData *pResData, sal_uInt16 nMode = MERGE_MODE_NORMAL );
221 : void ConvertMergeContent( OString &rText );
222 : void ConvertExportContent( OString &rText );
223 :
224 : void WriteToMerged(const OString &rText , bool bSDFContent);
225 : void SetChildWithText();
226 :
227 : void CutComment( OString &rText );
228 :
229 : public:
230 : Export( const OString &rOutput );
231 : Export(const OString &rMergeSource, const OString &rOutput, const OString &rLanguage);
232 : ~Export();
233 :
234 : void Init();
235 : int Execute( int nToken, const char * pToken ); ///< called from lexer
236 0 : void SetError() { bError = sal_True; }
237 13 : sal_Bool GetError() { return bError; }
238 : ParserQueue* pParseQueue; // public!!
239 : };
240 :
241 :
242 : //
243 : // class MergeEntrys
244 : //
245 :
246 : /// Purpose: holds information of data to merge
247 0 : class MergeEntrys
248 : {
249 : friend class MergeDataFile;
250 : private:
251 : OStringHashMap sText;
252 : OStringBoolHashMap bTextFirst;
253 : OStringHashMap sQuickHelpText;
254 : OStringBoolHashMap bQuickHelpTextFirst;
255 : OStringHashMap sTitle;
256 : OStringBoolHashMap bTitleFirst;
257 :
258 : public:
259 0 : MergeEntrys(){};
260 0 : void InsertEntry(const OString &rId, const OString &rText,
261 : const OString &rQuickHelpText, const OString &rTitle)
262 : {
263 :
264 0 : sText[ rId ] = rText;
265 0 : bTextFirst[ rId ] = true;
266 0 : sQuickHelpText[ rId ] = rQuickHelpText;
267 0 : bQuickHelpTextFirst[ rId ] = true;
268 0 : sTitle[ rId ] = rTitle;
269 0 : bTitleFirst[ rId ] = true;
270 0 : }
271 : sal_Bool GetText( OString &rReturn, sal_uInt16 nTyp, const OString &nLangIndex, sal_Bool bDel = sal_False );
272 :
273 : /**
274 : Generate QTZ string with ResData
275 : For executable which works one language and without PO files.
276 : */
277 : static OString GetQTZText(const ResData& rResData, const OString& rOrigText);
278 :
279 : };
280 :
281 : //
282 : // class MergeDataHashMap
283 : //
284 :
285 : class MergeData;
286 :
287 : /** Container for MergeData
288 :
289 : This class is an HashMap with a hidden insertion
290 : order. The class can used just like a simple
291 : HashMap, but good to know that it's use is
292 : more effective if the accessing(find) order
293 : match with the insertion order.
294 :
295 : In the most case, this match is good.
296 : (e.g. reading PO files of different languages,
297 : executables merging)
298 : */
299 : class MergeDataHashMap
300 : {
301 : private:
302 : typedef boost::unordered_map<OString, MergeData*, OStringHash> HashMap_t;
303 :
304 : public:
305 13 : MergeDataHashMap():bFirstSearch(true){};
306 13 : ~MergeDataHashMap(){};
307 :
308 : typedef HashMap_t::iterator iterator;
309 : typedef HashMap_t::const_iterator const_iterator;
310 :
311 : std::pair<iterator,bool> insert(const OString& rKey, MergeData* pMergeData);
312 : iterator find(const OString& rKey);
313 :
314 13 : iterator begin() {return m_aHashMap.begin();}
315 337 : iterator end() {return m_aHashMap.end();}
316 :
317 0 : const_iterator begin() const {return m_aHashMap.begin();}
318 0 : const_iterator end() const {return m_aHashMap.end();}
319 :
320 : private:
321 : bool bFirstSearch;
322 : iterator aLastInsertion;
323 : iterator aLastFound;
324 : iterator aFirstInOrder;
325 : HashMap_t m_aHashMap;
326 : };
327 :
328 : //
329 : // class MergeData
330 : //
331 :
332 : /// Purpose: holds information of data to merge (one resource)
333 : class MergeData
334 : {
335 : friend class MergeDataHashMap;
336 :
337 : public:
338 : OString sTyp;
339 : OString sGID;
340 : OString sLID;
341 : OString sFilename;
342 : MergeEntrys* pMergeEntrys;
343 : private:
344 : MergeDataHashMap::iterator m_aNextData;
345 : public:
346 : MergeData( const OString &rTyp, const OString &rGID, const OString &rLID , const OString &rFilename );
347 : ~MergeData();
348 : MergeEntrys* GetMergeEntries();
349 :
350 : sal_Bool operator==( ResData *pData );
351 : };
352 :
353 : //
354 : // class MergeDataFile
355 : //
356 :
357 : /// Purpose: holds information of data to merge, read from PO file
358 : class MergeDataFile
359 : {
360 : private:
361 : MergeDataHashMap aMap;
362 : std::set<OString> aLanguageSet;
363 :
364 : MergeData *GetMergeData( ResData *pResData , bool bCaseSensitve = false );
365 : void InsertEntry(const OString &rTYP, const OString &rGID,
366 : const OString &rLID, const OString &nLang,
367 : const OString &rTEXT, const OString &rQHTEXT,
368 : const OString &rTITLE, const OString &sFilename,
369 : bool bFirstLang, bool bCaseSensitive);
370 : public:
371 : explicit MergeDataFile(
372 : const OString &rFileName, const OString& rFile,
373 : bool bCaseSensitive, bool bWithQtz = true );
374 : ~MergeDataFile();
375 :
376 :
377 : std::vector<OString> GetLanguages() const;
378 0 : const MergeDataHashMap& getMap() const { return aMap; }
379 :
380 : MergeEntrys *GetMergeEntrys( ResData *pResData );
381 : MergeEntrys *GetMergeEntrysCaseSensitive( ResData *pResData );
382 :
383 : static OString CreateKey(const OString& rTYP, const OString& rGID,
384 : const OString& rLID, const OString& rFilename , bool bCaseSensitive = false);
385 : };
386 :
387 :
388 162390 : class QueueEntry
389 : {
390 : public:
391 32478 : QueueEntry(int nTypVal, const OString &rLineVal)
392 32478 : : nTyp(nTypVal), sLine(rLineVal)
393 : {
394 32478 : }
395 : int nTyp;
396 : OString sLine;
397 : };
398 :
399 : class ParserQueue
400 : {
401 : public:
402 :
403 : ParserQueue( Export& aExportObj );
404 : ~ParserQueue();
405 :
406 : inline void Push( const QueueEntry& aEntry );
407 : bool bCurrentIsM; // public ?
408 : bool bNextIsM; // public ?
409 : bool bLastWasM; // public ?
410 : bool bMflag; // public ?
411 :
412 : void Close();
413 : private:
414 : std::queue<QueueEntry>* aQueueNext;
415 : std::queue<QueueEntry>* aQueueCur;
416 : std::queue<QueueEntry>* aQref;
417 :
418 : Export& aExport;
419 : bool bStart;
420 :
421 : inline void Pop( std::queue<QueueEntry>& aQueue );
422 :
423 : };
424 : #endif // _EXPORT_HXX
425 :
426 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|