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 - curt@flightgear.org
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>
31 #include "sg_path.hxx"
35 * define directory path separators
38 #if defined( macintosh )
39 static const char sgDirPathSep = ':';
40 static const char sgDirPathSepBad = '/';
42 static const char sgDirPathSep = '/';
43 static const char sgDirPathSepBad = '\\';
47 static const char sgSearchPathSep = ';';
49 static const char sgSearchPathSep = ':';
53 // If Unix, replace all ":" with "/". If MacOS, replace all "/" with
54 // ":" it should go without saying that neither of these characters
55 // should be used in file or directory names. In windoze, allow the
56 // second character to be a ":" for things like c:\foo\bar
61 for ( string::size_type i = 0; i < path.size(); ++i ) {
63 // for windoze, don't replace the ":" for the second character
68 if ( path[i] == sgDirPathSepBad ) {
69 path[i] = sgDirPathSep;
75 // default constructor
82 // create a path based on "path"
83 SGPath::SGPath( const std::string& p )
96 void SGPath::set( const string& p ) {
102 // append another piece to the existing path
103 void SGPath::append( const string& p ) {
104 if ( path.size() == 0 ) {
107 if ( p[0] != sgDirPathSep ) {
108 path += sgDirPathSep;
115 //add a new path component to the existing path string
116 void SGPath::add( const string& p ) {
117 append( sgSearchPathSep+p );
121 // concatenate a string to the end of the path without inserting a
123 void SGPath::concat( const string& p ) {
124 if ( path.size() == 0 ) {
133 // Get the file part of the path (everything after the last path sep)
134 string SGPath::file() const {
135 int index = path.rfind(sgDirPathSep);
137 return path.substr(index + 1);
144 // get the directory part of the path.
145 string SGPath::dir() const {
146 int index = path.rfind(sgDirPathSep);
148 return path.substr(0, index);
154 // get the base part of the path (everything but the extension.)
155 string SGPath::base() const {
156 int index = path.rfind(".");
158 return path.substr(0, index);
164 // get the extention (everything after the final ".")
165 string SGPath::extension() const {
166 int index = path.rfind(".");
168 return path.substr(index + 1);
174 bool SGPath::exists() const {
175 FILE* fp = fopen( path.c_str(), "r");
184 string_list sgPathSplit( const string &search_path ) {
185 string tmp = search_path;
192 int index = tmp.find(sgSearchPathSep);
194 result.push_back( tmp.substr(0, index) );
195 tmp = tmp.substr( index + 1 );
198 result.push_back( tmp );