]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Update the SoundSample api so we can request that a copy of the sample be
[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 - curt@flightgear.org
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 )
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
116 // concatenate a string to the end of the path without inserting a
117 // path separator
118 void SGPath::concat( const string& p ) {
119     if ( path.size() == 0 ) {
120         path = p;
121     } else {
122         path += p;
123     }
124     fix();
125 }
126
127
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);
131     if (index >= 0) {
132         return path.substr(index + 1);
133     } else {
134         return "";
135     }
136 }
137   
138
139 // get the directory part of the path.
140 string SGPath::dir() const {
141     int index = path.rfind(sgDirPathSep);
142     if (index >= 0) {
143         return path.substr(0, index);
144     } else {
145         return "";
146     }
147 }
148
149 // get the base part of the path (everything but the extension.)
150 string SGPath::base() const {
151     int index = path.rfind(".");
152     if (index >= 0) {
153         return path.substr(0, index);
154     } else {
155         return "";
156     }
157 }
158
159 // get the extention (everything after the final ".")
160 string SGPath::extension() const {
161     int index = path.rfind(".");
162     if (index >= 0) {
163         return path.substr(index + 1);
164     } else {
165         return "";
166     }
167 }
168
169 bool SGPath::exists() const {
170     FILE* fp = fopen( path.c_str(), "r");
171     if (fp == 0) {
172         return false;
173     }
174     fclose(fp);
175     return true;
176 }
177
178
179 string_list sgPathSplit( const string &search_path ) {
180     string tmp = search_path;
181     string_list result;
182     result.clear();
183
184     bool done = false;
185
186     while ( !done ) {
187         int index = tmp.find(sgSearchPathSep);
188         if (index >= 0) {
189             result.push_back( tmp.substr(0, index) );
190             tmp = tmp.substr( index + 1 );
191         } else {
192             if ( !tmp.empty() )
193                 result.push_back( tmp );
194             done = true;
195         }
196     }
197
198     return result;
199 }