tec::FilePath

OS File system path separator.

#include <filesystem.hpp>

Public Types

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

Public Functions

Name
FilePath()
Builds a empty path.
FilePath(std::string & other, std::size_t pos =0, std::size_t count =std::string::npos)
Builds a path from a string or substring.
FilePath(const std::string_view & other, std::size_t pos =0, std::size_t count =std::string_view::npos)
Builds a path from a string_view or substring there of.
FilePath(const std::wstring & other, std::size_t pos =0, std::size_t count =std::wstring::npos)
Builds a path from a wstring or substring.
bool DirExists() const
Check if a directory exists.
bool FileExists() const
Check if a file exists.
std::string FileName() const
Extract a filename from a path.
std::string FileExtension() const
Extract a extension from a path to a file.
FilePath BasePath() const
Return base path of a full path.
bool isAbsolutePath() const
Is an absolute or relative path ?
FilePath Subpath(std::size_t begin, std::size_t end =FilePath::npos) const
Return a subpath.
FilePath SubpathFrom(const std::string & needle, bool include =false) const
Return a subpath.
void NormalizePath()
Normalize path to the OS format.
bool isValidPath() const
Check if the path is a valid absolute o relative path.
FilePath::NFilePath GetNativePath() const
Returns a path on the native OS encoding.
std::string toString() const
Returns the string representation of a path.
std::string toGenericString() const
Returns a generic string representation of a path (uses \ as path separator ALWAYS)
bool empty() const
Return if this FilePath is empty.
FilePath & operator=(const FilePath & rhs)
FilePath & operator=(const std::string & str)
FilePath & operator=(const std::wstring & wstr)
FilePath & operator=(const std::string_view & str)
FilePath & operator=(const char * str)
FilePath & operator+=(const FilePath & rhs)
Concatenate a path.
FilePath & operator+=(const char * lhs)
Concatenate a path.
FilePath & operator+=(const std::string & lhs)
Concatenate a path.
FilePath & operator+=(const std::wstring & lhs)
Concatenate a path.
FilePath & operator+=(const std::string_view & lhs)
Concatenate a path.
FilePath & operator/=(const FilePath & rhs)
Append a subdirectory or file.
FilePath & operator/=(const char * rhs)
Append a subdirectory or file.
FilePath & operator/=(const std::string & rhs)
Append a subdirectory or file.
FilePath & operator/=(const std::wstring & rhs)
Append a subdirectory or file.
FilePath GetUserSettingsPath()
Returns the path to the User settings folder.
FilePath GetUserDataPath()
Returns the path to the User persistent data folder (for save files, for example)
FilePath GetUserCachePath()
Returns the path to the User cache folder.
bool MkDir(const FilePath & path)
Try to create a directory route.
bool MkPath(const FilePath & path)
Try to create a full path.
FilePath GetProgramPath()
Try to obtain the full path to the program binary file.
FilePath GetAssetsBasePath()
Return the base directory where search the assets.
FilePath GetAssetPath(const FilePath & asset)
returns the full path to an asset
FilePath GetAssetPath(const std::string & asset)
returns the full path to an asset
FilePath GetAssetPath(const char * asset)
returns the full path to an asset
void SetAssetsBasePath(FilePath new_base)
Sets the base directory where search the assets.

Public Attributes

Name
const char PATH_SEPARATOR_C
const std::size_t npos
Native string format for paths.

Public Types Documentation

typedef NFilePath

typedef std::string tec::FilePath::NFilePath;

OS File system path separator.

Public Functions Documentation

function FilePath

explicit FilePath()

Builds a empty path.

function FilePath

FilePath(
    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 FilePath

FilePath(
    const std::string_view & other,
    std::size_t pos =0,
    std::size_t count =std::string_view::npos
)

Builds a path from a string_view or substring there of.

Parameters:

  • other A string_view 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 FilePath

FilePath(
    const std::wstring & other,
    std::size_t pos =0,
    std::size_t count =std::wstring::npos
)

Builds a path from a wstring or substring.

Parameters:

  • other A wstring 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 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 directory exists

function FileName

std::string FileName() const

Extract a filename from a path.

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

function FileExtension

std::string FileExtension() const

Extract a extension from a path to a file.

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

function BasePath

FilePath BasePath() const

Return base path of a full path.

Return: Empty string if is an invalid path. Returned path would have the final slash

If is a path of a file, returns the path to the dir that contains the file If is a path of a directory, returns the path to the dir that contains the directory (like cd .. && pwd)

function isAbsolutePath

bool isAbsolutePath() const

Is an absolute or relative path ?

function Subpath

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

Return a subpath.

Parameters:

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

function SubpathFrom

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

Return a subpath.

Parameters:

  • needle path element to search
  • include Includes the needle element on the output ?
FilePath f("./assets/foo/bar/mesh.obj");
f.SubpathFrom(assets);       // ==> "/foo/bar/mesh.obj"
f.SubpathFrom(assets, true); // ==> "/assets/foo/bar/mesh.obj"

function NormalizePath

void NormalizePath()

Normalize path to the OS format.

  • Convert slashes to the correct OS slash
  • Remove drive unit if is a *NIX OSnormalized path

function isValidPath

bool isValidPath() const

Check if the path is a valid absolute o relative path.

function GetNativePath

FilePath::NFilePath GetNativePath() const

Returns a path on the native OS encoding.

  • Normalize path
  • return wstring on Windows
  • return string on *NIX native string of the path

function toString

inline std::string toString() const

Returns the string representation of a path.

function toGenericString

std::string toGenericString() const

Returns a generic string representation of a path (uses \ as path separator ALWAYS)

function empty

inline bool empty() const

Return if this FilePath is empty.

function operator=

inline FilePath & operator=(
    const FilePath & rhs
)

function operator=

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

function operator=

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

function operator=

inline FilePath & operator=(
    const std::string_view & str
)

function operator=

inline FilePath & operator=(
    const char * str
)

function operator+=

inline FilePath & operator+=(
    const FilePath & rhs
)

Concatenate a path.

function operator+=

inline FilePath & operator+=(
    const char * lhs
)

Concatenate a path.

function operator+=

inline FilePath & operator+=(
    const std::string & lhs
)

Concatenate a path.

function operator+=

inline FilePath & operator+=(
    const std::wstring & lhs
)

Concatenate a path.

function operator+=

inline FilePath & operator+=(
    const std::string_view & lhs
)

Concatenate a path.

function operator/=

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

Append a subdirectory or file.

function operator/=

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

Append a subdirectory or file.

function operator/=

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

Append a subdirectory or file.

function operator/=

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

Append a subdirectory or file.

function GetUserSettingsPath

static FilePath GetUserSettingsPath()

Returns the path to the User settings folder.

Return: string with the full path. Empty string if fails

Usually this paths are : *nix : /home/USER/.config/APPNAME/ OSX : /Users/USER/Library/Application Support/APPNAME/ WIN : C:\Users\USER\AppData\Roaming\APPNAME\

function GetUserDataPath

static FilePath GetUserDataPath()

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

Return: string with the full path. Empty string if fails

Usually this paths are : *nix : /home/USER/.local/share/APPNAME/ OSX : /Users/USER/Library/Application Support/APPNAME/data/ WIN : C:\Users\USER\AppData\Roaming\APPNAME\data\

function GetUserCachePath

static FilePath GetUserCachePath()

Returns the path to the User cache folder.

Return: string with the full path. Empty string if fails

Usually this paths are : *nix : /home/USER/.cache/APPNAME/ OSX : /Users/USER/Library/Application Support/APPNAME/cache/ WIN : C:\Users\USER\AppData\Local\APPNAME\

function MkDir

static bool MkDir(
    const FilePath & path
)

Try to create a directory route.

Return: True if success or the dir exists

function MkPath

static bool MkPath(
    const FilePath & path
)

Try to create a full path.

Parameters:

  • path Absolute path

Return: True if success.

function GetProgramPath

static FilePath GetProgramPath()

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

Return: string with the full path. Empty string if fails

function GetAssetsBasePath

static FilePath GetAssetsBasePath()

Return the base directory where search the assets.

If isn't set, then would try to search a valid directory path, probing with this paths:

  • ./assets/
  • EXE_PATH/assets/
  • EXE_PATH/../assets/
  • EXE_PATH/../share/assets/ Were EXE_PATH is the value returned by GetProgramPath() If find a valid path, then stores it for the future

function GetAssetPath

static FilePath GetAssetPath(
    const FilePath & asset
)

returns the full path to an asset

Parameters:

  • asset Relative path to asset base folder that identify a asset file (for example "shaders/foo.vert")

function GetAssetPath

static FilePath GetAssetPath(
    const std::string & asset
)

returns the full path to an asset

Parameters:

  • asset String path-like that identify a asset file (for example "shaders/foo.vert")

function GetAssetPath

static FilePath GetAssetPath(
    const char * asset
)

returns the full path to an asset

Parameters:

  • asset String path-like that identify a asset file (for example "shaders/foo.vert")

function SetAssetsBasePath

static void SetAssetsBasePath(
    FilePath new_base
)

Sets the base directory where search the assets.

Public Attributes Documentation

variable PATH_SEPARATOR_C

static const char PATH_SEPARATOR_C = '/';

variable npos

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

Native string format for paths.


Updated on 6 September 2021 at 18:30:10 UTC