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>
32 #if defined( _MSC_VER) || defined(__MINGW32__)
35 #include "sg_path.hxx"
39 * define directory path separators
42 #if defined( macintosh )
43 static const char sgDirPathSep = ':';
44 static const char sgDirPathSepBad = '/';
46 static const char sgDirPathSep = '/';
47 static const char sgDirPathSepBad = '\\';
50 #if defined( WIN32 ) && !defined(__CYGWIN__)
51 static const char sgSearchPathSep = ';';
53 static const char sgSearchPathSep = ':';
57 // If Unix, replace all ":" with "/". If MacOS, replace all "/" with
58 // ":" it should go without saying that neither of these characters
59 // should be used in file or directory names. In windoze, allow the
60 // second character to be a ":" for things like c:\foo\bar
65 for ( string::size_type i = 0; i < path.size(); ++i ) {
67 // for windoze, don't replace the ":" for the second character
72 if ( path[i] == sgDirPathSepBad ) {
73 path[i] = sgDirPathSep;
79 // default constructor
86 // create a path based on "path"
87 SGPath::SGPath( const std::string& p )
100 void SGPath::set( const string& p ) {
106 // append another piece to the existing path
107 void SGPath::append( const string& p ) {
108 if ( path.size() == 0 ) {
111 if ( p[0] != sgDirPathSep ) {
112 path += sgDirPathSep;
119 //add a new path component to the existing path string
120 void SGPath::add( const string& p ) {
121 append( sgSearchPathSep+p );
125 // concatenate a string to the end of the path without inserting a
127 void SGPath::concat( const string& p ) {
128 if ( path.size() == 0 ) {
137 // Get the file part of the path (everything after the last path sep)
138 string SGPath::file() const {
139 int index = path.rfind(sgDirPathSep);
141 return path.substr(index + 1);
148 // get the directory part of the path.
149 string SGPath::dir() const {
150 int index = path.rfind(sgDirPathSep);
152 return path.substr(0, index);
158 // get the base part of the path (everything but the extension.)
159 string SGPath::base() const {
160 int index = path.rfind(".");
161 if ((index >= 0) && (path.find("/", index) == string::npos)) {
162 return path.substr(0, index);
168 // get the extension (everything after the final ".")
169 // but make sure no "/" follows the "." character (otherwise it
170 // is has to be a directory name containing a ".").
171 string SGPath::extension() const {
172 int index = path.rfind(".");
173 if ((index >= 0) && (path.find("/", index) == string::npos)) {
174 return path.substr(index + 1);
180 bool SGPath::exists() const {
181 FILE* fp = fopen( path.c_str(), "r");
189 #if defined( _MSC_VER) || defined(__MINGW32__)
190 # define sgMkDir(d,m) _mkdir(d)
192 # define sgMkDir(d,m) mkdir(d,m)
196 int SGPath::create_dir( mode_t mode ) {
197 string_list dirlist = sgPathSplit(dir());
198 if ( dirlist.empty() )
200 string path = dirlist[0];
201 string_list path_elements = sgPathBranchSplit(path);
202 bool absolute = !path.empty() && path[0] == sgDirPathSep;
205 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
206 dir.concat( path_elements[0] );
207 #if defined( _MSC_VER) || defined(__MINGW32__)
208 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
209 dir.append( path_elements[1] );
215 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
216 dir.append(path_elements[i]);
219 return 0; // Directory already exists
221 if ( sgMkDir( dir.c_str(), mode) ) {
222 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
225 for(; i < path_elements.size(); i++) {
226 dir.append(path_elements[i]);
227 if ( sgMkDir( dir.c_str(), mode) ) {
228 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
236 string_list sgPathBranchSplit( const string &dirpath ) {
237 string_list path_elements;
238 string element, path = dirpath;
239 while ( path.size() ) {
240 size_t p = path.find( sgDirPathSep );
241 if ( p != string::npos ) {
242 element = path.substr( 0, p );
243 path.erase( 0, p + 1 );
248 if ( element.size() )
249 path_elements.push_back( element );
251 return path_elements;
255 string_list sgPathSplit( const string &search_path ) {
256 string tmp = search_path;
263 int index = tmp.find(sgSearchPathSep);
265 result.push_back( tmp.substr(0, index) );
266 tmp = tmp.substr( index + 1 );
269 result.push_back( tmp );