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 Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
26 #include <simgear/compiler.h>
28 #include <simgear_config.h>
29 #include <simgear/debug/logstream.hxx>
33 #if defined( _MSC_VER) || defined(__MINGW32__)
36 #include "sg_path.hxx"
40 * define directory path separators
43 #if defined( macintosh )
44 static const char sgDirPathSep = ':';
45 static const char sgDirPathSepBad = '/';
47 static const char sgDirPathSep = '/';
48 static const char sgDirPathSepBad = '\\';
51 #if defined( WIN32 ) && !defined(__CYGWIN__)
52 static const char sgSearchPathSep = ';';
54 static const char sgSearchPathSep = ':';
58 // If Unix, replace all ":" with "/". If MacOS, replace all "/" with
59 // ":" it should go without saying that neither of these characters
60 // should be used in file or directory names. In windoze, allow the
61 // second character to be a ":" for things like c:\foo\bar
66 for ( string::size_type i = 0; i < path.size(); ++i ) {
68 // for windoze, don't replace the ":" for the second character
73 if ( path[i] == sgDirPathSepBad ) {
74 path[i] = sgDirPathSep;
80 // default constructor
87 // create a path based on "path"
88 SGPath::SGPath( const std::string& p )
101 void SGPath::set( const string& p ) {
107 // append another piece to the existing path
108 void SGPath::append( const string& p ) {
109 if ( path.size() == 0 ) {
112 if ( p[0] != sgDirPathSep ) {
113 path += sgDirPathSep;
120 //add a new path component to the existing path string
121 void SGPath::add( const string& p ) {
122 append( sgSearchPathSep+p );
126 // concatenate a string to the end of the path without inserting a
128 void SGPath::concat( const string& p ) {
129 if ( path.size() == 0 ) {
138 // Get the file part of the path (everything after the last path sep)
139 string SGPath::file() const {
140 int index = path.rfind(sgDirPathSep);
142 return path.substr(index + 1);
149 // get the directory part of the path.
150 string SGPath::dir() const {
151 int index = path.rfind(sgDirPathSep);
153 return path.substr(0, index);
159 // get the base part of the path (everything but the extension.)
160 string SGPath::base() const {
161 int index = path.rfind(".");
162 if ((index >= 0) && (path.find("/", index) == string::npos)) {
163 return path.substr(0, index);
169 // get the extention (everything after the final ".")
170 // but make sure no "/" follows the "." character (otherwise it
171 // is has to be a directory name containing a ".").
172 string SGPath::extension() const {
173 int index = path.rfind(".");
174 if ((index >= 0) && (path.find("/", index) == string::npos)) {
175 return path.substr(index + 1);
181 bool SGPath::exists() const {
182 FILE* fp = fopen( path.c_str(), "r");
190 #if defined( _MSC_VER) || defined(__MINGW32__)
191 # define sgMkDir(d,m) _mkdir(d)
193 # define sgMkDir(d,m) mkdir(d,m)
197 void SGPath::create_dir( mode_t mode ) {
198 string_list dirlist = sgPathSplit(dir());
199 string path = dirlist[0];
200 string_list path_elements = sgPathBranchSplit(path);
201 bool absolute = !path.empty() && path[0] == sgDirPathSep;
204 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
205 dir.concat( path_elements[0] );
206 #if defined( _MSC_VER) || defined(__MINGW32__)
207 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
208 dir.append( path_elements[1] );
214 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
215 dir.append(path_elements[i]);
218 return; // Directory already exists
220 if ( sgMkDir( dir.c_str(), mode) ) {
221 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
224 for(;i < path_elements.size(); i++) {
225 dir.append(path_elements[i]);
226 if ( sgMkDir( dir.c_str(), mode) ) {
227 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
233 string_list sgPathBranchSplit( const string &dirpath ) {
234 string_list path_elements;
235 string element, path = dirpath;
236 while ( path.size() ) {
237 size_t p = path.find( sgDirPathSep );
238 if ( p != string::npos ) {
239 element = path.substr( 0, p );
240 path.erase( 0, p + 1 );
245 if ( element.size() )
246 path_elements.push_back( element );
248 return path_elements;
252 string_list sgPathSplit( const string &search_path ) {
253 string tmp = search_path;
260 int index = tmp.find(sgSearchPathSep);
262 result.push_back( tmp.substr(0, index) );
263 tmp = tmp.substr( index + 1 );
266 result.push_back( tmp );