LCOV - code coverage report
Current view: top level - rsc/source/tools - rsctools.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 152 0.0 %
Date: 2014-04-14 Functions: 0 19 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             : 
      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           0 : int rsc_strnicmp( const char *string1, const char *string2, size_t count )
      37             : {
      38             :     size_t i;
      39             : 
      40           0 :     for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
      41             :     {
      42           0 :         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      43           0 :             return -1;
      44           0 :         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      45           0 :             return 1;
      46             :     }
      47           0 :     if( i == count )
      48           0 :         return 0;
      49           0 :     else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      50           0 :         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           0 : int rsc_stricmp( const char *string1, const char *string2 ){
      58             :     int i;
      59             : 
      60           0 :     for( i = 0; string1[ i ] && string2[ i ]; i++ )
      61             :     {
      62           0 :         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      63           0 :             return -1;
      64           0 :         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      65           0 :             return 1;
      66             :     }
      67           0 :     if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
      68           0 :         return -1;
      69           0 :     else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
      70           0 :         return 1;
      71           0 :     return 0;
      72             : }
      73             : 
      74           0 : char* rsc_strdup( const char* pStr )
      75             : {
      76           0 :     int nLen = strlen( pStr );
      77           0 :     char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
      78           0 :     memcpy( pBuffer, pStr, nLen+1 );
      79           0 :     return pBuffer;
      80             : }
      81             : 
      82           0 : OString GetTmpFileName()
      83             : {
      84           0 :     OUString aTmpURL, aTmpFile;
      85           0 :     osl_createTempFile( NULL, NULL, &aTmpURL.pData );
      86           0 :     osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
      87           0 :     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           0 : OString OutputFile(const OString &rInput, const char * pExt)
     131             : {
     132           0 :     sal_Int32 nSepInd = rInput.lastIndexOf('.');
     133             : 
     134           0 :     if( nSepInd != -1 )
     135             :     {
     136           0 :         return rInput.copy(0, nSepInd + 1).concat(OString(pExt));
     137             :     }
     138             : 
     139           0 :     return rInput.concat(OString(".")).concat(OString(pExt));
     140             : }
     141             : 
     142           0 : 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           0 :     bool bInQuotes = false;
     149             : 
     150             :     // program name
     151           0 :     ppCmd->Append( rsc_strdup( *ppArgv ) );
     152           0 :     for( i = 1; i < nArgc; i++ )
     153             :     {
     154           0 :         if( '@' == **(ppArgv +i) ){ // when @, then response file
     155           0 :             if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
     156           0 :                 return( (*(ppArgv +i)) );
     157           0 :             nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
     158           0 :             while( nItems )
     159             :             {
     160           0 :                 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           0 :                     unsigned int n = 0;
     169           0 :                     while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
     170           0 :                            n +1 < sizeof( szBuffer )  )
     171             :                     {
     172           0 :                         n++;
     173             :                         nItems = fread( &szBuffer[ n ], 1,
     174           0 :                                         sizeof( char ), fFile );
     175           0 :                         if( szBuffer[n] == '"' )
     176           0 :                             bInQuotes = !bInQuotes;
     177             :                     }
     178           0 :                     szBuffer[ n ] = '\0';
     179           0 :                     ppCmd->Append( rsc_strdup( szBuffer ) );
     180             :                 }
     181           0 :                 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
     182             :             };
     183             : 
     184           0 :             fclose( fFile );
     185             :         }
     186             :         else
     187           0 :             ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
     188             :     };
     189           0 :     ppCmd->Append( (void *)0 );
     190           0 :     return NULL;
     191             : }
     192             : 
     193             : 
     194           0 : RscPtrPtr :: RscPtrPtr()
     195             : {
     196           0 :     nCount = 0;
     197           0 :     pMem = NULL;
     198           0 : }
     199             : 
     200           0 : RscPtrPtr :: ~RscPtrPtr()
     201             : {
     202           0 :     Reset();
     203           0 : }
     204             : 
     205           0 : void RscPtrPtr :: Reset()
     206             : {
     207             :     sal_uInt32 i;
     208             : 
     209           0 :     if( pMem )
     210             :     {
     211           0 :         for( i = 0; i < nCount; i++ )
     212             :         {
     213           0 :             if( pMem[ i ] )
     214           0 :                rtl_freeMemory( pMem[ i ] );
     215             :         }
     216           0 :         rtl_freeMemory( (void *)pMem );
     217             :     };
     218           0 :     nCount = 0;
     219           0 :     pMem = NULL;
     220           0 : }
     221             : 
     222           0 : sal_uInt32 RscPtrPtr :: Append( void * pBuffer )
     223             : {
     224           0 :     if( !pMem )
     225           0 :         pMem = (void **)rtl_allocateMemory( (nCount +1) * sizeof( void * ) );
     226             :     else
     227             :         pMem = (void **)rtl_reallocateMemory( (void *)pMem,
     228             :                          ((nCount +1) * sizeof( void * )
     229           0 :                        ) );
     230           0 :     pMem[ nCount ] = pBuffer;
     231           0 :     return nCount++;
     232             : }
     233             : 
     234           0 : void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry )
     235             : {
     236           0 :     if( nEntry < nCount )
     237           0 :         return pMem[ nEntry ];
     238           0 :     return NULL;
     239             : }
     240             : 
     241           0 : RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
     242             : {
     243           0 :     short               nSwapTest = 1;
     244             :     RSCBYTEORDER_TYPE   nMachineOrder;
     245             : 
     246           0 :     bSwap = false;
     247           0 :     if( nOrder != RSC_SYSTEMENDIAN )
     248             :     {
     249           0 :         if( (sal_uInt8)*(sal_uInt8 *)&nSwapTest )
     250           0 :             nMachineOrder = RSC_LITTLEENDIAN;
     251             :         else
     252           0 :             nMachineOrder = RSC_BIGENDIAN;
     253           0 :         bSwap = nOrder != nMachineOrder;
     254             :     }
     255           0 :     nByteOrder = nOrder;
     256           0 :     nLen = 0;
     257           0 :     pMem = NULL;
     258           0 : }
     259             : 
     260           0 : RscWriteRc :: ~RscWriteRc()
     261             : {
     262           0 :     if( pMem )
     263           0 :         rtl_freeMemory( pMem );
     264           0 : }
     265             : 
     266           0 : sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
     267             : {
     268           0 :     sal_uInt32 nOrigPos = nLen;
     269           0 :     nLen += nSize;
     270           0 :     if( pMem )
     271           0 :         pMem = (char*)rtl_reallocateMemory( pMem, nLen );
     272           0 :     if( pMem )
     273           0 :         memset( pMem + nOrigPos, 0, nSize );
     274           0 :     return nOrigPos;
     275             : }
     276             : 
     277           0 : char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
     278             : {
     279           0 :     if( !pMem )
     280             :     {
     281           0 :         pMem = (char *)rtl_allocateMemory( nLen );
     282           0 :         memset( pMem, 0, nLen );
     283             :     }
     284           0 :     return pMem + nSize;
     285             : }
     286             : 
     287             : 
     288           0 : void RscWriteRc :: Put( sal_uInt16 nVal )
     289             : {
     290             :     sal_uInt32  nOldLen;
     291             : 
     292           0 :     nOldLen = IncSize( sizeof( nVal ) );
     293           0 :     PutAt( nOldLen, nVal );
     294           0 : }
     295             : 
     296           0 : void RscWriteRc :: PutUTF8( char * pStr )
     297             : {
     298           0 :     sal_uInt32 nStrLen = 0;
     299           0 :     if( pStr )
     300           0 :         nStrLen = strlen( pStr );
     301             : 
     302           0 :     sal_uInt32  n = nStrLen +1;
     303           0 :     if( n % 2 )
     304             :         // align to 2
     305           0 :         n++;
     306             : 
     307           0 :     sal_uInt32  nOldLen = IncSize( n );
     308           0 :     memcpy( GetPointer( nOldLen ), pStr, nStrLen );
     309             :     // 0 terminated
     310           0 :     pMem[ nOldLen + nStrLen ] = '\0';
     311           0 : }
     312             : 
     313             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10