LCOV - code coverage report
Current view: top level - codemaker/source/codemaker - global.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 109 167 65.3 %
Date: 2014-04-11 Functions: 15 23 65.2 %
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 "osl/process.h"
      21             : #include "rtl/strbuf.hxx"
      22             : #include "rtl/ustring.hxx"
      23             : #include "osl/thread.h"
      24             : #include "osl/file.hxx"
      25             : 
      26             : #include <string.h>
      27             : #if defined(SAL_W32)
      28             : #include <io.h>
      29             : 
      30             : #include <direct.h>
      31             : #include <errno.h>
      32             : #endif
      33             : 
      34             : #ifdef UNX
      35             : #include <sys/stat.h>
      36             : #include <errno.h>
      37             : #include <unistd.h>
      38             : #endif
      39             : 
      40             : #include "codemaker/global.hxx"
      41             : 
      42             : #ifdef SAL_UNX
      43             : #define SEPARATOR '/'
      44             : #else
      45             : #define SEPARATOR '\\'
      46             : #endif
      47             : 
      48             : using namespace ::rtl;
      49             : using namespace ::osl;
      50             : 
      51             : 
      52       18813 : OString getTempDir(const OString& sFileName)
      53             : {
      54       18813 :     sal_Int32 index = 0;
      55             : #ifdef SAL_UNX
      56       18813 :     if ((index=sFileName.lastIndexOf('/')) > 0)
      57       18813 :         return sFileName.copy(0, index);
      58             : #else
      59             :     if ((index=sFileName.lastIndexOf('\\')) > 0)
      60             :         return sFileName.copy(0, index);
      61             : #endif
      62           0 :     return ".";
      63             : }
      64             : 
      65       18813 : OString createFileNameFromType( const OString& destination,
      66             :                                 const OString& typeName,
      67             :                                 const OString& postfix,
      68             :                                 bool bLowerCase,
      69             :                                 const OString& prefix )
      70             : {
      71       18813 :     OString type(typeName.replace('.', '/'));
      72             : 
      73       18813 :     if (bLowerCase)
      74             :     {
      75           0 :         type = typeName.toAsciiLowerCase();
      76             :     }
      77             : 
      78       18813 :     sal_uInt32 length = destination.getLength();
      79             : 
      80       18813 :     bool withPoint = false;
      81       18813 :     if (length == 0)
      82             :     {
      83           0 :         length++;
      84           0 :         withPoint = true;
      85             :     }
      86             : 
      87       18813 :     length += prefix.getLength() + type.getLength() + postfix.getLength();
      88             : 
      89       18813 :     bool withSeparator = false;
      90       56439 :     if (!(destination.endsWith("\\") || destination.endsWith("/"))
      91       37626 :         && !(type.startsWith("\\") || type.startsWith("/")))
      92             :     {
      93       18813 :         length++;
      94       18813 :         withSeparator = true;
      95             :     }
      96             : 
      97       37626 :     OStringBuffer fileNameBuf(length);
      98             : 
      99       18813 :     if (withPoint)
     100           0 :         fileNameBuf.append('.');
     101             :     else
     102       18813 :         fileNameBuf.append(destination.getStr(), destination.getLength());
     103             : 
     104       18813 :     if (withSeparator)
     105       18813 :         fileNameBuf.append("/", 1);
     106             : 
     107       37626 :     OString tmpStr(type);
     108       18813 :     if (!prefix.isEmpty())
     109             :     {
     110           0 :         tmpStr = type.replaceAt(type.lastIndexOf('/')+1, 0, prefix);
     111             :     }
     112             : 
     113       18813 :     fileNameBuf.append(tmpStr.getStr(), tmpStr.getLength());
     114       18813 :     fileNameBuf.append(postfix.getStr(), postfix.getLength());
     115             : 
     116       37626 :     OString fileName(fileNameBuf.makeStringAndClear());
     117             : 
     118             :     sal_Char token;
     119             : #ifdef SAL_UNX
     120       18813 :     fileName = fileName.replace('\\', '/');
     121       18813 :     token = '/';
     122             : #else
     123             :     fileName = fileName.replace('/', '\\');
     124             :     token = '\\';
     125             : #endif
     126             : 
     127       37626 :     OStringBuffer buffer(length);
     128             : 
     129       18813 :     sal_Int32 nIndex = 0;
     130      244440 :     do
     131             :     {
     132      263253 :         buffer.append(fileName.getToken(0, token, nIndex).getStr());
     133      263253 :         if( nIndex == -1 )
     134       18813 :             break;
     135             : 
     136      244440 :         if (buffer.isEmpty() || OString(".") == buffer.getStr())
     137             :         {
     138       18813 :             buffer.append(token);
     139       18813 :             continue;
     140             :         }
     141             : 
     142             : #if defined(SAL_UNX)
     143      225627 :         if (mkdir((char*)buffer.getStr(), 0777) == -1)
     144             : #else
     145             :         if (mkdir((char*)buffer.getStr()) == -1)
     146             : #endif
     147             :         {
     148      225170 :             if ( errno == ENOENT )
     149           0 :                 return OString();
     150             :         }
     151             : 
     152      225627 :         buffer.append(token);
     153      244440 :     } while( nIndex != -1 );
     154             : 
     155       37626 :     OUString uSysFileName;
     156       18813 :     OSL_VERIFY( FileBase::getSystemPathFromFileURL(
     157             :         convertToFileUrl(fileName), uSysFileName) == FileBase::E_None );
     158       37626 :     return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());;
     159             : }
     160             : 
     161       37626 : bool fileExists(const OString& fileName)
     162             : {
     163       37626 :     FILE  *f= fopen(fileName.getStr(), "r");
     164             : 
     165       37626 :     if (f != NULL)
     166             :     {
     167           0 :         fclose(f);
     168           0 :         return true;
     169             :     }
     170             : 
     171       37626 :     return false;
     172             : }
     173             : 
     174           0 : bool checkFileContent(const OString& targetFileName, const OString& tmpFileName)
     175             : {
     176           0 :     FILE  *target = fopen(targetFileName.getStr(), "r");
     177           0 :     FILE  *tmp = fopen(tmpFileName.getStr(), "r");
     178           0 :     bool    bFindChanges = false;
     179             : 
     180           0 :     if (target != NULL && tmp != NULL)
     181             :     {
     182             :         sal_Char    buffer1[1024+1];
     183             :         sal_Char    buffer2[1024+1];
     184           0 :         sal_Int32   n1 = 0;
     185           0 :         sal_Int32   n2 = 0;
     186             : 
     187           0 :         while ( !bFindChanges && !feof(target) && !feof(tmp))
     188             :         {
     189           0 :             n1 = fread(buffer1, sizeof(sal_Char), 1024, target);
     190           0 :             n2 = fread(buffer2, sizeof(sal_Char), 1024, tmp);
     191             : 
     192           0 :             if ( n1 != n2 )
     193           0 :                 bFindChanges = true;
     194             :             else
     195           0 :                 if ( memcmp(buffer1, buffer2, n2) != 0 )
     196           0 :                     bFindChanges =  true;
     197             :         }
     198             :     }
     199             : 
     200           0 :     if (target) fclose(target);
     201           0 :     if (tmp) fclose(tmp);
     202             : 
     203           0 :     return bFindChanges;
     204             : }
     205             : 
     206       18813 : bool makeValidTypeFile(const OString& targetFileName, const OString& tmpFileName,
     207             :                            bool bFileCheck)
     208             : {
     209       18813 :     if (bFileCheck) {
     210           0 :         if (checkFileContent(targetFileName, tmpFileName)) {
     211           0 :             if ( !unlink(targetFileName.getStr()) )
     212           0 :                 if ( !rename(tmpFileName.getStr(), targetFileName.getStr()) )
     213           0 :                     return true;
     214             :         } else
     215           0 :             return removeTypeFile(tmpFileName);
     216             :     } else {
     217       18813 :         if (fileExists(targetFileName))
     218           0 :             if (!removeTypeFile(targetFileName))
     219           0 :                 return false;
     220             : 
     221       18813 :         if ( rename(tmpFileName.getStr(), targetFileName.getStr()) ) {
     222           0 :             if (errno == EEXIST)
     223           0 :                 return true;
     224             :         } else
     225       18813 :             return true;
     226             :     }
     227           0 :     return false;
     228             : }
     229             : 
     230           0 : bool removeTypeFile(const OString& fileName)
     231             : {
     232           0 :     if ( !unlink(fileName.getStr()) )
     233           0 :         return true;
     234             : 
     235           0 :     return false;
     236             : }
     237             : 
     238       37657 : OUString convertToFileUrl(const OString& fileName)
     239             : {
     240       37657 :     if ( fileName.startsWith("file://") )
     241             :     {
     242           0 :         return OStringToOUString(fileName, osl_getThreadTextEncoding());
     243             :     }
     244             : 
     245       37657 :     OUString uUrlFileName;
     246       75314 :     OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
     247       37657 :     if ( fileName.startsWith(".") || fileName.indexOf(SEPARATOR) < 0 )
     248             :     {
     249           0 :         OUString uWorkingDir;
     250           0 :         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
     251             :         {
     252             :             OSL_ASSERT(false);
     253             :         }
     254           0 :         if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
     255             :             != FileBase::E_None)
     256             :         {
     257             :             OSL_ASSERT(false);
     258           0 :         }
     259             :     } else
     260             :     {
     261       37657 :         if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
     262             :             != FileBase::E_None)
     263             :         {
     264             :             OSL_ASSERT(false);
     265             :         }
     266             :     }
     267             : 
     268       75314 :     return uUrlFileName;
     269             : }
     270             : 
     271             : 
     272             : 
     273             : // FileStream
     274             : 
     275       18813 : FileStream::FileStream()
     276       18813 :     : m_file(NULL)
     277             : {
     278       18813 : }
     279             : 
     280       37626 : FileStream::~FileStream()
     281             : {
     282       18813 :     if ( isValid() )
     283           0 :         osl_closeFile(m_file);
     284       18813 : }
     285             : 
     286       56439 : bool FileStream::isValid()
     287             : {
     288       56439 :     if ( m_file )
     289       37626 :         return true;
     290             : 
     291       18813 :     return false;
     292             : }
     293             : 
     294       18813 : void FileStream::createTempFile(const OString& sPath)
     295             : {
     296       18813 :     OString sTmp(".");
     297       37626 :     OUString sTmpPath;
     298       37626 :     OUString sTmpName;
     299             : 
     300       18813 :     if (!sPath.isEmpty())
     301       18813 :         sTmp = sPath;
     302             : 
     303       18813 :     sTmpPath = convertToFileUrl(sTmp);
     304             : 
     305       18813 :     if (osl_createTempFile(sTmpPath.pData, &m_file, &sTmpName.pData) == osl_File_E_None) {
     306             : #ifdef SAL_UNX
     307             :         sal_uInt64 uAttr = osl_File_Attribute_OwnWrite |
     308             :                            osl_File_Attribute_OwnRead |
     309             :                            osl_File_Attribute_GrpWrite |
     310             :                            osl_File_Attribute_GrpRead |
     311       18813 :                            osl_File_Attribute_OthRead;
     312       18813 :         if (osl_setFileAttributes(sTmpName.pData, uAttr) != osl_File_E_None) {
     313           0 :             m_file = NULL;
     314       18813 :             return;
     315             :         }
     316             : #endif
     317       18813 :         OUString sSysTmpName;
     318       18813 :         FileBase::getSystemPathFromFileURL(sTmpName, sSysTmpName);
     319       18813 :         m_name = OUStringToOString(sSysTmpName, osl_getThreadTextEncoding());
     320             :     } else
     321       18813 :         m_file = NULL;
     322             : }
     323             : 
     324       18813 : void FileStream::close()
     325             : {
     326       18813 :     if ( isValid() )
     327             :     {
     328       18813 :         osl_closeFile(m_file);
     329       18813 :         m_file = NULL;
     330       18813 :         m_name = OString();
     331             :     }
     332       18813 : }
     333             : 
     334       46435 : bool FileStream::write(void const * buffer, sal_uInt64 size) {
     335      139305 :     while (size > 0) {
     336             :         sal_uInt64 written;
     337       46435 :         if (osl_writeFile(m_file, buffer, size, &written) != osl_File_E_None) {
     338           0 :             return false;
     339             :         }
     340             :         OSL_ASSERT(written <= size);
     341       46435 :         size -= written;
     342       46435 :         buffer = static_cast< char const * >(buffer) + written;
     343             :     }
     344       46435 :     return true;
     345             : }
     346             : 
     347      180574 : FileStream &operator<<(FileStream& o, sal_uInt32 i) {
     348             :     sal_uInt64 writtenBytes;
     349      180574 :     OString s = OString::number((sal_Int32)i);
     350      180574 :     osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
     351      180574 :     return o;
     352             : }
     353     2274566 : FileStream &operator<<(FileStream& o, char const * s) {
     354             :     sal_uInt64 writtenBytes;
     355     2274566 :     osl_writeFile(o.m_file, s, strlen(s), &writtenBytes);
     356     2274566 :     return o;
     357             : }
     358           0 : FileStream &operator<<(FileStream& o, ::rtl::OString* s) {
     359             :     sal_uInt64 writtenBytes;
     360           0 :     osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
     361           0 :     return o;
     362             : }
     363      889219 : FileStream &operator<<(FileStream& o, const ::rtl::OString& s) {
     364             :     sal_uInt64 writtenBytes;
     365      889219 :     osl_writeFile(o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
     366      889219 :     return o;
     367             : 
     368             : }
     369           0 : FileStream &operator<<(FileStream& o, ::rtl::OStringBuffer* s) {
     370             :     sal_uInt64 writtenBytes;
     371           0 :     osl_writeFile(o.m_file, s->getStr(), s->getLength() * sizeof(sal_Char), &writtenBytes);
     372           0 :     return o;
     373             : }
     374           0 : FileStream &operator<<(FileStream& o, const ::rtl::OStringBuffer& s) {
     375             :     sal_uInt64 writtenBytes;
     376             :     osl_writeFile(
     377           0 :         o.m_file, s.getStr(), s.getLength() * sizeof(sal_Char), &writtenBytes);
     378           0 :     return o;
     379             : }
     380             : 
     381      758931 : FileStream & operator <<(FileStream & out, rtl::OUString const & s) {
     382      758931 :     return out << OUStringToOString(s, RTL_TEXTENCODING_UTF8);
     383             : }
     384             : 
     385           0 : CannotDumpException::~CannotDumpException() throw () {}
     386             : 
     387             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10