about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-09-05 11:46:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-09-05 11:46:34 -0700
commit9f78ec2d5e2a74ec24cc20cd3b1d60c3bdb9d605 (patch)
tree19a22a8ad181d7bc9727afc52e649a645a384e1b
parent0d9d275314f98dceec1118e80eaab89a00c3ad3e (diff)
downloadmu-9f78ec2d5e2a74ec24cc20cd3b1d60c3bdb9d605.tar.gz
2155 - `mu` can now load all files in a directory
-rw-r--r--020run.cc25
-rw-r--r--enumerate/enumerate.cc2
2 files changed, 26 insertions, 1 deletions
diff --git a/020run.cc b/020run.cc
index 7c2da3b7..8b24b2f2 100644
--- a/020run.cc
+++ b/020run.cc
@@ -197,6 +197,10 @@ atexit(cleanup_main);
 
 :(code)
 void load_permanently(string filename) {
+  if (is_directory(filename)) {
+    load_all_permanently(filename);
+    return;
+  }
   ifstream fin(filename.c_str());
   fin.peek();
   if (!fin) {
@@ -211,6 +215,27 @@ void load_permanently(string filename) {
   // End load_permanently.
 }
 
+bool is_directory(string path) {
+  struct stat info;
+  if (stat(path.c_str(), &info)) return false;  // error
+  return info.st_mode & S_IFDIR;
+}
+
+void load_all_permanently(string dir) {
+  dirent** files;
+  int num_files = scandir(dir.c_str(), &files, NULL, alphasort);
+  for (int i = 0; i < num_files; ++i) {
+    string curr_file = files[i]->d_name;
+    if (!isdigit(curr_file.at(0))) continue;
+    load_permanently(dir+'/'+curr_file);
+    free(files[i]);
+    files[i] = NULL;
+  }
+  free(files);
+}
+:(before "End Includes")
+#include<dirent.h>
+
 //:: On startup, load everything in core.mu
 :(before "End Load Recipes")
 load_permanently("core.mu");
diff --git a/enumerate/enumerate.cc b/enumerate/enumerate.cc
index 02349213..58497909 100644
--- a/enumerate/enumerate.cc
+++ b/enumerate/enumerate.cc
@@ -19,7 +19,7 @@ int enumerate_files_in_cwd_until(string last_file) {
   int num_files = scandir(".", &files, NULL, alphasort);
   for (int i = 0; i < num_files; ++i) {
     string curr_file = files[i]->d_name;
-    if (!isdigit(curr_file[0])) continue;
+    if (!isdigit(curr_file.at(0))) continue;
     if (!last_file.empty() && curr_file > last_file) break;
     cout << curr_file << '\n';
   }