Stop propagating method names that violate the coding standard
[oota-llvm.git] / include / llvm / System / Path.h
1 //===- llvm/System/Path.h - Path Operating System Concept -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys::Path class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_PATH_H
15 #define LLVM_SYSTEM_PATH_H
16
17 #include <string>
18 #include <vector>
19
20 namespace llvm {
21 namespace sys {
22
23   /// This class provides an abstraction for the path to a file or directory 
24   /// in the operating system's filesystem and provides various basic operations 
25   /// on it.  Note that this class only represents the name of a path to a file
26   /// or directory which may or may not be valid for a given machine's file 
27   /// system. A Path ensures that the name it encapsulates is syntactical valid
28   /// for the operating system it is running on but does not ensure correctness
29   /// for any particular file system. A Path either references a file or a 
30   /// directory and the distinction is consistently maintained. Most operations
31   /// on the class have invariants that require the Path object to be either a
32   /// file path or a directory path, but not both. Those operations will also 
33   /// leave the object as either a file path or object path. There is exactly 
34   /// one invalid Path which is the empty path. The class should never allow any
35   /// other syntactically invalid non-empty path name to be assigned. Empty
36   /// paths are required in order to indicate an error result. If the path is
37   /// empty, the isValid operation will return false. All operations will fail
38   /// if isValid is false. Operations that change the path will either return 
39   /// false if it would cause a syntactically invalid path name (in which case 
40   /// the Path object is left unchanged) or throw an std::string exception 
41   /// indicating the error.
42   /// @since 1.4
43   /// @brief An abstraction for operating system paths.
44   class Path {
45     /// @name Constructors
46     /// @{
47     public:
48       /// Construct a path to the root directory of the file system. The root
49       /// directory is a top level directory above which there are no more 
50       /// directories. For example, on UNIX, the root directory is /. On Windows
51       /// it is C:\. Other operating systems may have different notions of
52       /// what the root directory is.
53       /// @throws nothing
54       static Path GetRootDirectory();
55
56       /// Construct a path to a unique temporary directory that is created in
57       /// a "standard" place for the operating system. The directory is 
58       /// guaranteed to be created on exit from this function. If the directory 
59       /// cannot be created, the function will throw an exception.
60       /// @throws std::string indicating why the directory could not be created.
61       /// @brief Constrct a path to an new, unique, existing temporary
62       /// directory.
63       static Path GetTemporaryDirectory();
64
65       /// Determine the platform-specific location of a library by first
66       /// searching a list of library paths, then searching a list of "well
67       /// known" paths for the platform. T
68       /// @returns a valid Path object if the library was found, an invalid
69       /// one otherwise.
70       /// @throws nothing
71       /// @brief Locate a library in a platform specific manner.
72       static Path GetLibraryPath(const std::string& basename, 
73                                  const std::vector<std::string>& LibPaths);
74       /// 
75       /// Construct a path to the first system library directory. The
76       /// implementation of Path on a given platform must ensure that this
77       /// directory both exists and also contains standard system libraries
78       /// suitable for linking into programs.
79       /// @throws nothing
80       /// @brief Construct a path to the first system library directory
81       static Path GetSystemLibraryPath1();
82
83       /// Construct a path to the second system library directory. The
84       /// implementation of Path on a given platform must ensure that this
85       /// directory both exists and also contains standard system libraries
86       /// suitable for linking into programs. Note that the "second" system
87       /// library directory may or may not be different from the first. 
88       /// @throws nothing
89       /// @brief Construct a path to the second system library directory
90       static Path GetSystemLibraryPath2();
91
92       /// Construct a path to the default LLVM configuration directory. The 
93       /// implementation must ensure that this is a well-known (same on many
94       /// systems) directory in which llvm configuration files exist. For 
95       /// example, on Unix, the /etc/llvm directory has been selected.
96       /// @throws nothing
97       /// @brief Construct a path to the default LLVM configuration directory
98       static Path GetLLVMDefaultConfigDir();
99
100       /// Construct a path to the LLVM installed configuration directory. The
101       /// implementation must ensure that this refers to the "etc" directory of
102       /// the LLVM installation. This is the location where configuration files
103       /// will be located for a particular installation of LLVM on a machine.
104       /// @throws nothing
105       /// @brief Construct a path to the LLVM installed configuration directory
106       static Path GetLLVMConfigDir();
107
108       /// Construct a path to the current user's home directory. The
109       /// implementation must use an operating system specific mechanism for
110       /// determining the user's home directory. For example, the environment 
111       /// variable "HOME" could be used on Unix. If a given operating system 
112       /// does not have the concept of a user's home directory, this static
113       /// constructor must provide the same result as GetRootDirectory.
114       /// @throws nothing
115       /// @brief Construct a path to the current user's "home" directory
116       static Path GetUserHomeDirectory();
117
118       /// Return the suffix commonly used on file names that contain a shared
119       /// object, shared archive, or dynamic link library. Such files are 
120       /// linked at runtime into a process and their code images are shared 
121       /// between processes. 
122       /// @returns The dynamic link library suffix for the current platform.
123       /// @brief Return the dynamic link library suffix.
124       static std::string GetDLLSuffix();
125
126       /// This is one of the very few ways in which a path can be constructed
127       /// with a syntactically invalid name. The only *legal* invalid name is an 
128       /// empty one. Other invalid names are not permitted. Empty paths are
129       /// provided so that they can be used to indicate null or error results in
130       /// other lib/System functionality.
131       /// @throws nothing
132       /// @brief Construct an empty (and invalid) path.
133       Path() : path() {}
134
135       /// This constructor will accept a std::string as a path but if verifies
136       /// that the path string has a legal syntax for the operating system on
137       /// which it is running. This allows a path to be taken in from outside
138       /// the program. However, if the path is not valid, the Path object will
139       /// be set to an empty string and an exception will be thrown.
140       /// @throws std::string if the path string is not legal.
141       /// @param unverified_path The path to verify and assign.
142       /// @brief Construct a Path from a string.
143       explicit Path(std::string unverified_path);
144
145     /// @}
146     /// @name Operators
147     /// @{
148     public:
149       /// Makes a copy of \p that to \p this.
150       /// @returns \p this
151       /// @throws nothing
152       /// @brief Assignment Operator
153       Path & operator = ( const Path & that ) {
154         path = that.path;
155         return *this;
156       }
157
158       /// Compares \p this Path with \p that Path for equality.
159       /// @returns true if \p this and \p that refer to the same thing.
160       /// @throws nothing
161       /// @brief Equality Operator
162       bool operator == (const Path& that) const {
163         return 0 == path.compare(that.path) ;
164       }
165
166       /// Compares \p this Path with \p that Path for inequality.
167       /// @returns true if \p this and \p that refer to different things.
168       /// @throws nothing
169       /// @brief Inequality Operator
170       bool operator !=( const Path & that ) const {
171         return 0 != path.compare( that.path );
172       }
173
174       /// Determines if \p this Path is less than \p that Path. This is required
175       /// so that Path objects can be placed into ordered collections (e.g.
176       /// std::map). The comparison is done lexicographically as defined by
177       /// the std::string::compare method.
178       /// @returns true if \p this path is lexicographically less than \p that.
179       /// @throws nothing
180       /// @brief Less Than Operator
181       bool operator< (const Path& that) const { 
182         return 0 > path.compare( that.path ); 
183       }
184
185     /// @}
186     /// @name Accessors
187     /// @{
188     public:
189       /// This function will use an operating system specific algorithm to
190       /// determine if the current value of \p this is a syntactically valid
191       /// path name for the operating system. The path name does not need to
192       /// exist, validity is simply syntactical. Empty paths are always invalid.
193       /// @returns true iff the path name is syntactically legal for the 
194       /// host operating system. 
195       /// @brief Determine if a path is syntactically valid or not.
196       bool isValid() const;
197
198       /// This function determines if the contents of the path name are
199       /// empty. That is, the path has a zero length.
200       /// @returns true iff the path is empty.
201       /// @brief Determines if the path name is empty (invalid).
202       bool isEmpty() const { return path.empty(); }
203
204       /// This function determines if the path name in this object is intended
205       /// to reference a legal file name (as opposed to a directory name). This
206       /// function does not verify anything with the file system, it merely
207       /// determines if the syntax of the path represents a file name or not.
208       /// @returns true if this path name references a file.
209       /// @brief Determines if the path name references a file.
210       bool isFile() const;
211
212       /// This function determines if the path name in this object is intended
213       /// to reference a legal directory name (as opposed to a file name). This
214       /// function does not verify anything with the file system, it merely
215       /// determines if the syntax of the path represents a directory name or
216       /// not.
217       /// @returns true if the path name references a directory
218       /// @brief Determines if the path name references a directory.
219       bool isDirectory() const;
220
221       /// This function determines if the path name in this object references
222       /// the root (top level directory) of the file system. The details of what
223       /// is considered the "root" may vary from system to system so this method
224       /// will do the necessary checking. 
225       /// @returns true iff the path name references the root directory.
226       /// @brief Determines if the path references the root directory.
227       bool isRootDirectory() const;
228
229       /// This function opens the file associated with the path name provided by 
230       /// the Path object and reads its magic number. If the magic number at the
231       /// start of the file matches \p magic, true is returned. In all other
232       /// cases (file not found, file not accessible, etc.) it returns false.
233       /// @returns true if the magic number of the file matches \p magic.
234       /// @brief Determine if file has a specific magic number
235       bool hasMagicNumber(const std::string& magic) const;
236
237       /// This function determines if the path name in the object references an
238       /// archive file by looking at its magic number.
239       /// @returns true if the file starts with the magic number for an archive
240       /// file.
241       /// @brief Determine if the path references an archive file.
242       bool isArchive() const;
243
244       /// This function determines if the path name in the object references an
245       /// LLVM Bytecode file by looking at its magic number.
246       /// @returns true if the file starts with the magic number for LLVM 
247       /// bytecode files.
248       /// @brief Determine if the path references a bytecode file.
249       bool isBytecodeFile() const;
250
251       /// This function determines if the path name references an existing file
252       /// or directory in the file system. Unlike isFile and isDirectory, this
253       /// function actually checks for the existence of the file or directory.
254       /// @returns true if the pathname references an existing file.
255       /// @brief Determines if the path is a file or directory in
256       /// the file system.
257       bool exists() const;
258
259       /// This function determines if the path name references a readable file
260       /// or directory in the file system. Unlike isFile and isDirectory, this 
261       /// function actually checks for the existence and readability (by the
262       /// current program) of the file or directory.
263       /// @returns true if the pathname references a readable file.
264       /// @brief Determines if the path is a readable file or directory
265       /// in the file system.
266       bool readable() const;
267
268       /// This function determines if the path name references a writable file
269       /// or directory in the file system. Unlike isFile and isDirectory, this 
270       /// function actually checks for the existence and writability (by the
271       /// current program) of the file or directory.
272       /// @returns true if the pathname references a writable file.
273       /// @brief Determines if the path is a writable file or directory
274       /// in the file system.
275       bool writable() const;
276
277       /// This function determines if the path name references an executable 
278       /// file in the file system. Unlike isFile and isDirectory, this 
279       /// function actually checks for the existence and executability (by 
280       /// the current program) of the file.
281       /// @returns true if the pathname references an executable file.
282       /// @brief Determines if the path is an executable file in the file 
283       /// system.
284       bool executable() const;
285
286       /// This function returns the current contents of the path as a
287       /// std::string. This allows the underlying path string to be manipulated
288       /// by other software.
289       /// @returns std::string containing the path name.
290       /// @brief Returns the path as a std::string.
291       std::string get() const { return path; }
292
293       /// This function returns the last component of the path name. If the
294       /// isDirectory() function would return true then this returns the name
295       /// of the last directory in the path. If the isFile() function would
296       /// return true then this function returns the name of the file without
297       /// any of the preceding directories.
298       /// @returns std::string containing the last component of the path name.
299       /// @brief Returns the last component of the path name.
300       std::string getLast() const;
301
302       /// This function strips off the path and suffix of the file name and
303       /// returns just the basename.
304       /// @returns std::string containing the basename of the path
305       /// @throws nothing
306       /// @brief Get the base name of the path
307       std::string getBasename() const;
308
309       /// @returns a c string containing the path name.
310       /// @brief Returns the path as a C string.
311       const char* const c_str() const { return path.c_str(); }
312
313     /// @}
314     /// @name Mutators
315     /// @{
316     public:
317       /// The path name is cleared and becomes empty. This is an invalid
318       /// path name but is the *only* invalid path name. This is provided
319       /// so that path objects can be used to indicate the lack of a 
320       /// valid path being found.
321       void clear() { path.clear(); }
322
323       /// This method attempts to set the Path object to \p unverified_path
324       /// and interpret the name as a directory name.  The \p unverified_path 
325       /// is verified. If verification succeeds then \p unverified_path 
326       /// is accepted as a directory and true is returned. Otherwise,
327       /// the Path object remains unchanged and false is returned.
328       /// @returns true if the path was set, false otherwise.
329       /// @param unverified_path The path to be set in Path object.
330       /// @throws nothing
331       /// @brief Set a full path from a std::string
332       bool setDirectory(const std::string& unverified_path);
333
334       /// This method attempts to set the Path object to \p unverified_path
335       /// and interpret the name as a file name.  The \p unverified_path 
336       /// is verified. If verification succeeds then \p unverified_path 
337       /// is accepted as a file name and true is returned. Otherwise,
338       /// the Path object remains unchanged and false is returned.
339       /// @returns true if the path was set, false otherwise.
340       /// @param unverified_path The path to be set in Path object.
341       /// @throws nothing
342       /// @brief Set a full path from a std::string
343       bool setFile(const std::string& unverified_path);
344
345       /// The \p dirname is added to the end of the Path if it is a legal
346       /// directory name for the operating system. The precondition for this 
347       /// function is that the Path must reference a directory name (i.e.
348       /// isDirectory() returns true).
349       /// @param dirname A string providing the directory name to
350       /// be added to the end of the path.
351       /// @returns false if the directory name could not be added
352       /// @throws nothing
353       /// @brief Adds the name of a directory to a Path.
354       bool appendDirectory( const std::string& dirname );
355
356       /// One directory component is removed from the Path name. The Path must
357       /// refer to a non-root directory name (i.e. isDirectory() returns true
358       /// but isRootDirectory() returns false). Upon exit, the Path will 
359       /// refer to the directory above it.
360       /// @throws nothing
361       /// @returns false if the directory name could not be removed.
362       /// @brief Removes the last directory component of the Path.
363       bool elideDirectory();
364
365       /// The \p filename is added to the end of the Path if it is a legal
366       /// directory name for the operating system. The precondition for this
367       /// function is that the Path reference a directory name (i.e. 
368       /// isDirectory() returns true).
369       /// @throws nothing
370       /// @returns false if the file name could not be added.
371       /// @brief Appends the name of a file.
372       bool appendFile( const std::string& filename );
373
374       /// One file component is removed from the Path name. The Path must
375       /// refer to a file (i.e. isFile() returns true). Upon exit, 
376       /// the Path will refer to the directory above it.
377       /// @throws nothing
378       /// @returns false if the file name could not be removed
379       /// @brief Removes the last file component of the path.
380       bool elideFile();
381
382       /// A period and the \p suffix are appended to the end of the pathname.
383       /// The precondition for this function is that the Path reference a file
384       /// name (i.e. isFile() returns true). If the Path is not a file, no 
385       /// action is taken and the function returns false. If the path would
386       /// become invalid for the host operating system, false is returned.
387       /// @returns false if the suffix could not be added, true if it was.
388       /// @throws nothing
389       /// @brief Adds a period and the \p suffix to the end of the pathname. 
390       bool appendSuffix(const std::string& suffix);
391
392       /// The suffix of the filename is removed. The suffix begins with and
393       /// includes the last . character in the filename after the last directory 
394       /// separator and extends until the end of the name. If no . character is
395       /// after the last directory separator, then the file name is left
396       /// unchanged (i.e. it was already without a suffix) but the function return
397       /// false.
398       /// @returns false if there was no suffix to remove, true otherwise.
399       /// @throws nothing
400       /// @brief Remove the suffix from a path name.
401       bool elideSuffix();
402
403       /// This method attempts to create a directory in the file system with the
404       /// same name as the Path object. The \p create_parents parameter controls
405       /// whether intermediate directories are created or not. if \p
406       /// create_parents is true, then an attempt will be made to create all
407       /// intermediate directories. If \p create_parents is false, then only the
408       /// final directory component of the Path name will be created. The 
409       /// created directory will have no entries. 
410       /// @returns false if the Path does not reference a directory, true 
411       /// otherwise.
412       /// @param create_parents Determines whether non-existent directory
413       /// components other than the last one (the "parents") are created or not.
414       /// @throws std::string if an error occurs.
415       /// @brief Create the directory this Path refers to.
416       bool createDirectory( bool create_parents = false );
417
418       /// This method attempts to create a file in the file system with the same 
419       /// name as the Path object. The intermediate directories must all exist
420       /// at the time this method is called. Use createDirectories to 
421       /// accomplish that. The created file will be empty upon return from this
422       /// function.
423       /// @returns false if the Path does not reference a file, true otherwise.
424       /// @throws std::string if an error occurs.
425       /// @brief Create the file this Path refers to.
426       bool createFile();
427
428       /// This method attempts to destroy the directory named by the last in 
429       /// the Path name.  If \p remove_contents is false, an attempt will be 
430       /// made to remove just the directory that this Path object refers to 
431       /// (the final Path component). If \p remove_contents is true, an attempt
432       /// will be made to remove the entire contents of the directory, 
433       /// recursively. 
434       /// @param destroy_contents Indicates whether the contents of a destroyed
435       /// directory should also be destroyed (recursively). 
436       /// @returns false if the Path does not refer to a directory, true 
437       /// otherwise.
438       /// @throws std::string if there is an error.
439       /// @brief Removes the file or directory from the filesystem.
440       bool destroyDirectory( bool destroy_contents = false );
441
442       /// This method attempts to destroy the file named by the last item in the 
443       /// Path name. 
444       /// @returns false if the Path does not refer to a file, true otherwise.
445       /// @throws std::string if there is an error.
446       /// @brief Destroy the file this Path refers to.
447       bool destroyFile(); 
448
449     /// @}
450     /// @name Data
451     /// @{
452     private:
453         std::string path; ///< Platform agnostic storage for the path name.
454
455     /// @}
456   };
457 }
458 }
459
460 // vim: sw=2
461
462 #endif