SimGear - Simulator Construction Tools
SGPath Class Reference

#include <sg_path.hxx>

Classes

struct  Permissions
 

Public Types

typedef Permissions(* PermissionChecker) (const SGPath &)
 
enum  StandardLocation {
  HOME, DESKTOP, DOWNLOADS, DOCUMENTS,
  PICTURES
}
 

Public Member Functions

 SGPath (PermissionChecker validator=NULL)
 
 SGPath (const SGPath &p)
 
 SGPath (const std::string &p, PermissionChecker validator=NULL)
 
 SGPath (const std::wstring &p, PermissionChecker validator=NULL)
 
 SGPath (const SGPath &p, const std::string &r, PermissionChecker validator=NULL)
 
 ~SGPath ()
 
void add (const std::string &p)
 
void append (const std::string &p)
 
std::string base () const
 
const char * c_str () const
 
bool canRead () const
 
bool canWrite () const
 
std::string complete_lower_extension () const
 
void concat (const std::string &p)
 
int create_dir (mode_t mode=0755)
 
std::string dir () const
 
SGPath dirPath () const
 
bool exists () const
 
std::string extension () const
 
std::string file () const
 
std::string file_base () const
 
std::string fileUrl () const
 
PermissionChecker getPermissionChecker () const
 
bool isAbsolute () const
 
bool isDir () const
 
bool isFile () const
 
bool isNull () const
 
bool isRelative () const
 
std::string local8BitStr () const
 
std::string lower_extension () const
 
time_t modTime () const
 
bool operator!= (const SGPath &other) const
 
SGPath operator/ (const std::string &p) const
 
SGPathoperator= (const SGPath &p)
 
SGPathoperator= (const char *p)
 
bool operator== (const SGPath &other) const
 
SGPath realpath () const
 
bool remove ()
 
bool rename (const SGPath &newName)
 
void set (const std::string &p)
 
void set_cached (bool cached)
 
void setPermissionChecker (PermissionChecker validator)
 
size_t sizeInBytes () const
 
std::string str () const
 
std::string str_native () const
 
std::string utf8Str () const
 
std::wstring wstr () const
 

Static Public Member Functions

static SGPath desktop (const SGPath &def=SGPath())
 
static SGPath documents (const SGPath &def=SGPath())
 
static SGPath fromEnv (const char *name, const SGPath &def=SGPath())
 
static SGPath fromLocal8Bit (const char *name)
 
static SGPath fromUtf8 (const std::string &bytes, PermissionChecker p=NULL)
 
static SGPath home (const SGPath &def=SGPath())
 
static std::string join (const std::vector< SGPath > &paths, const std::string &joinWith)
 
static std::vector< SGPathpathsFromEnv (const char *name)
 
static std::vector< SGPathpathsFromLocal8Bit (const std::string &paths)
 
static std::vector< SGPathpathsFromUtf8 (const std::string &paths)
 
static SGPath standardLocation (StandardLocation type, const SGPath &def=SGPath())
 

Static Public Attributes

static const char pathListSep [2] = ":"
 

Private Member Functions

void checkAccess () const
 
void fix ()
 
bool permissionsAllowsWrite () const
 
void validate () const
 

Private Attributes

bool _cached: 1
 
bool _cacheEnabled: 1
 cacheing can be disbled if required More...
 
bool _canRead: 1
 
bool _canWrite: 1
 
bool _exists: 1
 
bool _isDir: 1
 
bool _isFile: 1
 
time_t _modTime
 
PermissionChecker _permission_checker
 
bool _rwCached: 1
 
size_t _size
 
std::string path
 

Detailed Description

A class to hide path separator difference across platforms and assist in managing file system path names.

Paths can be input in any platform format and will be converted automatically to the proper format.

Definition at line 51 of file sg_path.hxx.

Member Typedef Documentation

typedef Permissions(* SGPath::PermissionChecker) (const SGPath &)

Definition at line 63 of file sg_path.hxx.

Member Enumeration Documentation

Enumerator
HOME 
DESKTOP 
DOWNLOADS 
DOCUMENTS 
PICTURES 

Definition at line 289 of file sg_path.hxx.

Constructor & Destructor Documentation

SGPath::SGPath ( PermissionChecker  validator = NULL)
explicit

