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>
34 #include "sg_path.hxx"
38 * define directory path separators
41 #if defined( macintosh )
42 static const char sgDirPathSep = ':';
43 static const char sgDirPathSepBad = '/';
45 static const char sgDirPathSep = '/';
46 static const char sgDirPathSepBad = '\\';
49 #if defined( WIN32 ) && !defined(__CYGWIN__)
50 static const char sgSearchPathSep = ';';
52 static const char sgSearchPathSep = ':';
56 // If Unix, replace all ":" with "/". If MacOS, replace all "/" with
57 // ":" it should go without saying that neither of these characters
58 // should be used in file or directory names. In windoze, allow the
59 // second character to be a ":" for things like c:\foo\bar
64 for ( string::size_type i = 0; i < path.size(); ++i ) {
66 // for windoze, don't replace the ":" for the second character
71 if ( path[i] == sgDirPathSepBad ) {
72 path[i] = sgDirPathSep;
78 // default constructor
85 // create a path based on "path"
86 SGPath::SGPath( const std::string& p )
99 void SGPath::set( const string& p ) {
105 // append another piece to the existing path
106 void SGPath::append( const string& p ) {
107 if ( path.size() == 0 ) {
110 if ( p[0] != sgDirPathSep ) {
111 path += sgDirPathSep;
118 //add a new path component to the existing path string
119 void SGPath::add( const string& p ) {
120 append( sgSearchPathSep+p );
124 // concatenate a string to the end of the path without inserting a
126 void SGPath::concat( const string& p ) {
127 if ( path.size() == 0 ) {
136 // Get the file part of the path (everything after the last path sep)
137 string SGPath::file() const {
138 int index = path.rfind(sgDirPathSep);
140 return path.substr(index + 1);
147 // get the directory part of the path.
148 string SGPath::dir() const {
149 int index = path.rfind(sgDirPathSep);
151 return path.substr(0, index);
157 // get the base part of the path (everything but the extension.)
158 string SGPath::base() const {
159 int index = path.rfind(".");
160 if ((index >= 0) && (path.find("/", index) == string::npos)) {
161 return path.substr(0, index);
167 // get the extention (everything after the final ".")
168 // but make sure no "/" follows the "." character (otherwise it
169 // is has to be a directory name containing a ".").
170 string SGPath::extension() const {
171 int index = path.rfind(".");
172 if ((index >= 0) && (path.find("/", index) == string::npos)) {
173 return path.substr(index + 1);
179 bool SGPath::exists() const {
180 FILE* fp = fopen( path.c_str(), "r");
189 void SGPath::create_dir( mode_t mode ) {
190 string_list dirlist = sgPathSplit(dir());
191 SGPath dir = dirlist[0];
193 for(i=1; dir.exists() && i < dirlist.size(); i++) {
196 for(;i < dirlist.size(); i++) {
197 string subdir = dirlist[i];
198 if ( mkdir( subdir.c_str(), mode) ) {
199 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
206 string_list sgPathSplit( const string &search_path ) {
207 string tmp = search_path;
214 int index = tmp.find(sgSearchPathSep);
216 result.push_back( tmp.substr(0, index) );
217 tmp = tmp.substr( index + 1 );
220 result.push_back( tmp );