Antra

Standardbibliothek

Antra::Path

import Antra::Path

File paths that work the same on Windows, macOS and Linux.

Paths come out in the system's own spelling: join, normalize, split, dirname and parent use \ on Windows and / elsewhere. Going in, Windows takes both \ and / (so "../resources" works everywhere) and knows C:\ and \\server\share as roots; elsewhere only / separates. portable gives a path with / for files that travel between systems.

import Antra::Path
val icon = Path.join(System.script_dir().!, "assets", "icon.png")
Path.basename(icon)     # "icon.png"
Path.extension(icon)    # ".png"
icon                    # "C:\\game\\assets\\icon.png" on Windows
Path.portable(icon)     # "C:/game/assets/icon.png" on Windows

Überblick#

Pfade zusammensetzen und zerlegen, ohne an / oder \ zu denken. Was herauskommt, ist in der Schreibweise des Systems: join, normalize, split, dirname und parent liefern unter Windows \, sonst /. Hinein darf unter Windows beides, "../resources" funktioniert also überall; außerdem gelten dort C:\ bzw. \\server\freigabe als Wurzel. portable liefert einen Pfad mit / auf jedem System — für Dateien, die zwischen Systemen wandern (Konfiguration, Manifest, Archiv-Eintrag).

import Antra::Path
import Antra::System

val icon = Path.join(System.script_dir().!, "assets", "icon.png")
                                       # "C:\\game\\assets\\icon.png" unter Windows
Path.basename(icon)                    # "icon.png"
Path.stem(icon)                        # "icon"
Path.extension(icon)                   # ".png"
Path.dirname("dist/app/main.ant")      # "dist/app"
Path.parent("/")                       # undef — oben angekommen
Path.normalize("a//b/./c/../d/")       # "a/b/d"
Path.split("/usr/local/bin")           # ["/", "usr", "local", "bin"]
Path.absolute?("C:\\Spiele")           # true unter Windows
Path.native("C:/Users/ada")            # "C:\\Users\\ada" unter Windows
Path.portable(icon)                    # "C:/game/assets/icon.png" unter Windows
Path.separator()                       # "/" bzw. "\\"
Path.list_separator()                  # ":" bzw. ";" (wie in PATH)

Path rechnet nur mit Text; ob es die Datei gibt, sagt FS.exists(...).

Funktionen#

fn absolute?(path :- String) :- Bool

true for a path that does not depend on the current directory: /usr/bin, on Windows also C:\Users or \\server\share.

Path.absolute?("/etc/hosts")   # true
Path.absolute?("lib/x.ant")    # false
fn basename(path :- String) :- String

The last part of a path (a trailing / is ignored).

Path.basename("dist/app/main.ant")   # "main.ant"
Path.basename("dist/app/")           # "app"
fn dirname(path :- String) :- String

The folder a path is in: "a/b" for "a/b/c", "." for a plain name, the root for a path directly below it.

Path.dirname("dist/app/main.ant")   # "dist/app"
Path.dirname("main.ant")            # "."
Path.dirname("/etc")                # "/"
fn extension(path :- String) :- String

The file extension with its dot, "" without one. A leading dot (.profile) is part of the name, not an extension.

Path.extension("game.tar.gz")   # ".gz"
Path.extension(".profile")      # ""
fn join(first :- String, ..rest :- ..String) :- String

Joins path parts with the system's separator (\ on Windows, else /). Empty parts are skipped; a part that is absolute starts over, as in a shell cd.

Path.join("dist", "app", "main.ant")   # "dist/app/main.ant"
Path.join("dist/", "/tmp", "x")        # "/tmp/x"
Path.join("C:\\game", "assets")        # "C:\\game\\assets" on Windows
fn list_separator() :- String

The separator of path lists like PATH: ";" on Windows, else ":".

String.split(System.get_env_or("PATH", ""), Path.list_separator())
fn native(path :- String) :- String

The path in the system's own spelling (\ on Windows), for a path that did not come from these functions; Antra itself takes either form.

Path.native("C:/Users/ada")   # "C:\\Users\\ada" on Windows
fn normalize(path :- String) :- String

Cleans a path up: the system's separator, no . or empty parts, .. resolved where possible, no trailing separator. The file system is not consulted (symlinks stay as written). An empty path becomes ".".

Path.normalize("a//b/./c/../d/")   # "a/b/d"
Path.normalize("../x/..")          # ".."
fn parent(path :- String) :- String?

Like dirname, but undef where there is nothing above: at a root and for "." — the end condition when walking up a directory tree.

Path.parent("/home/ada")   # "/home"
Path.parent("/")           # undef
fn portable(path :- String) :- String

The path with / as separator on every system, for files that travel between systems (a manifest, a config, an archive entry). On Windows \ becomes /; elsewhere \ is an ordinary character and stays.

Path.portable("app\\src\\main.ant")   # "app/src/main.ant" on Windows
fn separator() :- String

The system's path separator: "\\" on Windows, else "/".

Path.separator()        # "/" on Linux and macOS
fn split(path :- String) :- [String]

The parts of a path; an absolute path starts with its root.

Path.split("/usr/local/bin")   # ["/", "usr", "local", "bin"]
Path.split("a/b")              # ["a", "b"]
fn stem(path :- String) :- String

The last part without its extension.

Path.stem("levels/forest.and")   # "forest"

Quelle dieser Seite