FileUtilities.h revision 218893
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/Path.h"
19
20namespace llvm {
21
22  /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
23  /// the files match, 1 if they are different, and 2 if there is a file error.
24  /// This function allows you to specify an absolete and relative FP error that
25  /// is allowed to exist.  If you specify a string to fill in for the error
26  /// option, it will set the string to an error message if an error occurs, or
27  /// if the files are different.
28  ///
29  int DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
30                             const sys::PathWithStatus &FileB,
31                             double AbsTol, double RelTol,
32                             std::string *Error = 0);
33
34
35  /// FileRemover - This class is a simple object meant to be stack allocated.
36  /// If an exception is thrown from a region, the object removes the filename
37  /// specified (if deleteIt is true).
38  ///
39  class FileRemover {
40    sys::Path Filename;
41    bool DeleteIt;
42  public:
43    FileRemover() : DeleteIt(false) {}
44
45    explicit FileRemover(const sys::Path &filename, bool deleteIt = true)
46      : Filename(filename), DeleteIt(deleteIt) {}
47
48    ~FileRemover() {
49      if (DeleteIt) {
50        // Ignore problems deleting the file.
51        Filename.eraseFromDisk();
52      }
53    }
54
55    /// setFile - Give ownership of the file to the FileRemover so it will
56    /// be removed when the object is destroyed.  If the FileRemover already
57    /// had ownership of a file, remove it first.
58    void setFile(const sys::Path &filename, bool deleteIt = true) {
59      if (DeleteIt)
60        Filename.eraseFromDisk();
61
62      Filename = filename;
63      DeleteIt = deleteIt;
64    }
65
66    /// releaseFile - Take ownership of the file away from the FileRemover so it
67    /// will not be removed when the object is destroyed.
68    void releaseFile() { DeleteIt = false; }
69  };
70} // End llvm namespace
71
72#endif
73