dymokomi/luce-usd
Source of release 0.9.5
## Asset paths resolved as OpenUSD's default resolver and Sdf's layer
## utilities do (pxr/usd/ar/defaultResolver.cpp, pxr/usd/sdf/layerUtils.cpp):
## an absolute path stays as it is; a relative one is anchored at the
## directory of the layer that authored it; inside a package layer
## ("/a/b.usdz[c/d.usda]") a relative path names another file of the
## package ("/a/b.usdz[c/e.usda]"). Paths are normalized lexically, and a
## layer's identity is its normalized absolute path.
##
## Roots are luce-std paths' (`/`; on Windows `C:/`, `//server/share/` and
## `\\?\` device paths too), and identifiers separate with `/`: on Windows a
## backslash is read as one, as OpenUSD's resolver does (TfNormPath).
import c
from luce_std import files, paths, process
from luce_geocore.core.growing import Growing
## `path` made absolute from the working directory and normalized.
pub func absolute(path: str, out: Growing[u8]*) -> !:
out.clear()
if paths.is_absolute(path):
try normalize_into(path, out)
return
# A relative path, or on Windows one rooted on the working directory's drive (`\a`).
let here = try process.current_directory()
defer here.release()
let joined = try paths.join(here.value, path)
defer joined.release()
try normalize_into(joined.value, out)
## Whether `path` starts at a root (`/a`; on Windows `C:/a`, `\\server\share`, `\a`):
## an asset path anchored nowhere.
pub func rooted(path: str) -> bool:
return paths.is_rooted(path)
## The identifier `asset` names when authored in the layer `anchor` (an
## identifier: an absolute path, or a package path "outer[inner]").
pub func anchored(asset: str, anchor: str, out: Growing[u8]*) -> !:
out.clear()
if rooted(asset):
try normalize_into(asset, out)
return
if let split = package_split(anchor):
# Relative to the packaged layer, within the package.
var inner = Growing[u8]()
defer inner.close()
try inner.assign(directory_of(split.1).bytes)
if inner.count > 0: try inner.push('/')
for byte in asset.bytes: try inner.push(byte)
var normal = Growing[u8]()
defer normal.close()
try normalize_into((str)inner.view(), &normal)
try out.assign(split.0.bytes)
try out.push('[')
for byte in normal.view(): try out.push(byte)
try out.push(']')
return
var joined = Growing[u8]()
defer joined.close()
try joined.assign(directory_of(anchor).bytes)
try joined.push('/')
for byte in asset.bytes: try joined.push(byte)
try normalize_into((str)joined.view(), out)
## A package identifier's outer file and inner path, split at its last
## top-level bracket ("a.usdz[b.usda]" → ("a.usdz", "b.usda")).
pub func package_split(identifier: str) -> (str, str)?:
if identifier.length < 3 or identifier.bytes[identifier.length - 1] != ']': return none
var depth: usize = 0
var at = identifier.length
while at > 0:
at -= 1
let byte = identifier.bytes[at]
if byte == ']': depth += 1
elif byte == '[':
depth -= 1
if depth == 0: return ((str)identifier.bytes[..<at], (str)identifier.bytes[at + 1..<identifier.length - 1])
return none
## Whether the file an identifier names exists (a package's outer file).
pub func exists(identifier: str) -> bool:
var target = identifier
while let split = package_split(target): target = split.0
var native = Growing[u8]()
defer native.close()
native.assign(target.bytes) catch failure: return false
native.push(0) catch failure: return false
return files.exists((c.str)native.items.data)
## Everything before the last '/' ("" for none); a root stays whole ("/", "C:/").
pub func directory_of(path: str) -> str:
let prefix = paths.root(path)
var at = path.length
while at > prefix.length:
at -= 1
if path.bytes[at] == '/': return (str)path.bytes[..<at]
return prefix
## `path` with "." and ".." components and repeated separators removed, `/`
## between components (a Windows backslash too); its root kept and never
## crossed. Leading ".." components of a relative path stay.
pub func normalize_into(path: str, out: Growing[u8]*) -> !:
out.clear()
var slashed = Growing[u8]()
defer slashed.close()
for byte in path.bytes: try slashed.push('/' if paths.separator(byte) else byte)
let text = (str)slashed.view()
let prefix = paths.root(text)
try out.assign(prefix.bytes)
let rooted_path = prefix.length > 0
# A drive-relative root (`C:`) takes its first component without a separator.
let drive_only = prefix.length == 2 and prefix.bytes[1] == ':'
let floor = out.count
var at = prefix.length
while at < text.length:
while at < text.length and text.bytes[at] == '/': at += 1
let start = at
while at < text.length and text.bytes[at] != '/': at += 1
let part = (str)text.bytes[start..<at]
if part.length == 0 or part == ".": continue
if part == "..":
# Drop the previous component unless there is none (or it is "..").
var last = out.count
while last > floor and out.items[last - 1] != '/': last -= 1
let previous = (str)out.items[last..<out.count]
if out.count > floor and previous != "..":
out.count = last - 1 if last > floor else floor
continue
if rooted_path: continue
if out.count > 0 and out.items[out.count - 1] != '/' and not (drive_only and out.count == floor): try out.push('/')
for byte in part.bytes: try out.push(byte)