tec::Path

#include <filesystem.hpp>

Public Types

Name
typedef std::string NativePath
OS File system path separator.

Public Functions

Name
Path()
assets:/shaders/ where to find shaders
Path(const std::string_view & other)
Builds a Path from a string_view or substring there of.
Path(const std::string & other, std::size_t pos =0, std::size_t count =std::string::npos)
Builds a Path from a string or substring.
Path(std::string && other)
template <size_t N>
Path(const char(&) other[N])
This template builds a Path from a string literal.
Path(const std::wstring & other)
Builds a path from a wstring.
bool DirExists() const
Check if a directory exists.
bool FileExists() const
Check if a file exists.
std::string FileName() const
Extract the filename from a Path.
std::string FileStem() const
Extract a filename without the extension from a Path to a file.
std::string FileExtension() const
Extract an extension from a Path to a file.
Path BasePath() const
Return base path of a full path.
bool isAbsolutePath() const
Is this an absolute or relative Path?
Path Relative() const
Get the relative part of an absolute path.
Path Subpath(std::size_t begin, std::size_t end =Path::npos) const
Return a subpath.
Path SubpathFrom(const std::string & needle, bool include =false) const
Return a subpath.
std::unique_ptr< FILE > OpenFile(PATH_OPEN_FLAGS open_mode =FS_DEFAULT) const
Open the file at this path as a C-style stream for C functions.
std::unique_ptr< std::fstream > OpenStream(PATH_OPEN_FLAGS open_mode =FS_DEFAULT) const
Open the file at this Path as a std::fstream.
std::string toString() const
Returns the string representation of this Path.
bool empty() const
operator bool() const
Path & operator=(const Path & rhs)
Path & operator=(const std::string & str)
Path & operator=(std::string && str)
Path & operator=(const std::wstring & wstr)
Path & operator=(const std::string_view & strv)
template <size_t N>
Path &
operator=(const char(&) stra[N])
Path & operator/=(const Path & rhs)
Append a subdirectory or file.
Path & operator/=(const std::string_view rhs)
Append a subdirectory or file.
Path & operator/=(const char * rhs)
Append a subdirectory or file.
Path & operator/=(const std::string & rhs)
Append a subdirectory or file.
Path & operator/=(const std::wstring & rhs)
Append a subdirectory or file.
Path GetUserSettingsPath()
Returns the path to the User settings folder.
Path GetUserDataPath()
Returns the path to the User persistent data folder (for save files, for example)
Path GetUserCachePath()
Returns the path to the User cache folder.
bool MkDir(const Path & path)
Try to create a directory.
bool MkPath(const Path & path)
Try to create the directory chain of a full path.
void Listing(const Path & path)
void Remove(const Path & path)
Remove the file or directory at the given Path.
size_t RemoveAll(const Path & path)
Remove everything at the given Path.
Path GetProgramPath()
Try to obtain the full path to the program binary file.
void LocateAssets()
Attempts locate the assets directory This function will try to search for the first valid directory, probing in this order:
Path GetAssetsBasePath()
Return the absolute Path to the assets directory.
void SetAssetsBasePath(Path new_base)
Sets the directory root in which to search for assets Calling this function while (or after) any assets are loading may have undesired results.

Public Attributes

Name
constexpr char PATH_NATIVE_CHAR
constexpr char PATH_CHAR
Native string format for paths.
const std::size_t npos
The tec Path separator.
const Path assets
const Path scripts
The assets:/ pseudo root.
const Path shaders
assets:/scripts/ where to find scripts

Public Types Documentation

typedef NativePath

typedef std::string tec::Path::NativePath;

OS File system path separator.

Public Functions Documentation

function Path

explicit Path()

assets:/shaders/ where to find shaders

Builds an empty Path

function Path

explicit Path(
    const std::string_view & other
)

Builds a Path from a string_view or substring there of.

Parameters:

  • other A string_view with a path

function Path

explicit Path(
    const std::string & other,
    std::size_t pos =0,
    std::size_t count =std::string::npos
)

Builds a Path from a string or substring.

Parameters:

  • other A string with a path
  • pos Begin of the range to get a slice (default = 0)
  • count How many bytes to grab from other (default = size of other)

function Path

explicit Path(
    std::string && other
)

function Path

template <size_t N>
inline Path(
    const char(&) other[N]
)

