GL Studio C++ Runtime API
FilePathClass Class Reference

#include <file_path_class.h>

Public Member Functions

 FilePathClass ()
 
 FilePathClass (const std::string &stringPath)
 
 FilePathClass (const char *stringPath)
 
 FilePathClass (const FilePathClass &other)
 
FilePathClassoperator= (const FilePathClass &other)
 
 FilePathClass (FilePathClass &&other)
 
FilePathClassoperator= (FilePathClass &&other)
 
bool IsValid () const
 
bool FileExists () const
 
std::string PathRelativeTo (const FilePathClass &basePath) const
 
const std::string & Path () const
 
std::string Directory () const
 
std::string FileName () const
 
std::string FileExtension () const
 
void FileExtension (const std::string &ext)
 
bool operator== (const FilePathClass &path) const
 
bool operator!= (const FilePathClass &path) const
 
bool SetToCWD ()
 
void WriteAsAbsolutePath (bool value)
 
bool WriteAsAbsolutePath ()
 
bool ReadPathString (const FilePathClass &basePath, const std::string &strPath)
 
void WritePathString (const FilePathClass &basePath, std::string *strPath)
 

Static Public Member Functions

static const std::string & InvalidPathChars ()
 
static FilePathClass GetCWD ()
 
static bool StringIsAbsolutePath (const std::string &strPath)
 
static bool StringIsRelativePath (const std::string &strPath)
 
static bool StringIsValidPath (const std::string &strPath)
 

Protected Attributes

std::string _path
 
bool _writeAsAbsolutePath
 

Friends

std::ostream & operator<< (std::ostream &outstr, const FilePathClass &path)
 
std::istream & operator>> (std::istream &instr, FilePathClass &path)
 

Detailed Description

The FilePathClass represents a single absolute path. There are methods for getting the directory, filename, file extension, and checking the validity of the path. You can get a relative path string by comparing two FilePathClass objects. FilePathClass also has methods for reading/writing absolute or relative path strings, the class remembers which was last read and writes out the same way. You can change this behavior by modifying the WriteAsAbsolutePath() flag.

Constructor & Destructor Documentation

◆ FilePathClass() [1/5]

Default construction. Path() is initially "" and invalid

◆ FilePathClass() [2/5]

FilePathClass ( const std::string &  stringPath)

Implicit conversion

Parameters
stringPathThe path to set.

◆ FilePathClass() [3/5]

FilePathClass ( const char *  stringPath)

Implicit conversion

Parameters
stringPathThe path to set.

◆ FilePathClass() [4/5]

FilePathClass ( const FilePathClass other)
inline

Copy construction

Parameters
otherThe object to copy from.

◆ FilePathClass() [5/5]

FilePathClass ( FilePathClass &&  other)
inline

Move construction

Parameters
otherThe object to move from.

Member Function Documentation

◆ Directory()

std::string Directory ( ) const
Returns
The directory containing the referenced file (includes trailing slash) or "" if the path is not valid.

◆ FileExists()

bool FileExists ( ) const
Returns
True if this file path references a file that exists.
Note
Applies only to files; returns false for directories. Returns true for symbolic links that exist, regardless whether the link's target exists.

◆ FileExtension() [1/2]

std::string FileExtension ( ) const
Returns
The file extension for the file (everything after the last ".") or "" if the path is not valid, doesn't reference a file, or the file has no extension.

◆ FileExtension() [2/2]

void FileExtension ( const std::string &  ext)

Replace the current file extension with the specified string. This will also insert a '.' into the filename if needed.

Parameters
extThe new extension (e.g. "txt")

◆ FileName()

std::string FileName ( ) const
Returns
The filename of the file (including the extension) or "" if the path is not valid or doesn't reference a file.

◆ GetCWD()

static FilePathClass GetCWD ( )
inlinestatic
Returns
A file path set to the current working directory.

◆ InvalidPathChars()

static const std::string & InvalidPathChars ( )
static
Returns
A string containing the characters that invalid to use in a path.

◆ IsValid()

