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