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 if (_stat (path.c_str(), &buf ) < 0) {
219 _isFile = ((S_IFREG & buf.st_mode ) !=0);
220 _isDir = ((S_IFDIR & buf.st_mode ) !=0);
226 if (stat(path.c_str(), &buf ) < 0) {
230 _isFile = ((S_ISREG(buf.st_mode )) != 0);
231 _isDir = ((S_ISDIR(buf.st_mode )) != 0);
238 bool SGPath::exists() const
244 bool SGPath::isDir() const
247 return _exists && _isDir;
250 bool SGPath::isFile() const
253 return _exists && _isFile;
257 # define sgMkDir(d,m) _mkdir(d)
259 # define sgMkDir(d,m) mkdir(d,m)
263 int SGPath::create_dir( mode_t mode ) {
264 string_list dirlist = sgPathSplit(dir());
265 if ( dirlist.empty() )
267 string path = dirlist[0];
268 string_list path_elements = sgPathBranchSplit(path);
269 bool absolute = !path.empty() && path[0] == sgDirPathSep;
272 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
273 dir.concat( path_elements[0] );
275 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
276 dir.append( path_elements[1] );
282 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
283 dir.append(path_elements[i]);
286 return 0; // Directory already exists
288 if ( sgMkDir( dir.c_str(), mode) ) {
289 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
292 for(; i < path_elements.size(); i++) {
293 dir.append(path_elements[i]);
294 if ( sgMkDir( dir.c_str(), mode) ) {
295 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
303 string_list sgPathBranchSplit( const string &dirpath ) {
304 string_list path_elements;
305 string element, path = dirpath;
306 while ( path.size() ) {
307 size_t p = path.find( sgDirPathSep );
308 if ( p != string::npos ) {
309 element = path.substr( 0, p );
310 path.erase( 0, p + 1 );
315 if ( element.size() )
316 path_elements.push_back( element );
318 return path_elements;
322 string_list sgPathSplit( const string &search_path ) {
323 string tmp = search_path;
330 int index = tmp.find(sgSearchPathSep);
332 result.push_back( tmp.substr(0, index) );
333 tmp = tmp.substr( index + 1 );
336 result.push_back( tmp );
344 bool SGPath::isAbsolute() const
351 // detect '[A-Za-z]:/'
352 if (path.size() > 2) {
353 if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
359 return (path[0] == sgDirPathSep);
362 bool SGPath::isNull() const
364 return path.empty() || (path == "");