Skip to content

Commit

Permalink
Utility: added Directory::tmp().
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Aug 5, 2016
1 parent ef15c2a commit 12a8abb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Corrade/Utility/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,35 @@ std::string Directory::configurationDir(const std::string& applicationName) {
#endif
}

std::string Directory::tmp() {
#ifdef CORRADE_TARGET_UNIX
/* Sandboxed OSX, iOS */
#ifdef CORRADE_TARGET_APPLE
if(isSandboxed()) return join(path(path(executableLocation())), "tmp");
#endif

/* Common Unix */
return "/tmp";

#elif defined(CORRADE_TARGET_WINDOWS)
/* Windows */

/* Get path size */
char c;
const std::size_t size = GetTempPath(1, &c);

/* Get the path, remove the trailing slash */
std::string path(size, '\0');
GetTempPath(size, &path[0]);
if(path.size()) path.resize(path.size() - 1);

/* Convert to forward slashes */
return fromNativeSeparators(path);
#else
Warning() << "Utility::Directory::tmp(): not implemented on this platform";
#endif
}

std::vector<std::string> Directory::list(const std::string& path, Flags flags) {
std::vector<std::string> list;

Expand Down
14 changes: 14 additions & 0 deletions src/Corrade/Utility/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ class CORRADE_UTILITY_EXPORT Directory {
*/
static std::string configurationDir(const std::string& name);

/**
* @brief Temporary dir
*
* On Unix and non-sandboxed OSX, the directory is equivalent to `/tmp`.
* On sandboxed OSX and iOS the directory is the `/tmp` subfolder of
* the app sandbox. On Windows the directory is equivalent to `%TEMP%`.
* On other systems or if the directory can't be found, empty string is
* returned.
* @note The path is returned with forward slashes on all platforms.
* Use @ref toNativeSeparators() to convert it to
* platform-specific format, if needed.
*/
static std::string tmp();

/**
* @brief Check if the file exists
*
Expand Down
25 changes: 25 additions & 0 deletions src/Corrade/Utility/Test/DirectoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct DirectoryTest: TestSuite::Tester {
void executableLocation();
void home();
void configurationDir();
void tmp();

void list();
void listSkipDirectories();
Expand Down Expand Up @@ -101,6 +102,7 @@ DirectoryTest::DirectoryTest() {
&DirectoryTest::executableLocation,
&DirectoryTest::home,
&DirectoryTest::configurationDir,
&DirectoryTest::tmp,

&DirectoryTest::list,
&DirectoryTest::listSkipDirectories,
Expand Down Expand Up @@ -431,6 +433,29 @@ void DirectoryTest::configurationDir() {
#endif
}

void DirectoryTest::tmp() {
const std::string dir = Directory::tmp();
Debug() << "Temporary dir found as:" << dir;

#ifdef CORRADE_TARGET_UNIX
CORRADE_VERIFY(Directory::fileExists(dir));
CORRADE_VERIFY(dir.find("tmp") != std::string::npos);

#elif defined(CORRADE_TARGET_WINDOWS)
/* Next to %APPDATA%/Local/Temp there is %APPDATA%/Local/Microsoft */
CORRADE_VERIFY(Directory::fileExists(dir));
CORRADE_VERIFY(Directory::fileExists(Directory::join(Directory::path(dir), "Microsoft")));

/* On Windows it also shouldn't contain backslashes */
CORRADE_COMPARE(dir.find('\\'), std::string::npos);

/* No idea elsewhere */
#else
CORRADE_EXPECT_FAIL("Not implemented yet.");
CORRADE_COMPARE(dir, "(not implemented)");
#endif
}

void DirectoryTest::list() {
#if defined(CORRADE_TARGET_IOS) && defined(CORRADE_TESTSUITE_TARGET_XCTEST)
CORRADE_EXPECT_FAIL_IF(!std::getenv("SIMULATOR_UDID"),
Expand Down

0 comments on commit 12a8abb

Please sign in to comment.