LCOV - code coverage report
Current view: top level - libreoffice/l10ntools/source - export2.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 69 156 44.2 %
Date: 2012-12-27 Functions: 8 14 57.1 %
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 "export.hxx"
      23             : #include <stdio.h>
      24             : #include <osl/time.h>
      25             : #include <osl/process.h>
      26             : #include <rtl/strbuf.hxx>
      27             : #include <rtl/ustring.hxx>
      28             : #include <sal/macros.h>
      29             : #include <iostream>
      30             : #include <iomanip>
      31             : #include <time.h>
      32             : #include <stdlib.h>
      33             : 
      34             : //flags for handleArguments()
      35             : #define STATE_NON       0x0001
      36             : #define STATE_INPUT     0x0002
      37             : #define STATE_OUTPUT    0x0003
      38             : #define STATE_PRJ       0x0004
      39             : #define STATE_ROOT      0x0005
      40             : #define STATE_MERGESRC  0x0006
      41             : #define STATE_LANGUAGES 0x0007
      42             : 
      43             : //
      44             : // class ResData();
      45             : //
      46             : 
      47             : /*****************************************************************************/
      48        1722 : ResData::~ResData()
      49             : /*****************************************************************************/
      50             : {
      51         861 :     if ( pStringList ) {
      52             :         // delete existing res. of type StringList
      53          19 :         for ( size_t i = 0; i < pStringList->size(); i++ ) {
      54          17 :             ExportListEntry* test = (*pStringList)[ i ];
      55          17 :             if( test != NULL ) delete test;
      56             :         }
      57           2 :         delete pStringList;
      58             :     }
      59         861 :     if ( pFilterList ) {
      60             :         // delete existing res. of type FilterList
      61           0 :         for ( size_t i = 0; i < pFilterList->size(); i++ ) {
      62           0 :             ExportListEntry* test = (*pFilterList)[ i ];
      63           0 :             delete test;
      64             :         }
      65           0 :         delete pFilterList;
      66             :     }
      67         861 :     if ( pItemList ) {
      68             :         // delete existing res. of type ItemList
      69           0 :         for ( size_t i = 0; i < pItemList->size(); i++ ) {
      70           0 :             ExportListEntry* test = (*pItemList)[ i ];
      71           0 :             delete test;
      72             :         }
      73           0 :         delete pItemList;
      74             :     }
      75         861 :     if ( pUIEntries ) {
      76             :         // delete existing res. of type UIEntries
      77           0 :         for ( size_t i = 0; i < pUIEntries->size(); i++ ) {
      78           0 :             ExportListEntry* test = (*pUIEntries)[ i ];
      79           0 :             delete test;
      80             :         }
      81           0 :         delete pUIEntries;
      82             :     }
      83         861 : }
      84             : 
      85             : //
      86             : // class Export
      87             : //
      88             : 
      89             : /*****************************************************************************/
      90          21 : rtl::OString Export::sLanguages;
      91             : /*****************************************************************************/
      92             : 
      93          21 : bool Export::handleArguments(
      94             :     int argc, char * argv[], HandledArgs& o_aHandledArgs)
      95             : {
      96          21 :     o_aHandledArgs = HandledArgs();
      97          21 :     sLanguages = "";
      98          21 :     sal_uInt16 nState = STATE_NON;
      99             : 
     100         231 :     for( int i = 1; i < argc; i++ )
     101             :     {
     102         210 :         if ( OString( argv[ i ] ).toAsciiUpperCase() == "-I" )
     103             :         {
     104          21 :             nState = STATE_INPUT; // next token specifies source file
     105             :         }
     106         189 :         else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-O" )
     107             :         {
     108          21 :             nState = STATE_OUTPUT; // next token specifies the dest file
     109             :         }
     110         168 :         else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-P" )
     111             :         {
     112          14 :             nState = STATE_PRJ; // next token specifies the cur. project
     113             :         }
     114         154 :         else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-R" )
     115             :         {
     116           7 :             nState = STATE_ROOT; // next token specifies path to project root
     117             :         }
     118         147 :         else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-M" )
     119             :         {
     120          21 :             nState = STATE_MERGESRC; // next token specifies the merge database
     121             :         }
     122         126 :         else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-L" )
     123             :         {
     124          21 :             nState = STATE_LANGUAGES;
     125             :         }
     126             :         else
     127             :         {
     128         105 :             switch ( nState )
     129             :             {
     130             :                 case STATE_NON:
     131             :                 {
     132           0 :                     return false;    // no valid command line
     133             :                 }
     134             :                 case STATE_INPUT:
     135             :                 {
     136          21 :                     o_aHandledArgs.m_sInputFile = OString( argv[i] );
     137             :                 }
     138          21 :                 break;
     139             :                 case STATE_OUTPUT:
     140             :                 {
     141          21 :                     o_aHandledArgs.m_sOutputFile = OString( argv[i] );
     142             :                 }
     143          21 :                 break;
     144             :                 case STATE_PRJ:
     145             :                 {
     146          14 :                     o_aHandledArgs.m_sPrj = OString( argv[i] );
     147             :                 }
     148          14 :                 break;
     149             :                 case STATE_ROOT:
     150             :                 {
     151           7 :                     o_aHandledArgs.m_sPrjRoot = OString( argv[i] );
     152             :                 }
     153           7 :                 break;
     154             :                 case STATE_MERGESRC:
     155             :                 {
     156          21 :                     o_aHandledArgs.m_sMergeSrc = OString( argv[i] );
     157          21 :                     o_aHandledArgs.m_bMergeMode = true;
     158             :                 }
     159          21 :                 break;
     160             :                 case STATE_LANGUAGES:
     161             :                 {
     162          21 :                     sLanguages = OString( argv[i] );
     163             :                 }
     164          21 :                 break;
     165             :             }
     166             :         }
     167             :     }
     168          42 :     if( !o_aHandledArgs.m_sInputFile.isEmpty() &&
     169          21 :         !o_aHandledArgs.m_sOutputFile.isEmpty() )
     170             :     {
     171          21 :         return true;
     172             :     }
     173             :     else
     174             :     {
     175           0 :         o_aHandledArgs = HandledArgs();
     176           0 :         return false;
     177             :     }
     178             : }
     179             : 
     180           0 : void Export::writeUsage(const OString& rName, const OString& rFileType)
     181             : {
     182             :     std::cout
     183           0 :         << "Syntax: " << rName.getStr()
     184           0 :         << " [-p Prj] [-r PrjRoot] -i FileIn -o FileOut"
     185           0 :         << " [-m DataBase] [-l l1,l2,...]\n"
     186           0 :         << " Prj:      Project\n"
     187           0 :         << " PrjRoot:  Path to project root (../.. etc.)\n"
     188           0 :         << " FileIn:   Source files (*." << rFileType.getStr() << ")\n"
     189           0 :         << " FileOut:  Destination file (*.*)\n"
     190           0 :         << " DataBase: Mergedata (*.po)\n"
     191           0 :         << " -l: Restrict the handled languages; l1, l2, ... are elements of"
     192           0 :         << " (de, en-US, ...)\n";
     193           0 : }
     194             : 
     195             : /*****************************************************************************/
     196          14 : void Export::SetLanguages( std::vector<rtl::OString> val ){
     197             : /*****************************************************************************/
     198          14 :     aLanguages = val;
     199          14 :     isInitialized = true;
     200          14 : }
     201             : /*****************************************************************************/
     202           0 : std::vector<rtl::OString> Export::GetLanguages(){
     203             : /*****************************************************************************/
     204           0 :     return aLanguages;
     205             : }
     206          21 : std::vector<rtl::OString> Export::aLanguages       = std::vector<rtl::OString>();
     207          21 : std::vector<rtl::OString> Export::aForcedLanguages = std::vector<rtl::OString>();
     208             : 
     209             : /*****************************************************************************/
     210           0 : rtl::OString Export::QuoteHTML( rtl::OString const &rString )
     211             : /*****************************************************************************/
     212             : {
     213           0 :     rtl::OStringBuffer sReturn;
     214           0 :     for ( sal_Int32 i = 0; i < rString.getLength(); i++ ) {
     215           0 :         rtl::OString sTemp = rString.copy( i );
     216           0 :         if ( sTemp.match( "<Arg n=" ) ) {
     217           0 :             while ( i < rString.getLength() && rString[i] != '>' ) {
     218           0 :                  sReturn.append(rString[i]);
     219           0 :                 i++;
     220             :             }
     221           0 :             if ( rString[i] == '>' ) {
     222           0 :                 sReturn.append('>');
     223           0 :                 i++;
     224             :             }
     225             :         }
     226           0 :         if ( i < rString.getLength()) {
     227           0 :             switch ( rString[i]) {
     228             :                 case '<':
     229           0 :                     sReturn.append("&lt;");
     230           0 :                 break;
     231             : 
     232             :                 case '>':
     233           0 :                     sReturn.append("&gt;");
     234           0 :                 break;
     235             : 
     236             :                 case '\"':
     237           0 :                     sReturn.append("&quot;");
     238           0 :                 break;
     239             : 
     240             :                 case '\'':
     241           0 :                     sReturn.append("&apos;");
     242           0 :                 break;
     243             : 
     244             :                 case '&':
     245           0 :                     if ((( i + 4 ) < rString.getLength()) &&
     246           0 :                         ( rString.copy( i, 5 ) == "&amp;" ))
     247           0 :                             sReturn.append(rString[i]);
     248             :                     else
     249           0 :                         sReturn.append("&amp;");
     250           0 :                 break;
     251             : 
     252             :                 default:
     253           0 :                     sReturn.append(rString[i]);
     254           0 :                 break;
     255             :             }
     256             :         }
     257           0 :     }
     258           0 :     return sReturn.makeStringAndClear();
     259             : }
     260             : 
     261           0 : void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString )
     262             : {
     263           0 :     if( hasUTF8ByteOrderMarker( rString ) )
     264           0 :         rString = rString.copy(3);
     265           0 : }
     266             : 
     267           0 : bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString )
     268             : {
     269           0 :     return rString.getLength() >= 3 && rString[0] == '\xEF' &&
     270           0 :            rString[1] == '\xBB' && rString[2] == '\xBF' ;
     271             : }
     272             : 
     273             : /*****************************************************************************/
     274           0 : rtl::OString Export::UnquoteHTML( rtl::OString const &rString )
     275             : /*****************************************************************************/
     276             : {
     277           0 :     rtl::OStringBuffer sReturn;
     278           0 :     for (sal_Int32 i = 0; i != rString.getLength();) {
     279           0 :         if (rString.match("&amp;", i)) {
     280           0 :             sReturn.append('&');
     281           0 :             i += RTL_CONSTASCII_LENGTH("&amp;");
     282           0 :         } else if (rString.match("&lt;", i)) {
     283           0 :             sReturn.append('<');
     284           0 :             i += RTL_CONSTASCII_LENGTH("&lt;");
     285           0 :         } else if (rString.match("&gt;", i)) {
     286           0 :             sReturn.append('>');
     287           0 :             i += RTL_CONSTASCII_LENGTH("&gt;");
     288           0 :         } else if (rString.match("&quot;", i)) {
     289           0 :             sReturn.append('"');
     290           0 :             i += RTL_CONSTASCII_LENGTH("&quot;");
     291           0 :         } else if (rString.match("&apos;", i)) {
     292           0 :             sReturn.append('\'');
     293           0 :             i += RTL_CONSTASCII_LENGTH("&apos;");
     294             :         } else {
     295           0 :             sReturn.append(rString[i]);
     296           0 :             ++i;
     297             :         }
     298             :     }
     299           0 :     return sReturn.makeStringAndClear();
     300             : }
     301             : 
     302         343 : bool Export::isSourceLanguage(const rtl::OString &rLanguage)
     303             : {
     304         343 :     return !isAllowed(rLanguage);
     305             : }
     306             : 
     307         343 : bool Export::isAllowed(const rtl::OString &rLanguage)
     308             : {
     309         343 :     return !rLanguage.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US"));
     310             : }
     311             : 
     312             : bool Export::isInitialized = false;
     313             : 
     314             : /*****************************************************************************/
     315          14 : void Export::InitLanguages( bool bMergeMode ){
     316             : /*****************************************************************************/
     317          14 :     if( !isInitialized )
     318             :     {
     319          14 :         rtl::OString sTmp;
     320          14 :         OStringBoolHashMap aEnvLangs;
     321             : 
     322          14 :         sal_Int32 nIndex = 0;
     323          14 :         do
     324             :         {
     325          14 :             rtl::OString aToken = sLanguages.getToken(0, ',', nIndex);
     326          14 :             sTmp = aToken.getToken(0, '=').trim();
     327          14 :             if( bMergeMode && !isAllowed( sTmp ) ){}
     328          14 :             else if( !( (sTmp[0]=='x' || sTmp[0]=='X') && sTmp[1]=='-' ) ){
     329          14 :                 aLanguages.push_back( sTmp );
     330          14 :             }
     331             :         }
     332             :         while ( nIndex >= 0 );
     333             : 
     334          14 :         isInitialized = true;
     335             :     }
     336          77 : }
     337             : 
     338             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10