summaryrefslogtreecommitdiffstats
path: root/llvm/support/lib/Support/FileUtilities.cpp
diff options
context:
space:
mode:
authorMisha Brukman <brukman+llvm@gmail.com>2003-08-07 21:28:50 +0000
committerMisha Brukman <brukman+llvm@gmail.com>2003-08-07 21:28:50 +0000
commit3581f8542de06e719acecfa0abc991372f432a84 (patch)
tree0b2fe68965c67c054e47abb684e14fccf4f7abc4 /llvm/support/lib/Support/FileUtilities.cpp
parent250bf3674c2785f55dcdb226740d3418eefa0936 (diff)
downloadbcm5719-llvm-3581f8542de06e719acecfa0abc991372f432a84.tar.gz
bcm5719-llvm-3581f8542de06e719acecfa0abc991372f432a84.zip
Moved removeFile() and getUniqueFilename() into FileUtilities.
llvm-svn: 7691
Diffstat (limited to 'llvm/support/lib/Support/FileUtilities.cpp')
-rw-r--r--llvm/support/lib/Support/FileUtilities.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/llvm/support/lib/Support/FileUtilities.cpp b/llvm/support/lib/Support/FileUtilities.cpp
index 35bdf1e9a4d..9f9ef306868 100644
--- a/llvm/support/lib/Support/FileUtilities.cpp
+++ b/llvm/support/lib/Support/FileUtilities.cpp
@@ -54,3 +54,38 @@ void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
std::remove(New.c_str());
}
}
+
+/// removeFile - Delete the specified file
+///
+void removeFile(const std::string &Filename) {
+ std::remove(Filename.c_str());
+}
+
+/// getUniqueFilename - Return a filename with the specified prefix. If the
+/// file does not exist yet, return it, otherwise add a suffix to make it
+/// unique.
+///
+std::string getUniqueFilename(const std::string &FilenameBase) {
+ if (!std::ifstream(FilenameBase.c_str()))
+ return FilenameBase; // Couldn't open the file? Use it!
+
+ // Create a pattern for mkstemp...
+ char *FNBuffer = new char[FilenameBase.size()+8];
+ strcpy(FNBuffer, FilenameBase.c_str());
+ strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
+
+ // Agree on a temporary file name to use....
+ int TempFD;
+ if ((TempFD = mkstemp(FNBuffer)) == -1) {
+ std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
+ << " directory!\n";
+ exit(1);
+ }
+
+ // We don't need to hold the temp file descriptor... we will trust that noone
+ // will overwrite/delete the file while we are working on it...
+ close(TempFD);
+ std::string Result(FNBuffer);
+ delete[] FNBuffer;
+ return Result;
+}
OpenPOWER on IntegriCloud