llvm.org GIT mirror llvm / 55cf815
Add a simple routine to determine the typical system directory for temporary data. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139725 91177308-0d34-0410-b5e6-96231b3b80d8 Douglas Gregor 9 years ago
2 changed file(s) with 39 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
186186 /// @result The root directory of \a path if it has one, otherwise
187187 /// "".
188188 const StringRef root_directory(StringRef path);
189
189
190190 /// @brief Get root path.
191191 ///
192192 /// Equivalent to root_name + root_directory.
263263 /// @result true if \a value is a path separator character on the host OS
264264 bool is_separator(char value);
265265
266 /// @brief Get the typical temporary directory for the system, e.g.,
267 /// "/var/tmp" or "C:/TEMP"
268 ///
269 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
270 /// rather than one that potentially persists longer.
271 /// @param Result Holds the resulting path name.
272 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &result);
273
266274 /// @brief Has root name?
267275 ///
268276 /// root_name != ""
489489 }
490490 }
491491
492 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &result) {
493 result.clear();
494
495 // Check whether the temporary directory is specified by an environment
496 // variable.
497 const char *EnvironmentVariable;
498 #ifdef LLVM_ON_WIN32
499 EnvironmentVariable = "TEMP";
500 #else
501 EnvironmentVariable = "TMPDIR";
502 #endif
503 if (char *RequestedDir = getenv(EnvironmentVariable)) {
504 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
505 return;
506 }
507
508 // Fall back to a system default.
509 const char *DefaultResult;
510 #ifdef LLVM_ON_WIN32
511 (void)erasedOnReboot;
512 DefaultResult = "C:\TEMP";
513 #else
514 if (erasedOnReboot)
515 DefaultResult = "/tmp";
516 else
517 DefaultResult = "/var/tmp";
518 #endif
519 result.append(DefaultResult, DefaultResult + strlen(DefaultResult));
520 }
521
492522 bool has_root_name(const Twine &path) {
493523 SmallString<128> path_storage;
494524 StringRef p = path.toStringRef(path_storage);