Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.1

0.9.1 / src / ar.lucb

## 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.
import c
from luce_std import files
from luce_geocore.core.growing import Growing

extern func getcwd(buffer: u8*, size: usize) -> c.str?
extern func strlen(text: c.str) -> usize

## `path` made absolute from the working directory and normalized.
pub func absolute(path: str, out: Growing[u8]*) -> !:
    out.clear()
    if path.length > 0 and path.bytes[0] == '/':
        try normalize_into(path, out)
        return
    var buffer: u8[4096]
    let found = getcwd(&buffer[0], 4096) else error(files.invalid_options, "the working directory cannot be read")
    var joined = Growing[u8]()
    defer joined.close()
    try joined.assign(buffer[..<strlen(found)])
    try joined.push('/')
    for byte in path.bytes: try joined.push(byte)
    try normalize_into((str)joined.view(), out)

## 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 asset.length > 0 and asset.bytes[0] == '/':
        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).
pub func directory_of(path: str) -> str:
    var at = path.length
    while at > 0:
        at -= 1
        if path.bytes[at] == '/': return (str)path.bytes[..<at] if at > 0 else "/"
    return ""

## `path` with "." and ".." components and repeated separators removed; a
## leading "/" kept. Leading ".." components of a relative path stay.
pub func normalize_into(path: str, out: Growing[u8]*) -> !:
    out.clear()
    let rooted = path.length > 0 and path.bytes[0] == '/'
    if rooted: try out.push('/')
    let floor = out.count
    var at: usize = 0
    while at < path.length:
        while at < path.length and path.bytes[at] == '/': at += 1
        let start = at
        while at < path.length and path.bytes[at] != '/': at += 1
        let part = (str)path.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: continue
        if out.count > floor: try out.push('/')
        for byte in part.bytes: try out.push(byte)