This template builds a Path from a string literal.

function Path

explicit Path(
    const std::wstring & other
)

Builds a path from a wstring.

Parameters:

  • other A wstring containing a path

function DirExists

bool DirExists() const

Check if a directory exists.

Return: true if the directory exists

function FileExists

bool FileExists() const

Check if a file exists.

Return: true if the file exists and is accessible

function FileName

std::string FileName() const

Extract the filename from a Path.

Return: Empty string if is an invalid path for a file

function FileStem

std::string FileStem() const

Extract a filename without the extension from a Path to a file.

Return: Empty string if is an invalid path for a file

function FileExtension

std::string FileExtension() const

Extract an extension from a Path to a file.

Return: Empty string if isn't a file or does not have an extension

function BasePath

Path BasePath() const

Return base path of a full path.

  • If this is a path to a file, returns a Path to the directory that contains the file.
  • If this is a path to a directory, returns a Path to the containing directory (similar to cd .. && pwd) Containing Path or Empty if it was an invalid Path. Returned path will have a final slash

function isAbsolutePath

bool isAbsolutePath() const

Is this an absolute or relative Path?

function Relative

Path Relative() const

Get the relative part of an absolute path.

Return: a relative path

  • If this path is already relative, this has no effect.

function Subpath

Path Subpath(
    std::size_t begin,
    std::size_t end =Path::npos
) const

Return a subpath.

Parameters:

  • begin First element (each element is separated by a path separator)
  • end Last element (default Path::npos)
Path f("assets/foo/bar/mesh.obj");
f.Subpath(2, Path::npos); // ==> "bar/mesh.obj"
f.Subpath(0, 2); // ==> "assets/foo"

function SubpathFrom

Path SubpathFrom(
    const std::string & needle,
    bool include =false
) const

Return a subpath.

Parameters:

  • needle path element to search
  • include true to include the needle element in the result
Path f("./assets/foo/bar/mesh.obj");
f.SubpathFrom("assets");       // ==> "foo/bar/mesh.obj"
f.SubpathFrom("assets", true); // ==> "assets/foo/bar/mesh.obj"

function OpenFile

std::unique_ptr< FILE > OpenFile(
    PATH_OPEN_FLAGS open_mode =FS_DEFAULT
) const

Open the file at this path as a C-style stream for C functions.

Parameters:

  • open_mode what access is required, default is readonly access to existing files. Will throw a PathException if the file does not exist, or if the open_mode is invalid.

Return: a std::unique_ptr wrapped FILE pointer with a deleter for RAII.

function OpenStream

std::unique_ptr< std::fstream > OpenStream(
    PATH_OPEN_FLAGS open_mode =FS_DEFAULT
) const

Open the file at this Path as a std::fstream.

Parameters:

  • open_mode what access is required, default is readonly access to existing files. Will throw a PathException if the file does not exist, or if the open_mode is invalid.

Return: a pointer to a std::fstream.

function toString

inline std::string toString() const

Returns the string representation of this Path.

function empty

inline bool empty() const

Return: true if this Path is empty

function operator bool

inline operator bool() const

Return: true if this Path is valid.

function operator=

inline Path & operator=(
    const Path & rhs
)

function operator=

inline Path & operator=(
    const std::string & str
)

function operator=

inline Path & operator=(
    std::string && str
)

function operator=

inline Path & operator=(
    const std::wstring & wstr
)

function operator=

inline Path & operator=(
    const std::string_view & strv
)

function operator=

template <size_t N>
inline Path & operator=(
    const char(&) stra[N]
)

function operator/=

inline Path & operator/=(
    const Path & rhs
)

Append a subdirectory or file.

function operator/=

inline Path & operator/=(
    const std::string_view rhs
)

Append a subdirectory or file.

function operator/=

inline Path & operator/=(
    const char * rhs
)

Append a subdirectory or file.

function operator/=

inline Path & operator/=(
    const std::string & rhs
)

Append a subdirectory or file.

function operator/=

inline Path & operator/=(
    const std::wstring & rhs
)

Append a subdirectory or file.

function GetUserSettingsPath

static Path GetUserSettingsPath()

Returns the path to the User settings folder.

Return: The full Path. Empty/invalid if it fails

