2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-17 13:12:51 -05:00
|
|
|
import os::getcwd;
|
2011-06-17 17:52:37 -05:00
|
|
|
import os_fs;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-03-10 08:56:51 -06:00
|
|
|
native "rust" mod rustrt {
|
2011-09-01 19:27:58 -05:00
|
|
|
fn rust_file_is_dir(path: str::sbuf) -> int;
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
|
|
|
|
2011-09-01 19:27:58 -05:00
|
|
|
fn path_sep() -> istr { ret str::from_char(os_fs::path_sep); }
|
2011-03-10 08:56:51 -06:00
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
type path = istr;
|
2011-03-10 08:56:51 -06:00
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
fn dirname(p: &path) -> path {
|
2011-09-01 19:27:58 -05:00
|
|
|
let i: int = str::rindex(p, os_fs::path_sep as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
if i == -1 {
|
2011-09-01 19:27:58 -05:00
|
|
|
i = str::rindex(p, os_fs::alt_path_sep as u8);
|
2011-08-24 18:00:26 -05:00
|
|
|
if i == -1 { ret ~"."; }
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::substr(p, 0u, i as uint);
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
fn basename(p: &path) -> path {
|
2011-09-01 19:27:58 -05:00
|
|
|
let i: int = str::rindex(p, os_fs::path_sep as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
if i == -1 {
|
2011-09-01 19:27:58 -05:00
|
|
|
i = str::rindex(p, os_fs::alt_path_sep as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
if i == -1 { ret p; }
|
2011-06-07 19:54:22 -05:00
|
|
|
}
|
2011-09-01 19:27:58 -05:00
|
|
|
let len = str::byte_len(p);
|
2011-07-27 07:19:39 -05:00
|
|
|
if i + 1 as uint >= len { ret p; }
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::slice(p, i + 1 as uint, len);
|
2011-06-07 19:54:22 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-20 19:47:14 -05:00
|
|
|
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
|
2011-08-24 18:00:26 -05:00
|
|
|
fn connect(pre: &path, post: &path) -> path {
|
2011-09-01 19:27:58 -05:00
|
|
|
let len = str::byte_len(pre);
|
2011-08-19 17:16:48 -05:00
|
|
|
ret if pre[len - 1u] == os_fs::path_sep as u8 {
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-06-16 18:55:46 -05:00
|
|
|
// Trailing '/'?
|
2011-06-15 13:19:50 -05:00
|
|
|
pre + post
|
|
|
|
} else { pre + path_sep() + post };
|
2011-03-15 19:11:55 -05:00
|
|
|
}
|
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
fn file_is_dir(p: &path) -> bool {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::as_buf(p, { |buf|
|
2011-09-01 14:58:24 -05:00
|
|
|
rustrt::rust_file_is_dir(buf) != 0
|
|
|
|
});
|
2011-08-24 18:00:26 -05:00
|
|
|
}
|
2011-03-10 08:56:51 -06:00
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
fn list_dir(p: &path) -> [istr] {
|
|
|
|
let p = p;
|
2011-09-01 19:27:58 -05:00
|
|
|
let pl = str::byte_len(p);
|
2011-08-19 17:16:48 -05:00
|
|
|
if pl == 0u || p[pl - 1u] as char != os_fs::path_sep { p += path_sep(); }
|
2011-08-24 18:00:26 -05:00
|
|
|
let full_paths: [istr] = [];
|
2011-09-01 17:51:27 -05:00
|
|
|
for filename: istr in os_fs::list_dir(p) {
|
2011-09-01 19:27:58 -05:00
|
|
|
if !str::eq(filename, ~".") {
|
|
|
|
if !str::eq(filename, ~"..") { full_paths += [p + filename]; }
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret full_paths;
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
2011-06-17 13:12:51 -05:00
|
|
|
|
2011-08-24 18:00:26 -05:00
|
|
|
fn path_is_absolute(p: &path) -> bool {
|
2011-08-31 18:38:09 -05:00
|
|
|
ret os_fs::path_is_absolute(p);
|
2011-08-24 18:00:26 -05:00
|
|
|
}
|
2011-06-17 13:12:51 -05:00
|
|
|
|
2011-06-17 17:52:37 -05:00
|
|
|
// FIXME: under Windows, we should prepend the current drive letter to paths
|
|
|
|
// that start with a slash.
|
2011-08-24 18:00:26 -05:00
|
|
|
fn make_absolute(p: &path) -> path {
|
|
|
|
if path_is_absolute(p) {
|
|
|
|
ret p;
|
|
|
|
} else {
|
2011-08-25 13:11:21 -05:00
|
|
|
ret connect(getcwd(), p);
|
2011-08-24 18:00:26 -05:00
|
|
|
}
|
2011-06-17 13:12:51 -05:00
|
|
|
}
|
|
|
|
|
2011-03-16 16:58:02 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-05-05 15:53:57 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-16 16:58:02 -05:00
|
|
|
// End:
|