Further revisions of the FDHandle idea. In this version we use ownership
[oota-llvm.git] / include / llvm / Support / FileUtilities.h
1 //===- 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 SUPPORT_FILEUTILITIES_H
16 #define SUPPORT_FILEUTILITIES_H
17
18 #include <string>
19
20 namespace llvm {
21
22 /// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
23 /// name a readable file.
24 ///
25 bool CheckMagic (const std::string &FN, const std::string &Magic);
26
27 /// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
28 /// archive. The file named FN must exist.
29 ///
30 bool IsArchive (const std::string &FN);
31
32 /// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
33 /// bytecode file. The file named FN must exist.
34 ///
35 bool IsBytecode (const std::string &FN);
36
37 /// IsSharedObject - Returns trus IFF the file named FN appears to be a shared
38 /// object with an ELF header. The file named FN must exist.
39 ///
40 bool IsSharedObject(const std::string &FN);
41
42 /// FileOpenable - Returns true IFF Filename names an existing regular file
43 /// which we can successfully open.
44 ///
45 bool FileOpenable (const std::string &Filename);
46
47 /// DiffFiles - Compare the two files specified, returning true if they are
48 /// different or if there is a file error.  If you specify a string to fill in
49 /// for the error option, it will set the string to an error message if an error
50 /// occurs, allowing the caller to distinguish between a failed diff and a file
51 /// system error.
52 ///
53 bool DiffFiles(const std::string &FileA, const std::string &FileB,
54                std::string *Error = 0);
55
56
57 /// MoveFileOverIfUpdated - If the file specified by New is different than Old,
58 /// or if Old does not exist, move the New file over the Old file.  Otherwise,
59 /// remove the New file.
60 ///
61 void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
62  
63 /// removeFile - Delete the specified file
64 ///
65 void removeFile(const std::string &Filename);
66
67 /// getUniqueFilename - Return a filename with the specified prefix.  If the
68 /// file does not exist yet, return it, otherwise add a suffix to make it
69 /// unique.
70 ///
71 std::string getUniqueFilename(const std::string &FilenameBase);
72
73 ///
74 /// Method: MakeFileExecutable()
75 ///
76 /// Description:
77 ///     This method turns on whatever access attributes are needed to make the
78 ///     specified file executable.
79 ///
80 /// Return value:
81 ///     True  - The operation succeeded.
82 ///     False - The operation failed.
83 ///
84 /// Notes:
85 ///     In case of failure, the file's access attributes are unspecified.
86 ///
87 bool MakeFileExecutable (const std::string & Filename);
88
89 ///
90 /// Method: MakeFileReadable()
91 ///
92 /// Description:
93 ///     This method turns on whatever access attributes are needed to make the
94 ///     specified file readable.
95 ///
96 /// Return value:
97 ///     True  - The operation succeeded.
98 ///     False - The operation failed.
99 ///
100 /// Notes:
101 ///     In case of failure, the file's access attributes are unspecified.
102 ///
103 bool MakeFileReadable (const std::string & Filename);
104
105
106 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
107 /// when the object is destroyed.  This handle acts similarly to an
108 /// std::auto_ptr, in that the copy constructor and assignment operators
109 /// transfer ownership of the handle.  This means that FDHandle's do not have
110 /// value semantics.
111 ///
112 class FDHandle {
113   int FD;
114 public:
115   FDHandle() : FD(-1) {}
116   FDHandle(int fd) : FD(fd) {}
117   FDHandle(FDHandle &RHS) : FD(RHS.FD) {
118     RHS.FD = -1;       // Transfer ownership
119   }
120
121   ~FDHandle() throw();
122
123   /// get - Get the current file descriptor, without releasing ownership of it.
124   int get() const { return FD; }
125   operator int() const { return FD; }
126
127   FDHandle &operator=(int fd) throw();
128
129   FDHandle &operator=(FDHandle &RHS) {
130     int fd = RHS.FD;
131     RHS.FD = -1;       // Transfer ownership
132     return operator=(fd);
133   }
134
135   /// release - Take ownership of the file descriptor away from the FDHandle
136   /// object, so that the file is not closed when the FDHandle is destroyed.
137   int release() {
138     int Ret = FD;
139     FD = -1;
140     return Ret;
141   }
142 };
143 } // End llvm namespace
144
145 #endif