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 /// CopyFile - Copy the specified source file to the specified destination,
32 /// overwriting destination if it exists.  This returns true on failure.
33 ///
34 bool CopyFile(const std::string &Dest, const std::string &Src);
35
36 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
37 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
38 /// remove the New file.
39 ///
40 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
41  
42 /// removeFile - Delete the specified file.
43 ///
44 void removeFile(const std::string &Filename);
45
46 /// getUniqueFilename - Return a filename with the specified prefix.  If the
47 /// file does not exist yet, return it, otherwise add a suffix to make it
48 /// unique.
49 ///
50 std::string getUniqueFilename(const std::string &FilenameBase);
51
52 /// MakeFileExecutable - This method turns on whatever access attributes are
53 /// needed to make the specified file executable.  It returns true on success.
54 /// In case of failure, the file's access attributes are unspecified.
55 ///
56 bool MakeFileExecutable(const std::string &Filename);
57
58 /// MakeFileReadable - This method turns on whatever access attributes are
59 /// needed to make the specified file readable.  It returns true on success.
60 /// In case of failure, the file's access attributes are unspecified.
61 ///
62 bool MakeFileReadable(const std::string &Filename);
63
64 /// ReadFileIntoAddressSpace - Attempt to map the specific file into the 
65 /// address space of the current process for reading.  If this succeeds, 
66 /// return the address of the buffer and the length of the file mapped.  On 
67 /// failure, return null.
68 void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
69
70 /// UnmapFileFromAddressSpace - Remove the specified file from the current
71 /// address space.
72 void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
73
74
75 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
76 /// when the object is destroyed.  This handle acts similarly to an
77 /// std::auto_ptr, in that the copy constructor and assignment operators
78 /// transfer ownership of the handle.  This means that FDHandle's do not have
79 /// value semantics.
80 ///
81 class FDHandle {
82   int FD;
83 public:
84   FDHandle() : FD(-1) {}
85   FDHandle(int fd) : FD(fd) {}
86   FDHandle(FDHandle &RHS) : FD(RHS.FD) {
87     RHS.FD = -1;       // Transfer ownership
88   }
89
90   ~FDHandle() throw();
91
92   /// get - Get the current file descriptor, without releasing ownership of it.
93   int get() const { return FD; }
94   operator int() const { return FD; }
95
96   FDHandle &operator=(int fd) throw();
97
98   FDHandle &operator=(FDHandle &RHS) {
99     int fd = RHS.FD;
100     RHS.FD = -1;       // Transfer ownership
101     return operator=(fd);
102   }
103
104   /// release - Take ownership of the file descriptor away from the FDHandle
105   /// object, so that the file is not closed when the FDHandle is destroyed.
106   int release() {
107     int Ret = FD;
108     FD = -1;
109     return Ret;
110   }
111 };
112
113   /// FileRemover - This class is a simple object meant to be stack allocated.
114   /// If an exception is thrown from a region, the object removes the filename
115   /// specified (if deleteIt is true).
116   ///
117   class FileRemover {
118     std::string Filename;
119     bool DeleteIt;
120   public:
121     FileRemover(const std::string &filename, bool deleteIt = true)
122       : Filename(filename), DeleteIt(deleteIt) {}
123     
124     ~FileRemover() {
125       if (DeleteIt) removeFile(Filename);
126     }
127
128     /// releaseFile - Take ownership of the file away from the FileRemover so it
129     /// will not be removed when the object is destroyed.
130     void releaseFile() { DeleteIt = false; }
131   };
132 } // End llvm namespace
133
134 #endif