bool IsValid ( ) const
Returns
True if the path points to a directory or file that COULD exist. (i.e. Path() will return a valid path as close as we can verify it.)

◆ operator!=()

bool operator!= ( const FilePathClass path) const
inline

Inequality operator If paths are not equal it doesn't guarantee that they reference different files.

Parameters
pathThe path to compare.
Returns
Whether or not the paths are not equal.

◆ operator=() [1/2]

FilePathClass & operator= ( const FilePathClass other)
inline

Copy assignment

Parameters
otherThe object to copy from.
Returns
The resulting object (this).

◆ operator=() [2/2]

FilePathClass & operator= ( FilePathClass &&  other)
inline

Move assignment

Parameters
otherThe object to move from.
Returns
The resulting object (this).

◆ operator==()

bool operator== ( const FilePathClass path) const

Equality operator If paths are equal they reference the same file.

Parameters
pathThe path to compare.
Returns
Whether or not the paths are equal.

◆ Path()

const std::string & Path ( ) const
inline
Returns
A string containing the complete path.
Note
This method returns whatever is stored in the path whether it is valid or not.

◆ PathRelativeTo()

std::string PathRelativeTo ( const FilePathClass basePath) const
Parameters
basePathThe path to calculate the relative path from.
Returns
A string containing the relative path to this path from the given path's directory.
Note
Returns Path() if the relative path cannot be determined.

◆ ReadPathString()

bool ReadPathString ( const FilePathClass basePath,
const std::string &  strPath 
)

Read a relative or absolute path from a string

Precondition
None
Postcondition
If strPath was an absolute path or invalid then this path will be set to strPath and WriteAsAbsolutePath() will be true If strPath was relative, this path will be set to basePath + strPath and WriteAsAbsolutePath() will be false
Parameters
basePathUsed to build a complete path if strPath contains a relative path
strPathThe string to read into
Returns
true on success or false if there was an error (basePath or strPath are invalid)

◆ SetToCWD()

bool SetToCWD ( )

Set this object to point to the current working directory

Returns
true on success, false if there was an error

◆ StringIsAbsolutePath()

static bool StringIsAbsolutePath ( const std::string &  strPath)
static
Parameters
strPathThe string to check.
Returns
true If the given string contains an absolute path.

◆ StringIsRelativePath()

static bool StringIsRelativePath ( const std::string &  strPath)
static
Parameters
strPathThe string to check.
Returns
True if the given string contains an relative path.

◆ StringIsValidPath()

static bool StringIsValidPath ( const std::string &  strPath)
static
Parameters
strPathThe string to check.
Returns
true If the given string contains a valid or absolute path as close as we can verify it.

◆ WriteAsAbsolutePath() [1/2]

bool WriteAsAbsolutePath ( )
inline
Returns
Whether or not paths will be written as absolute.

◆ WriteAsAbsolutePath() [2/2]

void WriteAsAbsolutePath ( bool  value)
inline

Set whether or not paths will be written as absolute paths.

Parameters
valueThe new path write value to set.

◆ WritePathString()

void WritePathString ( const FilePathClass basePath,
std::string *  strPath 
)

Get the path string to write to a file This strPath will be either the absolute or relative path depending on the current setting of the WriteAsAbsolutePath() flag

Parameters
basePathUsed to build a relative path if WriteAsAbsolutePath is false
strPathThe string to write to

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  outstr,
const FilePathClass path 
)
friend

Stream out operator

Parameters
outstrStream to write path to.
pathPath to write to the stream.
Returns
The stream in its current state.

◆ operator>>

std::istream & operator>> ( std::istream &  instr,
FilePathClass path 
)
friend

Stream in operator

Parameters
instrThe stream to read path from.
pathReturned path read from the stream.
Returns
The stream in its current state.

Member Data Documentation

◆ _path

std::string _path
protected

The complete path: format is "<directory>/<filename>.<extension>" where / is last slash and . is the first dot after the last slash

◆ _writeAsAbsolutePath

bool _writeAsAbsolutePath
protected

Controls how the Path is written out when WritePathString is called Defaults to false


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