•file.Type, •file.Name

This commit is contained in:
dzaima 2022-02-05 18:41:21 +02:00
parent 802b0611b8
commit d85fb9a592
4 changed files with 46 additions and 6 deletions

View File

@ -11,6 +11,7 @@
/* sysfn.c*/M(type,"•Type") M(decp,"•Decompose") M(primInd,"•PrimInd") M(glyph,"•Glyph") A(fill,"•FillFn") M(sys,"•getsys") A(grLen,"•GroupLen") D(grOrd,"•GroupOrd") \
/* sysfn.c*/M(repr,"•Repr") M(fmt,"•Fmt") A(asrt,"!") A(casrt,"!") M(out,"•Out") M(show,"•Show") A(bqn,"•BQN") A(sh,"•SH") M(fromUtf8,"•FromUTF8") M(currentError,"•CurrentError") \
/* sysfn.c*/D(cmp,"•Cmp") A(hash,"•Hash") M(unixTime,"•UnixTime") M(monoTime,"•MonoTime") M(delay,"•Delay") M(makeRand,"•MakeRand") M(reBQN,"•ReBQN") M(exit,"•Exit") M(getLine,"•GetLine") \
/* sysfn.c*/M(fName,"•file.Name") \
/* inverse.c*/M(setInvReg, "(SetInvReg)") M(setInvSwap, "(SetInvSwap)") M(nativeInvReg, "(NativeInvReg)") M(nativeInvSwap, "(NativeInvSwap)") \
/*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \
/*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \

View File

@ -669,11 +669,21 @@ static void sys_gcFn() {
}
static NFnDesc* listDesc;
static NFnDesc* fTypeDesc;
static NFnDesc* fListDesc;
B list_c1(B d, B x) {
return file_list(path_rel(nfn_objU(d), x));
}
B ftype_c1(B d, B x) {
return m_c32(file_type(path_rel(nfn_objU(d), x)));
}
B fName_c1(B t, B x) {
if (!isArr(x) || rnk(x)!=1) thrM("•file.Name: Argument must be a character vector");
return path_name(x);
}
B unixTime_c1(B t, B x) {
dec(x);
return m_f64(time(NULL));
@ -996,7 +1006,7 @@ B sys_c1(B t, B x) {
if(!fileNS.u) {
REQ_PATH;
#define F(X) m_nfn(X##Desc, inc(path))
fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(list), F(fBytes), F(fChars), F(fLines));
fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(fList), F(fBytes), F(fChars), F(fLines), F(fType), inc(bi_fName));
#undef F
}
cr = inc(fileNS);
@ -1062,11 +1072,12 @@ void sysfn_init() {
fileAtDesc = registerNFn(m_str8l("(file).At"), fileAt_c1, fileAt_c2);
fLinesDesc = registerNFn(m_str8l("(file).Lines"), flines_c1, flines_c2);
fBytesDesc = registerNFn(m_str8l("(file).Bytes"), fbytes_c1, fbytes_c2);
listDesc = registerNFn(m_str8l("(file).List"), list_c1, c2_bad);
fListDesc = registerNFn(m_str8l("(file).List"), list_c1, c2_bad);
fTypeDesc = registerNFn(m_str8l("(file).Type"), ftype_c1, c2_bad);
importDesc = registerNFn(m_str32(U"•Import"), import_c1, import_c2);
reBQNDesc = registerNFn(m_str8l("(REPL)"), repl_c1, repl_c2);
}
void sysfnPost_init() {
file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines");
file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","name");
c(BMd1,bi_bitcast)->im = bitcast_im;
}

View File

@ -4,13 +4,21 @@
#include "mut.h"
#include "talloc.h"
FILE* file_open(B path, char* desc, char* mode) { // doesn't consume
char* toCStr(B path) {
u64 plen = utf8lenB(path);
TALLOC(char, p, plen+1);
toUTF8(path, p);
p[plen] = 0;
FILE* f = fopen(p, mode);
return p;
}
void freeCStr(char* p) {
TFREE(p);
}
FILE* file_open(B path, char* desc, char* mode) { // doesn't consume
char* p = toCStr(path);
FILE* f = fopen(p, mode);
freeCStr(p);
if (f==NULL) thrF("Couldn't %S file \"%R\"", desc, path);
return f;
}
@ -205,3 +213,22 @@ B file_list(B path) {
dec(path);
return res;
}
#include <sys/types.h>
#include <sys/stat.h>
char file_type(B path) {
char* p = toCStr(path);
struct stat path_stat;
stat(p, &path_stat);
freeCStr(p);
i64 mode = path_stat.st_mode;
if (S_ISREG (mode)) return 'f';
if (S_ISDIR (mode)) return 'd';
if (S_ISLNK (mode)) return 'l';
if (S_ISFIFO(mode)) return 'p';
if (S_ISSOCK(mode)) return 's';
if (S_ISBLK (mode)) return 'b';
if (S_ISCHR (mode)) return 'c';
thrM("Unexpected file type");
}

View File

@ -17,3 +17,4 @@ void file_wChars(B path, B x); // consumes path
void file_wBytes(B path, B x); // consumes path
B file_list(B path); // consumes
char file_type(B path); // consumes