Default constructor

Definition at line 186 of file sg_path.cxx.

Referenced by fromEnv(), fromLocal8Bit(), fromUtf8(), and realpath().

SGPath::SGPath ( const SGPath p)

Copy contructor

Definition at line 244 of file sg_path.cxx.

SGPath::SGPath ( const std::string &  p,
PermissionChecker  validator = NULL 
)

Construct a path based on the starting path provided.

Parameters
pinitial path

Definition at line 197 of file sg_path.cxx.

SGPath::SGPath ( const std::wstring &  p,
PermissionChecker  validator = NULL 
)
explicit

Definition at line 208 of file sg_path.cxx.

SGPath::SGPath ( const SGPath p,
const std::string &  r,
PermissionChecker  validator = NULL 
)

Construct a path based on the starting path provided and a relative subpath

Parameters
pinitial path
rrelative subpath

Definition at line 220 of file sg_path.cxx.

SGPath::~SGPath ( )

Destructor

Definition at line 278 of file sg_path.cxx.

Member Function Documentation

void SGPath::add ( const std::string &  p)

Append a new piece to the existing path. Inserts a search path separator to the existing path and the new patch component.

Parameters
padditional path component
void SGPath::append ( const std::string &  p)

Append another piece to the existing path. Inserts a path separator between the existing component and the new component.

Parameters
padditional path component

Definition at line 310 of file sg_path.cxx.

Referenced by simgear::HTTPDirectory::absolutePath(), SGText::appendText(), simgear::HTTPDirectory::copyInstalledChildren(), createFile(), simgear::pkg::Catalog::createFromPath(), simgear::HTTPDirectory::didFailToUpdateFile(), simgear::HTTPDirectory::didUpdateFile(), simgear::HTTPDirectory::dirIndexUpdated(), simgear::pkg::Install::PackageArchiveDownloader::extractCurrentFile(), simgear::Dir::file(), simgear::FileGetRequest::FileGetRequest(), simgear::HTTPDirectory::hashForChild(), SGTime::init(), simgear::SGTerraSync::WorkerThread::isPathCached(), SGStarData::load(), main(), simgear::pkg::Install::PackageArchiveDownloader::onDone(), operator/(), simgear::pkg::Install::PackageArchiveDownloader::PackageArchiveDownloader(), simgear::HTTPDirectory::parseDirIndex(), simgear::HTTPRepoPrivate::parseHashCache(), simgear::pkg::Catalog::parseProps(), simgear::pkg::Install::parseRevision(), parseTest(), simgear::pkg::Catalog::parseTimestamp(), simgear::pkg::Install::primarySetPath(), TestPackageChannel::processRequestHeaders(), simgear::ReaderWriterSTG::_ModelBin::read(), SGMaterial::read_properties(), simgear::HTTPDirectory::removeChild(), SGPath(), simgear::ReaderWriterSTG::_ModelBin::sharedOptions(), simgear::SGTerraSync::WorkerThread::start(), simgear::Dir::tempDir(), test_update_dir(), testAbandonCorruptFiles(), testAbandonMissingFiles(), testAddCatalog(), testBasicClone(), testCopyInstalledChildren(), testDestroyDuringSync(), testInstallPackage(), testInstallTarPackage(), testLossOfLocalFiles(), testMergeExistingFileWithoutDownload(), testModifyLocalFiles(), testPlainTar(), testRefreshCatalog(), testRemoveCatalog(), testServerModifyDuringSync(), testTarGz(), testUninstall(), SGTime::updateLocal(), simgear::SGTerraSync::WorkerThread::updateSyncSlot(), verifyFileNotPresent(), verifyFileState(), simgear::HTTPRepoPrivate::writeHashCache(), simgear::pkg::Install::writeRevisionFile(), and simgear::pkg::Catalog::writeTimestamp().

string SGPath::base ( ) const

Get the base part of the path (everything but the final extension.)

Returns
the base string

Definition at line 385 of file sg_path.cxx.

const char* SGPath::c_str ( ) const
inline

Get the path string

Returns
path in "C" string (ptr to char array) form.

Definition at line 204 of file sg_path.hxx.

Referenced by SGDem::addRoot(), SGDemTile::cacheTile(), SGDemLevel::close(), SGDemRoot::createLevel(), and SGDemTile::SGDemTile().

bool SGPath::canRead ( ) const

Check if reading file is allowed. Readabilty does not imply the existance of the file.

Note
By default all files will be marked as readable. No check is made if the operating system allows the given file to be read. Derived classes may actually implement custom read/write rights.

Definition at line 550 of file sg_path.cxx.

Referenced by rename(), and test_permissions().

bool SGPath::canWrite ( ) const

Definition at line 557 of file sg_path.cxx.

Referenced by rename(), and test_permissions().

void SGPath::checkAccess ( ) const
private

Definition at line 533 of file sg_path.cxx.

Referenced by canRead(), and canWrite().

string SGPath::complete_lower_extension ( ) const

Get the complete extension part of the path (everything after the first ".") this might look like 'tar.gz' or 'txt.Z', or might be identical to 'extension' above the extension is converted to lowercase.

Returns
the extension string

Definition at line 435 of file sg_path.cxx.

void SGPath::concat ( const std::string &  p)

Concatenate a string to the end of the path without inserting a path separator.

Parameters
padditional path suffix

Definition at line 341 of file sg_path.cxx.

Referenced by main(), SGBinObject::read_bin(), and simgear::Dir::tempDir().

SGPath SGPath::desktop ( const SGPath def = SGPath())
static

Get path to the user's desktop directory

Definition at line 975 of file sg_path.cxx.

string SGPath::dir ( ) const

Get the directory part of the path.

Returns
directory string

Definition at line 370 of file sg_path.cxx.

Referenced by create_dir(), createFile(), dirPath(), simgear::pkg::Install::PackageArchiveDownloader::extractCurrentFile(), simgear::Dir::parent(), realpath(), sgLoad3DModel_internal(), and validate().

SGPath SGPath::dirPath ( ) const

return the path of the parent directory of this path.

Definition at line 379 of file sg_path.cxx.

Referenced by create_dir().

SGPath SGPath::documents ( const SGPath def = SGPath())
static

Get path to the user's documents directory

Definition at line 981 of file sg_path.cxx.

bool SGPath::exists ( ) const

Determine if file exists by attempting to fopen it.

Returns
true if file exists, otherwise returns false.

Definition at line 543 of file sg_path.cxx.

Referenced by SGDem::addRoot(), SGDemTile::cacheTile(), simgear::pkg::Root::RootPrivate::checkPersistentCache(), simgear::Dir::children(), simgear::HTTPRepoPrivate::computeHashForPath(), simgear::HTTPDirectory::copyInstalledChildren(), simgear::pkg::Catalog::createFromPath(), simgear::pkg::Root::existingInstallForPackage(), simgear::SGModelLib::findDataFile(), simgear::ResourceManager::findPath(), simgear::HTTPDirectory::HTTPDirectory(), SGXmlSound::init(), simgear::pkg::Package::isInstalled(), simgear::SGTerraSync::WorkerThread::isPathCached(), simgear::loadWAVFromFile(), main(), simgear::pkg::Install::PackageArchiveDownloader::onDone(), simgear::FileGetRequest::onFail(), simgear::HTTPDirectory::parseDirIndex(), simgear::HTTPRepoPrivate::parseHashCache(), simgear::pkg::Install::parseRevision(), TestPackageChannel::processRequestHeaders(), simgear::pkg::Root::RootPrivate::queueLoadFromPersistentCache(), simgear::ReaderWriterSTG::_ModelBin::read(), simgear::SGReaderWriterXML::readNode(), simgear::ReaderWriterSTG::readNode(), rename(), simgear::BasePathProvider::resolve(), SGDemTile::SGDemTile(), sgLoad3DModel_internal(), simgear::SGTerraSync::WorkerThread::start(), test_computeHashForPath(), test_permissions(), test_setRemoveOnDestroy(), test_update_dir(), testAddCatalog(), testRemoveCatalog(), simgear::SGTerraSync::WorkerThread::updateSyncSlot(), verifyFileNotPresent(), and verifyFileState().

string SGPath::extension ( ) const

Get the extension part of the path (everything after the final ".")

Returns
the extension string

Definition at line 422 of file sg_path.cxx.

