Bump version to 5.0-14
[LibreOffice.git] / filter / source / placeware / tempfile.cxx
blobf573badaed3759ac3eb56805931a4884e4963948
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 ( !pValue )
38 #if defined(SOLARIS) || defined (LINUX)
39 pValue = P_tmpdir;
40 #else
41 return osl_File_E_NOENT;
42 #endif
46 oslFileError error;
47 rtl_uString *ustrTempPath = NULL;
49 rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
50 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
51 rtl_uString_release( ustrTempPath );
53 return error;
55 #else
57 #if defined _MSC_VER
58 #pragma warning(push, 1)
59 #endif
61 #define WIN32_LEAN_AND_MEAN
62 #include <windows.h>
63 #include <malloc.h>
65 #if defined _MSC_VER
66 #pragma warning(pop)
67 #endif
69 oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
71 WCHAR szBuffer[MAX_PATH];
72 LPWSTR lpBuffer = szBuffer;
73 DWORD nBufferLength = SAL_N_ELEMENTS(szBuffer) - 1;
75 DWORD nLength;
76 oslFileError error;
80 nLength = GetTempPathW( SAL_N_ELEMENTS(szBuffer), lpBuffer );
81 if ( nLength > nBufferLength )
83 nLength++;
84 lpBuffer = (LPWSTR)alloca( sizeof(WCHAR) * nLength );
85 nBufferLength = nLength - 1;
87 } while ( nLength > nBufferLength );
89 if ( nLength )
91 rtl_uString *ustrTempPath = NULL;
93 if ( '\\' == lpBuffer[nLength-1] )
94 lpBuffer[nLength-1] = 0;
96 rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
98 error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
100 rtl_uString_release( ustrTempPath );
102 else
103 error = GetLastError() == ERROR_SUCCESS ? osl_File_E_None : osl_File_E_INVAL;
105 return error;
107 #endif
109 #include "tempfile.hxx"
111 PlaceWareTempFile::PlaceWareTempFile( const OUString& rTempFileURL )
112 :osl::File( rTempFileURL ), maURL( rTempFileURL )
116 PlaceWareTempFile::~PlaceWareTempFile()
118 close();
120 if( !maURL.isEmpty() )
121 osl::File::remove( maURL );
124 OUString PlaceWareTempFile::createTempFileURL()
126 OUString aTempFileURL;
128 const sal_uInt32 nRadix = 26;
130 OUString aTempDirURL;
131 /* oslFileError nRC = */ my_getTempDirURL( &aTempDirURL.pData );
133 static sal_uInt32 u = osl_getGlobalTimer();
134 for ( sal_uInt32 nOld = u; ++u != nOld; )
136 u %= (nRadix*nRadix*nRadix);
137 OUString aTmp( aTempDirURL );
138 if( !aTmp.endsWith("/") )
139 aTmp += "/";
140 aTmp += OUString::number( (unsigned) u, nRadix );
141 aTmp += ".tmp";
143 osl::File aFile( aTmp );
144 osl::FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
145 if ( err == FileBase::E_None )
147 aTempFileURL = aTmp;
148 aFile.close();
149 break;
151 else if ( err != FileBase::E_EXIST )
153 // if f.e. name contains invalid chars stop trying to create files
154 break;
158 return aTempFileURL;
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */