]> git.mxchange.org Git - simgear.git/blob - simgear/misc/sg_path.cxx
Make get_subsystem safe during destruction of the manager.
[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 General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #include <simgear/compiler.h>
26
27 #include <simgear_config.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #ifdef _WIN32
32 #  include <direct.h>
33 #endif
34 #include "sg_path.hxx"
35
36 using std::string;
37
38
39 /**
40  * define directory path separators
41  */
42
43 static const char sgDirPathSep = '/';
44 static const char sgDirPathSepBad = '\\';
45
46 #ifdef _WIN32
47 static const char sgSearchPathSep = ';';
48 #else
49 static const char sgSearchPathSep = ':';
50 #endif
51
52
53 // If Unix, replace all ":" with "/".  In windoze, allow the
54 // second character to be a ":" for things like c:\foo\bar
55
56 void
57 SGPath::fix()
58 {
59     for ( string::size_type i = 0; i < path.size(); ++i ) {
60 #if defined( WIN32 )
61         // for windoze, don't replace the ":" for the second character
62         if ( i == 1 ) {
63             continue;
64         }
65 #endif
66         if ( path[i] == sgDirPathSepBad ) {
67             path[i] = sgDirPathSep;
68         }
69     }
70 }
71
72
73 // default constructor
74 SGPath::SGPath()
75     : path(""),
76     _cached(false)
77 {
78 }
79
80
81 // create a path based on "path"
82 SGPath::SGPath( const std::string& p )
83     : path(p),
84     _cached(false)
85 {
86     fix();
87 }
88
89 // create a path based on "path" and a "subpath"
90 SGPath::SGPath( const SGPath& p, const std::string& r )
91     : path(p.path),
92     _cached(false)
93 {
94     append(r);
95     fix();
96 }
97
98 SGPath::SGPath(const SGPath& p) :
99   path(p.path),
100   _cached(p._cached),
101   _exists(p._exists),
102   _isDir(p._isDir),
103   _isFile(p._isFile)
104 {
105 }
106     
107 SGPath& SGPath::operator=(const SGPath& p)
108 {
109   path = p.path;
110   _cached = p._cached;
111   _exists = p._exists;
112   _isDir = p._isDir;
113   _isFile = p._isFile;
114   return *this;
115 }
116
117 // destructor
118 SGPath::~SGPath() {
119 }
120
121
122 // set path
123 void SGPath::set( const string& p ) {
124     path = p;
125     fix();
126     _cached = false;
127 }
128
129
130 // append another piece to the existing path
131 void SGPath::append( const string& p ) {
132     if ( path.size() == 0 ) {
133         path = p;
134     } else {
135         if ( p[0] != sgDirPathSep ) {
136             path += sgDirPathSep;
137         }
138         path += p;
139     }
140     fix();
141     _cached = false;
142 }
143
144 //add a new path component to the existing path string
145 void SGPath::add( const string& p ) {
146     append( sgSearchPathSep+p );
147 }
148
149
150 // concatenate a string to the end of the path without inserting a
151 // path separator
152 void SGPath::concat( const string& p ) {
153     if ( path.size() == 0 ) {
154         path = p;
155     } else {
156         path += p;
157     }
158     fix();
159     _cached = false;
160 }
161
162
163 // Get the file part of the path (everything after the last path sep)
164 string SGPath::file() const {
165     int index = path.rfind(sgDirPathSep);
166     if (index >= 0) {
167         return path.substr(index + 1);
168     } else {
169         return "";
170     }
171 }
172   
173
174 // get the directory part of the path.
175 string SGPath::dir() const {
176     int index = path.rfind(sgDirPathSep);
177     if (index >= 0) {
178         return path.substr(0, index);
179     } else {
180         return "";
181     }
182 }
183
184 // get the base part of the path (everything but the extension.)
185 string SGPath::base() const {
186     int index = path.rfind(".");
187     if ((index >= 0) && (path.find("/", index) == string::npos)) {
188         return path.substr(0, index);
189     } else {
190         return "";
191     }
192 }
193
194 // get the extension (everything after the final ".")
195 // but make sure no "/" follows the "." character (otherwise it
196 // is has to be a directory name containing a ".").
197 string SGPath::extension() const {
198     int index = path.rfind(".");
199     if ((index >= 0)  && (path.find("/", index) == string::npos)) {
200         return path.substr(index + 1);
201     } else {
202         return "";
203     }
204 }
205
206 void SGPath::validate() const
207 {
208   if (_cached) {
209     return;
210   }
211   
212 #ifdef _WIN32
213   struct _stat buf ;
214
215   if (_stat (path.c_str(), &buf ) < 0) {
216     _exists = false;
217   } else {
218     _exists = true;
219     _isFile = ((S_IFREG & buf.st_mode ) !=0);
220     _isDir = ((S_IFDIR & buf.st_mode ) !=0);
221   }
222
223 #else
224   struct stat buf ;
225
226   if (stat(path.c_str(), &buf ) < 0) {
227     _exists = false;
228   } else {
229     _exists = true;
230     _isFile = ((S_ISREG(buf.st_mode )) != 0);
231     _isDir = ((S_ISDIR(buf.st_mode )) != 0);
232   }
233   
234 #endif
235   _cached = true;
236 }
237
238 bool SGPath::exists() const
239 {
240   validate();
241   return _exists;
242 }
243
244 bool SGPath::isDir() const
245 {
246   validate();
247   return _exists && _isDir;
248 }
249
250 bool SGPath::isFile() const
251 {
252   validate();
253   return _exists && _isFile;
254 }
255
256 #ifdef _WIN32
257 #  define sgMkDir(d,m)       _mkdir(d)
258 #else
259 #  define sgMkDir(d,m)       mkdir(d,m)
260 #endif
261
262
263 int SGPath::create_dir( mode_t mode ) {
264     string_list dirlist = sgPathSplit(dir());
265     if ( dirlist.empty() )
266         return -1;
267     string path = dirlist[0];
268     string_list path_elements = sgPathBranchSplit(path);
269     bool absolute = !path.empty() && path[0] == sgDirPathSep;
270
271     unsigned int i = 1;
272     SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
273     dir.concat( path_elements[0] );
274 #ifdef _WIN32
275     if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
276         dir.append( path_elements[1] );
277         i = 2;
278     }
279 #endif
280     struct stat info;
281     int r;
282     for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
283         dir.append(path_elements[i]);
284     }
285     if ( r == 0 ) {
286         return 0; // Directory already exists
287     }
288     if ( sgMkDir( dir.c_str(), mode) ) {
289         SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
290         return -2;
291     }
292     for(; i < path_elements.size(); i++) {
293         dir.append(path_elements[i]);
294         if ( sgMkDir( dir.c_str(), mode) ) {
295             SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
296             return -2;
297         }
298     }
299
300     return 0;
301 }
302
303 string_list sgPathBranchSplit( const string &dirpath ) {
304     string_list path_elements;
305     string element, path = dirpath;
306     while ( path.size() ) {
307         size_t p = path.find( sgDirPathSep );
308         if ( p != string::npos ) {
309             element = path.substr( 0, p );
310             path.erase( 0, p + 1 );
311         } else {
312             element = path;
313             path = "";
314         }
315         if ( element.size() )
316             path_elements.push_back( element );
317     }
318     return path_elements;
319 }
320
321
322 string_list sgPathSplit( const string &search_path ) {
323     string tmp = search_path;
324     string_list result;
325     result.clear();
326
327     bool done = false;
328
329     while ( !done ) {
330         int index = tmp.find(sgSearchPathSep);
331         if (index >= 0) {
332             result.push_back( tmp.substr(0, index) );
333             tmp = tmp.substr( index + 1 );
334         } else {
335             if ( !tmp.empty() )
336                 result.push_back( tmp );
337             done = true;
338         }
339     }
340
341     return result;
342 }
343
344 bool SGPath::isAbsolute() const
345 {
346   if (path.empty()) {
347     return false;
348   }
349   
350 #ifdef _WIN32
351   // detect '[A-Za-z]:/'
352   if (path.size() > 2) {
353     if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
354       return true;
355     }
356   }
357 #endif
358   
359   return (path[0] == sgDirPathSep);
360 }
361
362 bool SGPath::isNull() const
363 {
364   return path.empty() || (path == "");
365 }