Skip to content

Commit

Permalink
Add a general function for getting the temp directory.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 301173847
Change-Id: Ie8b9e4484b6d8031b59e51bb9ebca6d9ad4de79a
  • Loading branch information
DeepMind Technologies Ltd authored and open_spiel@google.com committed Mar 16, 2020
1 parent 4759d6b commit 0e05222
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
7 changes: 1 addition & 6 deletions open_spiel/utils/data_logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@
namespace open_spiel {
namespace {

std::string GetEnv(const std::string& key, const std::string& default_value) {
char* val = getenv(key.c_str());
return ((val != nullptr) ? std::string(val) : default_value);
}

void TestDataLogger() {
std::string val = std::to_string(std::rand()); // NOLINT
std::string tmp_dir = GetEnv("TMPDIR", "/tmp");
std::string tmp_dir = file::GetTmpDir();
std::string dir = tmp_dir + "/open_spiel-test-" + val;
std::string filename = dir + "/data-test.jsonl";

Expand Down
7 changes: 7 additions & 0 deletions open_spiel/utils/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,11 @@ bool Remove(const std::string& path) {
}
}

std::string GetEnv(const std::string& key, const std::string& default_value) {
char* val = std::getenv(key.c_str());
return ((val != nullptr) ? std::string(val) : default_value);
}

std::string GetTmpDir() { return GetEnv("TMPDIR", "/tmp"); }

} // namespace open_spiel::file
3 changes: 3 additions & 0 deletions open_spiel/utils/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ bool Mkdir(const std::string& path, int mode = 0755); // Make a directory.
bool Mkdirs(const std::string& path, int mode = 0755); // Mkdir recursively.
bool Remove(const std::string& path); // Remove/delete the file/directory.

std::string GetEnv(const std::string& key, const std::string& default_value);
std::string GetTmpDir();

} // namespace open_spiel::file

#endif // THIRD_PARTY_OPEN_SPIEL_UTILS_FILE_H_
7 changes: 1 addition & 6 deletions open_spiel/utils/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
namespace open_spiel::file {
namespace {

std::string GetEnv(const std::string& key, const std::string& default_value) {
char* val = getenv(key.c_str());
return ((val != nullptr) ? std::string(val) : default_value);
}

void TestFile() {
std::string val = std::to_string(std::rand()); // NOLINT
std::string tmp_dir = GetEnv("TMPDIR", "/tmp");
std::string tmp_dir = file::GetTmpDir();
std::string dir = tmp_dir + "/open_spiel-test-" + val;
std::string filename = dir + "/test-file.txt";

Expand Down

0 comments on commit 0e05222

Please sign in to comment.