Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Path Struct Reference

Represents a posix or windows file system path. More...

#include <Path.h>

Classes

struct  ParsedView
 Holds the various parsed components of a path. More...
 
struct  Posix
 
struct  Windows
 

Public Types

enum  Type {
  AsPosix ,
  AsWindows ,
  AsNative = AsPosix
}
 Path type (windows or posix) More...
 

Static Public Member Functions

static bool join (String &output, Span< const StringView > inputs, StringView separator=SeparatorStringView(), bool skipEmpty=false)
 Joins multiple StringView with a Separator into an output String. More...
 
static bool parseNameExtension (const StringView input, StringView &name, StringView &extension)
 Splits a StringView of type "name.ext" into "name" and "ext". More...
 
static bool parse (StringView input, Path::ParsedView &pathView, Type type)
 Splits a Posix or Windows path into a ParsedView. More...
 
static StringView dirname (StringView input, Type type, int repeat=0)
 Returns the directory name of a path. More...
 
static StringView basename (StringView input, Type type)
 Returns the base name of a path. More...
 
static StringView basename (StringView input, StringView suffix)
 Returns the base name of a path. More...
 
static bool isAbsolute (StringView input, Type type)
 Checks if a path is absolute. More...
 
static constexpr StringView SeparatorStringView ()
 Path separator StringView for current platform. More...
 
static bool normalize (StringView view, Vector< StringView > &components, String *output, Type type)
 Resolves all .. to output a normalized path String. More...
 
static bool relativeFromTo (StringView source, StringView destination, String &output, Type type, Type outputType=AsNative)
 Get relative path that appended to source resolves to destination. More...
 
static bool append (String &output, Span< const StringView > paths, Type inputType)
 Append to an existing path a series of StringView with a separator. More...
 
static bool endsWithSeparator (StringView path)
 Check if the path ends with a Windows or Posix separator. More...
 
static StringView removeStartingSeparator (StringView path)
 Return a path without its (potential) starting separator. More...
 
static bool normalizeUNCAndTrimQuotes (StringView fileLocation, Vector< StringView > &components, String &outputPath, Type type)
 

Static Public Attributes

static constexpr char Separator = '/'
 Path separator char for current platform. More...
 

Detailed Description

Represents a posix or windows file system path.

Member Enumeration Documentation

◆ Type

Path type (windows or posix)

Member Function Documentation

◆ append()

static bool SC::Path::append ( String output,
Span< const StringView paths,
Type  inputType 
)
static

Append to an existing path a series of StringView with a separator.

Parameters
[out]outputThe destination string containing the existing path, that will be extended
[in]pathsThe path components to join, appended to output
[in]inputTypeSpecify to append as Windows or Posix path components
Returns
true if the output path can joined properly

◆ basename() [1/2]

static StringView SC::Path::basename ( StringView  input,
StringView  suffix 
)
static

Returns the base name of a path.

Suffix is stripped if existing. Trailing separators are ignored.

For example:

Path::basename("/a/basename.html", ".html") == "basename";
static StringView basename(StringView input, Type type)
Returns the base name of a path.
Parameters
[in]inputThe StringView with path to be parsed. Trailing separators are ignored.
[in]suffixThe StringView extension (or suffix in general) to strip if existing.
Returns
Substring of input holding the base name

◆ basename() [2/2]

static StringView SC::Path::basename ( StringView  input,
Type  type 
)
static

Returns the base name of a path.

Trailing separators are ignored.

For example:

Path::basename("/a/basename", Path::AsPosix) == "basename";
Path::basename("/a/basename//", Path::AsPosix) == "basename";
Parameters
[in]inputThe StringView with path to be parsed. Trailing separators are ignored.
[in]typeSpecify to parse as Windows or Posix path
Returns
Substring of input holding the base name

◆ dirname()

static StringView SC::Path::dirname ( StringView  input,
Type  type,
int  repeat = 0 
)
static

Returns the directory name of a path.

Trailing separators are ignored.

For example:

Path::dirname("/dirname/basename", Path::AsPosix) == "/dirname";
Path::dirname("/dirname/basename//", Path::AsPosix) == "/dirname";
Path::dirname("C:\\dirname\\basename", Path::AsWindows) == "C:\\dirname";
Path::dirname("\\dirname\\basename\\\\", Path::AsWindows) == "\\dirname";
static StringView dirname(StringView input, Type type, int repeat=0)
Returns the directory name of a path.
Parameters
[in]inputThe StringView with path to be parsed. Trailing separators are ignored.
[in]typeSpecify to parse as Windows or Posix path
repeathow many directory levels should be removed dirname("/1/2/3/4", repeat=1) == "/1/2"
Returns
Substring of input holding the directory name

◆ endsWithSeparator()

static bool SC::Path::endsWithSeparator ( StringView  path)
static

Check if the path ends with a Windows or Posix separator.

Parameters
pathThe path to check
Returns
true if path ends with a separator

◆ isAbsolute()

static bool SC::Path::isAbsolute ( StringView  input,
Type  type 
)
static

Checks if a path is absolute.

For example:

Path::isAbsolute("/dirname/basename", Path::AsPosix) == true; // Posix Absolute
Path::isAbsolute("./dirname/basename", Path::AsPosix) == false; // Posix Relative
Path::isAbsolute("C:\\dirname\\basename", Path::AsWindows) == true; // Windows with Drive
Path::isAbsolute("\\\\server\\dir", Path::AsWindows) == true; // Windows with Network
Path::isAbsolute("\\\\?\\C:\\server\\dir", Path::AsWindows) == true; // Windows with Long
Path::isAbsolute("..\\dirname\\basename", Path::AsWindows) == false; // Windows relative
static bool isAbsolute(StringView input, Type type)
Checks if a path is absolute.
Parameters
[in]inputThe StringView with path to be parsed. Trailing separators are ignored.
[in]typeSpecify to parse as Windows or Posix path
Returns
true if input is absolute

◆ join()

static bool SC::Path::join ( String output,
Span< const StringView inputs,
StringView  separator = SeparatorStringView(),
bool  skipEmpty = false 
)
static

Joins multiple StringView with a Separator into an output String.

Parameters
[out]outputThe output string receiving the path
[in]inputsThe input paths to join
[in]separatorThe separator to use. By default / on Posix and \ on Windows
[in]skipEmptyIf true will skip empty entries in inputs Span
Returns
true if the Path was successfully joined

◆ normalize()

static bool SC::Path::normalize ( StringView  view,
Vector< StringView > &  components,
String output,
Type  type 
)
static

Resolves all .. to output a normalized path String.

For example:

Path::normalize("/Users/SC/../Documents/", cmp, &path, Path::AsPosix);
SC_RELEASE_ASSERT(path == "/Users/Documents");
static bool normalize(StringView view, Vector< StringView > &components, String *output, Type type)
Resolves all .. to output a normalized path String.
Parameters
viewThe path to be normalized (but it should not be a view() of the output String)
componentsThe parsed components that once joined will provide the normalized string
[out]output(Optional) pointer to String that will receive the normalized Path (if nullptr)
[in]typeSpecify to parse as Windows or Posix path
Returns
true if the Path was successfully parsed and normalized

◆ parse()

static bool SC::Path::parse ( StringView  input,
Path::ParsedView pathView,
Type  type 
)
static

Splits a Posix or Windows path into a ParsedView.

Parameters
[in]inputThe StringView with path to be parsed
[out]pathViewThe output parsed ParsedView with all components of path
[in]typeSpecify to parse as Windows or Posix path
Returns
true if path was parsed successfully

◆ parseNameExtension()

static bool SC::Path::parseNameExtension ( const StringView  input,
StringView name,
StringView extension 
)
static

Splits a StringView of type "name.ext" into "name" and "ext".

Parameters
[in]inputAn input path coded as UTF8 sequence (ex. "name.ext")
[out]nameOutput string holding name ("name" in "name.ext")
[out]extensionOutput string holding extension ("ext" in "name.ext")
Returns
false if both name and extension will be empty after trying to parse them

Example:

SC_TEST_EXPECT(Path::parseNameExtension("name.ext", name, ext));
SC_TEST_EXPECT(name == "name");
SC_TEST_EXPECT(ext == "ext");
SC_TEST_EXPECT(name.isEmpty());
SC_TEST_EXPECT(ext.isEmpty());
SC_TEST_EXPECT(name.isEmpty());
SC_TEST_EXPECT(ext.isEmpty());
SC_TEST_EXPECT(name.isEmpty());
SC_TEST_EXPECT(ext == "ext");
SC_TEST_EXPECT(name == "name");
SC_TEST_EXPECT(ext.isEmpty());
SC_TEST_EXPECT(Path::parseNameExtension("name.name.ext", name, ext));
SC_TEST_EXPECT(name == "name.name");
SC_TEST_EXPECT(ext == "ext");
SC_TEST_EXPECT(name == "name.");
SC_TEST_EXPECT(ext.isEmpty());
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
static bool parseNameExtension(const StringView input, StringView &name, StringView &extension)
Splits a StringView of type "name.ext" into "name" and "ext".

◆ relativeFromTo()

static bool SC::Path::relativeFromTo ( StringView  source,
StringView  destination,
String output,
Type  type,
Type  outputType = AsNative 
)
static

Get relative path that appended to source resolves to destination.

For example:

Path::relativeFromTo("/a/b/1/2/3", "/a/b/d/e", path, Path::AsPosix, Path::AsPosix);
SC_TEST_ASSERT(path == "../../../d/e");
static bool relativeFromTo(StringView source, StringView destination, String &output, Type type, Type outputType=AsNative)
Get relative path that appended to source resolves to destination.
Parameters
[in]sourceThe source Path
[in]destinationThe destination Path
[out]outputThe output relative path computed that transforms source into destination
[in]typeSpecify to parse as Windows or Posix path
[in]outputTypeSpecify if the output relative path should be formatted as a Posix or Windows path
Returns
true if source and destination paths can be properly parsed as absolute paths

◆ removeStartingSeparator()

static StringView SC::Path::removeStartingSeparator ( StringView  path)
static

Return a path without its (potential) starting separator.

Parameters
pathThe path to use
Returns
A StringView without its initial separator

◆ SeparatorStringView()

static constexpr StringView SC::Path::SeparatorStringView ( )
inlinestaticconstexpr

Path separator StringView for current platform.

Member Data Documentation

◆ Separator

constexpr char SC::Path::Separator = '/'
staticconstexpr

Path separator char for current platform.


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