1
0
Fork 0

- don't prepend slash on relative paths or archaic paths with drive letters

- documentation updates
This commit is contained in:
mfranz 2007-06-14 11:26:16 +00:00
parent 8e4209c5cb
commit 183672f8a2

View file

@ -19,20 +19,25 @@ var _gen_ifmt_test = func(ifmt) {
}
}
# Generate file type test predicates for the following types:
# Usage: io.isdir(io.stat(filename)[2]);
# Generate file type test predicates isdir(), isreg(), islnk(), etc.
# Usage: var s = io.stat(filename); # nil -> doesn't exist, broken link
# if (s != nil and io.isdir(s[2])) { ... }
var ifmts = {dir:4, reg:8, lnk:10, sock:12, fifo:1, blk:6, chr:2};
foreach(fmt; keys(ifmts))
caller(0)[0]["is" ~ fmt] = _gen_ifmt_test(ifmts[fmt]);
# Removes superfluous slashes, emtpy and "." elements, expands
# all ".." elements, and turns all backslashes into slashes.
# The result will start with a slash if it started with a slash
# or backslash, it will end without slash. Should be applied on
# absolute paths, otherwise ".." elements might be resolved wrongly.
var fixpath = func(path) {
var d = "";
for(var i = 0; i < size(path); i += 1) {
if(path[i] == `\\`) d ~= "/";
else d ~= chr(path[i]);
}
var prefix = path[0] == `/` ? "/" : "";
var stack = [];
foreach(var e; split("/", d)) {
if(e == "." or e == "") continue;
@ -40,8 +45,8 @@ var fixpath = func(path) {
else append(stack, e);
}
if(!size(stack)) return "/";
path = "";
foreach(var s; stack) path ~= "/" ~ s;
return path;
path = stack[0];
foreach(var s; subvec(stack, 1)) path ~= "/" ~ s;
return prefix ~ path;
}