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 extention (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 void SGPath::create_dir( mode_t mode ) {
197 string_list dirlist = sgPathSplit(dir());
198 string path = dirlist[0];
199 string_list path_elements = sgPathBranchSplit(path);
200 bool absolute = !path.empty() && path[0] == sgDirPathSep;
203 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
204 dir.concat( path_elements[0] );
205 #if defined( _MSC_VER) || defined(__MINGW32__)
206 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
207 dir.append( path_elements[1] );
213 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
214 dir.append(path_elements[i]);
217 return; // Directory already exists
219 if ( sgMkDir( dir.c_str(), mode) ) {
220 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
223 for(;i < path_elements.size(); i++) {
224 dir.append(path_elements[i]);
225 if ( sgMkDir( dir.c_str(), mode) ) {
226 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
232 string_list sgPathBranchSplit( const string &dirpath ) {
233 string_list path_elements;
234 string element, path = dirpath;
235 while ( path.size() ) {
236 size_t p = path.find( sgDirPathSep );
237 if ( p != string::npos ) {
238 element = path.substr( 0, p );
239 path.erase( 0, p + 1 );
244 if ( element.size() )
245 path_elements.push_back( element );
247 return path_elements;
251 string_list sgPathSplit( const string &search_path ) {
252 string tmp = search_path;
259 int index = tmp.find(sgSearchPathSep);
261 result.push_back( tmp.substr(0, index) );
262 tmp = tmp.substr( index + 1 );
265 result.push_back( tmp );