Add useful method, minor cleanups
[oota-llvm.git] / include / llvm / Support / FileUtilities.h
index b17bca9e991b372b78c4f89f9cc55c5b6c9e93ce..c9f52c324880721744ee78e42d5c62fbc9d87a1b 100644 (file)
@@ -1,4 +1,4 @@
-//===- Support/FileUtilities.h - File System Utilities ----------*- C++ -*-===//
+//===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
 // 
 //                     The LLVM Compiler Infrastructure
 //
@@ -12,8 +12,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef SUPPORT_FILEUTILITIES_H
-#define SUPPORT_FILEUTILITIES_H
+#ifndef LLVM_SUPPORT_FILEUTILITIES_H
+#define LLVM_SUPPORT_FILEUTILITIES_H
 
 #include <string>
 
@@ -42,7 +42,7 @@ bool IsSharedObject(const std::string &FN);
 /// FileOpenable - Returns true IFF Filename names an existing regular file
 /// which we can successfully open.
 ///
-bool FileOpenable (const std::string &Filename);
+bool FileOpenable(const std::string &Filename);
 
 /// DiffFiles - Compare the two files specified, returning true if they are
 /// different or if there is a file error.  If you specify a string to fill in
@@ -53,6 +53,10 @@ bool FileOpenable (const std::string &Filename);
 bool DiffFiles(const std::string &FileA, const std::string &FileB,
                std::string *Error = 0);
 
+/// CopyFile - Copy the specified source file to the specified destination,
+/// overwriting destination if it exists.  This returns true on failure.
+///
+bool CopyFile(const std::string &Dest, const std::string &Src);
 
 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
@@ -60,7 +64,7 @@ bool DiffFiles(const std::string &FileA, const std::string &FileB,
 ///
 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
  
-/// removeFile - Delete the specified file
+/// removeFile - Delete the specified file.
 ///
 void removeFile(const std::string &Filename);
 
@@ -70,37 +74,38 @@ void removeFile(const std::string &Filename);
 ///
 std::string getUniqueFilename(const std::string &FilenameBase);
 
+/// MakeFileExecutable - This method turns on whatever access attributes are
+/// needed to make the specified file executable.  It returns true on success.
+/// In case of failure, the file's access attributes are unspecified.
 ///
-/// Method: MakeFileExecutable()
-///
-/// Description:
-///    This method turns on whatever access attributes are needed to make the
-///    specified file executable.
-///
-/// Return value:
-///    True  - The operation succeeded.
-///    False - The operation failed.
-///
-/// Notes:
-///    In case of failure, the file's access attributes are unspecified.
-///
-bool MakeFileExecutable (const std::string & Filename);
+bool MakeFileExecutable(const std::string &Filename);
 
+/// MakeFileReadable - This method turns on whatever access attributes are
+/// needed to make the specified file readable.  It returns true on success.
+/// In case of failure, the file's access attributes are unspecified.
 ///
-/// Method: MakeFileReadable()
-///
-/// Description:
-///    This method turns on whatever access attributes are needed to make the
-///    specified file readable.
-///
-/// Return value:
-///    True  - The operation succeeded.
-///    False - The operation failed.
-///
-/// Notes:
-///    In case of failure, the file's access attributes are unspecified.
-///
-bool MakeFileReadable (const std::string & Filename);
+bool MakeFileReadable(const std::string &Filename);
+
+/// getFileSize - Return the size of the specified file in bytes, or -1 if the
+/// file cannot be read or does not exist.
+long long getFileSize(const std::string &Filename);
+
+
+/// getFileTimestamp - Get the last modified time for the specified file in an
+/// unspecified format.  This is useful to allow checking to see if a file was
+/// updated since that last time the timestampt was aquired.  If the file does
+/// not exist or there is an error getting the time-stamp, zero is returned.
+unsigned long long getFileTimestamp(const std::string &Filename);
+
+/// ReadFileIntoAddressSpace - Attempt to map the specific file into the 
+/// address space of the current process for reading.  If this succeeds, 
+/// return the address of the buffer and the length of the file mapped.  On 
+/// failure, return null.
+void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
+
+/// UnmapFileFromAddressSpace - Remove the specified file from the current
+/// address space.
+void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
 
 
 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
@@ -140,6 +145,26 @@ public:
     return Ret;
   }
 };
+
+  /// FileRemover - This class is a simple object meant to be stack allocated.
+  /// If an exception is thrown from a region, the object removes the filename
+  /// specified (if deleteIt is true).
+  ///
+  class FileRemover {
+    std::string Filename;
+    bool DeleteIt;
+  public:
+    FileRemover(const std::string &filename, bool deleteIt = true)
+      : Filename(filename), DeleteIt(deleteIt) {}
+    
+    ~FileRemover() {
+      if (DeleteIt) removeFile(Filename);
+    }
+
+    /// releaseFile - Take ownership of the file away from the FileRemover so it
+    /// will not be removed when the object is destroyed.
+    void releaseFile() { DeleteIt = false; }
+  };
 } // End llvm namespace
 
 #endif