LCOV - code coverage report
Current view: top level - libreoffice/l10ntools/source - lngmerge.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 195 0.0 %
Date: 2012-12-27 Functions: 0 10 0.0 %
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             : #include "sal/config.h"
      21             : 
      22             : #include <cstddef>
      23             : #include <fstream>
      24             : #include <iterator>
      25             : #include <string>
      26             : 
      27             : #include "common.hxx"
      28             : #include "lngmerge.hxx"
      29             : 
      30             : namespace {
      31             : 
      32           0 : rtl::OString getBracketedContent(rtl::OString text) {
      33           0 :     return text.getToken(1, '[').getToken(0, ']');
      34             : }
      35             : 
      36             : }
      37             : 
      38             : //
      39             : // class LngParser
      40             : //
      41           0 : LngParser::LngParser(const rtl::OString &rLngFile,
      42             :     sal_Bool bULFFormat)
      43             :     : nError( LNG_OK )
      44             :     , pLines( NULL )
      45             :     , sSource( rLngFile )
      46           0 :     , bULF( bULFFormat )
      47             : {
      48           0 :     pLines = new LngLineList();
      49           0 :     std::ifstream aStream(sSource.getStr());
      50           0 :     if (aStream.is_open())
      51             :     {
      52           0 :         bool bFirstLine = true;
      53           0 :         std::string s;
      54           0 :         std::getline(aStream, s);
      55           0 :         while (!aStream.eof())
      56             :         {
      57           0 :             rtl::OString sLine(s.data(), s.length());
      58             : 
      59           0 :             if( bFirstLine )
      60             :             {
      61             :                 // Always remove UTF8 BOM from the first line
      62           0 :                 Export::RemoveUTF8ByteOrderMarker( sLine );
      63           0 :                 bFirstLine = false;
      64             :             }
      65             : 
      66           0 :             pLines->push_back( new rtl::OString(sLine) );
      67           0 :             std::getline(aStream, s);
      68           0 :         }
      69           0 :         pLines->push_back( new rtl::OString() );
      70             :     }
      71             :     else
      72           0 :         nError = LNG_COULD_NOT_OPEN;
      73           0 : }
      74             : 
      75           0 : LngParser::~LngParser()
      76             : {
      77           0 :     for ( size_t i = 0, n = pLines->size(); i < n; ++i )
      78           0 :         delete (*pLines)[ i ];
      79           0 :     pLines->clear();
      80           0 :     delete pLines;
      81           0 : }
      82             : 
      83           0 : sal_Bool LngParser::CreateSDF(const rtl::OString &rSDFFile,
      84             :     const rtl::OString &rPrj, const rtl::OString &rRoot)
      85             : {
      86             : 
      87           0 :     Export::InitLanguages( false );
      88           0 :     aLanguages = Export::GetLanguages();
      89             :     std::ofstream aSDFStream(
      90           0 :         rSDFFile.getStr(), std::ios_base::out | std::ios_base::trunc);
      91           0 :     if (!aSDFStream.is_open()) {
      92           0 :         nError = SDF_COULD_NOT_OPEN;
      93             :     }
      94           0 :     nError = SDF_OK;
      95             :     rtl::OString sActFileName(
      96           0 :         common::pathnameToken(sSource.getStr(), rRoot.getStr()));
      97             : 
      98           0 :     size_t nPos  = 0;
      99           0 :     sal_Bool bStart = true;
     100           0 :     rtl::OString sGroup, sLine;
     101           0 :     OStringHashMap Text;
     102           0 :     rtl::OString sID;
     103             : 
     104           0 :     while( nPos < pLines->size() ) {
     105           0 :         sLine = *(*pLines)[ nPos++ ];
     106           0 :         while( nPos < pLines->size() && !isNextGroup( sGroup , sLine ) ) {
     107           0 :             ReadLine( sLine , Text );
     108           0 :             sID = sGroup;
     109           0 :             sLine = *(*pLines)[ nPos++ ];
     110             :         };
     111           0 :         if( bStart ) {
     112           0 :             bStart = false;
     113           0 :             sID = sGroup;
     114             :         }
     115             :         else {
     116           0 :             WriteSDF( aSDFStream , Text , rPrj , rRoot , sActFileName , sID );
     117             :         }
     118             :     }
     119           0 :     aSDFStream.close();
     120           0 :     return true;
     121             : }
     122             : 
     123           0 : void LngParser::WriteSDF(std::ofstream &aSDFStream,
     124             :     OStringHashMap &rText_inout, const rtl::OString &rPrj,
     125             :     const rtl::OString &rRoot, const rtl::OString &rActFileName,
     126             :     const rtl::OString &rID)
     127             : {
     128             : 
     129           0 :    sal_Bool bExport = true;
     130           0 :    if ( bExport ) {
     131           0 :        rtl::OString sCur;
     132           0 :        for( unsigned int n = 0; n < aLanguages.size(); n++ ){
     133           0 :            sCur = aLanguages[ n ];
     134           0 :            rtl::OString sAct = rText_inout[ sCur ];
     135           0 :            if ( sAct.isEmpty() && !sCur.isEmpty() )
     136           0 :                sAct = rText_inout[ rtl::OString("en-US") ];
     137             : 
     138           0 :            rtl::OString sOutput( rPrj ); sOutput += "\t";
     139           0 :            if (rRoot.getLength())
     140           0 :                sOutput += rActFileName;
     141           0 :            sOutput += "\t0\t";
     142           0 :            sOutput += "LngText\t";
     143           0 :            sOutput += rID; sOutput += "\t\t\t\t0\t";
     144           0 :            sOutput += sCur; sOutput += "\t";
     145           0 :            sOutput += sAct; sOutput += "\t\t\t\t";
     146           0 :            aSDFStream << sOutput.getStr() << '\n';
     147           0 :        }
     148             :    }
     149           0 : }
     150             : 
     151           0 : bool LngParser::isNextGroup(rtl::OString &sGroup_out, const rtl::OString &sLine_in)
     152             : {
     153           0 :     const OString sLineTrim = sLine_in.trim();
     154           0 :     if ((sLineTrim[0] == '[') && (sLineTrim[sLineTrim.getLength() - 1] == ']'))
     155             :     {
     156           0 :         sGroup_out = getBracketedContent(sLineTrim).trim();
     157           0 :         return true;
     158             :     }
     159           0 :     return false;
     160             : }
     161             : 
     162           0 : void LngParser::ReadLine(const rtl::OString &rLine_in,
     163             :         OStringHashMap &rText_inout)
     164             : {
     165           0 :     if (!rLine_in.match(" *") && !rLine_in.match("/*"))
     166             :     {
     167           0 :         rtl::OString sLang(rLine_in.getToken(0, '=').trim());
     168           0 :         if (!sLang.isEmpty()) {
     169           0 :             rtl::OString sText(rLine_in.getToken(1, '"'));
     170           0 :             rText_inout[sLang] = sText;
     171           0 :         }
     172             :     }
     173           0 : }
     174             : 
     175           0 : sal_Bool LngParser::Merge(
     176             :     const rtl::OString &rSDFFile,
     177             :     const rtl::OString &rDestinationFile)
     178             : {
     179           0 :     Export::InitLanguages( true );
     180             :     std::ofstream aDestination(
     181           0 :         rDestinationFile.getStr(), std::ios_base::out | std::ios_base::trunc);
     182           0 :     if (!aDestination.is_open()) {
     183           0 :         nError = LNG_COULD_NOT_OPEN;
     184             :     }
     185           0 :     nError = LNG_OK;
     186             : 
     187           0 :     MergeDataFile aMergeDataFile( rSDFFile, sSource, false, true );
     188           0 :     rtl::OString sTmp( Export::sLanguages );
     189           0 :     if( sTmp.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("ALL")) )
     190           0 :         Export::SetLanguages( aMergeDataFile.GetLanguages() );
     191           0 :     aLanguages = Export::GetLanguages();
     192             : 
     193           0 :     size_t nPos = 0;
     194           0 :     sal_Bool bGroup = sal_False;
     195           0 :     rtl::OString sGroup;
     196             : 
     197             :     // seek to next group
     198           0 :     while ( nPos < pLines->size() && !bGroup )
     199             :     {
     200           0 :         rtl::OString sLine( *(*pLines)[ nPos ] );
     201           0 :         sLine = sLine.trim();
     202           0 :         if (( sLine[0] == '[' ) &&
     203           0 :             ( sLine[sLine.getLength() - 1] == ']' ))
     204             :         {
     205           0 :             sGroup = getBracketedContent(sLine).trim();
     206           0 :             bGroup = sal_True;
     207             :         }
     208           0 :         nPos ++;
     209           0 :     }
     210             : 
     211           0 :     while ( nPos < pLines->size()) {
     212           0 :         OStringHashMap Text;
     213           0 :         rtl::OString sID( sGroup );
     214           0 :         std::size_t nLastLangPos = 0;
     215             : 
     216           0 :         ResData  *pResData = new ResData( "", sID , sSource );
     217           0 :         pResData->sResTyp = "LngText";
     218           0 :         PFormEntrys *pEntrys = aMergeDataFile.GetPFormEntrys( pResData );
     219             :         // read languages
     220           0 :         bGroup = sal_False;
     221             : 
     222           0 :         rtl::OString sLanguagesDone;
     223             : 
     224           0 :         while ( nPos < pLines->size() && !bGroup )
     225             :         {
     226           0 :             rtl::OString sLine( *(*pLines)[ nPos ] );
     227           0 :             sLine = sLine.trim();
     228           0 :             if (( sLine[0] == '[' ) &&
     229           0 :                 ( sLine[sLine.getLength() - 1] == ']' ))
     230             :             {
     231           0 :                 sGroup = getBracketedContent(sLine).trim();
     232           0 :                 bGroup = sal_True;
     233           0 :                 nPos ++;
     234           0 :                 sLanguagesDone = "";
     235             :             }
     236             :             else
     237             :             {
     238           0 :                 sal_Int32 n = 0;
     239           0 :                 rtl::OString sLang(sLine.getToken(0, '=', n));
     240           0 :                 if (n == -1 || static_cast<bool>(sLine.match("/*")))
     241             :                 {
     242           0 :                     ++nPos;
     243             :                 }
     244             :                 else
     245             :                 {
     246           0 :                     sLang = sLang.trim();
     247             : 
     248           0 :                     rtl::OString sSearch( ";" );
     249           0 :                     sSearch += sLang;
     250           0 :                     sSearch += ";";
     251             : 
     252           0 :                     if (( sLanguagesDone.indexOf( sSearch ) != -1 )) {
     253           0 :                         LngLineList::iterator it = pLines->begin();
     254           0 :                         std::advance( it, nPos );
     255           0 :                         pLines->erase( it );
     256             :                     }
     257           0 :                     if( bULF && pEntrys )
     258             :                     {
     259           0 :                         if( !sLang.isEmpty() )
     260             :                         {
     261           0 :                             rtl::OString sNewText;
     262           0 :                             pEntrys->GetText( sNewText, STRING_TYP_TEXT, sLang, sal_True );
     263           0 :                             if( sLang == "qtz" )
     264           0 :                                 sNewText = sNewText.copy(6);
     265             : 
     266           0 :                             if ( !sNewText.isEmpty()) {
     267           0 :                                 rtl::OString *pLine = (*pLines)[ nPos ];
     268             : 
     269           0 :                                 rtl::OString sText1( sLang );
     270           0 :                                 sText1 += " = \"";
     271             :                                 // escape quotes, unescape double escaped quotes fdo#56648
     272           0 :                                 sText1 += sNewText.replaceAll("\"","\\\"").replaceAll("\\\\\"","\\\"");
     273           0 :                                 sText1 += "\"";
     274           0 :                                 *pLine = sText1;
     275           0 :                                 Text[ sLang ] = sNewText;
     276           0 :                             }
     277             :                         }
     278           0 :                         nLastLangPos = nPos;
     279           0 :                         nPos ++;
     280           0 :                         sLanguagesDone += sSearch;
     281             :                     }
     282             :                     else {
     283           0 :                         nLastLangPos = nPos;
     284           0 :                         nPos ++;
     285           0 :                         sLanguagesDone += sSearch;
     286           0 :                     }
     287           0 :                 }
     288             :             }
     289           0 :         }
     290           0 :         rtl::OString sCur;
     291           0 :         if ( nLastLangPos )
     292             :         {
     293           0 :             for(size_t n = 0; n < aLanguages.size(); ++n)
     294             :             {
     295           0 :                 sCur = aLanguages[ n ];
     296           0 :                 if( !sCur.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US")) && Text[sCur].isEmpty() && pEntrys )
     297             :                 {
     298             : 
     299           0 :                     rtl::OString sNewText;
     300           0 :                     pEntrys->GetText( sNewText, STRING_TYP_TEXT, sCur, sal_True );
     301           0 :                     if( sCur == "qtz" )
     302           0 :                                 sNewText = sNewText.copy(6);
     303           0 :                     if (( !sNewText.isEmpty()) &&
     304           0 :                         !(( sCur.equalsL(RTL_CONSTASCII_STRINGPARAM("x-comment"))) && ( sNewText == "-" )))
     305             :                     {
     306           0 :                         rtl::OString sLine;
     307           0 :                         sLine += sCur;
     308           0 :                         sLine += " = \"";
     309             :                         // escape quotes, unescape double escaped quotes fdo#56648
     310           0 :                         sLine += sNewText.replaceAll("\"","\\\"").replaceAll("\\\\\"","\\\"");
     311           0 :                         sLine += "\"";
     312             : 
     313           0 :                         nLastLangPos++;
     314           0 :                         nPos++;
     315             : 
     316           0 :                         if ( nLastLangPos < pLines->size() ) {
     317           0 :                             LngLineList::iterator it = pLines->begin();
     318           0 :                             std::advance( it, nLastLangPos );
     319           0 :                             pLines->insert( it, new rtl::OString(sLine) );
     320             :                         } else {
     321           0 :                             pLines->push_back( new rtl::OString(sLine) );
     322           0 :                         }
     323           0 :                     }
     324             :                 }
     325             :             }
     326             :         }
     327             : 
     328           0 :         delete pResData;
     329           0 :     }
     330             : 
     331           0 :     for ( size_t i = 0; i < pLines->size(); ++i )
     332           0 :         aDestination << (*pLines)[i]->getStr() << '\n';
     333             : 
     334           0 :     aDestination.close();
     335           0 :     return sal_True;
     336           0 : }
     337             : 
     338             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10