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 // For windows, replace "\" by "/".
60 string::size_type sz = path.size();
61 for ( string::size_type i = 0; i < sz; ++i ) {
62 if ( path[i] == sgDirPathSepBad ) {
63 path[i] = sgDirPathSep;
67 while ((sz>1)&&(path[sz-1]==sgDirPathSep))
74 // default constructor
83 // create a path based on "path"
84 SGPath::SGPath( const std::string& p )
92 // create a path based on "path" and a "subpath"
93 SGPath::SGPath( const SGPath& p, const std::string& r )
96 _cacheEnabled(p._cacheEnabled)
102 SGPath::SGPath(const SGPath& p) :
105 _cacheEnabled(p._cacheEnabled),
113 SGPath& SGPath::operator=(const SGPath& p)
117 _cacheEnabled = p._cacheEnabled;
121 _modTime = p._modTime;
131 void SGPath::set( const string& p ) {
137 void SGPath::set_cached(bool cached)
139 _cacheEnabled = cached;
142 // append another piece to the existing path
143 void SGPath::append( const string& p ) {
144 if ( path.size() == 0 ) {
147 if ( p[0] != sgDirPathSep ) {
148 path += sgDirPathSep;
156 //add a new path component to the existing path string
157 void SGPath::add( const string& p ) {
158 append( sgSearchPathSep+p );
162 // concatenate a string to the end of the path without inserting a
164 void SGPath::concat( const string& p ) {
165 if ( path.size() == 0 ) {
175 // Get the file part of the path (everything after the last path sep)
176 string SGPath::file() const
178 string::size_type index = path.rfind(sgDirPathSep);
179 if (index != string::npos) {
180 return path.substr(index + 1);
187 // get the directory part of the path.
188 string SGPath::dir() const {
189 int index = path.rfind(sgDirPathSep);
191 return path.substr(0, index);
197 // get the base part of the path (everything but the extension.)
198 string SGPath::base() const
200 string::size_type index = path.rfind(".");
201 string::size_type lastSep = path.rfind(sgDirPathSep);
203 // tolerate dots inside directory names
204 if ((lastSep != string::npos) && (index < lastSep)) {
208 if (index != string::npos) {
209 return path.substr(0, index);
215 string SGPath::file_base() const
217 string::size_type index = path.rfind(sgDirPathSep);
218 if (index == string::npos) {
219 index = 0; // no separator in the name
221 ++index; // skip past the separator
224 string::size_type firstDot = path.find(".", index);
225 if (firstDot == string::npos) {
226 return path.substr(index); // no extensions
229 return path.substr(index, firstDot - index);
232 // get the extension (everything after the final ".")
233 // but make sure no "/" follows the "." character (otherwise it
234 // is has to be a directory name containing a ".").
235 string SGPath::extension() const {
236 int index = path.rfind(".");
237 if ((index >= 0) && (path.find("/", index) == string::npos)) {
238 return path.substr(index + 1);
244 string SGPath::lower_extension() const {
245 return boost::to_lower_copy(extension());
248 string SGPath::complete_lower_extension() const
250 string::size_type index = path.rfind(sgDirPathSep);
251 if (index == string::npos) {
252 index = 0; // no separator in the name
254 ++index; // skip past the separator
257 string::size_type firstDot = path.find(".", index);
258 if ((firstDot != string::npos) && (path.find(sgDirPathSep, firstDot) == string::npos)) {
259 return boost::to_lower_copy(path.substr(firstDot + 1));
265 void SGPath::validate() const
267 if (_cached && _cacheEnabled) {
274 bool remove_trailing = false;
275 if ( path.length() > 1 && path[path.length()-1] == '/' )
276 remove_trailing=true;
277 if (_stat (path.substr(0,remove_trailing?path.length()-1:path.length()).c_str(), &buf ) < 0) {
281 _isFile = ((S_IFREG & buf.st_mode ) !=0);
282 _isDir = ((S_IFDIR & buf.st_mode ) !=0);
283 _modTime = buf.st_mtime;
289 if (stat(path.c_str(), &buf ) < 0) {
293 _isFile = ((S_ISREG(buf.st_mode )) != 0);
294 _isDir = ((S_ISDIR(buf.st_mode )) != 0);
295 _modTime = buf.st_mtime;
302 bool SGPath::exists() const
308 bool SGPath::isDir() const
311 return _exists && _isDir;
314 bool SGPath::isFile() const
317 return _exists && _isFile;
321 # define sgMkDir(d,m) _mkdir(d)
323 # define sgMkDir(d,m) mkdir(d,m)
327 int SGPath::create_dir( mode_t mode ) {
328 string_list dirlist = sgPathSplit(dir());
329 if ( dirlist.empty() )
331 string path = dirlist[0];
332 string_list path_elements = sgPathBranchSplit(path);
333 bool absolute = !path.empty() && path[0] == sgDirPathSep;
336 SGPath dir = absolute ? string( 1, sgDirPathSep ) : "";
337 dir.concat( path_elements[0] );
339 if ( dir.str().find(':') != string::npos && path_elements.size() >= 2 ) {
340 dir.append( path_elements[1] );
346 for(; ( r = stat( dir.c_str(), &info ) ) == 0 && i < path_elements.size(); i++) {
347 dir.append(path_elements[i]);
350 return 0; // Directory already exists
352 if ( sgMkDir( dir.c_str(), mode) ) {
353 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
356 for(; i < path_elements.size(); i++) {
357 dir.append(path_elements[i]);
358 if ( sgMkDir( dir.c_str(), mode) ) {
359 SG_LOG( SG_IO, SG_ALERT, "Error creating directory: " + dir.str() );
367 string_list sgPathBranchSplit( const string &dirpath ) {
368 string_list path_elements;
369 string element, path = dirpath;
370 while ( path.size() ) {
371 size_t p = path.find( sgDirPathSep );
372 if ( p != string::npos ) {
373 element = path.substr( 0, p );
374 path.erase( 0, p + 1 );
379 if ( element.size() )
380 path_elements.push_back( element );
382 return path_elements;
386 string_list sgPathSplit( const string &search_path ) {
387 string tmp = search_path;
394 int index = tmp.find(sgSearchPathSep);
396 result.push_back( tmp.substr(0, index) );
397 tmp = tmp.substr( index + 1 );
400 result.push_back( tmp );
408 bool SGPath::isAbsolute() const
415 // detect '[A-Za-z]:/'
416 if (path.size() > 2) {
417 if (isalpha(path[0]) && (path[1] == ':') && (path[2] == sgDirPathSep)) {
423 return (path[0] == sgDirPathSep);
426 bool SGPath::isNull() const
428 return path.empty() || (path == "");
431 std::string SGPath::str_native() const
434 std::string s = str();
435 std::string::size_type pos;
436 std::string nativeSeparator;
437 nativeSeparator = sgDirPathSepBad;
439 while( (pos=s.find( sgDirPathSep )) != std::string::npos ) {
440 s.replace( pos, 1, nativeSeparator );
448 bool SGPath::remove()
450 int err = ::unlink(c_str());
452 SG_LOG(SG_IO, SG_WARN, "file remove failed: (" << str() << ") " << strerror(errno));
457 time_t SGPath::modTime() const
463 bool SGPath::operator==(const SGPath& other) const
465 return (path == other.path);
468 bool SGPath::operator!=(const SGPath& other) const
470 return (path != other.path);
473 bool SGPath::rename(const SGPath& newName)
475 if (::rename(c_str(), newName.c_str()) != 0) {
476 SG_LOG(SG_IO, SG_WARN, "renamed failed: from " << str() << " to " << newName.str()
477 << " reason: " << strerror(errno));
486 std::string SGPath::realpath() const
488 #if defined(_WIN32) || (defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050)
489 // Not implemented for Windows yet. Return original path instead.
491 // Workaround for Mac OS 10.5. Somehow fgfs crashes on Mac at ::realpath.
492 // simply returning path works on Mac since absolute path is passed from the GUI launcher
495 char* buf = ::realpath(path.c_str(), NULL);
498 SG_LOG(SG_IO, SG_ALERT, "ERROR: The path '" << path << "' does not exist in the file system.");