Include PathV1.h in files that use it.
[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 is distributed under the University of Illinois Open Source
6 // 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/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/PathV1.h"
21
22 namespace llvm {
23
24   /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
25   /// the files match, 1 if they are different, and 2 if there is a file error.
26   /// This function allows you to specify an absolute and relative FP error that
27   /// is allowed to exist.  If you specify a string to fill in for the error
28   /// option, it will set the string to an error message if an error occurs, or
29   /// if the files are different.
30   ///
31   int DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
32                              const sys::PathWithStatus &FileB,
33                              double AbsTol, double RelTol,
34                              std::string *Error = 0);
35
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     SmallString<128> Filename;
43     bool DeleteIt;
44   public:
45     FileRemover() : DeleteIt(false) {}
46
47     explicit FileRemover(const Twine& filename, bool deleteIt = true)
48       : DeleteIt(deleteIt) {
49       filename.toVector(Filename);
50     }
51
52     ~FileRemover() {
53       if (DeleteIt) {
54         // Ignore problems deleting the file.
55         bool existed;
56         sys::fs::remove(Filename.str(), existed);
57       }
58     }
59
60     /// setFile - Give ownership of the file to the FileRemover so it will
61     /// be removed when the object is destroyed.  If the FileRemover already
62     /// had ownership of a file, remove it first.
63     void setFile(const Twine& filename, bool deleteIt = true) {
64       if (DeleteIt) {
65         // Ignore problems deleting the file.
66         bool existed;
67         sys::fs::remove(Filename.str(), existed);
68       }
69
70       Filename.clear();
71       filename.toVector(Filename);
72       DeleteIt = deleteIt;
73     }
74
75     /// releaseFile - Take ownership of the file away from the FileRemover so it
76     /// will not be removed when the object is destroyed.
77     void releaseFile() { DeleteIt = false; }
78   };
79 } // End llvm namespace
80
81 #endif