From 9f78ec2d5e2a74ec24cc20cd3b1d60c3bdb9d605 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 5 Sep 2015 11:46:22 -0700 Subject: 2155 - `mu` can now load all files in a directory --- 020run.cc | 25 +++++++++++++++++++++++++ enumerate/enumerate.cc | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) 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 + //:: 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'; } -- cgit 1.4.1-2-gfad0