bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / placeware / tempfile.cxx
blob7df4cf622b68366a927b3b1743beb97503f4f1ba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/file.h>
21 #include <sal/macros.h>
23 #if defined( UNX)
25 #include <stdio.h>
26 #include <string.h>
27 #include <osl/thread.h>
29 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
31 const char *pValue = getenv( "TEMP" );
33 if ( !pValue )
35 pValue = getenv( "TMP" );
36 #if defined(SOLARIS) || defined (LINUX)
37 if ( !pValue )
38 pValue = P_tmpdir;
39 #endif
42 if ( pValue )
44 oslFileError error;
45 rtl_uString *ustrTempPath = NULL;
47 rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
48 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
49 rtl_uString_release( ustrTempPath );
51 return error;
53 else
54 return osl_File_E_NOENT;
56 #else
58 #ifndef _WIN32_WINNT
59 # define _WIN32_WINNT 0x0400
60 # define _CTYPE_DISABLE_MACROS /* wg. dynamischer C-Runtime MH */
61 #endif
63 #if defined _MSC_VER
64 #pragma warning(push, 1)
65 #endif
67 #define WIN32_LEAN_AND_MEAN
68 #include <windows.h>
69 #include <malloc.h>
71 #if defined _MSC_VER
72 #pragma warning(pop)
73 #endif
75 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
77 WCHAR szBuffer[MAX_PATH];
78 LPWSTR lpBuffer = szBuffer;
79 DWORD nBufferLength = SAL_N_ELEMENTS(szBuffer) - 1;
81 DWORD nLength;
82 oslFileError error;
86 nLength = GetTempPathW( SAL_N_ELEMENTS(szBuffer), lpBuffer );
87 if ( nLength > nBufferLength )
89 nLength++;
90 lpBuffer = (LPWSTR)alloca( sizeof(WCHAR) * nLength );
91 nBufferLength = nLength - 1;
93 } while ( nLength > nBufferLength );
95 if ( nLength )
97 rtl_uString *ustrTempPath = NULL;
99 if ( '\\' == lpBuffer[nLength-1] )
100 lpBuffer[nLength-1] = 0;
102 rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
104 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
106 rtl_uString_release( ustrTempPath );
108 else
109 error = GetLastError() == ERROR_SUCCESS ? osl_File_E_None : osl_File_E_INVAL;
111 return error;
113 #endif
115 #include "tempfile.hxx"
117 PlaceWareTempFile::PlaceWareTempFile( const OUString& rTempFileURL )
118 :osl::File( rTempFileURL ), maURL( rTempFileURL )
122 PlaceWareTempFile::~PlaceWareTempFile()
124 close();
126 if( !maURL.isEmpty() )
127 osl::File::remove( maURL );
130 OUString PlaceWareTempFile::createTempFileURL()
132 OUString aTempFileURL;
134 const sal_uInt32 nRadix = 26;
136 OUString aTempDirURL;
137 /* oslFileError nRC = */ my_getTempDirURL( &aTempDirURL.pData );
139 static sal_uInt32 u = osl_getGlobalTimer();
140 for ( sal_uInt32 nOld = u; ++u != nOld; )
142 u %= (nRadix*nRadix*nRadix);
143 OUString aTmp( aTempDirURL );
144 if( aTmp.getStr()[ aTmp.getLength() - 1 ] != sal_Unicode( '/' ) )
145 aTmp += "/";
146 aTmp += OUString::valueOf( (sal_Int32) (unsigned) u, nRadix );
147 aTmp += ".tmp";
149 osl::File aFile( aTmp );
150 osl::FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
151 if ( err == FileBase::E_None )
153 aTempFileURL = aTmp;
154 aFile.close();
155 break;
157 else if ( err != FileBase::E_EXIST )
159 // if f.e. name contains invalid chars stop trying to create files
160 break;
164 return aTempFileURL;
167 OUString PlaceWareTempFile::getFileURL()
169 return maURL;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */