#199: ADd method to return the absolute (real) path.

This commit is contained in:
ThorstenB 2012-01-12 21:44:15 +01:00
parent 7ee2633164
commit cee8c5b5c7
2 changed files with 24 additions and 5 deletions

View File

@ -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
}

View File

@ -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