For PR351:
[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 <string>
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 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
32 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
33 /// remove the New file.
34 ///
35 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
36  
37 /// removeFile - Delete the specified file.
38 ///
39 void removeFile(const std::string &Filename);
40
41 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
42 /// when the object is destroyed.  This handle acts similarly to an
43 /// std::auto_ptr, in that the copy constructor and assignment operators
44 /// transfer ownership of the handle.  This means that FDHandle's do not have
45 /// value semantics.
46 ///
47 class FDHandle {
48   int FD;
49 public:
50   FDHandle() : FD(-1) {}
51   FDHandle(int fd) : FD(fd) {}
52   FDHandle(FDHandle &RHS) : FD(RHS.FD) {
53     RHS.FD = -1;       // Transfer ownership
54   }
55
56   ~FDHandle() throw();
57
58   /// get - Get the current file descriptor, without releasing ownership of it.
59   int get() const { return FD; }
60   operator int() const { return FD; }
61
62   FDHandle &operator=(int fd) throw();
63
64   FDHandle &operator=(FDHandle &RHS) {
65     int fd = RHS.FD;
66     RHS.FD = -1;       // Transfer ownership
67     return operator=(fd);
68   }
69
70   /// release - Take ownership of the file descriptor away from the FDHandle
71   /// object, so that the file is not closed when the FDHandle is destroyed.
72   int release() {
73     int Ret = FD;
74     FD = -1;
75     return Ret;
76   }
77 };
78
79   /// FileRemover - This class is a simple object meant to be stack allocated.
80   /// If an exception is thrown from a region, the object removes the filename
81   /// specified (if deleteIt is true).
82   ///
83   class FileRemover {
84     std::string Filename;
85     bool DeleteIt;
86   public:
87     FileRemover(const std::string &filename, bool deleteIt = true)
88       : Filename(filename), DeleteIt(deleteIt) {}
89     
90     ~FileRemover() {
91       if (DeleteIt) removeFile(Filename);
92     }
93
94     /// releaseFile - Take ownership of the file away from the FileRemover so it
95     /// will not be removed when the object is destroyed.
96     void releaseFile() { DeleteIt = false; }
97   };
98 } // End llvm namespace
99
100 #endif