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>
35 #include "sg_path.hxx"
39 * define directory path separators
42 static const char sgDirPathSep = '/';
43 static const char sgDirPathSepBad = '\\';
46 static const char sgSearchPathSep = ';';
48 static const char sgSearchPathSep = ':';
52 // If Unix, replace all ":" with "/". If MacOS, replace all "/" with
53 // ":" it should go without saying that neither of these characters
54 // should be used in file or directory names. In windoze, allow the
55 // second character to be a ":" for things like c:\foo\bar
60 for ( string::size_type i = 0; i < path.size(); ++i ) {
62 // for windoze, don't replace the ":" for the second character
67 if ( path[i] == sgDirPathSepBad ) {
68 path[i] = sgDirPathSep;
74 // default constructor
81 // create a path based on "path"
82 SGPath::SGPath( const std::string& p )
95 void SGPath::set( const string& p ) {
101 // append another piece to the existing path
102 void SGPath::append( const string& p ) {
103 if ( path.size() == 0 ) {
106 if ( p[0] != sgDirPathSep ) {
107 path += sgDirPathSep;
114 //add a new path component to the existing path string
115 void SGPath::add( const string& p ) {
116 append( sgSearchPathSep+p );
120 // concatenate a string to the end of the path without inserting a
122 void SGPath::concat( const string& p ) {
123 if ( path.size() == 0 ) {
132 // Get the file part of the path (everything after the last path sep)
133 string SGPath::file() const {
134 int index = path.rfind(sgDirPathSep);
136 return path.substr(index + 1);
143 // get the directory part of the path.
144 string SGPath::dir() const {
145 int index = path.rfind(sgDirPathSep);
147 return path.substr(0, index);
153 // get the base part of the path (everything but the extension.)
154 string SGPath::base() const {
155 int index = path.rfind(".");
156 if ((index >= 0) && (path.find("/", index) == string::npos)) {
157 return path.substr(0, index);
163 // get the extension (everything after the final ".")
164 // but make sure no "/" follows the "." character (otherwise it
165 // is has to be a directory name containing a ".").
166 string SGPath::extension() const {
167 int index = path.rfind(".");
168 if ((index >= 0) && (path.find("/", index) == string::npos)) {
169 return path.substr(index + 1);
175 bool SGPath::exists() const {
176 FILE* fp = fopen( path.c_str(), "r");
185 # define sgMkDir(d,m) _mkdir(d)
187 # define sgMkDir(d,m) mkdir(d,m)
191 int SGPath::create_dir( mode_t mode ) {
192 string_list dirlist = sgPathSplit(dir());
193 if ( dirlist.empty() )
195 string path = dirlist[0];
196 string_list path_elements = sgPathBranchSplit(path);
197 bool absolute = !path.empty() && path[0] == sgDirPathSep;
200 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
201 dir.concat( path_elements[0] );
203 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
204 dir.append( path_elements[1] );
210 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
211 dir.append(path_elements[i]);
214 return 0; // Directory already exists
216 if ( sgMkDir( dir.c_str(), mode) ) {
217 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
220 for(; i < path_elements.size(); i++) {
221 dir.append(path_elements[i]);
222 if ( sgMkDir( dir.c_str(), mode) ) {
223 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
231 string_list sgPathBranchSplit( const string &dirpath ) {
232 string_list path_elements;
233 string element, path = dirpath;
234 while ( path.size() ) {
235 size_t p = path.find( sgDirPathSep );
236 if ( p != string::npos ) {
237 element = path.substr( 0, p );
238 path.erase( 0, p + 1 );
243 if ( element.size() )
244 path_elements.push_back( element );
246 return path_elements;
250 string_list sgPathSplit( const string &search_path ) {
251 string tmp = search_path;
258 int index = tmp.find(sgSearchPathSep);
260 result.push_back( tmp.substr(0, index) );
261 tmp = tmp.substr( index + 1 );
264 result.push_back( tmp );