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;
116 // concatenate a string to the end of the path without inserting a
118 void SGPath::concat( const string& p ) {
119 if ( path.size() == 0 ) {
128 // Get the file part of the path (everything after the last path sep)
129 string SGPath::file() const {
130 int index = path.rfind(sgDirPathSep);
132 return path.substr(index + 1);
139 // get the directory part of the path.
140 string SGPath::dir() const {
141 int index = path.rfind(sgDirPathSep);
143 return path.substr(0, index);
149 // get the base part of the path (everything but the extension.)
150 string SGPath::base() const {
151 int index = path.rfind(".");
153 return path.substr(0, index);
159 // get the extention (everything after the final ".")
160 string SGPath::extension() const {
161 int index = path.rfind(".");
163 return path.substr(index + 1);
169 bool SGPath::exists() const {
170 FILE* fp = fopen( path.c_str(), "r");
179 string_list sgPathSplit( const string &search_path ) {
180 string tmp = search_path;
187 int index = tmp.find(sgSearchPathSep);
189 result.push_back( tmp.substr(0, index) );
190 tmp = tmp.substr( index + 1 );
192 result.push_back( tmp );