LLVM API Documentation

FileUtilities.h
Go to the documentation of this file.
00001 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a family of utility functions which are useful for doing
00011 // various things with files.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
00016 #define LLVM_SUPPORT_FILEUTILITIES_H
00017 
00018 #include "llvm/Support/FileSystem.h"
00019 #include "llvm/Support/Path.h"
00020 
00021 namespace llvm {
00022 
00023   /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
00024   /// the files match, 1 if they are different, and 2 if there is a file error.
00025   /// This function allows you to specify an absolute and relative FP error that
00026   /// is allowed to exist.  If you specify a string to fill in for the error
00027   /// option, it will set the string to an error message if an error occurs, or
00028   /// if the files are different.
00029   ///
00030   int DiffFilesWithTolerance(StringRef FileA,
00031                              StringRef FileB,
00032                              double AbsTol, double RelTol,
00033                              std::string *Error = nullptr);
00034 
00035 
00036   /// FileRemover - This class is a simple object meant to be stack allocated.
00037   /// If an exception is thrown from a region, the object removes the filename
00038   /// specified (if deleteIt is true).
00039   ///
00040   class FileRemover {
00041     SmallString<128> Filename;
00042     bool DeleteIt;
00043   public:
00044     FileRemover() : DeleteIt(false) {}
00045 
00046     explicit FileRemover(const Twine& filename, bool deleteIt = true)
00047       : DeleteIt(deleteIt) {
00048       filename.toVector(Filename);
00049     }
00050 
00051     ~FileRemover() {
00052       if (DeleteIt) {
00053         // Ignore problems deleting the file.
00054         sys::fs::remove(Filename.str());
00055       }
00056     }
00057 
00058     /// setFile - Give ownership of the file to the FileRemover so it will
00059     /// be removed when the object is destroyed.  If the FileRemover already
00060     /// had ownership of a file, remove it first.
00061     void setFile(const Twine& filename, bool deleteIt = true) {
00062       if (DeleteIt) {
00063         // Ignore problems deleting the file.
00064         sys::fs::remove(Filename.str());
00065       }
00066 
00067       Filename.clear();
00068       filename.toVector(Filename);
00069       DeleteIt = deleteIt;
00070     }
00071 
00072     /// releaseFile - Take ownership of the file away from the FileRemover so it
00073     /// will not be removed when the object is destroyed.
00074     void releaseFile() { DeleteIt = false; }
00075   };
00076 } // End llvm namespace
00077 
00078 #endif