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 ************************************************************************/
32 #ifndef L10NTOOLS_DIRECTORY_HXX
33 #define L10NTOOLS_DIRECTORY_HXX
34 #include <l10ntools/directory.hxx>
37 #include <tools/string.hxx>
38 #include <tools/stream.hxx>
39 #include <tools/fsys.hxx>
40 #include <osl/file.hxx>
43 #include <boost/unordered_map.hpp>
44 #include <iterator> /* std::iterator*/
45 #include <set> /* std::set*/
46 #include <vector> /* std::vector*/
55 #define NO_TRANSLATE_ISO "x-no-translate"
57 #define JAPANESE_ISO "ja"
61 sal_Bool
operator()(const char* s1
, const char* s2
) const{
62 return strcmp(s1
,s2
)==0;
66 struct equalByteString
{
67 bool operator()( const ByteString
& rKey1
, const ByteString
& rKey2
) const {
68 return rKey1
.CompareTo( rKey2
)==COMPARE_EQUAL
;
71 struct lessByteString
{
72 bool operator()( const ByteString
& rKey1
, const ByteString
& rKey2
) const {
73 return rKey1
.CompareTo( rKey2
)==COMPARE_LESS
;
77 struct hashByteString
{
78 size_t operator()( const ByteString
& rName
) const{
79 return rtl_str_hashCode_WithLength(rName
.GetBuffer(), rName
.Len() );
85 typedef std::set
<ByteString
, lessByteString
> ByteStringSet
;
87 typedef boost::unordered_map
<ByteString
, ByteString
, hashByteString
,equalByteString
>
90 typedef boost::unordered_map
<ByteString
, bool , hashByteString
,equalByteString
>
91 ByteStringBoolHashMap
;
93 typedef boost::unordered_map
<ByteString
, PFormEntrys
* , hashByteString
,equalByteString
>
96 typedef boost::unordered_map
<ByteString
, MergeData
* , hashByteString
,equalByteString
>
99 #define SOURCE_LANGUAGE ByteString("en-US")
100 #define LIST_REFID "LIST_REFID"
102 typedef ByteStringHashMap ExportListEntry
;
103 typedef ::std::vector
< ExportListEntry
* > ExportListBase
;
112 ExportListBase maList
;
113 sal_uLong nSourceLanguageListEntryCount
;
116 ExportList() { nSourceLanguageListEntryCount
= 0; }
117 sal_uLong
GetSourceLanguageListEntryCount() { return nSourceLanguageListEntryCount
; }
118 void NewSourceLanguageListEntry() { nSourceLanguageListEntryCount
++; }
119 size_t size() const { return maList
.size(); }
120 void push_back( ExportListEntry
* item
) { maList
.push_back( item
); }
122 ExportListEntry
* operator [] ( size_t i
)
124 return ( i
< maList
.size() ) ? maList
[ i
] : NULL
;
128 #define REFID_NONE 0xFFFF
134 /******************************************************************************
135 * Purpose: holds mandatory data to export a single res (used with ResStack)
136 ******************************************************************************/
138 #define ID_LEVEL_NULL 0x0000
139 #define ID_LEVEL_AUTOID 0x0001
140 #define ID_LEVEL_TEXT 0x0002
141 #define ID_LEVEL_FIELDNAME 0x0003
142 #define ID_LEVEL_ACCESSPATH 0x0004
143 #define ID_LEVEL_IDENTIFIER 0x0005
144 #define ID_LEVEL_LISTINDEX 0x0006
150 sal_Bool
SetId( const ByteString
&rId
, sal_uInt16 nLevel
);
153 sal_uInt16 nChildIndex
;
156 sal_Bool bChildWithText
;
160 sal_Bool bQuickHelpText
;
164 sal_Bool bRestMerged
;
170 ByteString sFilename
;
172 ByteStringHashMap sText
;
173 sal_uInt16 nTextRefId
;
175 ByteStringHashMap sHelpText
;
176 sal_uInt16 nHelpTextRefId
;
178 ByteStringHashMap sQuickHelpText
;
179 sal_uInt16 nQuickHelpTextRefId
;
181 ByteStringHashMap sTitle
;
182 sal_uInt16 nTitleRefId
;
185 ByteStringHashMap aFallbackData
;
186 ByteStringHashMap aMergedLanguages
;
188 ExportList
*pStringList
;
189 ExportList
*pUIEntries
;
190 ExportList
*pItemList
;
191 ExportList
*pFilterList
;
192 ExportList
*pPairedList
;
197 void addFallbackData( ByteString
& sId
, const ByteString
& sText
);
198 bool getFallbackData( ByteString
& sId
, ByteString
& sText
);
200 void addMergedLanguage( ByteString
& sLang
);
201 bool isMerged( ByteString
& sLang
);
202 ResData( const ByteString
&rPF
, const ByteString
&rGId
)
206 nIdLevel( ID_LEVEL_NULL
),
208 bChildWithText( sal_False
),
210 bHelpText( sal_False
),
211 bQuickHelpText( sal_False
),
214 bRestMerged( sal_False
),
216 nTextRefId( REFID_NONE
),
217 nHelpTextRefId( REFID_NONE
),
218 nQuickHelpTextRefId( REFID_NONE
),
219 nTitleRefId( REFID_NONE
),
228 sGId
.EraseAllChars( '\r' );
229 sPForm
.EraseAllChars( '\r' );
231 ResData( const ByteString
&rPF
, const ByteString
&rGId
, const ByteString
&rFilename
)
234 nIdLevel( ID_LEVEL_NULL
),
236 bChildWithText( sal_False
),
238 bHelpText( sal_False
),
239 bQuickHelpText( sal_False
),
242 bRestMerged( sal_False
),
244 sFilename( rFilename
),
245 nTextRefId( REFID_NONE
),
246 nHelpTextRefId( REFID_NONE
),
247 nQuickHelpTextRefId( REFID_NONE
),
248 nTitleRefId( REFID_NONE
),
258 sGId
.EraseAllChars( '\r' );
259 sPForm
.EraseAllChars( '\r' );
270 /******************************************************************************
271 * Purpose: syntax check and export of *.src, called from lexer
272 ******************************************************************************/
274 #define LIST_NON 0x0000
275 #define LIST_STRING 0x0001
276 #define LIST_FILTER 0x0002
277 #define LIST_ITEM 0x0004
278 #define LIST_PAIRED 0x0005
279 #define LIST_UIENTRIES 0x0008
280 #define STRING_TYP_TEXT 0x0010
281 #define STRING_TYP_HELPTEXT 0x0020
282 #define STRING_TYP_QUICKHELPTEXT 0x0040
283 #define STRING_TYP_TITLE 0x0080
285 #define MERGE_MODE_NORMAL 0x0000
286 #define MERGE_MODE_LIST 0x0001
288 typedef ::std::vector
< ResData
* > ResStack
;
290 class WordTransformer
;
296 WordTransformer
*pWordTransformer
;
298 CharSet aCharSet
; // used charset in src
300 SvFileStream aOutput
;
302 ResStack aResStack
; // stack for parsing recursive
304 ByteString sActPForm
; // hold cur. system
306 sal_Bool bDefine
; // cur. res. in a define?
307 sal_Bool bNextMustBeDefineEOL
; // define but no \ at lineend
308 sal_uLong nLevel
; // res. recursiv? how deep?
309 sal_uInt16 nList
; // cur. res. is String- or FilterList
310 ByteString nListLang
;
311 sal_uLong nListIndex
;
312 sal_uLong nListLevel
;
316 sal_Bool bEnableExport
;
318 ByteString sMergeSrc
;
319 ByteString sLastListLine
;
320 sal_Bool bError
; // any errors while export?
322 sal_Bool bDontWriteOutput
;
323 ByteString sLastTextTyp
;
324 static bool isInitialized
;
325 ByteString sFilename
;
329 ParserQueue
* pParseQueue
; // public ?
330 static ByteString sLanguages
; // public ?
331 static ByteString sForcedLanguages
; // public ?
334 static bool skipProject( ByteString sPrj
) ;
335 static void InitLanguages( bool bMergeMode
= false );
336 static void InitForcedLanguages( bool bMergeMode
= false );
337 static std::vector
<ByteString
> GetLanguages();
338 static std::vector
<ByteString
> GetForcedLanguages();
340 static void SetLanguages( std::vector
<ByteString
> val
);
341 static void RemoveUTF8ByteOrderMarker( ByteString
&rString
);
342 static bool hasUTF8ByteOrderMarker( const ByteString
&rString
);
343 static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString
&rFilename
);
344 static bool fileHasUTF8ByteOrderMarker( const ByteString
&rString
);
345 static ByteString
GetIsoLangByIndex( sal_uInt16 nIndex
);
346 static void QuotHTML( ByteString
&rString
);
347 static bool CopyFile( const ByteString
& source
, const ByteString
& dest
);
349 static void QuotHTMLXRM( ByteString
&rString
);
350 static void UnquotHTML( ByteString
&rString
);
352 static const char* GetEnv( const char *pVar
);
353 static int getCurrentDirectory( rtl::OUString
& base_fqurl
, rtl::OUString
& base
);
355 static bool isSourceLanguage( const ByteString
&sLanguage
);
356 static bool isAllowed( const ByteString
&sLanguage
);
358 static bool LanguageAllowed( const ByteString
&nLanguage
);
359 static void Languages( std::vector
<ByteString
>::const_iterator
& begin
, std::vector
<ByteString
>::const_iterator
& end
);
360 static void getRandomName( const ByteString
& sPrefix
, ByteString
& sRandStr
, const ByteString
& sPostfix
);
361 static void getRandomName( ByteString
& sRandStr
);
362 static void getCurrentDir( std::string
& dir
);
364 static void replaceEncoding( ByteString
& rString
);
366 static ByteString
GetFallbackLanguage( const ByteString nLanguage
);
367 static void FillInFallbacks( ResData
*pResData
);
368 static void FillInListFallbacks( ExportList
*pList
, const ByteString
&nSource
, const ByteString
&nFallback
);
369 static ByteString
GetTimeStamp();
370 static sal_Bool
ConvertLineEnds( ByteString sSource
, ByteString sDestination
);
371 static ByteString
GetNativeFile( ByteString sSource
);
372 static DirEntry
GetTempFile();
374 static void DumpExportList( ByteString
& sListName
, ExportList
& aList
);
375 static ByteString
DumpMap( ByteString
& sMapName
, ByteStringHashMap
& aMap
);
378 static std::vector
<ByteString
> aLanguages
;
379 static std::vector
<ByteString
> aForcedLanguages
;
381 sal_Bool
ListExists( ResData
*pResData
, sal_uInt16 nLst
);
383 sal_Bool
WriteData( ResData
*pResData
, sal_Bool bCreateNew
= sal_False
);// called befor dest. cur ResData
384 sal_Bool
WriteExportList( ResData
*pResData
, ExportList
*pExportList
,
385 const ByteString
&rTyp
, sal_Bool bCreateNew
= sal_False
);
387 ByteString
MergePairedList( ByteString
& sLine
, ByteString
& sText
);
389 ByteString
FullId(); // creates cur. GID
391 bool PairedListFallback( ByteString
& sText
, ResData
& aResData
);
393 ByteString
GetPairedListID ( const ByteString
& sText
);
394 ByteString
GetPairedListString ( const ByteString
& sText
);
395 ByteString
StripList ( const ByteString
& sText
);
397 void UnmergeUTF8( ByteString
& sOrig
);
398 void InsertListEntry( const ByteString
&rText
, const ByteString
&rLine
);
399 void CleanValue( ByteString
&rValue
);
400 ByteString
GetText( const ByteString
&rSource
, int nToken
);
402 sal_Bool
PrepareTextToMerge( ByteString
&rText
, sal_uInt16 nTyp
,
403 ByteString
&nLangIndex
, ResData
*pResData
);
405 void MergeRest( ResData
*pResData
, sal_uInt16 nMode
= MERGE_MODE_NORMAL
);
406 void ConvertMergeContent( ByteString
&rText
);
408 void WriteToMerged( const ByteString
&rText
, bool bSDFContent
);
409 void SetChildWithText();
411 void CutComment( ByteString
&rText
);
414 Export( const ByteString
&rOutput
, sal_Bool bWrite
,
415 const ByteString
&rPrj
, const ByteString
&rPrjRoot
, const ByteString
& rFile
);
416 Export( const ByteString
&rOutput
, sal_Bool bWrite
,
417 const ByteString
&rPrj
, const ByteString
&rPrjRoot
,
418 const ByteString
&rMergeSource
, const ByteString
& rFile
);
422 int Execute( int nToken
, const char * pToken
); // called from lexer
423 void SetError() { bError
= sal_True
; }
424 sal_Bool
GetError() { return bError
; }
432 /******************************************************************************
433 * Purpose: holds information of data to merge (one pform)
434 ******************************************************************************/
436 class PFormEntrys
: public ByteString
438 friend class MergeDataFile
;
440 ByteString sHelpText
; // empty string
441 ByteStringHashMap sText
;
442 ByteStringBoolHashMap bTextFirst
;
443 ByteStringHashMap sQuickHelpText
;
444 ByteStringBoolHashMap bQuickHelpTextFirst
;
445 ByteStringHashMap sTitle
;
446 ByteStringBoolHashMap bTitleFirst
;
449 PFormEntrys( const ByteString
&rPForm
) : ByteString( rPForm
) {};
452 const ByteString
&nId
,
453 const ByteString
&rText
,
454 const ByteString
&rQuickHelpText
,
455 const ByteString
&rTitle
459 sText
[ nId
] = rText
;
460 bTextFirst
[ nId
] = true;
461 sQuickHelpText
[ nId
] = rQuickHelpText
;
462 bQuickHelpTextFirst
[ nId
] = true;
463 sTitle
[ nId
] = rTitle
;
464 bTitleFirst
[ nId
] = true;
466 sal_Bool
GetText( ByteString
&rReturn
, sal_uInt16 nTyp
, const ByteString
&nLangIndex
, sal_Bool bDel
= sal_False
);
467 sal_Bool
GetTransex3Text( ByteString
&rReturn
, sal_uInt16 nTyp
, const ByteString
&nLangIndex
, sal_Bool bDel
= sal_False
);
475 /******************************************************************************
476 * Purpose: holds information of data to merge (one ressource)
477 ******************************************************************************/
483 friend class MergeDataFile
;
488 ByteString sFilename
;
489 PFormEntrysHashMap aMap
;
491 MergeData( const ByteString
&rTyp
, const ByteString
&rGID
, const ByteString
&rLID
, const ByteString
&rFilename
)
492 : sTyp( rTyp
), sGID( rGID
), sLID( rLID
) , sFilename( rFilename
) {};
494 PFormEntrys
* InsertEntry( const ByteString
&rPForm
);
495 PFormEntrys
* GetPFormEntrys( ResData
*pResData
);
497 void Insert( const ByteString
& rPFO
, PFormEntrys
* pfEntrys
);
498 PFormEntrys
* GetPFObject( const ByteString
& rPFO
);
501 sal_Bool
operator==( ResData
*pData
);
505 // class MergeDataFile
508 /******************************************************************************
509 * Purpose: holds information of data to merge
510 ******************************************************************************/
516 ByteString sErrorLog
;
517 SvFileStream aErrLog
;
518 MergeDataHashMap aMap
;
519 std::set
<ByteString
> aLanguageSet
;
521 MergeData
*GetMergeData( ResData
*pResData
, bool bCaseSensitve
= false );
522 void InsertEntry( const ByteString
&rTYP
, const ByteString
&rGID
, const ByteString
&rLID
,
523 const ByteString
&rPFO
,
524 const ByteString
&nLang
, const ByteString
&rTEXT
,
525 const ByteString
&rQHTEXT
, const ByteString
&rTITLE
,
526 const ByteString
&sFilename
, bool bCaseSensitive
529 void WriteError( const ByteString
&rLine
);
532 MergeDataFile( const ByteString
&rFileName
, const ByteString
& rFile
, sal_Bool bErrLog
, CharSet aCharSet
, bool bCaseSensitive
= false );
536 std::vector
<ByteString
> GetLanguages();
538 PFormEntrys
*GetPFormEntrys( ResData
*pResData
);
539 PFormEntrys
*GetPFormEntrysCaseSensitive( ResData
*pResData
);
541 static ByteString
CreateKey( const ByteString
& rTYP
, const ByteString
& rGID
, const ByteString
& rLID
, const ByteString
& rFilename
, bool bCaseSensitive
= false );
548 QueueEntry( int nTypVal
, ByteString sLineVal
): nTyp( nTypVal
) , sLine( sLineVal
){};
557 ParserQueue( Export
& aExportObj
);
560 inline void Push( const QueueEntry
& aEntry
);
561 bool bCurrentIsM
; // public ?
562 bool bNextIsM
; // public ?
563 bool bLastWasM
; // public ?
564 bool bMflag
; // public ?
569 std::queue
<QueueEntry
>* aQueueNext
;
571 std::queue
<QueueEntry
>* aQueueCur
;
573 std::queue
<QueueEntry
>* aQref
;
579 inline void Pop( std::queue
<QueueEntry
>& aQueue
);
584 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */