|
21 | 21 |
|
22 | 22 | // C++ Standard Library includes
|
23 | 23 | #include <cstdio> // for remove()
|
| 24 | +#include <filesystem> // for std::filesystem::temp_directory_path() |
24 | 25 | #include <fstream> // for std::ofstream and std::ifstream
|
25 | 26 | #include <random>
|
26 | 27 | #include <sstream>
|
27 | 28 | #include <stdexcept>
|
28 | 29 |
|
29 | 30 | // System includes
|
30 | 31 | #ifdef _MSC_VER
|
31 |
| - #include <Windows.h> // for GetTempPath() |
32 | 32 | #include <io.h> // for _access_s()
|
33 | 33 | #include <process.h> // for _getpid()
|
34 | 34 | #else
|
35 |
| - #include <cstdlib> // for std::getenv() |
36 | 35 | #include <unistd.h> // for access() and getpid()
|
37 | 36 | #endif
|
38 | 37 |
|
@@ -190,52 +189,7 @@ namespace LibSgfcPlusPlus
|
190 | 189 |
|
191 | 190 | std::string SgfcUtility::GetTempFolderPath()
|
192 | 191 | {
|
193 |
| - std::string tempFolderPath; |
194 |
| - |
195 |
| -#ifdef _MSC_VER |
196 |
| - |
197 |
| - DWORD bufferLength = MAX_PATH + 1; |
198 |
| - char* buffer = new char[bufferLength]; |
199 |
| - |
200 |
| - // The Win32 API works with TCHAR, but since everything else in libsgfc++ |
201 |
| - // does not work with wchar or wstring we don't bother here and just use |
202 |
| - // char and string. |
203 |
| - |
204 |
| - DWORD getTempPathResult = GetTempPath(bufferLength, buffer); |
205 |
| - if (getTempPathResult == 0 || getTempPathResult > bufferLength) |
206 |
| - { |
207 |
| - delete[] buffer; |
208 |
| - throw std::runtime_error("Win32 API function GetTempPath() failed"); |
209 |
| - } |
210 |
| - |
211 |
| - tempFolderPath = buffer; |
212 |
| - delete[] buffer; |
213 |
| - |
214 |
| -#else |
215 |
| - |
216 |
| - // Environment variable names taken from |
217 |
| - // https://en.cppreference.com/w/cpp/filesystem/temp_directory_path |
218 |
| - static std::vector<std::string> environmentVariableNames = { "TMPDIR", "TMP", "TEMP", "TEMPDIR" }; |
219 |
| - |
220 |
| - tempFolderPath = "/tmp"; |
221 |
| - |
222 |
| - for ( const auto& environmentVariableName : environmentVariableNames ) |
223 |
| - { |
224 |
| - const char* environmentVariableValue = std::getenv(environmentVariableName.c_str()); |
225 |
| - if (environmentVariableValue != nullptr) |
226 |
| - { |
227 |
| - tempFolderPath = environmentVariableValue; |
228 |
| - break; |
229 |
| - } |
230 |
| - } |
231 |
| - |
232 |
| -#endif |
233 |
| - |
234 |
| - // On all platforms we have no guarantee that the folder actually exists. |
235 |
| - // This works purely by convention. Because we intend to replace all this |
236 |
| - // cruft with std::filesystem::temp_directory_path() anyway sooner or later, |
237 |
| - // this implementation is good enough. |
238 |
| - |
| 192 | + std::filesystem::path tempFolderPath = std::filesystem::temp_directory_path(); |
239 | 193 | return tempFolderPath;
|
240 | 194 | }
|
241 | 195 |
|
|
0 commit comments