]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / misc / sg_path.hxx
1 /**
2  * \file sg_path.hxx
3  * Routines to abstract out path separator differences between MacOS
4  * and the rest of the world.
5  */
6
7 // Written by Curtis L. Olson, started April 1999.
8 //
9 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Library General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27
28 #ifndef _SG_PATH_HXX
29 #define _SG_PATH_HXX
30
31 #include <sys/types.h>
32
33 #include <simgear/compiler.h>
34 #include <string>
35 #include <ctime>
36
37 #include <simgear/math/sg_types.hxx>
38
39 #ifdef _MSC_VER
40   typedef int mode_t;
41 #endif
42
43 /**
44  * A class to hide path separator difference across platforms and assist
45  * in managing file system path names.
46  *
47  * Paths can be input in any platform format and will be converted
48  * automatically to the proper format.
49  */
50
51 class SGPath {
52
53 public:
54
55     /** Default constructor */
56     SGPath();
57
58     /** Copy contructor */
59     SGPath(const SGPath& p);
60     
61     SGPath& operator=(const SGPath& p);
62
63     /**
64      * Construct a path based on the starting path provided.
65      * @param p initial path
66      */
67     SGPath( const std::string& p );
68
69     /**
70      * Construct a path based on the starting path provided and a relative subpath
71      * @param p initial path
72      * @param r relative subpath
73      */
74     SGPath( const SGPath& p, const std::string& r );
75
76     /** Destructor */
77     ~SGPath();
78
79     /**
80      * Set path to a new value
81      * @param p new path
82      */
83     void set( const std::string& p );
84     SGPath& operator= ( const char* p ) { this->set(p); return *this; }
85
86     bool operator==(const SGPath& other) const;
87     bool operator!=(const SGPath& other) const;
88     
89     /**
90      * Set if file information (exists, type, mod-time) is cached or
91      * retrieved each time it is queried. Caching is enabled by default
92      */
93     void set_cached(bool cached);
94     
95     /**
96      * Append another piece to the existing path.  Inserts a path
97      * separator between the existing component and the new component.
98      * @param p additional path component */
99     void append( const std::string& p );
100
101     /**
102      * Get a copy of this path with another piece appended.
103      *
104      * @param p additional path component
105      */
106     SGPath operator/( const std::string& p ) const;
107
108     /**
109      * Append a new piece to the existing path.  Inserts a search path
110      * separator to the existing path and the new patch component.
111      * @param p additional path component */
112     void add( const std::string& p );
113
114     /**
115      * Concatenate a string to the end of the path without inserting a
116      * path separator.
117      * @param p additional path suffix
118      */
119     void concat( const std::string& p );
120
121     /**
122      * Returns a string with the absolute pathname that names the same file, whose
123      * resolution does not involve '.', '..', or symbolic links.
124      */
125     std::string realpath() const;
126
127     /**
128      * Get the file part of the path (everything after the last path sep)
129      * @return file string
130      */
131     std::string file() const;
132   
133     /**
134      * Get the directory part of the path.
135      * @return directory string
136      */
137     std::string dir() const;
138   
139     /**
140      * Get the base part of the path (everything but the final extension.)
141      * @return the base string
142      */
143     std::string base() const;
144
145     /**
146      * Get the base part of the filename (everything before the first '.')
147      * @return the base filename
148      */
149     std::string file_base() const;
150
151     /**
152      * Get the extension part of the path (everything after the final ".")
153      * @return the extension string
154      */
155     std::string extension() const;
156     
157     /**
158      * Get the extension part of the path (everything after the final ".")
159      * converted to lowercase
160      * @return the extension string
161      */
162     std::string lower_extension() const;
163     
164     /**
165      * Get the complete extension part of the path (everything after the first ".")
166      * this might look like 'tar.gz' or 'txt.Z', or might be identical to 'extension' above
167      * the extension is converted to lowercase.
168      * @return the extension string
169      */
170     std::string complete_lower_extension() const;
171     
172     /**
173      * Get the path string
174      * @return path string
175      */
176     std::string str() const { return path; }
177
178     /**
179      * Get the path string
180      * @return path in "C" string (ptr to char array) form.
181      */
182     const char* c_str() const { return path.c_str(); }
183
184     /**
185      * Get the path string in OS native form
186      */
187     std::string str_native() const;
188
189     /**
190      * Determine if file exists by attempting to fopen it.
191      * @return true if file exists, otherwise returns false.
192      */
193     bool exists() const;
194
195     /**
196      * Create the designated directory.
197      * @return 0 on success, or <0 on failure.
198      */
199     int create_dir(mode_t mode);
200
201     bool isFile() const;
202     bool isDir() const;
203     
204     /**
205      * Opposite sense to isAbsolute
206      */
207     bool isRelative() const { return !isAbsolute(); }
208     
209     /**
210      * Is this an absolute path?
211      * I.e starts with a directory seperator, or a single character + colon
212      */
213     bool isAbsolute() const;
214     
215     /**
216      * check for default constructed path
217      */
218     bool isNull() const;
219     
220     /**
221      * delete the file, if possible
222      */
223     bool remove();
224     
225     /**
226      * modification time of the file
227      */
228     time_t modTime() const;
229     
230     /**
231      * rename the file / directory we point at, to a new name
232      * this may fail if the new location is on a different volume / share,
233      * or if the destination already exists, or is not writeable
234      */
235     bool rename(const SGPath& newName);
236
237     /**
238      * Get a path stored in the environment variable with the given \a name.
239      *
240      * @param name  Name of the environment variable
241      * @param def   Default path to return if the environment variable does not
242      *              exist or is empty.
243      */
244     static SGPath fromEnv(const char* name, const SGPath& def = SGPath());
245
246     /**
247      * Get path to user's home directory
248      */
249     static SGPath home();
250
251     /**
252      * Get path to the user's desktop directory
253      */
254     static SGPath desktop();
255
256 private:
257
258     void fix();
259
260     void validate() const;
261
262     std::string path;
263     
264     mutable bool _cached : 1;
265     bool _cacheEnabled : 1; ///< cacheing can be disbled if required
266     mutable bool _exists : 1;
267     mutable bool _isDir : 1;
268     mutable bool _isFile : 1;
269     mutable time_t _modTime;
270 };
271
272 /// Output to an ostream
273 template<typename char_type, typename traits_type>
274 inline
275 std::basic_ostream<char_type, traits_type>&
276 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGPath& p)
277 { return s << "Path \"" << p.str() << "\""; }
278
279
280 /**
281  * Split a directory string into a list of it's parent directories.
282  */
283 string_list sgPathBranchSplit( const std::string &path );
284
285 /**
286  * Split a directory search path into a vector of individual paths
287  */
288 string_list sgPathSplit( const std::string &search_path );
289
290
291 #endif // _SG_PATH_HXX
292
293