update dev300-m58
[ooovba.git] / filter / source / placeware / tempfile.cxx
blob8eec7ccb34b1c98746588f2679dc581e80645054
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tempfile.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_filter.hxx"
35 #include <osl/file.h>
37 #if defined( UNX) || defined(OS2)
39 #include <stdio.h>
40 #include <string.h>
41 #include <osl/thread.h>
43 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
45 const char *pValue = getenv( "TEMP" );
47 if ( !pValue )
49 pValue = getenv( "TMP" );
50 #if defined(SOLARIS) || defined (LINUX)
51 if ( !pValue )
52 pValue = P_tmpdir;
53 #endif
56 if ( pValue )
58 oslFileError error;
59 rtl_uString *ustrTempPath = NULL;
61 rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
62 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
63 rtl_uString_release( ustrTempPath );
65 return error;
67 else
68 return osl_File_E_NOENT;
70 #else
72 #ifdef NDEBUG
73 # define NO_DEBUG_CRT
74 #endif
76 #ifndef _WIN32_WINNT
77 # define _WIN32_WINNT 0x0400
78 # define _CTYPE_DISABLE_MACROS /* wg. dynamischer C-Runtime MH */
79 #endif
81 #if defined _MSC_VER
82 #pragma warning(push, 1)
83 #endif
85 #define WIN32_LEAN_AND_MEAN
86 #include <windows.h>
87 #include <malloc.h>
89 #if defined _MSC_VER
90 #pragma warning(pop)
91 #endif
93 #define elementsof(arr) (sizeof(arr)/sizeof(arr[0]))
95 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
97 WCHAR szBuffer[MAX_PATH];
98 LPWSTR lpBuffer = szBuffer;
99 DWORD nBufferLength = elementsof(szBuffer) - 1;
101 DWORD nLength;
102 oslFileError error;
106 nLength = GetTempPathW( elementsof(szBuffer), lpBuffer );
107 if ( nLength > nBufferLength )
109 nLength++;
110 lpBuffer = (LPWSTR)alloca( sizeof(WCHAR) * nLength );
111 nBufferLength = nLength - 1;
113 } while ( nLength > nBufferLength );
115 if ( nLength )
117 rtl_uString *ustrTempPath = NULL;
119 if ( '\\' == lpBuffer[nLength-1] )
120 lpBuffer[nLength-1] = 0;
122 rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
124 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
126 rtl_uString_release( ustrTempPath );
128 else
129 error = GetLastError() == ERROR_SUCCESS ? osl_File_E_None : osl_File_E_INVAL;
131 return error;
133 #endif
135 #include "tempfile.hxx"
137 using namespace rtl;
139 TempFile::TempFile( const OUString& rTempFileURL )
140 :osl::File( rTempFileURL ), maURL( rTempFileURL )
144 TempFile::~TempFile()
146 close();
148 if( maURL.getLength() )
149 osl::File::remove( maURL );
152 OUString TempFile::createTempFileURL()
154 OUString aTempFileURL;
156 const sal_uInt32 nRadix = 26;
158 OUString aTempDirURL;
159 /* oslFileError nRC = */ my_getTempDirURL( &aTempDirURL.pData );
161 static sal_uInt32 u = osl_getGlobalTimer();
162 for ( sal_uInt32 nOld = u; ++u != nOld; )
164 u %= (nRadix*nRadix*nRadix);
165 OUString aTmp( aTempDirURL );
166 if( aTmp.getStr()[ aTmp.getLength() - 1 ] != sal_Unicode( '/' ) )
167 aTmp += OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ));
168 aTmp += OUString::valueOf( (sal_Int32) (unsigned) u, nRadix );
169 aTmp += OUString::createFromAscii( ".tmp" );
171 osl::File aFile( aTmp );
172 osl::FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
173 if ( err == FileBase::E_None )
175 aTempFileURL = aTmp;
176 aFile.close();
177 break;
179 else if ( err != FileBase::E_EXIST )
181 // if f.e. name contains invalid chars stop trying to create files
182 break;
186 return aTempFileURL;
189 OUString TempFile::getFileURL()
191 return maURL;