Skip to content

Commit ba17c73

Browse files
committed
replace custom code with std::filesystem::temp_directory_path() (closes #18)
1 parent bf95232 commit ba17c73

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

src/SgfcUtility.cpp

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121

2222
// C++ Standard Library includes
2323
#include <cstdio> // for remove()
24+
#include <filesystem> // for std::filesystem::temp_directory_path()
2425
#include <fstream> // for std::ofstream and std::ifstream
2526
#include <random>
2627
#include <sstream>
2728
#include <stdexcept>
2829

2930
// System includes
3031
#ifdef _MSC_VER
31-
#include <Windows.h> // for GetTempPath()
3232
#include <io.h> // for _access_s()
3333
#include <process.h> // for _getpid()
3434
#else
35-
#include <cstdlib> // for std::getenv()
3635
#include <unistd.h> // for access() and getpid()
3736
#endif
3837

@@ -190,52 +189,7 @@ namespace LibSgfcPlusPlus
190189

191190
std::string SgfcUtility::GetTempFolderPath()
192191
{
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();
239193
return tempFolderPath;
240194
}
241195

src/SgfcUtility.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ namespace LibSgfcPlusPlus
158158
/// @brief Returns the full path to a folder that is suitable for temporary
159159
/// files. The path is guaranteed to exist and to be a directory.
160160
///
161-
/// This is a replacement for std::filesystem::temp_directory_path().
162-
/// This function was defined in C++17, but is not available on all
163-
/// platforms (notably on macOS it is available only from macOS 10.15), so
164-
/// we have to roll our own platform-independent function.
161+
/// The implementation makes use of std::filesystem::temp_directory_path(),
162+
/// which is defined in C++17 but may not be available on older platform
163+
/// versions (notably on macOS it is available only from macOS 10.15). For
164+
/// more details about how the path is determined, see
165+
/// https://en.cppreference.com/w/cpp/filesystem/temp_directory_path.
166+
///
167+
/// @exception std::filesystem::filesystem_error Is thrown if invoking the
168+
/// underlying operating system API results in an error. The exception
169+
/// object is constructed with the temporary folder path as the first path
170+
/// argument and the operating system error code as the error code argument.
165171
static std::string GetTempFolderPath();
166172

167173
/// @brief Returns the unique base name of a file that is extremely

0 commit comments

Comments
 (0)