]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.hxx
Make tsync part of libSimGearCore when building shared libraries
[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      * Append a new piece to the existing path.  Inserts a search path
103      * separator to the existing path and the new patch component.
104      * @param p additional path component */
105     void add( const std::string& p );
106
107     /**
108      * Concatenate a string to the end of the path without inserting a
109      * path separator.
110      * @param p additional path suffix
111      */
112     void concat( const std::string& p );
113
114     /**
115      * Returns a string with the absolute pathname that names the same file, whose
116      * resolution does not involve '.', '..', or symbolic links.
117      */
118     std::string realpath() const;
119
120     /**
121      * Get the file part of the path (everything after the last path sep)
122      * @return file string
123      */
124     std::string file() const;
125   
126     /**
127      * Get the directory part of the path.
128      * @return directory string
129      */
130     std::string dir() const;
131   
132     /**
133      * Get the base part of the path (everything but the final extension.)
134      * @return the base string
135      */
136     std::string base() const;
137
138     /**
139      * Get the base part of the filename (everything before the first '.')
140      * @return the base filename
141      */
142     std::string file_base() const;
143
144     /**
145      * Get the extension part of the path (everything after the final ".")
146      * @return the extension string
147      */
148     std::string extension() const;
149     
150     /**
151      * Get the extension part of the path (everything after the final ".")
152      * converted to lowercase
153      * @return the extension string
154      */
155     std::string lower_extension() const;
156     
157     /**
158      * Get the complete extension part of the path (everything after the first ".")
159      * this might look like 'tar.gz' or 'txt.Z', or might be identical to 'extension' above
160      * the extension is converted to lowercase.
161      * @return the extension string
162      */
163     std::string complete_lower_extension() const;
164     
165     /**
166      * Get the path string
167      * @return path string
168      */
169     std::string str() const { return path; }
170
171     /**
172      * Get the path string
173      * @return path in "C" string (ptr to char array) form.
174      */
175     const char* c_str() const { return path.c_str(); }
176
177     /**
178      * Get the path string in OS native form
179      */
180     std::string str_native() const;
181
182     /**
183      * Determine if file exists by attempting to fopen it.
184      * @return true if file exists, otherwise returns false.
185      */
186     bool exists() const;
187
188     /**
189      * Create the designated directory.
190      * @return 0 on success, or <0 on failure.
191      */
192     int create_dir(mode_t mode);
193
194     bool isFile() const;
195     bool isDir() const;
196     
197     /**
198      * Opposite sense to isAbsolute
199      */
200     bool isRelative() const { return !isAbsolute(); }
201     
202     /**
203      * Is this an absolute path?
204      * I.e starts with a directory seperator, or a single character + colon
205      */
206     bool isAbsolute() const;
207     
208     /**
209      * check for default constructed path
210      */
211     bool isNull() const;
212     
213     /**
214      * delete the file, if possible
215      */
216     bool remove();
217     
218     /**
219      * modification time of the file
220      */
221     time_t modTime() const;
222     
223     /**
224      * rename the file / directory we point at, to a new name
225      * this may fail if the new location is on a different volume / share,
226      * or if the destination already exists, or is not writeable
227      */
228     bool rename(const SGPath& newName);
229 private:
230
231     void fix();
232
233     void validate() const;
234
235     std::string path;
236     
237     mutable bool _cached : 1;
238     bool _cacheEnabled : 1; ///< cacheing can be disbled if required
239     mutable bool _exists : 1;
240     mutable bool _isDir : 1;
241     mutable bool _isFile : 1;
242     mutable time_t _modTime;
243 };
244
245 /// Output to an ostream
246 template<typename char_type, typename traits_type>
247 inline
248 std::basic_ostream<char_type, traits_type>&
249 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGPath& p)
250 { return s << "Path \"" << p.str() << "\""; }
251
252
253 /**
254  * Split a directory string into a list of it's parent directories.
255  */
256 string_list sgPathBranchSplit( const std::string &path );
257
258 /**
259  * Split a directory search path into a vector of individual paths
260  */
261 string_list sgPathSplit( const std::string &search_path );
262
263
264 #endif // _SG_PATH_HXX
265
266