1 // sg_path.cxx -- routines to abstract out path separator differences
2 // between MacOS and the rest of the world
4 // Written by Curtis L. Olson, started April 1999.
6 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/compiler.h>
27 #include <simgear_config.h>
28 #include <simgear/debug/logstream.hxx>
34 #include "sg_path.hxx"
40 * define directory path separators
43 static const char sgDirPathSep = '/';
44 static const char sgDirPathSepBad = '\\';
47 static const char sgSearchPathSep = ';';
49 static const char sgSearchPathSep = ':';
53 // If Unix, replace all ":" with "/". In windoze, allow the
54 // second character to be a ":" for things like c:\foo\bar
59 for ( string::size_type i = 0; i < path.size(); ++i ) {
61 // for windoze, don't replace the ":" for the second character
66 if ( path[i] == sgDirPathSepBad ) {
67 path[i] = sgDirPathSep;
73 // default constructor
81 // create a path based on "path"
82 SGPath::SGPath( const std::string& p )
89 // create a path based on "path" and a "subpath"
90 SGPath::SGPath( const SGPath& p, const std::string& r )
98 SGPath::SGPath(const SGPath& p) :
107 SGPath& SGPath::operator=(const SGPath& p)
123 void SGPath::set( const string& p ) {
130 // append another piece to the existing path
131 void SGPath::append( const string& p ) {
132 if ( path.size() == 0 ) {
135 if ( p[0] != sgDirPathSep ) {
136 path += sgDirPathSep;
144 //add a new path component to the existing path string
145 void SGPath::add( const string& p ) {
146 append( sgSearchPathSep+p );
150 // concatenate a string to the end of the path without inserting a
152 void SGPath::concat( const string& p ) {
153 if ( path.size() == 0 ) {
163 // Get the file part of the path (everything after the last path sep)
164 string SGPath::file() const {
165 int index = path.rfind(sgDirPathSep);
167 return path.substr(index + 1);
174 // get the directory part of the path.
175 string SGPath::dir() const {
176 int index = path.rfind(sgDirPathSep);
178 return path.substr(0, index);
184 // get the base part of the path (everything but the extension.)
185 string SGPath::base() const {
186 int index = path.rfind(".");
187 if ((index >= 0) && (path.find("/", index) == string::npos)) {
188 return path.substr(0, index);
194 // get the extension (everything after the final ".")
195 // but make sure no "/" follows the "." character (otherwise it
196 // is has to be a directory name containing a ".").
197 string SGPath::extension() const {
198 int index = path.rfind(".");
199 if ((index >= 0) && (path.find("/", index) == string::npos)) {
200 return path.substr(index + 1);
206 void SGPath::validate() const
215 bool remove_trailing = false;
216 if ( path.length() > 1 && path[path.length()-1] == '/' )
217 remove_trailing=true;
218 if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
222 _isFile = ((S_IFREG & buf.st_mode ) !=0);
223 _isDir = ((S_IFDIR & buf.st_mode ) !=0);
229 if (stat(path.c_str(), &buf ) < 0) {
233 _isFile = ((S_ISREG(buf.st_mode )) != 0);
234 _isDir = ((S_ISDIR(buf.st_mode )) != 0);
241 bool SGPath::exists() const
247 bool SGPath::isDir() const
250 return _exists && _isDir;
253 bool SGPath::isFile() const
256 return _exists && _isFile;
260 # define sgMkDir(d,m) _mkdir(d)
262 # define sgMkDir(d,m) mkdir(d,m)
266 int SGPath::create_dir( mode_t mode ) {
267 string_list dirlist = sgPathSplit(dir());
268 if ( dirlist.empty() )
270 string path = dirlist[0];
271 string_list path_elements = sgPathBranchSplit(path);
272 bool absolute = !path.empty() && path[0] == sgDirPathSep;
275 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
276 dir.concat( path_elements[0] );
278 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
279 dir.append( path_elements[1] );
285 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
286 dir.append(path_elements[i]);
289 return 0; // Directory already exists
291 if ( sgMkDir( dir.c_str(), mode) ) {
292 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
295 for(; i < path_elements.size(); i++) {
296 dir.append(path_elements[i]);
297 if ( sgMkDir( dir.c_str(), mode) ) {
298 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
306 string_list sgPathBranchSplit( const string &dirpath ) {
307 string_list path_elements;
308 string element, path = dirpath;
309 while ( path.size() ) {
310 size_t p = path.find( sgDirPathSep );
311 if ( p != string::npos ) {
312 element = path.substr( 0, p );
313 path.erase( 0, p + 1 );
318 if ( element.size() )
319 path_elements.push_back( element );
321 return path_elements;
325 string_list sgPathSplit( const string &search_path ) {
326 string tmp = search_path;
333 int index = tmp.find(sgSearchPathSep);
335 result.push_back( tmp.substr(0, index) );
336 tmp = tmp.substr( index + 1 );
339 result.push_back( tmp );
347 bool SGPath::isAbsolute() const
354 // detect '[A-Za-z]:/'
355 if (path.size() > 2) {
356 if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
362 return (path[0] == sgDirPathSep);
365 bool SGPath::isNull() const
367 return path.empty() || (path == "");