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