1193323Sed//===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines a family of utility functions which are useful for doing
11193323Sed// various things with files.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15193323Sed#ifndef LLVM_SUPPORT_FILEUTILITIES_H
16193323Sed#define LLVM_SUPPORT_FILEUTILITIES_H
17193323Sed
18221345Sdim#include "llvm/Support/FileSystem.h"
19218893Sdim#include "llvm/Support/Path.h"
20193323Sed
21193323Sednamespace llvm {
22193323Sed
23193323Sed  /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
24193323Sed  /// the files match, 1 if they are different, and 2 if there is a file error.
25221345Sdim  /// This function allows you to specify an absolute and relative FP error that
26193323Sed  /// is allowed to exist.  If you specify a string to fill in for the error
27193323Sed  /// option, it will set the string to an error message if an error occurs, or
28193323Sed  /// if the files are different.
29193323Sed  ///
30263508Sdim  int DiffFilesWithTolerance(StringRef FileA,
31263508Sdim                             StringRef FileB,
32193323Sed                             double AbsTol, double RelTol,
33193323Sed                             std::string *Error = 0);
34193323Sed
35193323Sed
36193323Sed  /// FileRemover - This class is a simple object meant to be stack allocated.
37193323Sed  /// If an exception is thrown from a region, the object removes the filename
38193323Sed  /// specified (if deleteIt is true).
39193323Sed  ///
40193323Sed  class FileRemover {
41221345Sdim    SmallString<128> Filename;
42193323Sed    bool DeleteIt;
43193323Sed  public:
44206083Srdivacky    FileRemover() : DeleteIt(false) {}
45206083Srdivacky
46221345Sdim    explicit FileRemover(const Twine& filename, bool deleteIt = true)
47221345Sdim      : DeleteIt(deleteIt) {
48221345Sdim      filename.toVector(Filename);
49221345Sdim    }
50193323Sed
51193323Sed    ~FileRemover() {
52193323Sed      if (DeleteIt) {
53193323Sed        // Ignore problems deleting the file.
54221345Sdim        bool existed;
55221345Sdim        sys::fs::remove(Filename.str(), existed);
56193323Sed      }
57193323Sed    }
58193323Sed
59206083Srdivacky    /// setFile - Give ownership of the file to the FileRemover so it will
60206083Srdivacky    /// be removed when the object is destroyed.  If the FileRemover already
61206083Srdivacky    /// had ownership of a file, remove it first.
62221345Sdim    void setFile(const Twine& filename, bool deleteIt = true) {
63221345Sdim      if (DeleteIt) {
64221345Sdim        // Ignore problems deleting the file.
65221345Sdim        bool existed;
66221345Sdim        sys::fs::remove(Filename.str(), existed);
67221345Sdim      }
68206083Srdivacky
69221345Sdim      Filename.clear();
70221345Sdim      filename.toVector(Filename);
71206083Srdivacky      DeleteIt = deleteIt;
72206083Srdivacky    }
73206083Srdivacky
74193323Sed    /// releaseFile - Take ownership of the file away from the FileRemover so it
75193323Sed    /// will not be removed when the object is destroyed.
76193323Sed    void releaseFile() { DeleteIt = false; }
77193323Sed  };
78193323Sed} // End llvm namespace
79193323Sed
80193323Sed#endif
81