This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
039load.cc
49 lines (42 loc) · 1.27 KB
/
039load.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
COMPILE_FN(load, compiledfn_load, "($f)",
load_file(to_string(lookup("$f")).c_str());
return nil;
)
void load_files(const char* ext) {
vector<char*> files = sorted_files(".", ext);
for (vector<char*>::iterator p = files.begin(); p != files.end(); ++p) {
if (!Last_file.empty() && *p > Last_file) break;
load_file(*p);
free(*p);
}
}
//// internals
void load_file(const char* filename) {
bool old_interactive = Interactive; Interactive = false;
ifstream f(filename);
if (f.fail()) return;
while (!f.eof()) {
TEMP(form, read(f));
//? cerr << form << '\n'; // uncomment to track down errors in .wart files
rmref(eval(form));
}
Interactive = old_interactive;
}
#include<dirent.h>
vector<char*> sorted_files(const char* dirname, const char* ext) {
vector<char*> result;
dirent** files;
int num_files = scandir(dirname, &files, NULL, alphasort);
for (int i = 0; i < num_files; ++i) {
unsigned long n = strlen(files[i]->d_name), extn = strlen(ext);
if (n < extn) continue;
if (strncmp(&files[i]->d_name[n-extn], ext, extn)) continue;
if (!isdigit(files[i]->d_name[0])) continue;
char* s = new char[n+1];
strncpy(s, files[i]->d_name, n+1);
result.push_back(s);
free(files[i]);
}
free(files);
return result;
}