LCOV - code coverage report
Current view: top level - l10ntools/inc - export.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 11 27 40.7 %
Date: 2014-11-03 Functions: 10 19 52.6 %
Legend: Lines: hit not hit

          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 INCLUDED_L10NTOOLS_INC_EXPORT_HXX
      21             : #define INCLUDED_L10NTOOLS_INC_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             : 
      58             : 
      59             : // class ResData
      60             : 
      61             : 
      62             : #define ID_LEVEL_NULL       0x0000
      63             : #define ID_LEVEL_TEXT       0x0002
      64             : #define ID_LEVEL_IDENTIFIER 0x0005
      65             : 
      66             : typedef std::vector< OString > ExportList;
      67             : 
      68             : /// Purpose: holds mandatory data to export a single res (used with ResStack)
      69         676 : class ResData
      70             : {
      71             : public:
      72             :     ResData( const OString &rGId );
      73             :     ResData( const OString &rGId , const OString &rFilename );
      74             :     bool SetId(const OString &rId, sal_uInt16 nLevel);
      75             : 
      76             :     sal_uInt16 nIdLevel;
      77             :     bool bChild;
      78             :     bool bChildWithText;
      79             : 
      80             :     bool bText;
      81             :     bool bQuickHelpText;
      82             :     bool bTitle;
      83             : 
      84             :     OString sResTyp;
      85             :     OString sId;
      86             :     OString sGId;
      87             :     OString sFilename;
      88             : 
      89             :     OStringHashMap sText;
      90             : 
      91             :     OStringHashMap sQuickHelpText;
      92             : 
      93             :     OStringHashMap sTitle;
      94             : 
      95             :     OString sTextTyp;
      96             : 
      97             :     ExportList  m_aList;
      98             : };
      99             : 
     100             : 
     101             : 
     102             : // class Export
     103             : 
     104             : 
     105             : #define LIST_NON                    0x0000
     106             : #define LIST_STRING                 0x0001
     107             : #define LIST_FILTER                 0x0002
     108             : #define LIST_ITEM                   0x0004
     109             : #define LIST_PAIRED                 0x0005
     110             : #define STRING_TYP_TEXT             0x0010
     111             : #define STRING_TYP_QUICKHELPTEXT    0x0040
     112             : #define STRING_TYP_TITLE            0x0080
     113             : 
     114             : #define MERGE_MODE_NORMAL           0x0000
     115             : #define MERGE_MODE_LIST             0x0001
     116             : 
     117             : typedef ::std::vector< ResData* > ResStack;
     118             : class ParserQueue;
     119             : 
     120             : /// Purpose: syntax check and export of *.src, called from lexer
     121             : class Export
     122             : {
     123             : private:
     124             :     union
     125             :     {
     126             :         std::ofstream* mSimple;
     127             :         PoOfstream* mPo;
     128             : 
     129             :     } aOutput;
     130             : 
     131             :     ResStack aResStack;                 ///< stack for parsing recursive
     132             : 
     133             :     bool bDefine;                       // cur. res. in a define?
     134             :     bool bNextMustBeDefineEOL;          ///< define but no \ at lineend
     135             :     std::size_t nLevel; // res. recursiv? how deep?
     136             :     sal_uInt16 nList;                       ///< cur. res. is List
     137             :     std::size_t nListIndex;
     138             :     std::size_t nListLevel;
     139             :     bool bMergeMode;
     140             :     OString sMergeSrc;
     141             :     bool bError;                        // any errors while export?
     142             :     bool bReadOver;
     143             :     OString sFilename;
     144             :     OString sLanguages;
     145             : 
     146             :     std::vector<OString> aLanguages;
     147             : 
     148             :     ParserQueue* pParseQueue;
     149             : 
     150             :     bool WriteData( ResData *pResData, bool bCreateNew = false ); ///< called before dest. cur ResData
     151             :     bool WriteExportList( ResData *pResData, ExportList& rExportList, const sal_uInt16 nTyp );
     152             : 
     153             :     OString MergePairedList( OString const & sLine , OString const & sText );
     154             : 
     155             :     OString FullId();                    ///< creates cur. GID
     156             : 
     157             :     OString GetPairedListID(const OString & rText);
     158             :     OString GetPairedListString(const OString& rText);
     159             :     OString StripList(const OString& rText);
     160             : 
     161             :     void InsertListEntry(const OString &rLine);
     162             :     void CleanValue( OString &rValue );
     163             :     OString GetText(const OString &rSource, int nToken);
     164             : 
     165             :     void ResData2Output( MergeEntrys *pEntry, sal_uInt16 nType, const OString& rTextType );
     166             :     void MergeRest( ResData *pResData );
     167             :     void ConvertMergeContent( OString &rText );
     168             :     void ConvertExportContent( OString &rText );
     169             : 
     170             :     void WriteToMerged(const OString &rText , bool bSDFContent);
     171             :     void SetChildWithText();
     172             : 
     173             :     void CutComment( OString &rText );
     174             : 
     175           0 :     void WriteUTF8ByteOrderMarkToOutput() { *aOutput.mSimple << '\xEF' << '\xBB' << '\xBF'; }
     176             : 
     177             : public:
     178             :     Export( const OString &rOutput );
     179             :     Export(const OString &rMergeSource, const OString &rOutput, const OString &rLanguage, bool bUTF8BOM);
     180             :     ~Export();
     181             : 
     182             :     void Init();
     183             :     int Execute( int nToken, const char * pToken ); ///< called from lexer
     184             : 
     185           0 :     void SetError() { bError = true; }
     186           8 :     bool GetError() { return bError; }
     187       23601 :     ParserQueue* GetParseQueue() { return pParseQueue; }
     188             : };
     189             : 
     190             : 
     191             : // class MergeEntrys
     192             : 
     193             : 
     194             : /// Purpose: holds information of data to merge
     195           0 : class MergeEntrys
     196             : {
     197             : friend class MergeDataFile;
     198             : private:
     199             :     OStringHashMap sText;
     200             :     OStringBoolHashMap bTextFirst;
     201             :     OStringHashMap sQuickHelpText;
     202             :     OStringBoolHashMap bQuickHelpTextFirst;
     203             :     OStringHashMap sTitle;
     204             :     OStringBoolHashMap bTitleFirst;
     205             : 
     206             : public:
     207           0 :     MergeEntrys(){};
     208           0 :     void InsertEntry(const OString &rId, const OString &rText,
     209             :         const OString &rQuickHelpText, const OString &rTitle)
     210             :     {
     211             : 
     212           0 :         sText[ rId ] = rText;
     213           0 :         bTextFirst[ rId ] = true;
     214           0 :         sQuickHelpText[ rId ] = rQuickHelpText;
     215           0 :         bQuickHelpTextFirst[ rId ] = true;
     216           0 :         sTitle[ rId ] = rTitle;
     217           0 :         bTitleFirst[ rId ] = true;
     218           0 :     }
     219             :     bool GetText( OString &rReturn, sal_uInt16 nTyp, const OString &nLangIndex, bool bDel = false );
     220             : 
     221             :     /**
     222             :       Generate QTZ string with ResData
     223             :       For executable which works one language and without PO files.
     224             :     */
     225             :     static OString GetQTZText(const ResData& rResData, const OString& rOrigText);
     226             : 
     227             : };
     228             : 
     229             : 
     230             : // class MergeDataHashMap
     231             : 
     232             : 
     233             : class MergeData;
     234             : 
     235             : /** Container for MergeData
     236             : 
     237             :   This class is an HashMap with a hidden insertion
     238             :   order. The class can used just like a simple
     239             :   HashMap, but good to know that it's use is
     240             :   more effective if the accessing(find) order
     241             :   match with the insertion order.
     242             : 
     243             :   In the most case, this match is good.
     244             :   (e.g. reading PO files of different languages,
     245             :   executables merging)
     246             : */
     247             : class MergeDataHashMap
     248             : {
     249             :     private:
     250             :         typedef boost::unordered_map<OString, MergeData*, OStringHash> HashMap_t;
     251             : 
     252             :     public:
     253           8 :         MergeDataHashMap():bFirstSearch(true){};
     254           8 :         ~MergeDataHashMap(){};
     255             : 
     256             :         typedef HashMap_t::iterator iterator;
     257             :         typedef HashMap_t::const_iterator const_iterator;
     258             : 
     259             :         std::pair<iterator,bool> insert(const OString& rKey, MergeData* pMergeData);
     260             :         iterator find(const OString& rKey);
     261             : 
     262           8 :         iterator begin() {return m_aHashMap.begin();}
     263         247 :         iterator end() {return m_aHashMap.end();}
     264             : 
     265           0 :         const_iterator begin() const {return m_aHashMap.begin();}
     266           0 :         const_iterator end() const {return m_aHashMap.end();}
     267             : 
     268             :     private:
     269             :         bool bFirstSearch;
     270             :         iterator aLastInsertion;
     271             :         iterator aLastFound;
     272             :         iterator aFirstInOrder;
     273             :         HashMap_t m_aHashMap;
     274             : };
     275             : 
     276             : 
     277             : // class MergeData
     278             : 
     279             : 
     280             : /// Purpose: holds information of data to merge (one resource)
     281             : class MergeData
     282             : {
     283             :     friend class MergeDataHashMap;
     284             : 
     285             : public:
     286             :     OString sTyp;
     287             :     OString sGID;
     288             :     OString sLID;
     289             :     OString sFilename;
     290             :     MergeEntrys* pMergeEntrys;
     291             : private:
     292             :     MergeDataHashMap::iterator m_aNextData;
     293             : public:
     294             :     MergeData( const OString &rTyp, const OString &rGID, const OString &rLID , const OString &rFilename );
     295             :     ~MergeData();
     296           0 :     MergeEntrys* GetMergeEntries() { return pMergeEntrys;}
     297             : 
     298             :     bool operator==( ResData *pData );
     299             : };
     300             : 
     301             : 
     302             : // class MergeDataFile
     303             : 
     304             : 
     305             : /// Purpose: holds information of data to merge, read from PO file
     306             : class MergeDataFile
     307             : {
     308             :     private:
     309             :         MergeDataHashMap aMap;
     310             :         std::set<OString> aLanguageSet;
     311             : 
     312             :         MergeData *GetMergeData( ResData *pResData , bool bCaseSensitve = false );
     313             :         void InsertEntry(const OString &rTYP, const OString &rGID,
     314             :             const OString &rLID, const OString &nLang,
     315             :             const OString &rTEXT, const OString &rQHTEXT,
     316             :             const OString &rTITLE, const OString &sFilename,
     317             :             bool bFirstLang, bool bCaseSensitive);
     318             :     public:
     319             :         explicit MergeDataFile(
     320             :             const OString &rFileName, const OString& rFile,
     321             :             bool bCaseSensitive, bool bWithQtz = true );
     322             :         ~MergeDataFile();
     323             : 
     324             : 
     325             :         std::vector<OString> GetLanguages() const;
     326           0 :         const MergeDataHashMap& getMap() const { return aMap; }
     327             : 
     328             :         MergeEntrys *GetMergeEntrys( ResData *pResData );
     329             :         MergeEntrys *GetMergeEntrysCaseSensitive( ResData *pResData );
     330             : 
     331             :         static OString CreateKey(const OString& rTYP, const OString& rGID,
     332             :             const OString& rLID, const OString& rFilename , bool bCaseSensitive = false);
     333             : };
     334             : 
     335             : 
     336      117965 : class QueueEntry
     337             : {
     338             : public:
     339       23593 :     QueueEntry(int nTypVal, const OString &rLineVal)
     340       23593 :         : nTyp(nTypVal), sLine(rLineVal)
     341             :     {
     342       23593 :     }
     343             :     int nTyp;
     344             :     OString sLine;
     345             : };
     346             : 
     347             : class ParserQueue
     348             : {
     349             : public:
     350             : 
     351             :     ParserQueue( Export& aExportObj );
     352             :     ~ParserQueue();
     353             : 
     354             :     inline void Push( const QueueEntry& aEntry );
     355             :     bool bCurrentIsM;  // public ?
     356             :     bool bNextIsM;   // public ?
     357             :     bool bLastWasM;   // public ?
     358             :     bool bMflag;   // public ?
     359             : 
     360             :     void Close();
     361             : private:
     362             :     std::queue<QueueEntry>* aQueueNext;
     363             :     std::queue<QueueEntry>* aQueueCur;
     364             : 
     365             :     Export& aExport;
     366             :     bool bStart;
     367             : 
     368             :     inline void Pop( std::queue<QueueEntry>& aQueue );
     369             : 
     370             : };
     371             : #endif // INCLUDED_L10NTOOLS_INC_EXPORT_HXX
     372             : 
     373             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10