Initial implementation of the Path operating system concept.
[oota-llvm.git] / include / llvm / System / Path.h
1 //===- llvm/System/Path.h ---------------------------------------*- 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
19 namespace llvm {
20 namespace sys {
21
22   /// This class provides an abstraction for the name of a path
23   /// to a file or directory in the filesystem and various basic operations
24   /// on it.
25   /// @since 1.4
26   /// @brief An abstraction for operating system paths.
27   class Path : public std::string {
28   /// @name Constructors
29   /// @{
30   public:
31     /// Creates a null (empty) path
32     /// @brief Default Constructor
33     Path () : std::string() {}
34
35     /// Creates a path from char*
36     /// @brief char* converter
37     Path ( const char * name ) : std::string(name) {
38       assert(is_valid());
39     }
40
41     /// @brief std::string converter
42     Path ( const std::string& name ) : std::string(name){
43       assert(is_valid());
44     };
45
46     /// Copies the path with copy-on-write semantics. The \p this Path
47     /// will reference \p the that Path until one of them is modified
48     /// at which point a full copy is taken before the write.
49     /// @brief Copy Constructor
50     Path ( const Path & that ) : std::string(that) {}
51
52     /// Releases storage associated with the Path object
53     /// @brief Destructor
54     ~Path ( void ) {};
55
56   /// @}
57   /// @name Operators
58   /// @{
59   public:
60     /// Makes a copy of \p that to \p this with copy-on-write semantics.
61     /// @returns \p this
62     /// @brief Assignment Operator
63     Path & operator = ( const Path & that ) {
64       this->assign (that);
65       return *this;
66     }
67
68     /// Comparies \p this Path with \p that Path for equality.
69     /// @returns true if \p this and \p that refer to the same item.
70     /// @brief Equality Operator
71     bool operator ==( const Path & that ) const {
72       return 0 == this->compare( that ) ;
73     }
74
75     /// Comparies \p this Path with \p that Path for inequality.
76     /// @returns true if \p this and \p that refer to different items.
77     /// @brief Inequality Operator
78     bool operator !=( const Path & that ) const {
79       return 0 != this->compare( that );
80     }
81
82   /// @}
83   /// @name Accessors
84   /// @{
85   public:
86     /// @returns true if the path is valid
87     /// @brief Determines if the path is valid (properly formed) or not.
88     bool is_valid() const;
89
90     /// @returns true if the path could reference a file
91     /// @brief Determines if the path is valid for a file reference.
92     bool is_file() const;
93
94     /// @returns true if the path could reference a directory
95     /// @brief Determines if the path is valid for a directory reference.
96     bool is_directory() const;
97
98     /// @brief Fills and zero terminates a buffer with the path
99     void fill( char* buffer, unsigned len ) const;
100
101   /// @}
102   /// @name Mutators
103   /// @{
104   public:
105       /// This ensures that the pathname is terminated with a /
106       /// @brief Make the path reference a directory.
107       void make_directory();
108
109       /// This ensures that the pathname is not terminated with a /
110       /// @brief Makes the path reference a file.
111       void make_file();
112
113       /// the file system.
114       /// @returns true if the pathname references an existing file.
115       /// @brief Determines if the path is a file or directory in
116       bool exists();
117
118       /// The \p dirname is added to the end of the Path.
119       /// @param dirname A string providing the directory name to
120       /// be appended to the path.
121       /// @brief Appends the name of a directory.
122       void append_directory( const std::string& dirname ) {
123         this->append( dirname );
124         make_directory();
125       }
126
127       /// The \p filename is added to the end of the Path.
128       /// @brief Appends the name of a file.
129       void append_file( const std::string& filename ) {
130         this->append( filename );
131       }
132
133       /// Directories will have no entries. Files will be zero length. If
134       /// the file or directory already exists, no error results.
135       /// @throws SystemException if any error occurs.
136       /// @brief Causes the file or directory to exist in the filesystem.
137       void create( bool create_parents = false );
138
139       void create_directory( void );
140       void create_directories( void );
141       void create_file( void );
142
143       /// Directories must be empty before they can be removed. If not,
144       /// an error will result. Files will be unlinked, even if another
145       /// process is using them.
146       /// @brief Removes the file or directory from the filesystem.
147       void remove( void );
148       void remove_directory( void );
149       void remove_file( void ); 
150
151       /// Find library.
152       void find_lib( const char * file );
153   /// @}
154   };
155 }
156 }
157
158 // vim: sw=2
159
160 #endif