about summary refs log tree commit diff
path: root/src/rt/rust_builtin.cpp
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-07-12 15:14:57 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-07-12 15:14:57 -0700
commit598b50e10a347b29d235bb99110975b96fdab160 (patch)
tree1e007218ddd511287d593def6321bfced68cf3af /src/rt/rust_builtin.cpp
parent27834c2a65f88a01a897e69b53b106e5021260f6 (diff)
downloadrust-598b50e10a347b29d235bb99110975b96fdab160.tar.gz
rust-598b50e10a347b29d235bb99110975b96fdab160.zip
stdlib: Move fs over to interior vectors by introducing a rust_list_files_ivec builtin
Diffstat (limited to 'src/rt/rust_builtin.cpp')
-rw-r--r--src/rt/rust_builtin.cpp40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 6057ee4cb6c..1caacd7a9d3 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -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) {