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 - http://www.flightgear.org/~curt
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 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.
25 #include <simgear/compiler.h>
27 #include <simgear_config.h>
28 #include <simgear/debug/logstream.hxx>
36 #include "sg_path.hxx"
38 #include <boost/algorithm/string/case_conv.hpp>
43 * define directory path separators
46 static const char sgDirPathSep = '/';
47 static const char sgDirPathSepBad = '\\';
50 static const char sgSearchPathSep = ';';
52 static const char sgSearchPathSep = ':';
56 // If Unix, replace all ":" with "/". In windoze, allow the
57 // second character to be a ":" for things like c:\foo\bar
62 for ( string::size_type i = 0; i < path.size(); ++i ) {
64 // for windoze, don't replace the ":" for the second character
69 if ( path[i] == sgDirPathSepBad ) {
70 path[i] = sgDirPathSep;
76 // default constructor
85 // create a path based on "path"
86 SGPath::SGPath( const std::string& p )
94 // create a path based on "path" and a "subpath"
95 SGPath::SGPath( const SGPath& p, const std::string& r )
98 _cacheEnabled(p._cacheEnabled)
104 SGPath::SGPath(const SGPath& p) :
107 _cacheEnabled(p._cacheEnabled),
115 SGPath& SGPath::operator=(const SGPath& p)
119 _cacheEnabled = p._cacheEnabled;
123 _modTime = p._modTime;
133 void SGPath::set( const string& p ) {
139 void SGPath::set_cached(bool cached)
141 _cacheEnabled = cached;
144 // append another piece to the existing path
145 void SGPath::append( const string& p ) {
146 if ( path.size() == 0 ) {
149 if ( p[0] != sgDirPathSep ) {
150 path += sgDirPathSep;
158 //add a new path component to the existing path string
159 void SGPath::add( const string& p ) {
160 append( sgSearchPathSep+p );
164 // concatenate a string to the end of the path without inserting a
166 void SGPath::concat( const string& p ) {
167 if ( path.size() == 0 ) {
177 // Get the file part of the path (everything after the last path sep)
178 string SGPath::file() const {
179 int index = path.rfind(sgDirPathSep);
181 return path.substr(index + 1);
188 // get the directory part of the path.
189 string SGPath::dir() const {
190 int index = path.rfind(sgDirPathSep);
192 return path.substr(0, index);
198 // get the base part of the path (everything but the extension.)
199 string SGPath::base() const {
200 unsigned int index = path.rfind(sgDirPathSep);
201 if (index == string::npos) {
202 index = 0; // no separator in the name
204 ++index; // skip past the separator
207 // find the first dot after the final separator
208 unsigned int firstDot = path.find(".", index);
209 if (firstDot == string::npos) {
210 return path; // no extension, return whole path
213 return path.substr(0, firstDot);
216 string SGPath::file_base() const
218 unsigned int index = path.rfind(sgDirPathSep);
219 if (index == string::npos) {
220 index = 0; // no separator in the name
222 ++index; // skip past the separator
225 unsigned int firstDot = path.find(".", index);
226 if (firstDot == string::npos) {
227 return path.substr(index); // no extensions
230 return path.substr(index, firstDot - index);
233 // get the extension (everything after the final ".")
234 // but make sure no "/" follows the "." character (otherwise it
235 // is has to be a directory name containing a ".").
236 string SGPath::extension() const {
237 int index = path.rfind(".");
238 if ((index >= 0) && (path.find("/", index) == string::npos)) {
239 return path.substr(index + 1);
245 string SGPath::lower_extension() const {
246 return boost::to_lower_copy(extension());
249 string SGPath::complete_lower_extension() const
251 unsigned int index = path.rfind(sgDirPathSep);
252 if (index == string::npos) {
253 index = 0; // no separator in the name
255 ++index; // skip past the separator
258 int firstDot = path.find(".", index);
259 if ((firstDot >= 0) && (path.find(sgDirPathSep, firstDot) == string::npos)) {
260 return boost::to_lower_copy(path.substr(firstDot + 1));
266 void SGPath::validate() const
268 if (_cached && _cacheEnabled) {
275 bool remove_trailing = false;
276 if ( path.length() > 1 && path[path.length()-1] == '/' )
277 remove_trailing=true;
278 if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
282 _isFile = ((S_IFREG & buf.st_mode ) !=0);
283 _isDir = ((S_IFDIR & buf.st_mode ) !=0);
284 _modTime = buf.st_mtime;
290 if (stat(path.c_str(), &buf ) < 0) {
294 _isFile = ((S_ISREG(buf.st_mode )) != 0);
295 _isDir = ((S_ISDIR(buf.st_mode )) != 0);
296 _modTime = buf.st_mtime;
303 bool SGPath::exists() const
309 bool SGPath::isDir() const
312 return _exists && _isDir;
315 bool SGPath::isFile() const
318 return _exists && _isFile;
322 # define sgMkDir(d,m) _mkdir(d)
324 # define sgMkDir(d,m) mkdir(d,m)
328 int SGPath::create_dir( mode_t mode ) {
329 string_list dirlist = sgPathSplit(dir());
330 if ( dirlist.empty() )
332 string path = dirlist[0];
333 string_list path_elements = sgPathBranchSplit(path);
334 bool absolute = !path.empty() && path[0] == sgDirPathSep;
337 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
338 dir.concat( path_elements[0] );
340 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
341 dir.append( path_elements[1] );
347 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
348 dir.append(path_elements[i]);
351 return 0; // Directory already exists
353 if ( sgMkDir( dir.c_str(), mode) ) {
354 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
357 for(; i < path_elements.size(); i++) {
358 dir.append(path_elements[i]);
359 if ( sgMkDir( dir.c_str(), mode) ) {
360 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
368 string_list sgPathBranchSplit( const string &dirpath ) {
369 string_list path_elements;
370 string element, path = dirpath;
371 while ( path.size() ) {
372 size_t p = path.find( sgDirPathSep );
373 if ( p != string::npos ) {
374 element = path.substr( 0, p );
375 path.erase( 0, p + 1 );
380 if ( element.size() )
381 path_elements.push_back( element );
383 return path_elements;
387 string_list sgPathSplit( const string &search_path ) {
388 string tmp = search_path;
395 int index = tmp.find(sgSearchPathSep);
397 result.push_back( tmp.substr(0, index) );
398 tmp = tmp.substr( index + 1 );
401 result.push_back( tmp );
409 bool SGPath::isAbsolute() const
416 // detect '[A-Za-z]:/'
417 if (path.size() > 2) {
418 if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
424 return (path[0] == sgDirPathSep);
427 bool SGPath::isNull() const
429 return path.empty() || (path == "");
432 std::string SGPath::str_native() const
435 std::string s = str();
436 std::string::size_type pos;
437 std::string nativeSeparator;
438 nativeSeparator = sgDirPathSepBad;
440 while( (pos=s.find( sgDirPathSep )) != std::string::npos ) {
441 s.replace( pos, 1, nativeSeparator );
449 bool SGPath::remove()
451 int err = ::unlink(c_str());
453 SG_LOG(SG_IO, SG_WARN, "file remove failed: (" << str() << ") " << strerror(errno));
458 time_t SGPath::modTime() const
464 bool SGPath::operator==(const SGPath& other) const
466 return (path == other.path);
469 bool SGPath::operator!=(const SGPath& other) const
471 return (path != other.path);