LCOV - code coverage report
Current view: top level - rsc/source/tools - rsctools.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 125 152 82.2 %
Date: 2014-04-11 Functions: 17 19 89.5 %
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             : 
      21             : #include <stdlib.h>
      22             : #include <stdio.h>
      23             : #if defined (WNT )
      24             : #include <direct.h>
      25             : #endif
      26             : #include <string.h>
      27             : #include <ctype.h>
      28             : 
      29             : #include <rscdef.hxx>
      30             : #include <rsctools.hxx>
      31             : 
      32             : #include <osl/file.h>
      33             : #include <rtl/alloc.h>
      34             : 
      35             : /* case insensitive compare of two strings up to a given length */
      36      143407 : int rsc_strnicmp( const char *string1, const char *string2, size_t count )
      37             : {
      38             :     size_t i;
      39             : 
      40      252325 :     for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
      41             :     {
      42      229604 :         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      43       81767 :             return -1;
      44      147837 :         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      45       38919 :             return 1;
      46             :     }
      47       22721 :     if( i == count )
      48       22609 :         return 0;
      49         112 :     else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      50         112 :         return -1;
      51           0 :     else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      52           0 :         return 1;
      53           0 :     return 0;
      54             : }
      55             : 
      56             : /* case insensitive compare of two strings */
      57      233372 : int rsc_stricmp( const char *string1, const char *string2 ){
      58             :     int i;
      59             : 
      60      339274 :     for( i = 0; string1[ i ] && string2[ i ]; i++ )
      61             :     {
      62      337140 :         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      63      165635 :             return -1;
      64      171505 :         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      65       65603 :             return 1;
      66             :     }
      67        2134 :     if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      68           0 :         return -1;
      69        2134 :     else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      70        1571 :         return 1;
      71         563 :     return 0;
      72             : }
      73             : 
      74       49274 : char* rsc_strdup( const char* pStr )
      75             : {
      76       49274 :     int nLen = strlen( pStr );
      77       49274 :     char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
      78       49274 :     memcpy( pBuffer, pStr, nLen+1 );
      79       49274 :     return pBuffer;
      80             : }
      81             : 
      82         790 : OString GetTmpFileName()
      83             : {
      84        1580 :     OUString aTmpURL, aTmpFile;
      85         790 :     osl_createTempFile( NULL, NULL, &aTmpURL.pData );
      86         790 :     osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
      87        1580 :     return OUStringToOString( aTmpFile, RTL_TEXTENCODING_MS_1252 );
      88             : }
      89             : 
      90           0 : bool Append(FILE * fDest, const OString &rTmpFile)
      91             : {
      92             : #define MAX_BUF 4096
      93           0 :     FILE *fSource = fopen(rTmpFile.getStr(), "rb");
      94           0 :     if( !fDest || !fSource )
      95             :     {
      96           0 :         if( fSource )
      97           0 :             fclose( fSource );
      98           0 :         return false;
      99             :     }
     100             : 
     101           0 :     bool bSuccess = true;
     102             :     char szBuf[ MAX_BUF ];
     103             :     size_t nItems;
     104             : 
     105           0 :     do //append
     106             :     {
     107           0 :         nItems = fread( szBuf, 1, MAX_BUF, fSource );
     108           0 :         bSuccess = (nItems == fwrite(szBuf, 1, nItems, fDest));
     109             :         SAL_WARN_IF(!bSuccess, "rsc", "short write");
     110             :     }
     111           0 :     while (MAX_BUF == nItems && bSuccess);
     112             : 
     113           0 :     fclose( fSource );
     114           0 :     return bSuccess;
     115             : }
     116             : 
     117           0 : bool Append(const OString &rOutputSrs, const OString &rTmpFile)
     118             : {
     119           0 :     FILE * fDest = fopen(rOutputSrs.getStr(), "ab");
     120             : 
     121           0 :     bool bRet = Append(fDest, rTmpFile);
     122             : 
     123           0 :     if( fDest )
     124           0 :         fclose( fDest );
     125             : 
     126           0 :     return bRet;
     127             : }
     128             : 
     129             : /* replaces extension of a file name */
     130         902 : OString OutputFile(const OString &rInput, const char * pExt)
     131             : {
     132         902 :     sal_Int32 nSepInd = rInput.lastIndexOf('.');
     133             : 
     134         902 :     if( nSepInd != -1 )
     135             :     {
     136         507 :         return rInput.copy(0, nSepInd + 1).concat(OString(pExt));
     137             :     }
     138             : 
     139         395 :     return rInput.concat(OString(".")).concat(OString(pExt));
     140             : }
     141             : 
     142         902 : char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
     143             : {
     144             :     FILE    *fFile;
     145             :     int     nItems;
     146             :     char    szBuffer[4096];       // file buffer
     147             :     sal_uInt32  i;
     148         902 :     bool bInQuotes = false;
     149             : 
     150             :     // program name
     151         902 :     ppCmd->Append( rsc_strdup( *ppArgv ) );
     152       13755 :     for( i = 1; i < nArgc; i++ )
     153             :     {
     154       12853 :         if( '@' == **(ppArgv +i) ){ // when @, then response file
     155         451 :             if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
     156           0 :                 return( (*(ppArgv +i)) );
     157         451 :             nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
     158       13683 :             while( nItems )
     159             :             {
     160       12781 :                 if( !isspace( szBuffer[ 0 ] ) )
     161             :                 {
     162             :                     /*
     163             :                      *  #i27914# double ticks '"' now have a duplicate function:
     164             :                      *  1. they define a string ( e.g. -DFOO="baz" )
     165             :                      *  2. a string can contain spaces, so -DFOO="baz zum" defines one
     166             :                      *  argument no two !
     167             :                      */
     168       12007 :                     unsigned int n = 0;
     169      800178 :                     while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
     170      388082 :                            n +1 < sizeof( szBuffer )  )
     171             :                     {
     172      388082 :                         n++;
     173             :                         nItems = fread( &szBuffer[ n ], 1,
     174      388082 :                                         sizeof( char ), fFile );
     175      388082 :                         if( szBuffer[n] == '"' )
     176           0 :                             bInQuotes = !bInQuotes;
     177             :                     }
     178       12007 :                     szBuffer[ n ] = '\0';
     179       12007 :                     ppCmd->Append( rsc_strdup( szBuffer ) );
     180             :                 }
     181       12781 :                 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
     182             :             };
     183             : 
     184         451 :             fclose( fFile );
     185             :         }
     186             :         else
     187       12402 :             ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
     188             :     };
     189         902 :     ppCmd->Append( (void *)0 );
     190         902 :     return NULL;
     191             : }
     192             : 
     193             : 
     194        2143 : RscPtrPtr :: RscPtrPtr()
     195             : {
     196        2143 :     nCount = 0;
     197        2143 :     pMem = NULL;
     198        2143 : }
     199             : 
     200        2143 : RscPtrPtr :: ~RscPtrPtr()
     201             : {
     202        2143 :     Reset();
     203        2143 : }
     204             : 
     205        2143 : void RscPtrPtr :: Reset()
     206             : {
     207             :     sal_uInt32 i;
     208             : 
     209        2143 :     if( pMem )
     210             :     {
     211       53109 :         for( i = 0; i < nCount; i++ )
     212             :         {
     213       50966 :             if( pMem[ i ] )
     214       49274 :                rtl_freeMemory( pMem[ i ] );
     215             :         }
     216        2143 :         rtl_freeMemory( (void *)pMem );
     217             :     };
     218        2143 :     nCount = 0;
     219        2143 :     pMem = NULL;
     220        2143 : }
     221             : 
     222       50966 : sal_uInt32 RscPtrPtr :: Append( void * pBuffer )
     223             : {
     224       50966 :     if( !pMem )
     225        2143 :         pMem = (void **)rtl_allocateMemory( (nCount +1) * sizeof( void * ) );
     226             :     else
     227             :         pMem = (void **)rtl_reallocateMemory( (void *)pMem,
     228             :                          ((nCount +1) * sizeof( void * )
     229       48823 :                        ) );
     230       50966 :     pMem[ nCount ] = pBuffer;
     231       50966 :     return nCount++;
     232             : }
     233             : 
     234      203810 : void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry )
     235             : {
     236      203810 :     if( nEntry < nCount )
     237      203810 :         return pMem[ nEntry ];
     238           0 :     return NULL;
     239             : }
     240             : 
     241       17725 : RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
     242             : {
     243       17725 :     short               nSwapTest = 1;
     244             :     RSCBYTEORDER_TYPE   nMachineOrder;
     245             : 
     246       17725 :     bSwap = false;
     247       17725 :     if( nOrder != RSC_SYSTEMENDIAN )
     248             :     {
     249        9497 :         if( (sal_uInt8)*(sal_uInt8 *)&nSwapTest )
     250        9497 :             nMachineOrder = RSC_LITTLEENDIAN;
     251             :         else
     252           0 :             nMachineOrder = RSC_BIGENDIAN;
     253        9497 :         bSwap = nOrder != nMachineOrder;
     254             :     }
     255       17725 :     nByteOrder = nOrder;
     256       17725 :     nLen = 0;
     257       17725 :     pMem = NULL;
     258       17725 : }
     259             : 
     260       17725 : RscWriteRc :: ~RscWriteRc()
     261             : {
     262       17725 :     if( pMem )
     263       17725 :         rtl_freeMemory( pMem );
     264       17725 : }
     265             : 
     266      206200 : sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
     267             : {
     268      206200 :     sal_uInt32 nOrigPos = nLen;
     269      206200 :     nLen += nSize;
     270      206200 :     if( pMem )
     271      178937 :         pMem = (char*)rtl_reallocateMemory( pMem, nLen );
     272      206200 :     if( pMem )
     273      178937 :         memset( pMem + nOrigPos, 0, nSize );
     274      206200 :     return nOrigPos;
     275             : }
     276             : 
     277      486938 : char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
     278             : {
     279      486938 :     if( !pMem )
     280             :     {
     281       17725 :         pMem = (char *)rtl_allocateMemory( nLen );
     282       17725 :         memset( pMem, 0, nLen );
     283             :     }
     284      486938 :     return pMem + nSize;
     285             : }
     286             : 
     287             : 
     288      154233 : void RscWriteRc :: Put( sal_uInt16 nVal )
     289             : {
     290             :     sal_uInt32  nOldLen;
     291             : 
     292      154233 :     nOldLen = IncSize( sizeof( nVal ) );
     293      154233 :     PutAt( nOldLen, nVal );
     294      154233 : }
     295             : 
     296       28293 : void RscWriteRc :: PutUTF8( char * pStr )
     297             : {
     298       28293 :     sal_uInt32 nStrLen = 0;
     299       28293 :     if( pStr )
     300       27861 :         nStrLen = strlen( pStr );
     301             : 
     302       28293 :     sal_uInt32  n = nStrLen +1;
     303       28293 :     if( n % 2 )
     304             :         // align to 2
     305       14354 :         n++;
     306             : 
     307       28293 :     sal_uInt32  nOldLen = IncSize( n );
     308       28293 :     memcpy( GetPointer( nOldLen ), pStr, nStrLen );
     309             :     // 0 terminated
     310       28293 :     pMem[ nOldLen + nStrLen ] = '\0';
     311       28293 : }
     312             : 
     313             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10