New method.
[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 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
32 /// files match, 1 if they are different, and 2 if there is a file error.  This
33 /// function differs from DiffFiles in that you can specify an absolete and
34 /// relative FP error that is allowed to exist.  If you specify a string to fill
35 /// in for the error option, it will set the string to an error message if an
36 /// error occurs, allowing the caller to distinguish between a failed diff and a
37 /// file system error.
38 ///
39 int DiffFilesWithTolerance(const std::string &FileA, const std::string &FileB,
40                            double AbsTol, double RelTol,
41                            std::string *Error = 0);
42
43
44 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
45 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
46 /// remove the New file.
47 ///
48 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
49  
50   /// FileRemover - This class is a simple object meant to be stack allocated.
51   /// If an exception is thrown from a region, the object removes the filename
52   /// specified (if deleteIt is true).
53   ///
54   class FileRemover {
55     sys::Path Filename;
56     bool DeleteIt;
57   public:
58     FileRemover(const sys::Path &filename, bool deleteIt = true)
59       : Filename(filename), DeleteIt(deleteIt) {}
60     
61     ~FileRemover() {
62       if (DeleteIt)
63         try {
64           Filename.destroyFile();
65         } catch (...) {}             // Ignore problems deleting the file.
66     }
67
68     /// releaseFile - Take ownership of the file away from the FileRemover so it
69     /// will not be removed when the object is destroyed.
70     void releaseFile() { DeleteIt = false; }
71   };
72 } // End llvm namespace
73
74 #endif