class Path

Class for storing and manipulating file paths.

Paths may be parsed from and to raw strings according to various platform specific path types.

Public

Constructors

Path

Path() noexcept = default

Path

Path(const String &pathStr, PathType type = PathType::Default)

Constructs a path by parsing the provided path string.

Throws exception if provided path is not valid.

pathStr
String containing the path. Ideally this should be an UTF-8 encoded string in order to support non-ANSI characters in the path.
type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

Path

Path(const char *pathStr, PathType type = PathType::Default)

Constructs a path by parsing the provided path null terminated string.

Throws exception if provided path is not valid.

pathStr
Null-terminated string containing the path. Ideally this should be an UTF-8 encoded string in order to support non-ANSI characters in the path.
type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

Path

Path(const Path &other)

Methods

Swap

void Swap(Path &path)

Swap internal data with another Path object.

Assign

void Assign(const Path &path)

Create a path from another Path object.

Assign

void Assign(const String &pathStr, PathType type = PathType::Default)

Constructs a path by parsing the provided path string.

Throws exception if provided path is not valid.

pathStr
String containing the path.
type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

Assign

void Assign(const char *pathStr, PathType type = PathType::Default)

Constructs a path by parsing the provided path null terminated string.

Throws exception if provided path is not valid.

pathStr
Null-terminated string containing the path.
type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

ToString

String ToString(PathType type = PathType::Default) const

Converts the path in a string according to platform path rules.

type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

Returns: String representing the path using the UTF8 string encoding.

ToPlatformString

WString ToPlatformString() const

IsDirectory

bool IsDirectory() const

Checks is the path a directory (contains no file-name).

IsFile

bool IsFile() const

Checks does the path point to a file.

IsAbsolute

bool IsAbsolute() const

Checks is the contained path absolute.

GetParent

Path GetParent() const

Returns parent path.

If current path points to a file the parent path will be the folder where the file is located. Or if it contains a directory the parent will be the parent directory. If no parent exists, same path will be returned.

GetAbsolute

Path GetAbsolute(const Path &base) const

Returns an absolute path by appending the current path to the provided base.

If path was already absolute no changes are made and copy of current path is returned. If base is not absolute, then the returned path will be made relative to base, but will not be absolute.

GetRelative

Path GetRelative(const Path &base) const

Returns a relative path by making the current path relative to the provided base.

Base must be a part of the current path. If base is not a part of the path no changes are made and a copy of the current path is returned.

GetDirectory

Path GetDirectory() const

Returns the path as a path to directory.

If path was pointing to a file, the filename is removed, otherwise no changes are made and exact copy is returned.

MakeParent

Path &MakeParent()

Makes the path the parent of the current path.

If current path points to a file the parent path will be the folder where the file is located. Or if it contains a directory the parent will be the parent directory. If no parent exists, same path will be returned.

MakeAbsolute

Path &MakeAbsolute(const Path &base)

Makes the current path absolute by appending it to base.

If path was already absolute no changes are made and copy of current path is returned. If base is not absolute, then the returned path will be made relative to base, but will not be absolute.

MakeRelative

Path &MakeRelative(const Path &base)

Makes the current path relative to the provided base.

Base must be a part of the current path. If base is not a part of the path no changes are made and a copy of the current path is returned.

Append

Path &Append(const Path &path)

Appends another path to the end of this path.

Includes

bool Includes(const Path &child, bool caseSensitive = false) const

Checks if the current path contains the provided path.

Equals

bool Equals(const Path &other, bool caseSensitive = false) const

Compares two paths and returns true if they match.

SetFilename

void SetFilename(const String &filename)

Change or set the filename in the path.

SetBasename

void SetBasename(const String &basename)

Change or set the base name in the path.

Base name changes the filename by changing its base to the provided value but keeping extension intact.

SetExtension

void SetExtension(const String &extension)

Change or set the extension of the filename in the path.

extension
Extension with a leading ".".

GetFilename

const String &GetFilename() const

Returns a filename with extension.

GetFilename

String GetFilename(bool extension) const

Returns a filename in the path.

extension
If true, returned filename will contain an extension.

GetExtension

String GetExtension() const

Returns file extension with the leading ".".

GetDirectoryCount

u32 GetDirectoryCount() const

Gets the number of directories in the path.

GetDirectory

const String &GetDirectory(u32 index) const

Gets a directory name with the specified index from the path.

GetDevice

const String &GetDevice() const

Returns path device (for example drive, volume, etc.) if one exists in the path.

GetNode

const String &GetNode() const

Returns path node (for example network name) if one exists in the path.

GetTail

const String &GetTail() const

Gets last element in the path, filename if it exists, otherwise the last directory.

PopTail

String PopTail()

Removes the last element in the path: filename if it exists, otherwise the last directory.

Returns the removed value.

GetSubPath

Path GetSubPath(u32 directoryCount) const

Returns the portion of the path containing the first directories.

PushDirectory

void PushDirectory(const String &dir)

Add new directory to the end of the path.

PopDirectory

void PopDirectory()

Removes the last directory from the end of the path.

Clear

void Clear()

Clears the path to nothing.

IsEmpty

bool IsEmpty() const

Returns true if no path has been set.

staticComparePathElem

static bool ComparePathElem(const String &left, const String &right, bool caseSensitive = false)

Compares two path elements (filenames, directory names, etc.).

staticCombine

static Path Combine(const Path &left, const Path &right)

Combines two paths and returns the result.

Right path should be relative.

staticCombine

static Path Combine(const Path &left, const Path &middle, const Path &right)

Combines three paths and returns the result.

Right path should be relative.

staticStripInvalid

static void StripInvalid(String &path)

Strips invalid characters from the provided string and replaces them with empty spaces.

Operators

operator=

Path &operator=(const String &pathStr)

Assigns a path by parsing the provided path string.

Path will be parsed according to the rules of the platform the application is being compiled to.

operator=

Path &operator=(const char *pathStr)

Assigns a path by parsing the provided path null terminated string.

Path will be parsed according to the rules of the platform the application is being compiled to.

operator=

Path &operator=(const Path &path)

operator==

bool operator==(const Path &path) const

Compares two paths and returns true if they match.

Comparison is case insensitive and paths will be compared as-is, without canonization.

operator!=

bool operator!=(const Path &path) const

Compares two paths and returns true if they don't match.

Comparison is case insensitive and paths will be compared as-is, without canonization.

operator[]

const String &operator[](u32 index) const

Gets a directory name with the specified index from the path.

operator+

Path operator+(const Path &rhs) const

Concatenates two paths.

operator+=

Path &operator+=(const Path &rhs)

Concatenates two paths.

Private

Methods

Assign

void Assign(const char *pathStr, u32 characterCount, PathType type = PathType::Default)

Constructs a path by parsing the provided raw string data.

Throws exception if provided path is not valid.

pathStr
String containing the path.
characterCount
Number of character in the provided path string.
type
If set to default path will be parsed according to the rules of the platform the application is being compiled to. Otherwise it will be parsed according to provided type.

SetNode

void SetNode(const String &node)

SetDevice

void SetDevice(const String &device)

BuildWindows

String BuildWindows() const

Build a Windows path string from internal path data.

BuildUnix

String BuildUnix() const

Build a Unix path string from internal path data.

ReportInvalidPath

void ReportInvalidPath(const String &path) const

Logs a fatal error for an incorrectly formatted path.

Fields

mDirectories

Vector<String> mDirectories

mDevice

String mDevice

mFilename

String mFilename

mNode

String mNode

mIsAbsolute

bool mIsAbsolute