stdlib: Move fs over to interior vectors by introducing a rust_list_files_ivec builtin
This commit is contained in:
parent
27834c2a65
commit
598b50e10a
5 changed files with 48 additions and 7 deletions
|
@ -43,16 +43,16 @@ fn connect(path pre, path post) -> path {
|
|||
|
||||
fn file_is_dir(path p) -> bool { ret rustrt::rust_file_is_dir(p) != 0; }
|
||||
|
||||
fn list_dir(path p) -> vec[str] {
|
||||
fn list_dir(path p) -> str[] {
|
||||
auto pl = str::byte_len(p);
|
||||
if (pl == 0u || p.(pl - 1u) as char != os_fs::path_sep) {
|
||||
p += path_sep();
|
||||
}
|
||||
let vec[str] full_paths = [];
|
||||
let str[] full_paths = ~[];
|
||||
for (str filename in os_fs::list_dir(p)) {
|
||||
if (!str::eq(filename, ".")) {
|
||||
if (!str::eq(filename, "..")) {
|
||||
vec::push[str](full_paths, p + filename);
|
||||
full_paths += ~[p + filename];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
|
||||
native "rust" mod rustrt {
|
||||
fn rust_list_files(str path) -> vec[str];
|
||||
fn rust_list_files_ivec(str path) -> @str[];
|
||||
fn rust_dirent_filename(os::libc::dirent ent) -> str;
|
||||
}
|
||||
|
||||
fn list_dir(str path) -> vec[str] {
|
||||
ret rustrt::rust_list_files(path);
|
||||
fn list_dir(str path) -> str[] {
|
||||
ret *rustrt::rust_list_files_ivec(path);
|
||||
// TODO ensure this is always closed
|
||||
|
||||
// FIXME: No idea why, but this appears to corrupt memory on OSX. I
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
native "rust" mod rustrt {
|
||||
fn rust_list_files(str path) -> vec[str];
|
||||
fn rust_list_files_ivec(str path) -> @str[];
|
||||
fn rust_file_is_dir(str path) -> int;
|
||||
}
|
||||
|
||||
fn list_dir(str path) -> vec[str] { ret rustrt::rust_list_files(path + "*"); }
|
||||
fn list_dir(str path) -> str[] { ret *rustrt::rust_list_files(path + "*"); }
|
||||
|
||||
fn path_is_absolute(str p) -> bool {
|
||||
ret str::char_at(p, 0u) == '/'
|
||||
|
|
|
@ -454,7 +454,9 @@ debug_opaque(rust_task *task, type_desc *t, uint8_t *front)
|
|||
}
|
||||
}
|
||||
|
||||
struct rust_box : rc_base<rust_box> {
|
||||
struct rust_box {
|
||||
RUST_REFCOUNTED(rust_box)
|
||||
|
||||
// FIXME `data` could be aligned differently from the actual box body data
|
||||
uint8_t data[];
|
||||
};
|
||||
|
@ -580,6 +582,42 @@ rust_list_files(rust_task *task, rust_str *path) {
|
|||
sizeof(rust_str*), strings.data());
|
||||
}
|
||||
|
||||
extern "C" CDECL rust_box*
|
||||
rust_list_files_ivec(rust_task *task, rust_str *path) {
|
||||
array_list<rust_str*> strings;
|
||||
#if defined(__WIN32__)
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData);
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
strings.push(c_str_to_rust(task, FindFileData.cFileName));
|
||||
} while (FindNextFile(hFind, &FindFileData));
|
||||
FindClose(hFind);
|
||||
}
|
||||
#else
|
||||
DIR *dirp = opendir((char*)path->data);
|
||||
if (dirp) {
|
||||
struct dirent *dp;
|
||||
while ((dp = readdir(dirp)))
|
||||
strings.push(c_str_to_rust(task, dp->d_name));
|
||||
closedir(dirp);
|
||||
}
|
||||
#endif
|
||||
rust_box *box = (rust_box *)task->malloc(sizeof(rust_box) +
|
||||
sizeof(rust_ivec));
|
||||
box->ref_count = 1;
|
||||
rust_ivec *iv = (rust_ivec *)&box->data;
|
||||
iv->fill = 0;
|
||||
|
||||
size_t alloc_sz = sizeof(rust_str *) * strings.size();
|
||||
iv->alloc = alloc_sz;
|
||||
iv->payload.ptr = (rust_ivec_heap *)
|
||||
task->kernel->malloc(alloc_sz + sizeof(size_t));
|
||||
iv->payload.ptr->fill = alloc_sz;
|
||||
memcpy(&iv->payload.ptr->data, strings.data(), alloc_sz);
|
||||
return box;
|
||||
}
|
||||
|
||||
#if defined(__WIN32__)
|
||||
extern "C" CDECL rust_str *
|
||||
rust_dirent_filename(rust_task *task, void* ent) {
|
||||
|
|
|
@ -29,6 +29,7 @@ rust_file_is_dir
|
|||
rust_get_stdin
|
||||
rust_get_stdout
|
||||
rust_list_files
|
||||
rust_list_files_ivec
|
||||
rust_process_wait
|
||||
rust_ptr_eq
|
||||
rust_run_program
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue