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>
34 #include "sg_path.hxx"
38 * define directory path separators
41 static const char sgDirPathSep = '/';
42 static const char sgDirPathSepBad = '\\';
45 static const char sgSearchPathSep = ';';
47 static const char sgSearchPathSep = ':';
51 // If Unix, replace all ":" with "/". In windoze, allow the
52 // second character to be a ":" for things like c:\foo\bar
57 for ( string::size_type i = 0; i < path.size(); ++i ) {
59 // for windoze, don't replace the ":" for the second character
64 if ( path[i] == sgDirPathSepBad ) {
65 path[i] = sgDirPathSep;
71 // default constructor
78 // create a path based on "path"
79 SGPath::SGPath( const std::string& p )
92 void SGPath::set( const string& p ) {
98 // append another piece to the existing path
99 void SGPath::append( const string& p ) {
100 if ( path.size() == 0 ) {
103 if ( p[0] != sgDirPathSep ) {
104 path += sgDirPathSep;
111 //add a new path component to the existing path string
112 void SGPath::add( const string& p ) {
113 append( sgSearchPathSep+p );
117 // concatenate a string to the end of the path without inserting a
119 void SGPath::concat( const string& p ) {
120 if ( path.size() == 0 ) {
129 // Get the file part of the path (everything after the last path sep)
130 string SGPath::file() const {
131 int index = path.rfind(sgDirPathSep);
133 return path.substr(index + 1);
140 // get the directory part of the path.
141 string SGPath::dir() const {
142 int index = path.rfind(sgDirPathSep);
144 return path.substr(0, index);
150 // get the base part of the path (everything but the extension.)
151 string SGPath::base() const {
152 int index = path.rfind(".");
153 if ((index >= 0) && (path.find("/", index) == string::npos)) {
154 return path.substr(0, index);
160 // get the extension (everything after the final ".")
161 // but make sure no "/" follows the "." character (otherwise it
162 // is has to be a directory name containing a ".").
163 string SGPath::extension() const {
164 int index = path.rfind(".");
165 if ((index >= 0) && (path.find("/", index) == string::npos)) {
166 return path.substr(index + 1);
172 bool SGPath::exists() const
177 if (_stat(path.c_str(), &buf) < 0) {
185 if (stat(path.c_str(), &buf) < 0) {
194 # define sgMkDir(d,m) _mkdir(d)
196 # define sgMkDir(d,m) mkdir(d,m)
200 int SGPath::create_dir( mode_t mode ) {
201 string_list dirlist = sgPathSplit(dir());
202 if ( dirlist.empty() )
204 string path = dirlist[0];
205 string_list path_elements = sgPathBranchSplit(path);
206 bool absolute = !path.empty() && path[0] == sgDirPathSep;
209 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
210 dir.concat( path_elements[0] );
212 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
213 dir.append( path_elements[1] );
219 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
220 dir.append(path_elements[i]);
223 return 0; // Directory already exists
225 if ( sgMkDir( dir.c_str(), mode) ) {
226 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
229 for(; i < path_elements.size(); i++) {
230 dir.append(path_elements[i]);
231 if ( sgMkDir( dir.c_str(), mode) ) {
232 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
240 string_list sgPathBranchSplit( const string &dirpath ) {
241 string_list path_elements;
242 string element, path = dirpath;
243 while ( path.size() ) {
244 size_t p = path.find( sgDirPathSep );
245 if ( p != string::npos ) {
246 element = path.substr( 0, p );
247 path.erase( 0, p + 1 );
252 if ( element.size() )
253 path_elements.push_back( element );
255 return path_elements;
259 string_list sgPathSplit( const string &search_path ) {
260 string tmp = search_path;
267 int index = tmp.find(sgSearchPathSep);
269 result.push_back( tmp.substr(0, index) );
270 tmp = tmp.substr( index + 1 );
273 result.push_back( tmp );