Referenced by lower_extension(), and sgLoad3DModel_internal().

string SGPath::file ( ) const

Get the file part of the path (everything after the last path sep)

Returns
file string

Definition at line 358 of file sg_path.cxx.

Referenced by simgear::pkg::Install::createFromPath(), main(), simgear::pathSortPredicate(), and realpath().

string SGPath::file_base ( ) const

Get the base part of the filename (everything before the first '.')

Returns
the base filename

Definition at line 402 of file sg_path.cxx.

Referenced by simgear::ReaderWriterSTG::_ModelBin::checkInsideBucket(), and simgear::ReaderWriterSTG::_ModelBin::read().

std::string SGPath::fileUrl ( ) const
void SGPath::fix ( )
private

Definition at line 169 of file sg_path.cxx.

Referenced by append(), concat(), set(), and SGPath().

SGPath SGPath::fromEnv ( const char *  name,
const SGPath def = SGPath() 
)
static

Get a path stored in the environment variable with the given name.

Parameters
nameName of the environment variable
defDefault path to return if the environment variable does not exist or is empty.

Definition at line 899 of file sg_path.cxx.

Referenced by getXDGDir(), and home().

SGPath SGPath::fromLocal8Bit ( const char *  name)
static

Definition at line 233 of file sg_path.cxx.

Referenced by main(), pathsFromLocal8Bit(), and realpath().

SGPath SGPath::fromUtf8 ( const std::string &  bytes,
PermissionChecker  p = NULL 
)
static

Definition at line 238 of file sg_path.cxx.

Referenced by dirPath(), pathsFromUtf8(), and SGInterpTable::SGInterpTable().

SGPath::PermissionChecker SGPath::getPermissionChecker ( ) const

Definition at line 297 of file sg_path.cxx.

Referenced by appleSpecialFolder(), and getXDGDir().

SGPath SGPath::home ( const SGPath def = SGPath())
static

Get path to user's home directory

Definition at line 965 of file sg_path.cxx.

Referenced by getXDGDir(), and standardLocation().

bool SGPath::isAbsolute ( ) const

Is this an absolute path? I.e starts with a directory seperator, or a single character + colon

Definition at line 682 of file sg_path.cxx.

Referenced by create_dir(), fileUrl(), isRelative(), realpath(), and test_path_dir().

bool SGPath::isFile ( ) const

Definition at line 569 of file sg_path.cxx.

Referenced by simgear::Dir::children(), and test_dir().

bool SGPath::isRelative ( ) const
inline

Opposite sense to isAbsolute

Definition at line 243 of file sg_path.hxx.

std::string SGPath::join ( const std::vector< SGPath > &  paths,
const std::string &  joinWith 
)
static

Definition at line 1030 of file sg_path.cxx.

string SGPath::lower_extension ( ) const

Get the extension part of the path (everything after the final ".") converted to lowercase

Returns
the extension string

Definition at line 431 of file sg_path.cxx.

Referenced by simgear::SGModelLib::loadDeferredModel(), simgear::SGModelLib::loadPagedModel(), simgear::ReaderWriterSTG::_ModelBin::DelayLoadReadFileCallback::AddModelLOD::operator()(), and SGMaterial::read_properties().

time_t SGPath::modTime ( ) const
bool SGPath::operator!= ( const SGPath other) const

Definition at line 778 of file sg_path.cxx.

SGPath SGPath::operator/ ( const std::string &  p) const

Get a copy of this path with another piece appended.

Parameters
padditional path component

Definition at line 325 of file sg_path.cxx.

SGPath & SGPath::operator= ( const SGPath p)

Definition at line 260 of file sg_path.cxx.

SGPath& SGPath::operator= ( const char *  p)
inline

Definition at line 98 of file sg_path.hxx.

bool SGPath::operator== ( const SGPath other) const

Definition at line 773 of file sg_path.cxx.

std::vector< SGPath > SGPath::pathsFromEnv ( const char *  name)
static

Definition at line 916 of file sg_path.cxx.

std::vector< SGPath > SGPath::pathsFromLocal8Bit ( const std::string &  paths)
static

Definition at line 952 of file sg_path.cxx.

std::vector< SGPath > SGPath::pathsFromUtf8 ( const std::string &  paths)
static

