LCOV - code coverage report
Current view: top level - idl/source/prj - svidl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 106 212 50.0 %
Date: 2014-04-11 Functions: 3 3 100.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             : #include <database.hxx>
      24             : #include <globals.hxx>
      25             : #include <command.hxx>
      26             : #include <rtl/ustring.hxx>
      27             : #include <osl/file.hxx>
      28             : 
      29             : #define BR 0x8000
      30          40 : sal_Bool FileMove_Impl( const OUString & rFile1, const OUString & rFile2, sal_Bool bImmerVerschieben )
      31             : {
      32             :     //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
      33          40 :     sal_uLong nC1 = 0;
      34          40 :     sal_uLong nC2 = 1;
      35          40 :     if( !bImmerVerschieben )
      36             :     {
      37          40 :         SvFileStream aOutStm1( rFile1, STREAM_STD_READ );
      38          80 :         SvFileStream aOutStm2( rFile2, STREAM_STD_READ );
      39          40 :         if( aOutStm1.GetError() == SVSTREAM_OK )
      40             :         {
      41           0 :             sal_uInt8 * pBuf1 = new sal_uInt8[ BR ];
      42           0 :             sal_uInt8 * pBuf2 = new sal_uInt8[ BR ];
      43           0 :             nC1 = aOutStm1.Read( pBuf1, BR );
      44           0 :             nC2 = aOutStm2.Read( pBuf2, BR );
      45           0 :             while( nC1 == nC2 )
      46             :             {
      47           0 :                 if( memcmp( pBuf1, pBuf2, nC1 ) )
      48             :                 {
      49           0 :                     nC1++;
      50           0 :                     break;
      51             :                 }
      52             :                 else
      53             :                 {
      54           0 :                     if( 0x8000 != nC1 )
      55           0 :                         break;
      56           0 :                     nC1 = aOutStm1.Read( pBuf1, BR );
      57           0 :                     nC2 = aOutStm2.Read( pBuf2, BR );
      58             :                 }
      59             :             }
      60           0 :             delete[] pBuf1;
      61           0 :             delete[] pBuf2;
      62          40 :         }
      63             :     }
      64          40 :     OUString fileURL2;
      65          40 :     osl::FileBase::getFileURLFromSystemPath( rFile2, fileURL2 );
      66          40 :     if( nC1 != nC2 )
      67             :     {// something has changed
      68          40 :         OUString fileURL1;
      69          40 :         osl::FileBase::getFileURLFromSystemPath( rFile1, fileURL1 );
      70             :         // move file
      71          40 :         if( osl::FileBase::E_None != osl::File::move( fileURL2, fileURL1 ) )
      72             :         {
      73             :             // delete both files
      74           0 :             osl::File::remove( fileURL1 );
      75           0 :             osl::File::remove( fileURL2 );
      76           0 :             return sal_False;
      77             :         }
      78          40 :         return sal_True;
      79             :     }
      80           0 :     return osl::FileBase::E_None == osl::File::remove( fileURL2 );
      81             : }
      82             : 
      83             : //This function gets a system path to a file [fname], creates a temp file in
      84             : //the same folder as [fname] and returns the system path of the temp file.
      85          40 : inline OUString tempFileHelper(OUString const & fname)
      86             : {
      87          40 :     OUString aTmpFile;
      88             : 
      89          40 :     sal_Int32 delimIndex = fname.lastIndexOf( '/' );
      90          40 :     if( delimIndex > 0 )
      91             :     {
      92          40 :         OUString aTmpDir( fname.copy( 0,  delimIndex ) );
      93          40 :         osl::FileBase::getFileURLFromSystemPath( aTmpDir, aTmpDir );
      94          40 :         osl::FileBase::createTempFile( &aTmpDir, 0, &aTmpFile );
      95          40 :         osl::FileBase::getSystemPathFromFileURL( aTmpFile, aTmpFile );
      96             :     }
      97             :     else
      98             :     {
      99           0 :         OStringBuffer aStr("invalid filename: ");
     100           0 :         aStr.append(OUStringToOString(fname, RTL_TEXTENCODING_UTF8));
     101           0 :         fprintf(stderr, "%s\n", aStr.getStr());
     102             :     }
     103          40 :     return aTmpFile;
     104             : }
     105             : 
     106           8 : int main ( int argc, char ** argv)
     107             : {
     108           8 :     OUString aTmpListFile;
     109          16 :     OUString aTmpSlotMapFile;
     110          16 :     OUString aTmpSfxItemFile;
     111          16 :     OUString aTmpDataBaseFile;
     112          16 :     OUString aTmpHelpIdFile;
     113          16 :     OUString aTmpCSVFile;
     114          16 :     OUString aTmpDocuFile;
     115          16 :     OUString aTmpDepFile;
     116             : 
     117          16 :     SvCommand aCommand( argc, argv );
     118             : 
     119           8 :     if( aCommand.nVerbosity != 0 )
     120           0 :         printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
     121             : 
     122           8 :     Init();
     123           8 :     SvIdlWorkingBase * pDataBase = new SvIdlWorkingBase(aCommand);
     124             : 
     125           8 :     int nExit = 0;
     126           8 :     if( !aCommand.aExportFile.isEmpty() )
     127             :     {
     128           8 :         osl::DirectoryItem aDI;
     129          16 :         osl::FileStatus fileStatus( osl_FileStatus_Mask_FileName );
     130           8 :         osl::DirectoryItem::get( aCommand.aExportFile, aDI );
     131           8 :         aDI.getFileStatus(fileStatus);
     132          16 :         pDataBase->SetExportFile( fileStatus.getFileName() );
     133             :     }
     134             : 
     135           8 :     if( ReadIdl( pDataBase, aCommand ) )
     136             :     {
     137           8 :         if( nExit == 0 && !aCommand.aDocuFile.isEmpty() )
     138             :         {
     139           0 :             aTmpDocuFile = tempFileHelper(aCommand.aDocuFile);
     140           0 :             SvFileStream aOutStm( aTmpDocuFile, STREAM_READWRITE | STREAM_TRUNC );
     141           0 :             if( !pDataBase->WriteDocumentation( aOutStm ) )
     142             :             {
     143           0 :                 nExit = -1;
     144           0 :                 OStringBuffer aStr("cannot write documentation file: ");
     145           0 :                 aStr.append(OUStringToOString(aCommand.aDocuFile, RTL_TEXTENCODING_UTF8));
     146           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     147           0 :             }
     148             :         }
     149           8 :         if( nExit == 0 && !aCommand.aListFile.isEmpty() )
     150             :         {
     151           8 :             aTmpListFile = tempFileHelper(aCommand.aListFile);
     152           8 :             SvFileStream aOutStm( aTmpListFile, STREAM_READWRITE | STREAM_TRUNC );
     153           8 :             if( !pDataBase->WriteSvIdl( aOutStm ) )
     154             :             {
     155           0 :                 nExit = -1;
     156           0 :                 OStringBuffer aStr("cannot write list file: ");
     157           0 :                 aStr.append(OUStringToOString(aCommand.aListFile, RTL_TEXTENCODING_UTF8));
     158           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     159           8 :             }
     160             :         }
     161           8 :         if( nExit == 0 && !aCommand.aSlotMapFile.isEmpty() )
     162             :         {
     163           8 :             aTmpSlotMapFile = tempFileHelper(aCommand.aSlotMapFile);
     164           8 :             SvFileStream aOutStm( aTmpSlotMapFile, STREAM_READWRITE | STREAM_TRUNC );
     165           8 :             if( !pDataBase->WriteSfx( aOutStm ) )
     166             :             {
     167           0 :                 nExit = -1;
     168           0 :                 OStringBuffer aStr("cannot write slotmap file: ");
     169           0 :                 aStr.append(OUStringToOString(aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8));
     170           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     171           8 :             }
     172             :         }
     173           8 :         if( nExit == 0 && !aCommand.aHelpIdFile.isEmpty() )
     174             :         {
     175           8 :             aTmpHelpIdFile = tempFileHelper(aCommand.aHelpIdFile);
     176           8 :             SvFileStream aStm( aTmpHelpIdFile, STREAM_READWRITE | STREAM_TRUNC );
     177           8 :             if (!pDataBase->WriteHelpIds( aStm ) )
     178             :             {
     179           0 :                 nExit = -1;
     180           0 :                 OStringBuffer aStr("cannot write help ID file: ");
     181           0 :                 aStr.append(OUStringToOString(aCommand.aHelpIdFile, RTL_TEXTENCODING_UTF8));
     182           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     183           8 :             }
     184             :         }
     185           8 :         if( nExit == 0 && !aCommand.aCSVFile.isEmpty() )
     186             :         {
     187           0 :             aTmpCSVFile = tempFileHelper(aCommand.aCSVFile);
     188           0 :             SvFileStream aStm( aTmpCSVFile, STREAM_READWRITE | STREAM_TRUNC );
     189           0 :             if (!pDataBase->WriteCSV( aStm ) )
     190             :             {
     191           0 :                 nExit = -1;
     192           0 :                 OStringBuffer aStr("cannot write CSV file: ");
     193           0 :                 aStr.append(OUStringToOString(aCommand.aCSVFile, RTL_TEXTENCODING_UTF8));
     194           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     195           0 :             }
     196             :         }
     197           8 :         if( nExit == 0 && !aCommand.aSfxItemFile.isEmpty() )
     198             :         {
     199           0 :             aTmpSfxItemFile = tempFileHelper(aCommand.aSfxItemFile);
     200           0 :             SvFileStream aOutStm( aTmpSfxItemFile, STREAM_READWRITE | STREAM_TRUNC );
     201           0 :             if( !pDataBase->WriteSfxItem( aOutStm ) )
     202             :             {
     203           0 :                 nExit = -1;
     204           0 :                 OStringBuffer aStr("cannot write item file: ");
     205           0 :                 aStr.append(OUStringToOString(aCommand.aSfxItemFile, RTL_TEXTENCODING_UTF8));
     206           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     207           0 :             }
     208             :         }
     209           8 :         if( nExit == 0 && !aCommand.aDataBaseFile.isEmpty() )
     210             :         {
     211           8 :             aTmpDataBaseFile = tempFileHelper(aCommand.aDataBaseFile);
     212           8 :             SvFileStream aOutStm( aTmpDataBaseFile, STREAM_READWRITE | STREAM_TRUNC );
     213           8 :             pDataBase->Save( aOutStm, aCommand.nFlags );
     214           8 :             if( aOutStm.GetError() != SVSTREAM_OK )
     215             :             {
     216           0 :                 nExit = -1;
     217           0 :                 OStringBuffer aStr("cannot write database file: ");
     218           0 :                 aStr.append(OUStringToOString(aCommand.aDataBaseFile, RTL_TEXTENCODING_UTF8));
     219           0 :                 fprintf(stderr, "%s\n", aStr.getStr());
     220           8 :             }
     221             :         }
     222           8 :         if (nExit == 0 && !aCommand.m_DepFile.isEmpty())
     223             :         {
     224           8 :             aTmpDepFile = tempFileHelper(aCommand.m_DepFile);
     225           8 :             SvFileStream aOutStm( aTmpDepFile, STREAM_READWRITE | STREAM_TRUNC );
     226           8 :             pDataBase->WriteDepFile(aOutStm, aCommand.aTargetFile);
     227           8 :             if( aOutStm.GetError() != SVSTREAM_OK )
     228             :             {
     229           0 :                 nExit = -1;
     230             :                 fprintf( stderr, "cannot write dependency file: %s\n",
     231             :                         OUStringToOString( aCommand.m_DepFile,
     232           0 :                             RTL_TEXTENCODING_UTF8 ).getStr() );
     233           8 :             }
     234             :         }
     235             :     }
     236             :     else
     237           0 :         nExit = -1;
     238             : 
     239           8 :     if( nExit == 0 )
     240             :     {
     241           8 :         bool bErr = false;
     242           8 :         sal_Bool bDoMove = aCommand.aTargetFile.isEmpty();
     243          16 :         OUString aErrFile, aErrFile2;
     244           8 :         if( !bErr && !aCommand.aListFile.isEmpty() )
     245             :         {
     246           8 :             bErr |= !FileMove_Impl( aCommand.aListFile, aTmpListFile, bDoMove );
     247           8 :             if( bErr ) {
     248           0 :                 aErrFile = aCommand.aListFile;
     249           0 :                 aErrFile2 = aTmpListFile;
     250             :             }
     251             :         }
     252           8 :         if( !bErr && !aCommand.aSlotMapFile.isEmpty() )
     253             :         {
     254           8 :             bErr |= !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
     255           8 :             if( bErr ) {
     256           0 :                 aErrFile = aCommand.aSlotMapFile;
     257           0 :                 aErrFile2 = aTmpSlotMapFile;
     258             :             }
     259             :         }
     260           8 :         if( !bErr && !aCommand.aSfxItemFile.isEmpty() )
     261             :         {
     262           0 :             bErr |= !FileMove_Impl( aCommand.aSfxItemFile, aTmpSfxItemFile, bDoMove );
     263           0 :             if( bErr ) {
     264           0 :                 aErrFile = aCommand.aSfxItemFile;
     265           0 :                 aErrFile2 = aTmpSfxItemFile;
     266             :             }
     267             :         }
     268           8 :         if( !bErr && !aCommand.aDataBaseFile.isEmpty() )
     269             :         {
     270           8 :             bErr |= !FileMove_Impl( aCommand.aDataBaseFile, aTmpDataBaseFile, bDoMove );
     271           8 :             if( bErr ) {
     272           0 :                 aErrFile = aCommand.aDataBaseFile;
     273           0 :                 aErrFile2 = aTmpDataBaseFile;
     274             :             }
     275             :         }
     276           8 :         if( !bErr && !aCommand.aHelpIdFile.isEmpty() )
     277             :         {
     278           8 :             bErr |= !FileMove_Impl( aCommand.aHelpIdFile, aTmpHelpIdFile, bDoMove );
     279           8 :             if( bErr ) {
     280           0 :                 aErrFile = aCommand.aHelpIdFile;
     281           0 :                 aErrFile2 = aTmpHelpIdFile;
     282             :             }
     283             :         }
     284           8 :         if( !bErr && !aCommand.aCSVFile.isEmpty() )
     285             :         {
     286           0 :             bErr |= !FileMove_Impl( aCommand.aCSVFile, aTmpCSVFile, bDoMove );
     287           0 :             if( bErr ) {
     288           0 :                 aErrFile = aCommand.aCSVFile;
     289           0 :                 aErrFile2 = aTmpCSVFile;
     290             :             }
     291             :         }
     292           8 :         if( !bErr && !aCommand.aDocuFile.isEmpty() )
     293             :         {
     294           0 :             bErr |= !FileMove_Impl( aCommand.aDocuFile, aTmpDocuFile, bDoMove );
     295           0 :             if( bErr ) {
     296           0 :                 aErrFile = aCommand.aDocuFile;
     297           0 :                 aErrFile2 = aTmpDocuFile;
     298             :             }
     299             :         }
     300           8 :         if (!bErr && !aCommand.m_DepFile.isEmpty())
     301             :         {
     302           8 :             bErr |= !FileMove_Impl( aCommand.m_DepFile, aTmpDepFile, bDoMove );
     303           8 :             if (bErr) {
     304           0 :                 aErrFile = aCommand.m_DepFile;
     305           0 :                 aErrFile2 = aTmpDepFile;
     306             :             }
     307             :         }
     308             : 
     309           8 :         if( bErr )
     310             :         {
     311           0 :             nExit = -1;
     312           0 :             OStringBuffer aStr("cannot move file from: ");
     313             :             aStr.append(OUStringToOString(aErrFile2,
     314           0 :                 RTL_TEXTENCODING_UTF8));
     315           0 :             aStr.append("\n              to file: ");
     316             :             aStr.append(OUStringToOString(aErrFile,
     317           0 :                 RTL_TEXTENCODING_UTF8));
     318           0 :             fprintf( stderr, "%s\n", aStr.getStr() );
     319             :         }
     320             :         else
     321             :         {
     322           8 :             if( !aCommand.aTargetFile.isEmpty() )
     323             :             {
     324             :                 // stamp file, because idl passed through correctly
     325             :                 SvFileStream aOutStm( aCommand.aTargetFile,
     326           8 :                                 STREAM_READWRITE | STREAM_TRUNC );
     327             :             }
     328           8 :         }
     329             :     }
     330             : 
     331           8 :     if( nExit != 0 )
     332             :     {
     333           0 :         if( !aCommand.aListFile.isEmpty() )
     334             :         {
     335           0 :             osl::FileBase::getSystemPathFromFileURL( aTmpListFile, aTmpListFile );
     336           0 :             osl::File::remove( aTmpListFile );
     337             :         }
     338           0 :         if( !aCommand.aSlotMapFile.isEmpty() )
     339             :         {
     340           0 :             osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile, aTmpSlotMapFile );
     341           0 :             osl::File::remove( aTmpSlotMapFile );
     342             :         }
     343           0 :         if( !aCommand.aSfxItemFile.isEmpty() )
     344             :         {
     345           0 :             osl::FileBase::getSystemPathFromFileURL( aTmpSfxItemFile, aTmpSfxItemFile );
     346           0 :             osl::File::remove( aTmpSfxItemFile );
     347             :         }
     348           0 :         if( !aCommand.aDataBaseFile.isEmpty() )
     349             :         {
     350           0 :             osl::FileBase::getSystemPathFromFileURL( aTmpDataBaseFile, aTmpDataBaseFile );
     351           0 :             osl::File::remove( aTmpDataBaseFile );
     352             :         }
     353             :     }
     354             : 
     355           8 :     delete pDataBase;
     356           8 :     DeInit();
     357           8 :     if( nExit != 0 )
     358           0 :         fprintf( stderr, "svidl terminated with errors\n" );
     359          16 :     return nExit;
     360             : }
     361             : 
     362             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10