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.
55 /** Default constructor */
58 /** Copy contructor */
59 SGPath(const SGPath& p);
61 SGPath& operator=(const SGPath& p);
64 * Construct a path based on the starting path provided.
65 * @param p initial path
67 SGPath( const std::string& p );
70 * Construct a path based on the starting path provided and a relative subpath
71 * @param p initial path
72 * @param r relative subpath
74 SGPath( const SGPath& p, const std::string& r );
80 * Set path to a new value
83 void set( const std::string& p );
84 SGPath& operator= ( const char* p ) { this->set(p); return *this; }
86 bool operator==(const SGPath& other) const;
87 bool operator!=(const SGPath& other) const;
90 * Set if file information (exists, type, mod-time) is cached or
91 * retrieved each time it is queried. Caching is enabled by default
93 void set_cached(bool cached);
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 );
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 );
108 * Concatenate a string to the end of the path without inserting a
110 * @param p additional path suffix
112 void concat( const std::string& p );
115 * Get the file part of the path (everything after the last path sep)
116 * @return file string
118 std::string file() const;
121 * Get the directory part of the path.
122 * @return directory string
124 std::string dir() const;
127 * Get the base part of the path (everything but the extension.)
128 * @return the base string
130 std::string base() const;
133 * Get the base part of the filename (everything before the first '.')
134 * @return the base filename
136 std::string file_base() const;
139 * Get the extension part of the path (everything after the final ".")
140 * @return the extension string
142 std::string extension() const;
145 * Get the extension part of the path (everything after the final ".")
146 * converted to lowercase
147 * @return the extension string
149 std::string lower_extension() const;
152 * Get the complete extension part of the path (everything after the first ".")
153 * this might look like 'tar.gz' or 'txt.Z', or might be identical to 'extension' above
154 * the extension is converted to lowercase.
155 * @return the extension string
157 std::string complete_lower_extension() const;
160 * Get the path string
161 * @return path string
163 std::string str() const { return path; }
166 * Get the path string
167 * @return path in "C" string (ptr to char array) form.
169 const char* c_str() const { return path.c_str(); }
172 * Get the path string in OS native form
174 std::string str_native() const;
177 * Determine if file exists by attempting to fopen it.
178 * @return true if file exists, otherwise returns false.
183 * Create the designated directory.
184 * @return 0 on success, or <0 on failure.
186 int create_dir(mode_t mode);
192 * Opposite sense to isAbsolute
194 bool isRelative() const { return !isAbsolute(); }
197 * Is this an absolute path?
198 * I.e starts with a directory seperator, or a single character + colon
200 bool isAbsolute() const;
203 * check for default constructed path
208 * delete the file, if possible
213 * modification time of the file
215 time_t modTime() const;
220 void validate() const;
224 mutable bool _cached : 1;
225 bool _cacheEnabled : 1; ///< cacheing can be disbled if required
226 mutable bool _exists : 1;
227 mutable bool _isDir : 1;
228 mutable bool _isFile : 1;
229 mutable time_t _modTime;
234 * Split a directory string into a list of it's parent directories.
236 string_list sgPathBranchSplit( const std::string &path );
239 * Split a directory search path into a vector of individual paths
241 string_list sgPathSplit( const std::string &search_path );
244 #endif // _SG_PATH_HXX