1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #include <osl/process.h> 28*cdf0e10cSrcweir #ifndef _RTL_OSTRINGBUFFER_HXX_ 29*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 30*cdf0e10cSrcweir #endif 31*cdf0e10cSrcweir #include <rtl/ustring.hxx> 32*cdf0e10cSrcweir #include <osl/thread.h> 33*cdf0e10cSrcweir #include <osl/file.hxx> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <stdlib.h> 36*cdf0e10cSrcweir #include <stdio.h> 37*cdf0e10cSrcweir #if defined(SAL_W32) || defined(SAL_OS2) 38*cdf0e10cSrcweir #include <io.h> 39*cdf0e10cSrcweir #include <direct.h> 40*cdf0e10cSrcweir #include <errno.h> 41*cdf0e10cSrcweir #endif 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #ifdef UNX 44*cdf0e10cSrcweir #include <sys/stat.h> 45*cdf0e10cSrcweir #include <errno.h> 46*cdf0e10cSrcweir #include <unistd.h> 47*cdf0e10cSrcweir #endif 48*cdf0e10cSrcweir #include <codemaker/global.hxx> 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #ifdef SAL_UNX 51*cdf0e10cSrcweir #define SEPARATOR '/' 52*cdf0e10cSrcweir #else 53*cdf0e10cSrcweir #define SEPARATOR '\\' 54*cdf0e10cSrcweir #endif 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir using namespace ::rtl; 57*cdf0e10cSrcweir using namespace ::osl; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir const OString inGlobalSet(const OUString & rValue) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir OString sValue( OUStringToOString(rValue, RTL_TEXTENCODING_UTF8) ); 62*cdf0e10cSrcweir static StringSet aGlobalMap; 63*cdf0e10cSrcweir StringSet::iterator iter = aGlobalMap.find( sValue ); 64*cdf0e10cSrcweir if( iter != aGlobalMap.end() ) 65*cdf0e10cSrcweir return *iter; 66*cdf0e10cSrcweir return *(aGlobalMap.insert( sValue ).first); 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir static sal_Bool isFileUrl(const OString& fileName) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir if (fileName.indexOf("file://") == 0 ) 72*cdf0e10cSrcweir return sal_True; 73*cdf0e10cSrcweir return sal_False; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir OUString convertToFileUrl(const OString& fileName) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir if ( isFileUrl(fileName) ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir return OStringToOUString(fileName, osl_getThreadTextEncoding()); 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir OUString uUrlFileName; 84*cdf0e10cSrcweir OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding()); 85*cdf0e10cSrcweir if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 ) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir OUString uWorkingDir; 88*cdf0e10cSrcweir if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) { 89*cdf0e10cSrcweir OSL_ASSERT(false); 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName) 92*cdf0e10cSrcweir != FileBase::E_None) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir OSL_ASSERT(false); 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir } else 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName) 99*cdf0e10cSrcweir != FileBase::E_None) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir OSL_ASSERT(false); 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir return uUrlFileName; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir //************************************************************************* 109*cdf0e10cSrcweir // FileStream 110*cdf0e10cSrcweir //************************************************************************* 111*cdf0e10cSrcweir FileStream::FileStream() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir FileStream::~FileStream() 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir if ( isValid() ) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir fflush(m_pFile); 120*cdf0e10cSrcweir fclose(m_pFile); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir sal_Bool FileStream::isValid() 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir if ( m_pFile ) 127*cdf0e10cSrcweir return sal_True; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir return sal_False; 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir void FileStream::open(const OString& name, FileAccessMode mode) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir if ( name.getLength() > 0 ) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir m_name = name; 137*cdf0e10cSrcweir m_pFile = fopen(m_name, checkAccessMode(mode)); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir void FileStream::close() 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir if ( isValid() ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir fflush(m_pFile); 146*cdf0e10cSrcweir fclose(m_pFile); 147*cdf0e10cSrcweir m_pFile = NULL; 148*cdf0e10cSrcweir m_name = OString(); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir const sal_Char* FileStream::checkAccessMode(FileAccessMode mode) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir switch( mode ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir case FAM_READ: 157*cdf0e10cSrcweir return "r"; 158*cdf0e10cSrcweir case FAM_WRITE: 159*cdf0e10cSrcweir return "w"; 160*cdf0e10cSrcweir case FAM_APPEND: 161*cdf0e10cSrcweir return "a"; 162*cdf0e10cSrcweir case FAM_READWRITE_EXIST: 163*cdf0e10cSrcweir return "r+"; 164*cdf0e10cSrcweir case FAM_READWRITE: 165*cdf0e10cSrcweir return "w+"; 166*cdf0e10cSrcweir case FAM_READAPPEND: 167*cdf0e10cSrcweir return "a+"; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir return "w+"; 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172