add recursive file searcher methods

This commit is contained in:
ValKmjolnir 2023-11-24 00:17:47 +08:00
parent cacf0ae86a
commit 2bb9655422
4 changed files with 102 additions and 77 deletions

View File

@ -85,3 +85,44 @@ var recursive_find_files = func(path) {
unix.closedir(dd);
return res;
}
var recursive_find_files_flat = func(path) {
var tree_files = recursive_find_files(path);
if (tree_files==nil) {
return [];
}
var flat = [];
var bfs = [tree_files];
while(size(bfs)!=0) {
var first = pop(bfs);
foreach(var file_record; first.files) {
if (ishash(file_record)) {
append(bfs, file_record);
continue;
}
append(flat, first.dir~"/"~file_record);
}
}
return flat;
}
var recursive_find_files_with_extension = func(path, extensions...) {
var in_vec = func(ext) {
foreach(var i; extensions) {
if (ext==i) {
return 1;
}
}
return 0;
}
var files = recursive_find_files_flat(path);
var res = [];
foreach(var filename; files) {
var tmp = split('.', filename);
if (size(tmp)>1 and in_vec(tmp[-1])) {
append(res, filename);
}
}
return res;
}

View File

@ -2,15 +2,15 @@ use std.file;
var check = func(dir_name) {
var ts = maketimestamp();
var f = file.find_all_files_with_extension(dir_name, "nas");
var files = file.recursive_find_files_with_extension(dir_name, "nas");
var res = [];
foreach(var k; f) {
foreach(var f; files) {
ts.stamp();
if (system("nasal -c "~dir_name~"/"~k~" 1>/dev/null 2>/dev/null")!=0) {
println("\e[31merror\e[0m ", dir_name, "/", k);
append(res, dir_name~"/"~k);
if (system("nasal -c "~f~" 1>/dev/null 2>/dev/null")!=0) {
println("\e[31merror\e[0m ", f);
append(res, f);
}
println("compiling ", dir_name, "/", k, " in \e[32m", ts.elapsedMSec(), "\e[0m ms");
println("compiling ", f, " in \e[32m", ts.elapsedMSec(), "\e[0m ms");
}
return res;
}

View File

@ -19,26 +19,10 @@ if (size(arg)<1) {
var needle = arg[0];
var do_flat = func(vec) {
var flat = [];
var bfs = [vec];
while(size(bfs)!=0) {
var d = pop(bfs);
foreach(var f; d.files) {
if (ishash(f)) {
append(bfs,f);
continue;
}
append(flat, d.dir~"/"~f);
}
}
sort(flat, func(a, b) {return cmp(a, b)<0});
return flat;
}
var result = [];
var all_files = file.recursive_find_files(".");
foreach(var f; do_flat(all_files)) {
var all_files = file.recursive_find_files_flat(".");
sort(all_files, func(a, b) {return cmp(a, b)<=0;});
foreach(var f; all_files) {
var pos = find(needle, f);
if (pos == -1) {
continue;