Definition at line 938 of file sg_path.cxx.

Referenced by pathsFromEnv().

bool SGPath::permissionsAllowsWrite ( ) const
private

Definition at line 1052 of file sg_path.cxx.

Referenced by create_dir(), and remove().

SGPath SGPath::realpath ( ) const

Returns a path with the absolute pathname that names the same file, whose resolution does not involve '.', '..', or symbolic links.

Definition at line 987 of file sg_path.cxx.

bool SGPath::rename ( const SGPath newName)

rename the file / directory we point at, to a new name this may fail if the new location is on a different volume / share, or if the destination already exists, or is not writeable

Definition at line 784 of file sg_path.cxx.

Referenced by simgear::pkg::Install::PackageArchiveDownloader::onDone(), and test_update_dir().

void SGPath::set ( const std::string &  p)

Set path to a new value

Parameters
pnew path

Definition at line 282 of file sg_path.cxx.

Referenced by operator=().

void SGPath::set_cached ( bool  cached)

Set if file information (exists, type, mod-time) is cached or retrieved each time it is queried. Caching is enabled by default

Definition at line 303 of file sg_path.cxx.

Referenced by simgear::Dir::Dir(), simgear::Dir::file(), simgear::pkg::Install::PackageArchiveDownloader::onDone(), and simgear::HTTPRepoPrivate::updatedFileContents().

void SGPath::setPermissionChecker ( PermissionChecker  validator)

Definition at line 290 of file sg_path.cxx.

Referenced by test_permissions().

SGPath SGPath::standardLocation ( StandardLocation  type,
const SGPath def = SGPath() 
)
static

Definition at line 834 of file sg_path.cxx.

Referenced by desktop(), documents(), and test_dir().

std::string SGPath::str ( ) const
inline

Get the path string

Returns
path string

Definition at line 193 of file sg_path.hxx.

Referenced by createBufferFromFile(), SGDemRoot::createLevel(), simgear::loadWAVFromFile(), and simgear::HTTPDirectory::parseDirIndex().

std::string SGPath::str_native ( ) const

Get the path string in OS native form

void SGPath::validate ( ) const
private

Definition at line 453 of file sg_path.cxx.

Referenced by checkAccess(), exists(), isDir(), isFile(), modTime(), and sizeInBytes().

Member Data Documentation

bool SGPath::_cached
mutableprivate

Definition at line 348 of file sg_path.hxx.

Referenced by append(), concat(), create_dir(), operator=(), remove(), rename(), set(), set_cached(), and validate().

bool SGPath::_cacheEnabled
private

cacheing can be disbled if required

Definition at line 350 of file sg_path.hxx.

Referenced by checkAccess(), operator=(), set_cached(), and validate().

bool SGPath::_canRead
mutableprivate

Definition at line 351 of file sg_path.hxx.

Referenced by canRead(), operator=(), and validate().

bool SGPath::_canWrite
mutableprivate

Definition at line 352 of file sg_path.hxx.

Referenced by canWrite(), operator=(), and validate().

bool SGPath::_exists
mutableprivate

Definition at line 353 of file sg_path.hxx.

Referenced by exists(), isDir(), isFile(), operator=(), and validate().

bool SGPath::_isDir
mutableprivate

Definition at line 354 of file sg_path.hxx.

Referenced by isDir(), operator=(), and validate().

bool SGPath::_isFile
mutableprivate

Definition at line 355 of file sg_path.hxx.

Referenced by isFile(), operator=(), and validate().

time_t SGPath::_modTime
mutableprivate

Definition at line 356 of file sg_path.hxx.

Referenced by modTime(), operator=(), and validate().

PermissionChecker SGPath::_permission_checker
private
bool SGPath::_rwCached
mutableprivate

Definition at line 349 of file sg_path.hxx.

Referenced by append(), checkAccess(), concat(), operator=(), remove(), rename(), set(), and setPermissionChecker().

size_t SGPath::_size
mutableprivate

Definition at line 357 of file sg_path.hxx.

Referenced by operator=(), sizeInBytes(), and validate().

const char SGPath::pathListSep = ":"
static

Definition at line 56 of file sg_path.hxx.

Referenced by sgPathSplit().


The documentation for this class was generated from the following files: