Skip to content

Filepath

Spice
import "std/io/filepath";

FilePath struct

Represents a path to a file or directory on the local file system

Constructors

ctor

Spice
public p FilePath.ctor()

Construct an empty file path

ctor

Spice
public p FilePath.ctor(string pathStr)

Construct a file path from a raw string

Parameters

Name Type Description
pathStr string Path string

ctor

Spice
public p FilePath.ctor(const String& pathStr)

Construct a file path from a String

Parameters

Name Type Description
pathStr const String& Path string

Methods

replaceSeparator

Spice
public p FilePath.replaceSeparator(char separator)

Replace every path separator in the path with the given one

Parameters

Name Type Description
separator char Separator to use

makeGeneric

Spice
public inline p FilePath.makeGeneric()

Convert the path to use the generic (UNIX) path separator

makeNative

Spice
public inline p FilePath.makeNative()

Convert the path to use the native path separator of the current platform

isEmpty

Spice
public inline f<bool> FilePath.isEmpty()

Checks if the filepath is empty

Returns: bool — Is the filepath empty

toString

Spice
public inline f<string> FilePath.toString()

Returns the content of the filepath without modifying.

Returns: string — The content of the filepath

toGenericString

Spice
public inline f<string> FilePath.toGenericString()

Returns the content of the filepath using the UNIX path separator.

Returns: string — The content of the filepath using the UNIX path separator

toNativeString

Spice
public inline f<string> FilePath.toNativeString()

Returns the content of the filepath using the native path separator.

Returns: string — The content of the filepath using the native path separator

getFileName

Spice
public inline f<String> FilePath.getFileName()

Returns the parent directory of the file or directory.

Example:
In: /home/user/file.txt
Out: file.txt

Returns: String — The parent directory of the file or directory

getParentDir

Spice
public inline f<String> FilePath.getParentDir()

Returns the parent directory of the file or directory.

Example:
In: /home/user/file.txt
Out: /home/user
In: file.txt
Out: (empty)

Returns: String — The parent directory of the file or directory

getBaseName

Spice
public inline f<String> FilePath.getBaseName()

Returns the base name of the file.

Example:
In: /home/user/file.txt
Out: file

Returns: String — The base name of the file

getExtension

Spice
public inline f<String> FilePath.getExtension()

Returns the extension of the file.

Example:
In: /home/user/file.txt
Out: txt

Returns: String — The extension of the file

replaceExtension

Spice
public inline p FilePath.replaceExtension(string newExtension)

Replaces the current file extension with the given new one. If the file has no extension, the new one is appended. An empty new extension removes the current one.

Example:
In: /home/user/file.txt
Out: /home/user/file.spice

Parameters

Name Type Description
newExtension string New file extension, with or without leading dot

exists

Spice
public inline f<bool> FilePath.exists()

Checks if the file or directory exists.

Returns: bool — True if the file or directory exists, false otherwise

hasRootPath

Spice
public inline f<bool> FilePath.hasRootPath()

Checks whether the path is absolute (starts with the UNIX path separator).

Returns: bool — True if the path is absolute

isAbsolute

Spice
public inline f<bool> FilePath.isAbsolute()

Checks whether the path is absolute. Alias for hasRootPath().

Returns: bool — True if the path is absolute

hasParentDirComponent

Spice
public f<bool> FilePath.hasParentDirComponent()

Checks whether any component of the path (before any symlink resolution) is exactly '..'. This is a cheap, purely lexical check meant to reject an obviously-escaping path before doing any filesystem work; it does not catch a symlink that resolves outside the intended root (use canonical()/equivalent() for that).

Returns: bool — True if the path has a '..' component

canonical

Spice
public f<FilePath> FilePath.canonical()

Resolves the path to an absolute, symlink-free, '.'/'..' -free canonical form.

Returns: FilePath — The canonical path, or an empty FilePath if resolution failed (e.g. the path does not exist)

equivalent

Spice
public f<bool> FilePath.equivalent(const FilePath& other)

Checks whether two paths refer to the same file after resolving symlinks and '.'/'..' components.

Parameters

Name Type Description
other const FilePath& Other path to compare against

Returns: bool — True if both paths resolve to the same canonical path (and resolution succeeded for both)

lexicallyNormal

Spice
public f<FilePath> FilePath.lexicallyNormal()

Lexically normalizes the path, like std::filesystem::path::lexically_normal: the separators are made generic ('/'), '.' and empty components are dropped and '..' components are resolved against the preceding component. '..' components at the start of a relative path are kept and '..' components directly after the root are dropped. The file system is not accessed, so symlinks are not resolved.

Returns: FilePath — The normalized path. '.' for an empty result of a relative path

lexicallyRelative

Spice
public f<FilePath> FilePath.lexicallyRelative(const FilePath& base)

Computes the path relative to the given base path, purely lexically, like std::filesystem::path::lexically_relative. Both paths are normalized first. The file system is not accessed, so symlinks are not resolved.

Parameters

Name Type Description
base const FilePath& Base path

Returns: FilePath — The relative path, '.' if both paths are equal, or an empty path if the roots of both paths differ (e.g. one is absolute and the other is not, or they are on different drives)

relative

Spice
public f<FilePath> FilePath.relative()

Computes the path relative to the current working directory, like std::filesystem::relative. Both the path and the working directory are made absolute and canonical, if they exist. Otherwise, they are only normalized lexically.

Returns: FilePath — The relative path, or an empty path if it could not be determined (e.g. the path is on another drive)

Functions

testFilePathPathSafetySmoke

Spice
public f<bool> testFilePathPathSafetySmoke()

Returns: bool

Operators

operator==

Spice
public inline f<bool> operator==(const FilePath& lhs, const FilePath& rhs)

Check if two file paths are equal

Parameters

Name Type Description
lhs const FilePath&
rhs const FilePath&

Returns: bool — Equal or not equal

operator!=

Spice
public inline f<bool> operator!=(const FilePath& lhs, const FilePath& rhs)

Check if two file paths are not equal

Parameters

Name Type Description
lhs const FilePath&
rhs const FilePath&

Returns: bool — Not equal or equal

operator/=

Spice
public p operator/=(FilePath& lhs, string rhs)

Append a path component to the path, inserting a separator if needed

Parameters

Name Type Description
lhs FilePath&
rhs string Path component to append

operator/=

Spice
public inline p operator/=(FilePath& lhs, const String& rhs)

Append a path component to the path, inserting a separator if needed

Parameters

Name Type Description
lhs FilePath&
rhs const String& Path component to append

operator/=

Spice
public inline p operator/=(FilePath& lhs, const FilePath& rhs)

Append a path component to the path, inserting a separator if needed

Parameters

Name Type Description
lhs FilePath&
rhs const FilePath& Path component to append

operator/

Spice
public inline f<FilePath> operator/(const FilePath& lhs, string rhs)

Join a path and a path component into a new file path, inserting a separator if needed

Parameters

Name Type Description
lhs const FilePath&
rhs string

Returns: FilePath — New joined file path

operator/

Spice
public inline f<FilePath> operator/(const FilePath& lhs, const String& rhs)

Join a path and a path component into a new file path, inserting a separator if needed

Parameters

Name Type Description
lhs const FilePath&
rhs const String&

Returns: FilePath — New joined file path

operator/

Spice
public inline f<FilePath> operator/(const FilePath& lhs, const FilePath& rhs)

Join two file paths into a new file path, inserting a separator if needed

Parameters

Name Type Description
lhs const FilePath&
rhs const FilePath&

Returns: FilePath — New joined file path