Update of /cvsroot/pclasses/pclasses2/src/System
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15622/src/System
Added Files:
Path.posix.cpp
Log Message:
- Added System::Path for obtaining dirName/baseName of a path. Also used to
retrieve absolute pathes.
--- NEW FILE: Path.posix.cpp ---
/***************************************************************************
* Copyright (C) 2005 by Christian Prochnow *
* cp...@se... *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "pclasses/System/Path.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
namespace P {
namespace System {
Path::Path(const Unicode::String& path)
{
// cut trailing "/" if not leading ...
if(path.size() > 1 && path.substr(path.size()-1) == separator())
_path = path.substr(0,path.size()-1);
else
_path = path;
}
Path::~Path()
{
}
Unicode::String Path::dirName() const throw()
{
Unicode::String::size_type slashat =
_path.find_last_of(separator());
if(slashat == Unicode::String::npos || slashat == 0)
return _path;
return _path.substr(0, slashat);
}
Unicode::String Path::absDirName() const throw(SystemError)
{
char* absDirName = realpath(dirName().utf8().c_str(), 0);
if(!absDirName)
throw SystemError(errno, "Could not resolve real path", P_SOURCEINFO);
Unicode::String res = Unicode::String::fromUtf8(absDirName);
free(absDirName);
return res;
}
Unicode::String Path::baseName() const throw()
{
Unicode::String::size_type slashat =
_path.find_last_of(separator());
if(slashat == Unicode::String::npos)
return _path;
return _path.substr(slashat + 1);
}
Unicode::String Path::path() const throw()
{
return _path;
}
Unicode::String Path::absPath() const throw(SystemError)
{
char* absPath = realpath(_path.utf8().c_str(), 0);
if(!absPath)
throw SystemError(errno, "Could not resolve real path", P_SOURCEINFO);
Unicode::String res = Unicode::String::fromUtf8(absPath);
free(absPath);
return res;
}
bool Path::isAccessible() const throw()
{
return 0 == access(_path.utf8().c_str(), F_OK);
}
Path& Path::operator=(const Unicode::String& path)
{
_path = path;
return *this;
}
const char* Path::separator() throw()
{
return "/";
}
} // !namespace System
} // !namespace P
|