mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-05-02 19:00:47 +08:00
✨ add recursive file searcher methods
This commit is contained in:
49
std/file.nas
49
std/file.nas
@@ -31,7 +31,7 @@ var new = func(filename, mode="r") {
|
||||
|
||||
var find_all_files_with_extension = func(path, extensions...) {
|
||||
var in_vec = func(ext) {
|
||||
foreach(var i;extensions) {
|
||||
foreach(var i; extensions) {
|
||||
if (ext==i) {
|
||||
return 1;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ var find_all_files_with_extension = func(path, extensions...) {
|
||||
return 0;
|
||||
}
|
||||
var res = [];
|
||||
foreach(var f;find_all_files(path)) {
|
||||
foreach(var f; find_all_files(path)) {
|
||||
var tmp = split('.', f);
|
||||
if (size(tmp)>1 and in_vec(tmp[-1])) {
|
||||
append(res, f);
|
||||
@@ -74,14 +74,55 @@ var recursive_find_files = func(path) {
|
||||
};
|
||||
while(var n = unix.readdir(dd)) {
|
||||
if (unix.isfile(path~"/"~n)) {
|
||||
append(res.files,n);
|
||||
append(res.files, n);
|
||||
} elsif (unix.isdir(path~"/"~n) and n!="." and n!="..") {
|
||||
var tmp = recursive_find_files(path~"/"~n);
|
||||
if (tmp!=nil) {
|
||||
append(res.files,tmp);
|
||||
append(res.files, tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user