From eb9550aa29d0cb0c454aa7e6f826158e178648c5 Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 22 Aug 2022 15:30:37 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2file.Parent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins.h | 2 +- src/builtins/sysfn.c | 10 +++++++--- src/core/stuff.h | 2 +- src/utils/file.c | 5 +++-- src/utils/file.h | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/builtins.h b/src/builtins.h index 1e18ca0e..9749fa9a 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -11,7 +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") \ +/* sysfn.c*/M(fName,"•file.Name") M(fParent,"•file.Parent") \ /* sysfn.c*/M(tRawMode,"•term.RawMode") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") M(tOutRaw,"•term.OutRaw") M(tErrRaw,"•term.ErrRaw") \ /* 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") M(heapDump,"•internal.HeapDump") \ diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 6fc1718a..fabe65eb 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -768,6 +768,10 @@ 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 fParent_c1(B t, B x) { + if (!isArr(x) || RNK(x)!=1) thrM("•file.Parent: Argument must be a character vector"); + return path_parent(x); +} B mapBytes_c1(B d, B x) { return mmap_file(path_rel(nfn_objU(d), x)); @@ -1169,7 +1173,7 @@ B sys_c1(B t, B x) { B path = m_f64(0); B name = m_f64(0); B wdpath = m_f64(0); - #define REQ_PATH ({ if(!path.u) path = q_N(comp_currPath)? bi_N : path_abs(path_dir(inc(comp_currPath))); path; }) + #define REQ_PATH ({ if(!path.u) path = q_N(comp_currPath)? bi_N : path_abs(path_parent(inc(comp_currPath))); path; }) #define REQ_NAME ({ if(!name.u) name = path_name(inc(comp_currPath)); name; }) for (usz i = 0; i < IA(x); i++) { B c = GetU(x,i); @@ -1182,7 +1186,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(fList), F(fBytes), F(fChars), F(fLines), F(fType), F(fExists), inc(bi_fName), F(fMapBytes), F(createdir), F(rename), F(remove)); + 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), F(fExists), inc(bi_fName), inc(bi_fParent), F(fMapBytes), F(createdir), F(rename), F(remove)); #undef F } cr = incG(fileNS); @@ -1263,6 +1267,6 @@ void sysfn_init() { ffiloadDesc = registerNFn(m_c32vec_0(U"•FFI"), c1_bad, ffiload_c2); } void sysfnPost_init() { - file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes","createdir","rename","remove"); + file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","parent","mapbytes","createdir","rename","remove"); c(BMd1,bi_bitcast)->im = bitcast_im; } diff --git a/src/core/stuff.h b/src/core/stuff.h index 6e4da477..e9b77deb 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -204,7 +204,7 @@ static i32 compare(B w, B x) { // doesn't consume; -1 if wx, 0 if w #undef CMP NOINLINE bool atomEqualF(B w, B x); -static bool atomEqual(B w, B x) { // doesn't consume (not that that matters really currently) +static bool atomEqual(B w, B x) { // doesn't consume if(isF64(w)&isF64(x)) return w.f==x.f; if (w.u==x.u) return true; if (!isVal(w) | !isVal(x)) return false; diff --git a/src/utils/file.c b/src/utils/file.c index 221bc5f9..17fdabea 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -117,15 +117,16 @@ B path_rel(B base, B rel) { // consumes rel; assumes base is a char vector or bi return r; } -B path_dir(B path) { +B path_parent(B path) { assert(isArr(path)); SGetU(path) usz pia = IA(path); if (pia==0) thrM("Empty file path"); guaranteeStr(path); - for (i64 i = (i64)pia-1; i >= 0; i--) { + for (i64 i = (i64)pia-2; i >= 0; i--) { if (o2cu(GetU(path, i))=='/') return taga(arr_shVec(TI(path,slice)(path, 0, i+1))); } + if (o2cu(GetU(path, 0))=='/') return path; dec(path); u32* rp; B r = m_c32arrv(&rp, 2); rp[0] = '.'; rp[1] = '/'; return r; diff --git a/src/utils/file.h b/src/utils/file.h index a5cdad9f..36cbe718 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -2,7 +2,7 @@ #include "utf.h" B path_rel(B base, B rel); // consumes rel; assumes base is a char vector or bi_N -B path_dir(B path); // consumes; returns directory part of file path, with trailing slash +B path_parent(B path); // consumes; returns the containing directory, with trailing slash B path_name(B path); // consumes; returns filename from a path B path_abs(B path); // consumes; returns absolute version of the path; propagates bi_N