Usually these paths are:

  • *nix : /home/$USER/.config/APPNAME/
  • OSX : /Users/$USER/Library/Application Support/APPNAME/
  • WIN : APPDATA%\APPNAME\ i.e. C:\Users\USERNAME%\AppData\Roaming\trillek\ on *nix platforms, XDG_CONFIG_HOME will be respected if set.

function GetUserDataPath

static Path GetUserDataPath()

Returns the path to the User persistent data folder (for save files, for example)

Return: The full Path. Empty/invalid if it fails

Usually these paths are:

  • *nix: /home/$USER/.local/share/APPNAME/
  • OS X: /Users/$USER/Library/Application Support/APPNAME/data/
  • WIN : APPDATA%\APPNAME\ i.e. C:\Users\USERNAME%\AppData\Roaming\trillek\data\ on *nix platforms, XDG_DATA_HOME will be respected if set.

function GetUserCachePath

static Path GetUserCachePath()

Returns the path to the User cache folder.

Return: The full Path. Empty/invalid if it fails

Usually these paths are:

  • *nix: /home/$USER/.cache/APPNAME/
  • OS X: /Users/$USER/Library/Application Support/APPNAME/cache/
  • WIN : LOCALAPPDATA%\APPNAME\ i.e. C:\Users\USERNAME%\AppData\Local\trillek\ on *nix platforms, XDG_CACHE_HOME will be respected if set.

function MkDir

static bool MkDir(
    const Path & path
)

Try to create a directory.

Return: true if success or the dir exists

function MkPath

static bool MkPath(
    const Path & path
)

Try to create the directory chain of a full path.

Parameters:

  • path Absolute Path

Return: True if success.

function Listing

static void Listing(
    const Path & path
)

function Remove

static void Remove(
    const Path & path
)

Remove the file or directory at the given Path.

Parameters:

  • path The Path to delete, if it's a directory, it must be empty. This method will throw a tec::PathException if the file does not exist, if the path is a non-empty directory, or if access otherwise prevents removing the file

function RemoveAll

static size_t RemoveAll(
    const Path & path
)

Remove everything at the given Path.

Parameters:

  • path The Path to delete, this may be a file or directory.

Return: the number of items deleted This method will throw a tec::PathException if the path does not exist, or if permissions otherwise prevent removing any file.

This function attempts to verify that it can delete all files before actually doing so, If any file fails the initial access checks, then no files or directories will be removed. This method will also not permit deleting certain kinds of special files.

Note: do not depend on the access check. A set of files can pass the initial access check, but still fail to delete, in which case RemoveAll will throw an exception and an unspecified number of files could be removed.

function GetProgramPath

static Path GetProgramPath()

Try to obtain the full path to the program binary file.

Return: Absolute Path to the current executable. Empty if this fails.

function LocateAssets

static void LocateAssets()

Attempts locate the assets directory This function will try to search for the first valid directory, probing in this order:

  • ./assets/
  • EXE_PATH/assets/
  • EXE_PATH/../assets/
  • EXE_PATH/../share/assets/ Were EXE_PATH is the value as returned by GetProgramPath(). The first valid path is cached, and can then be later read with GetAssetsBasePath().

function GetAssetsBasePath

static Path GetAssetsBasePath()

Return the absolute Path to the assets directory.

  • If this wasn't set, then it will first internally call LocateAssets()
  • If you intend to load an asset, use [Path::assets](/engine/Classes/classtec_1_1_path/#variable-assets) / foo or [Path](/engine/Classes/classtec_1_1_path/)("assets:/foo.bar")

function SetAssetsBasePath

static void SetAssetsBasePath(
    Path new_base
)

Sets the directory root in which to search for assets Calling this function while (or after) any assets are loading may have undesired results.

Public Attributes Documentation

variable PATH_NATIVE_CHAR

static constexpr char PATH_NATIVE_CHAR = '/';

variable PATH_CHAR

static constexpr char PATH_CHAR = '/';

Native string format for paths.

variable npos

static const std::size_t npos = std::string::npos;

The tec Path separator.

a value indicating "far as possible"

variable assets

static const Path assets {"assets:", "/"};

variable scripts

static const Path scripts {"assets:", "/scripts/"};

The assets:/ pseudo root.

variable shaders

static const Path shaders {"assets:", "/shaders/"};

assets:/scripts/ where to find scripts


Updated on 2022-07-24 at 19:27:35 +0000