Path

is a simple file path library for Carp.

Installation

(load "https://veitheller.de/git/carpentry/path@0.0.2")

Usage

The Path module mostly operates on String arguments. It allows you to split, join, and merge paths and extensions in a lot of different ways. It also has some functions to work with the PATH environment variable.

It assumes either Windows or POSIX-style separators.

</>

defn

(λ [a, b] String)

                    (</> before after)
                

joins before and after using the default path separator.

absolute

defn

(λ [(Ref String a)] (Maybe String))

                    (absolute p)
                

makes an absolute path from p.

absolute?

defn

(λ [(Ref String a)] Bool)

                    (absolute? p)
                

checks whether a path is absolute.

As such, it is the inverse to relative.

add-extension

defn

(λ [(Ref String a), (Ref String b)] String)

                    (add-extension p ext)
                

adds an extension ext to a path p.

basename

defn

(λ [(Ref String a)] String)

                    (basename p)
                

gets the basename of the path p.

cwd

defn

(λ [] (Maybe String))

                    (cwd)
                

returns the current working directory as a Maybe. The ways in which it can fail are OS-dependent, but it should happen relatively rare.

drop-extension

defn

(λ [(Ref String a)] String)

                    (drop-extension p)
                

drops the extension of a path p. Does nothing if there is none.

extension

defn

(λ [(Ref String a)] (Maybe String))

                    (extension p)
                

gets the extension of a file as a Maybe.

filename

defn

(λ [(Ref String a)] (Maybe String))

                    (filename p)
                

gets the filename of the path p as a (Maybe String).

It will return Nothing if an empty string is passed.

get-search-path

defn

(λ [] (Maybe (Array String)))

                    (get-search-path)
                

gets the PATH environment variable and splits it.

has-extension?

defn

(λ [(Ref String a)] Bool)

                    (has-extension? p)
                

cheks whether the path p has an extension.

is-extension?

defn

(λ [(Ref String a), (Ref String b)] Bool)

                    (is-extension? p ext)
                

checks whether the path p has the extension ext.

join

defn

(λ [(Ref (Array String) a)] String)

                    (join ps)
                

joins the path components ps into a path.

As such, it is the inverse to split.

path-max

external

Int

defines the maximum path length on this OS.

relative?

defn

(λ [(Ref String a)] Bool)

                    (relative? p)
                

checks whether a path is relative.

As such, it is the inverse to absolute.

replace-extension

defn

(λ [(Ref String a), (Ref String b)] String)

                    (replace-extension p ext)
                

replaces the extension of a path p with ext. Adds an extension if there previously was none.

search-path-separator

def

Char

is the separator for the PATH environment variable we use on this OS.

search-path-separator?

defn

(λ [Char] Bool)

                    (search-path-separator? c)
                

separator

def

Char

is the default separator we use on this OS.

separator?

defn

(λ [(Ref Char StaticLifetime)] Bool)

                    (separator? c)
                

checks whether the character c is a separator for the PATH environment variable on this OS.

separators

def

(Array Char)

is the possible separators we could use on this OS.

split

defn

(λ [(Ref String a)] (Array String))

                    (split p)
                

splits the path p into its components.

As such, it is the inverse to join.

split-extension

defn

(λ [(Ref String a)] (Maybe (Pair String String)))

                    (split-extension p)
                

splits the path p on its extension.

It will return a (Maybe (Pair String String)). Maybe because there might not be an extension, and Pair because it will return the part before and after the extension.

Examples on POSIX:

(split-extension "file.txt")
; => (Maybe.Just (Pair "file" "txt"))
(split-extension "file")
; => (Maybe.Nothing)
(split-extension "file/file.txt")
; => (Maybe.Just (Pair "file/file" "txt"))
(split-extension "file.txt/veit")
; => (Maybe.Nothing)
(split-extension "file.txt/veit.ext")
; => (Maybe.Just (Pair "file.txt/veit" "ext"))
(split-extension "file/path.txt.bob.fred")
; => (Maybe.Just (Pair "file/path.txt.bob" "fred"))

split-search-path

defn

(λ [(Ref String a)] (Array String))

                    (split-search-path p)
                

splits a PATH environment variable p.