✨ add recursive file searcher methods
This commit is contained in:
parent
cacf0ae86a
commit
2bb9655422
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;
|
||||
}
|
|
@ -1,37 +1,37 @@
|
|||
use std.padding;
|
||||
use std.file;
|
||||
|
||||
var source=file.find_all_files_with_extension("./src","cpp","h");
|
||||
sort(source,func(a,b) {return cmp(a,b)<0});
|
||||
var source = file.find_all_files_with_extension("./src", "cpp", "h");
|
||||
sort(source, func(a, b) {return cmp(a, b)<0});
|
||||
|
||||
var lib=file.find_all_files_with_extension("./std","nas");
|
||||
sort(lib,func(a,b) {return cmp(a,b)<0});
|
||||
var lib = file.find_all_files_with_extension("./std", "nas");
|
||||
sort(lib, func(a, b) {return cmp(a, b)<0});
|
||||
|
||||
var testfile=file.find_all_files_with_extension("./test","nas");
|
||||
sort(testfile,func(a,b) {return cmp(a,b)<0});
|
||||
var testfile = file.find_all_files_with_extension("./test", "nas");
|
||||
sort(testfile, func(a, b) {return cmp(a, b)<0});
|
||||
|
||||
var module=file.find_all_files_with_extension("./module","cpp","nas");
|
||||
sort(module,func(a,b) {return cmp(a,b)<0});
|
||||
var module = file.find_all_files_with_extension("./module", "cpp", "nas");
|
||||
sort(module, func(a, b) {return cmp(a, b)<0});
|
||||
|
||||
var longest = func(vec...) {
|
||||
var len=0;
|
||||
foreach(var v;vec)
|
||||
foreach(var f;v)
|
||||
len=size(f)>len?size(f):len;
|
||||
var len = 0;
|
||||
foreach(var v; vec)
|
||||
foreach(var f; v)
|
||||
len = size(f)>len? size(f):len;
|
||||
return len;
|
||||
}
|
||||
var padding_length=longest(source,lib,testfile,module);
|
||||
var padding_length = longest(source, lib, testfile, module);
|
||||
|
||||
var blank = func(s) {
|
||||
if (!size(s)) {
|
||||
return 1;
|
||||
}
|
||||
var space=[" "[0],"\n"[0],"\t"[0],"\r"[0]];
|
||||
for(var i=0;i<size(s);i+=1) {
|
||||
var flag=0;
|
||||
foreach(var j;space) {
|
||||
var space = [" "[0],"\n"[0],"\t"[0],"\r"[0]];
|
||||
for(var i = 0; i<size(s); i+=1) {
|
||||
var flag = 0;
|
||||
foreach(var j; space) {
|
||||
if (s[i]==j) {
|
||||
flag=1;
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
|
@ -41,44 +41,44 @@ var blank = func(s) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
var count = func(s,c) {
|
||||
var cnt=0;
|
||||
foreach(var i;split(c,s))
|
||||
cnt+=!blank(i);
|
||||
var count = func(s, c) {
|
||||
var cnt = 0;
|
||||
foreach(var i; split(c, s))
|
||||
cnt += !blank(i);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
var column = func(number) {
|
||||
number=number>=1000?substr(str(number/1000),0,4)~'k':str(number);
|
||||
return padding.leftpad(number,6);
|
||||
number = number>=1000? substr(str(number/1000), 0, 4)~'k':str(number);
|
||||
return padding.leftpad(number, 6);
|
||||
}
|
||||
|
||||
var calc = func(codetype,files,path="") {
|
||||
var calc = func(codetype, files, path = "") {
|
||||
println(codetype);
|
||||
var (bytes,ctx,line,semi,line_cnt,semi_cnt)=(0,"",0,0,0,0);
|
||||
forindex(var i;files) {
|
||||
var s=io.exists(path~files[i])?io.readfile(path~files[i]):"";
|
||||
(line_cnt,semi_cnt)=(count(s,'\n'),count(s,';'));
|
||||
println(padding.rightpad(files[i],padding_length),'|',
|
||||
column(line_cnt),' line |',
|
||||
column(semi_cnt),' semi |',
|
||||
padding.leftpad(str(int(size(s)/1024)),4),' kb | ',
|
||||
md5(s),' |');
|
||||
bytes+=size(s);
|
||||
ctx~=s;
|
||||
line+=line_cnt;
|
||||
semi+=semi_cnt;
|
||||
var (bytes, ctx, line, semi, line_cnt, semi_cnt) = (0, "", 0, 0, 0, 0);
|
||||
forindex(var i; files) {
|
||||
var s = io.exists(path~files[i])? io.readfile(path~files[i]):"";
|
||||
(line_cnt, semi_cnt) = (count(s, '\n'), count(s, ';'));
|
||||
println(padding.rightpad(files[i], padding_length), '|',
|
||||
column(line_cnt), ' line |',
|
||||
column(semi_cnt), ' semi |',
|
||||
padding.leftpad(str(int(size(s)/1024)), 4), ' kb | ',
|
||||
md5(s), ' |');
|
||||
bytes += size(s);
|
||||
ctx ~= s;
|
||||
line += line_cnt;
|
||||
semi += semi_cnt;
|
||||
}
|
||||
println(padding.rightpad("total:",padding_length),'|',
|
||||
column(line),' line |',
|
||||
column(semi),' semi |',
|
||||
padding.leftpad(str(int(bytes/1024)),4),' kb | ',
|
||||
md5(ctx),' |\n');
|
||||
println(padding.rightpad("total:", padding_length), '|',
|
||||
column(line), ' line |',
|
||||
column(semi), ' semi |',
|
||||
padding.leftpad(str(int(bytes/1024)), 4), ' kb | ',
|
||||
md5(ctx), ' |\n');
|
||||
return int(bytes/1024);
|
||||
}
|
||||
|
||||
var all=calc("source code:",source,"src/")
|
||||
+calc("lib:",lib,"std/")
|
||||
+calc("test file:",testfile,"test/")
|
||||
+calc("module:",module,"module/");
|
||||
println(padding.rightpad("total:",padding_length),'|',padding.leftpad(str(all),6),' kb |');
|
||||
var all = calc("source code:", source, "src/")
|
||||
+ calc("lib:", lib, "std/")
|
||||
+ calc("test file:", testfile, "test/")
|
||||
+ calc("module:", module, "module/");
|
||||
println(padding.rightpad("total:", padding_length), '|', padding.leftpad(str(all), 6), ' kb |');
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue