Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <osl/file.h>
21 : #include <sal/macros.h>
22 :
23 : #if defined( UNX)
24 :
25 : #include <stdio.h>
26 : #include <string.h>
27 : #include <osl/thread.h>
28 :
29 0 : oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
30 : {
31 0 : const char *pValue = getenv( "TEMP" );
32 :
33 0 : if ( !pValue )
34 : {
35 0 : pValue = getenv( "TMP" );
36 : #if defined(SOLARIS) || defined (LINUX)
37 0 : if ( !pValue )
38 0 : pValue = P_tmpdir;
39 : #endif
40 : }
41 :
42 0 : if ( pValue )
43 : {
44 : oslFileError error;
45 0 : rtl_uString *ustrTempPath = NULL;
46 :
47 0 : rtl_string2UString( &ustrTempPath, pValue, strlen( pValue ), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
48 0 : error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
49 0 : rtl_uString_release( ustrTempPath );
50 :
51 0 : return error;
52 : }
53 : else
54 0 : return osl_File_E_NOENT;
55 : }
56 : #else
57 :
58 : #ifdef NDEBUG
59 : # define NO_DEBUG_CRT
60 : #endif
61 :
62 : #ifndef _WIN32_WINNT
63 : # define _WIN32_WINNT 0x0400
64 : # define _CTYPE_DISABLE_MACROS /* wg. dynamischer C-Runtime MH */
65 : #endif
66 :
67 : #if defined _MSC_VER
68 : #pragma warning(push, 1)
69 : #endif
70 :
71 : #define WIN32_LEAN_AND_MEAN
72 : #include <windows.h>
73 : #include <malloc.h>
74 :
75 : #if defined _MSC_VER
76 : #pragma warning(pop)
77 : #endif
78 :
79 : oslFileError SAL_CALL my_getTempDirURL( rtl_uString** pustrTempDir )
80 : {
81 : WCHAR szBuffer[MAX_PATH];
82 : LPWSTR lpBuffer = szBuffer;
83 : DWORD nBufferLength = SAL_N_ELEMENTS(szBuffer) - 1;
84 :
85 : DWORD nLength;
86 : oslFileError error;
87 :
88 : do
89 : {
90 : nLength = GetTempPathW( SAL_N_ELEMENTS(szBuffer), lpBuffer );
91 : if ( nLength > nBufferLength )
92 : {
93 : nLength++;
94 : lpBuffer = (LPWSTR)alloca( sizeof(WCHAR) * nLength );
95 : nBufferLength = nLength - 1;
96 : }
97 : } while ( nLength > nBufferLength );
98 :
99 : if ( nLength )
100 : {
101 : rtl_uString *ustrTempPath = NULL;
102 :
103 : if ( '\\' == lpBuffer[nLength-1] )
104 : lpBuffer[nLength-1] = 0;
105 :
106 : rtl_uString_newFromStr( &ustrTempPath, reinterpret_cast<const sal_Unicode*>(lpBuffer) );
107 :
108 : error = osl_getFileURLFromSystemPath( ustrTempPath, pustrTempDir );
109 :
110 : rtl_uString_release( ustrTempPath );
111 : }
112 : else
113 : error = GetLastError() == ERROR_SUCCESS ? osl_File_E_None : osl_File_E_INVAL;
114 :
115 : return error;
116 : }
117 : #endif
118 :
119 : #include "tempfile.hxx"
120 :
121 : using ::rtl::OUString;
122 0 : TempFile::TempFile( const OUString& rTempFileURL )
123 0 : :osl::File( rTempFileURL ), maURL( rTempFileURL )
124 : {
125 0 : }
126 :
127 0 : TempFile::~TempFile()
128 : {
129 0 : close();
130 :
131 0 : if( !maURL.isEmpty() )
132 0 : osl::File::remove( maURL );
133 0 : }
134 :
135 0 : OUString TempFile::createTempFileURL()
136 : {
137 0 : OUString aTempFileURL;
138 :
139 0 : const sal_uInt32 nRadix = 26;
140 :
141 0 : OUString aTempDirURL;
142 0 : /* oslFileError nRC = */ my_getTempDirURL( &aTempDirURL.pData );
143 :
144 0 : static sal_uInt32 u = osl_getGlobalTimer();
145 0 : for ( sal_uInt32 nOld = u; ++u != nOld; )
146 : {
147 0 : u %= (nRadix*nRadix*nRadix);
148 0 : OUString aTmp( aTempDirURL );
149 0 : if( aTmp.getStr()[ aTmp.getLength() - 1 ] != sal_Unicode( '/' ) )
150 0 : aTmp += OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ));
151 0 : aTmp += OUString::valueOf( (sal_Int32) (unsigned) u, nRadix );
152 0 : aTmp += OUString( RTL_CONSTASCII_USTRINGPARAM( ".tmp" ));
153 :
154 0 : osl::File aFile( aTmp );
155 0 : osl::FileBase::RC err = aFile.open(osl_File_OpenFlag_Create);
156 0 : if ( err == FileBase::E_None )
157 : {
158 0 : aTempFileURL = aTmp;
159 0 : aFile.close();
160 : break;
161 : }
162 0 : else if ( err != FileBase::E_EXIST )
163 : {
164 : // if f.e. name contains invalid chars stop trying to create files
165 : break;
166 : }
167 0 : }
168 :
169 0 : return aTempFileURL;
170 : }
171 :
172 0 : OUString TempFile::getFileURL()
173 : {
174 0 : return maURL;
175 : }
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|