]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
MSVC fix.
[simgear.git] / simgear / misc / sg_path.cxx
1 // sg_path.cxx -- routines to abstract out path separator differences
2 //               between MacOS and the rest of the world
3 //
4 // Written by Curtis L. Olson, started April 1999.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
22 //
23 // $Id$
24
25
26 #include <simgear/compiler.h>
27
28 #include <simgear_config.h>
29 #include <stdio.h>
30
31 #include "sg_path.hxx"
32
33
34 /**
35  * define directory path separators
36  */
37
38 #if defined( macintosh )
39 static const char sgDirPathSep = ':';
40 static const char sgDirPathSepBad = '/';
41 #else
42 static const char sgDirPathSep = '/';
43 static const char sgDirPathSepBad = '\\';
44 #endif
45
46 #if defined( WIN32 ) && !defined(__CYGWIN__)
47 static const char sgSearchPathSep = ';';
48 #else
49 static const char sgSearchPathSep = ':';
50 #endif
51
52
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
57
58 void
59 SGPath::fix()
60 {
61     for ( string::size_type i = 0; i < path.size(); ++i ) {
62 #if defined( WIN32 )
63         // for windoze, don't replace the ":" for the second character
64         if ( i == 1 ) {
65             continue;
66         }
67 #endif
68         if ( path[i] == sgDirPathSepBad ) {
69             path[i] = sgDirPathSep;
70         }
71     }
72 }
73
74
75 // default constructor
76 SGPath::SGPath()
77     : path("")
78 {
79 }
80
81
82 // create a path based on "path"
83 SGPath::SGPath( const std::string& p )
84     : path(p)
85 {
86     fix();
87 }
88
89
90 // destructor
91 SGPath::~SGPath() {
92 }
93
94
95 // set path
96 void SGPath::set( const string& p ) {
97     path = p;
98     fix();
99 }
100
101
102 // append another piece to the existing path
103 void SGPath::append( const string& p ) {
104     if ( path.size() == 0 ) {
105         path = p;
106     } else {
107         if ( p[0] != sgDirPathSep ) {
108             path += sgDirPathSep;
109         }
110         path += p;
111     }
112     fix();
113 }
114
115 //add a new path component to the existing path string
116 void SGPath::add( const string& p ) {
117     append( sgSearchPathSep+p );
118 }
119
120
121 // concatenate a string to the end of the path without inserting a
122 // path separator
123 void SGPath::concat( const string& p ) {
124     if ( path.size() == 0 ) {
125         path = p;
126     } else {
127         path += p;
128     }
129     fix();
130 }
131
132
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);
136     if (index >= 0) {
137         return path.substr(index + 1);
138     } else {
139         return "";
140     }
141 }
142   
143
144 // get the directory part of the path.
145 string SGPath::dir() const {
146     int index = path.rfind(sgDirPathSep);
147     if (index >= 0) {
148         return path.substr(0, index);
149     } else {
150         return "";
151     }
152 }
153
154 // get the base part of the path (everything but the extension.)
155 string SGPath::base() const {
156     int index = path.rfind(".");
157     if ((index >= 0) && (path.find("/", index) == string::npos)) {
158         return path.substr(0, index);
159     } else {
160         return "";
161     }
162 }
163
164 // get the extention (everything after the final ".")
165 // but make sure no "/" follows the "." character (otherwise it
166 // is has to be a directory name containing a ".").
167 string SGPath::extension() const {
168     int index = path.rfind(".");
169     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
170         return path.substr(index + 1);
171     } else {
172         return "";
173     }
174 }
175
176 bool SGPath::exists() const {
177     FILE* fp = fopen( path.c_str(), "r");
178     if (fp == 0) {
179         return false;
180     }
181     fclose(fp);
182     return true;
183 }
184
185
186 string_list sgPathSplit( const string &search_path ) {
187     string tmp = search_path;
188     string_list result;
189     result.clear();
190
191     bool done = false;
192
193     while ( !done ) {
194         int index = tmp.find(sgSearchPathSep);
195         if (index >= 0) {
196             result.push_back( tmp.substr(0, index) );
197             tmp = tmp.substr( index + 1 );
198         } else {
199             if ( !tmp.empty() )
200                 result.push_back( tmp );
201             done = true;
202         }
203     }
204
205     return result;
206 }