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>
36 #include <simgear/math/sg_types.hxx>
43 * A class to hide path separator difference across platforms and assist
44 * in managing file system path names.
46 * Paths can be input in any platform format and will be converted
47 * automatically to the proper format.
58 /** Default constructor */
61 SGPath(const SGPath& p);
63 SGPath& operator=(const SGPath& p);
66 * Construct a path based on the starting path provided.
67 * @param p initial path
69 SGPath( const std::string& p );
75 * Set path to a new value
78 void set( const std::string& p );
79 SGPath& operator= ( const char* p ) { this->set(p); return *this; }
82 * Append another piece to the existing path. Inserts a path
83 * separator between the existing component and the new component.
84 * @param p additional path component */
85 void append( const std::string& p );
88 * Append a new piece to the existing path. Inserts a search path
89 * separator to the existing path and the new patch component.
90 * @param p additional path component */
91 void add( const std::string& p );
94 * Concatenate a string to the end of the path without inserting a
96 * @param p additional path suffix
98 void concat( const std::string& p );
101 * Get the file part of the path (everything after the last path sep)
102 * @return file string
104 std::string file() const;
107 * Get the directory part of the path.
108 * @return directory string
110 std::string dir() const;
113 * Get the base part of the path (everything but the extension.)
114 * @return the base string
116 std::string base() const;
119 * Get the extension part of the path (everything after the final ".")
120 * @return the extension string
122 std::string extension() const;
125 * Get the path string
126 * @return path string
128 std::string str() const { return path; }
131 * Get the path string
132 * @return path in "C" string (ptr to char array) form.
134 const char* c_str() const { return path.c_str(); }
137 * Determine if file exists by attempting to fopen it.
138 * @return true if file exists, otherwise returns false.
143 * Create the designated directory.
144 * @return 0 on success, or <0 on failure.
146 int create_dir(mode_t mode);
152 * Opposite sense to isAbsolute
154 bool isRelative() const { return !isAbsolute(); }
157 * Is this an absolute path?
158 * I.e starts with a directory seperator, or a single character + colon
160 bool isAbsolute() const;
165 void validate() const;
167 mutable bool _cached;
168 mutable bool _exists;
170 mutable bool _isFile;
175 * Split a directory string into a list of it's parent directories.
177 string_list sgPathBranchSplit( const std::string &path );
180 * Split a directory search path into a vector of individual paths
182 string_list sgPathSplit( const std::string &search_path );
185 #endif // _SG_PATH_HXX