Apparently destroyFile() now throws an exception. Since this class is
[oota-llvm.git] / include / llvm / Support / FileUtilities.h
1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a family of utility functions which are useful for doing
11 // various things with files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
16 #define LLVM_SUPPORT_FILEUTILITIES_H
17
18 #include "llvm/System/Path.h"
19
20 namespace llvm {
21
22 /// DiffFiles - Compare the two files specified, returning true if they are
23 /// different or if there is a file error.  If you specify a string to fill in
24 /// for the error option, it will set the string to an error message if an error
25 /// occurs, allowing the caller to distinguish between a failed diff and a file
26 /// system error.
27 ///
28 bool DiffFiles(const std::string &FileA, const std::string &FileB,
29                std::string *Error = 0);
30
31 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
32 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
33 /// remove the New file.
34 ///
35 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
36  
37   /// FileRemover - This class is a simple object meant to be stack allocated.
38   /// If an exception is thrown from a region, the object removes the filename
39   /// specified (if deleteIt is true).
40   ///
41   class FileRemover {
42     sys::Path Filename;
43     bool DeleteIt;
44   public:
45     FileRemover(const sys::Path &filename, bool deleteIt = true)
46       : Filename(filename), DeleteIt(deleteIt) {}
47     
48     ~FileRemover() {
49       if (DeleteIt)
50         try {
51           Filename.destroyFile();
52         } catch (...) {}             // Ignore problems deleting the file.
53     }
54
55     /// releaseFile - Take ownership of the file away from the FileRemover so it
56     /// will not be removed when the object is destroyed.
57     void releaseFile() { DeleteIt = false; }
58   };
59 } // End llvm namespace
60
61 #endif