1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tempfile.cxx,v $
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"
37 #if defined( UNX) || defined(OS2)
41 #include <osl/thread.h>
43 oslFileError SAL_CALL
my_getTempDirURL( rtl_uString
** pustrTempDir
)
45 const char *pValue
= getenv( "TEMP" );
49 pValue
= getenv( "TMP" );
50 #if defined(SOLARIS) || defined (LINUX)
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
);
68 return osl_File_E_NOENT
;
77 # define _WIN32_WINNT 0x0400
78 # define _CTYPE_DISABLE_MACROS /* wg. dynamischer C-Runtime MH */
82 #pragma warning(push, 1)
85 #define WIN32_LEAN_AND_MEAN
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;
106 nLength
= GetTempPathW( elementsof(szBuffer
), lpBuffer
);
107 if ( nLength
> nBufferLength
)
110 lpBuffer
= (LPWSTR
)alloca( sizeof(WCHAR
) * nLength
);
111 nBufferLength
= nLength
- 1;
113 } while ( nLength
> nBufferLength
);
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
);
129 error
= GetLastError() == ERROR_SUCCESS
? osl_File_E_None
: osl_File_E_INVAL
;
135 #include "tempfile.hxx"
139 TempFile::TempFile( const OUString
& rTempFileURL
)
140 :osl::File( rTempFileURL
), maURL( rTempFileURL
)
144 TempFile::~TempFile()
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
)
179 else if ( err
!= FileBase::E_EXIST
)
181 // if f.e. name contains invalid chars stop trying to create files
189 OUString
TempFile::getFileURL()