From cee8c5b5c7a37321104391393594a2c2fea5a096 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Thu, 12 Jan 2012 21:44:15 +0100 Subject: [PATCH] #199: ADd method to return the absolute (real) path. --- simgear/misc/sg_path.cxx | 23 ++++++++++++++++++----- simgear/misc/sg_path.hxx | 6 ++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 92a16945..e4b3cbf9 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -109,7 +109,7 @@ SGPath::SGPath(const SGPath& p) : _modTime(p._modTime) { } - + SGPath& SGPath::operator=(const SGPath& p) { path = p.path; @@ -142,12 +142,12 @@ void SGPath::set_cached(bool cached) // append another piece to the existing path void SGPath::append( const string& p ) { if ( path.size() == 0 ) { - path = p; + path = p; } else { if ( p[0] != sgDirPathSep ) { path += sgDirPathSep; } - path += p; + path += p; } fix(); _cached = false; @@ -163,9 +163,9 @@ void SGPath::add( const string& p ) { // path separator void SGPath::concat( const string& p ) { if ( path.size() == 0 ) { - path = p; + path = p; } else { - path += p; + path += p; } fix(); _cached = false; @@ -483,3 +483,16 @@ bool SGPath::rename(const SGPath& newName) return true; } +std::string SGPath::realpath() const +{ +#ifdef _WIN32 + // Not implemented for Windows yet. Return original path instead. + return path; +#else + char* buf = ::realpath(path.c_str(), NULL); + std::string p(buf); + free(buf); + return p; +#endif +} + diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index 969eec0b..1f9dfbe7 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -111,6 +111,12 @@ public: */ void concat( const std::string& p ); + /** + * Returns a string with the absolute pathname that names the same file, whose + * resolution does not involve '.', '..', or symbolic links. + */ + std::string realpath() const; + /** * Get the file part of the path (everything after the last path sep) * @return file string