3 * Routines to abstract out path separator differences between MacOS
4 * and the rest of the world.
7 // Written by Curtis L. Olson, started April 1999.
9 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
31 #include <sys/types.h>
33 #include <simgear/compiler.h>
37 #include <simgear/math/sg_types.hxx>
44 * A class to hide path separator difference across platforms and assist
45 * in managing file system path names.
47 * Paths can be input in any platform format and will be converted
48 * automatically to the proper format.
60 typedef Permissions (*PermissonChecker)(const SGPath&);
62 /** Default constructor */
63 explicit SGPath(PermissonChecker validator = NULL);
65 /** Copy contructor */
66 SGPath(const SGPath& p);
68 SGPath& operator=(const SGPath& p);
71 * Construct a path based on the starting path provided.
72 * @param p initial path
74 SGPath( const std::string& p, PermissonChecker validator = NULL );
77 * Construct a path based on the starting path provided and a relative subpath
78 * @param p initial path
79 * @param r relative subpath
81 SGPath( const SGPath& p,
83 PermissonChecker validator = NULL );
89 * Set path to a new value
92 void set( const std::string& p );
93 SGPath& operator= ( const char* p ) { this->set(p); return *this; }
95 bool operator==(const SGPath& other) const;
96 bool operator!=(const SGPath& other) const;
98 void setPermissonChecker(PermissonChecker validator);
99 PermissonChecker getPermissonChecker() const;
102 * Set if file information (exists, type, mod-time) is cached or
103 * retrieved each time it is queried. Caching is enabled by default
105 void set_cached(bool cached);
108 * Append another piece to the existing path. Inserts a path
109 * separator between the existing component and the new component.
110 * @param p additional path component */
111 void append( const std::string& p );
114 * Get a copy of this path with another piece appended.
116 * @param p additional path component
118 SGPath operator/( const std::string& p ) const;
121 * Append a new piece to the existing path. Inserts a search path
122 * separator to the existing path and the new patch component.
123 * @param p additional path component */
124 void add( const std::string& p );
127 * Concatenate a string to the end of the path without inserting a
129 * @param p additional path suffix
131 void concat( const std::string& p );
134 * Returns a string with the absolute pathname that names the same file, whose
135 * resolution does not involve '.', '..', or symbolic links.
137 std::string realpath() const;
140 * Get the file part of the path (everything after the last path sep)
141 * @return file string
143 std::string file() const;
146 * Get the directory part of the path.
147 * @return directory string
149 std::string dir() const;
152 * Get the base part of the path (everything but the final extension.)
153 * @return the base string
155 std::string base() const;
158 * Get the base part of the filename (everything before the first '.')
159 * @return the base filename
161 std::string file_base() const;
164 * Get the extension part of the path (everything after the final ".")
165 * @return the extension string
167 std::string extension() const;
170 * Get the extension part of the path (everything after the final ".")
171 * converted to lowercase
172 * @return the extension string
174 std::string lower_extension() const;
177 * Get the complete extension part of the path (everything after the first ".")
178 * this might look like 'tar.gz' or 'txt.Z', or might be identical to 'extension' above
179 * the extension is converted to lowercase.
180 * @return the extension string
182 std::string complete_lower_extension() const;
185 * Get the path string
186 * @return path string
188 std::string str() const { return path; }
191 * Get the path string
192 * @return path in "C" string (ptr to char array) form.
194 const char* c_str() const { return path.c_str(); }
197 * Get the path string in OS native form
199 std::string str_native() const;
202 * Determine if file exists by attempting to fopen it.
203 * @return true if file exists, otherwise returns false.
208 * Create the designated directory.
209 * @return 0 on success, or <0 on failure.
211 int create_dir(mode_t mode);
214 * Check if reading file is allowed. Readabilty does not imply the existance
217 * @note By default all files will be marked as readable. No check is made
218 * if the operating system allows the given file to be read. Derived
219 * classes may actually implement custom read/write rights.
221 bool canRead() const;
222 bool canWrite() const;
228 * Opposite sense to isAbsolute
230 bool isRelative() const { return !isAbsolute(); }
233 * Is this an absolute path?
234 * I.e starts with a directory seperator, or a single character + colon
236 bool isAbsolute() const;
239 * check for default constructed path
244 * delete the file, if possible
249 * modification time of the file
251 time_t modTime() const;
254 * rename the file / directory we point at, to a new name
255 * this may fail if the new location is on a different volume / share,
256 * or if the destination already exists, or is not writeable
258 bool rename(const SGPath& newName);
261 * Get a path stored in the environment variable with the given \a name.
263 * @param name Name of the environment variable
264 * @param def Default path to return if the environment variable does not
267 static SGPath fromEnv(const char* name, const SGPath& def = SGPath());
270 * Get path to user's home directory
272 static SGPath home(const SGPath& def = SGPath());
275 * Get path to the user's desktop directory
277 static SGPath desktop(const SGPath& def = SGPath());
283 void validate() const;
284 void checkAccess() const;
287 PermissonChecker _permission_checker;
289 mutable bool _cached : 1;
290 mutable bool _rwCached : 1;
291 bool _cacheEnabled : 1; ///< cacheing can be disbled if required
292 mutable bool _canRead : 1;
293 mutable bool _canWrite : 1;
294 mutable bool _exists : 1;
295 mutable bool _isDir : 1;
296 mutable bool _isFile : 1;
297 mutable time_t _modTime;
300 /// Output to an ostream
301 template<typename char_type, typename traits_type>
303 std::basic_ostream<char_type, traits_type>&
304 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGPath& p)
305 { return s << "Path \"" << p.str() << "\""; }
309 * Split a directory string into a list of it's parent directories.
311 string_list sgPathBranchSplit( const std::string &path );
314 * Split a directory search path into a vector of individual paths
316 string_list sgPathSplit( const std::string &search_path );
319 #endif // _SG_PATH_HXX