From 81e09956d35bbac6ec4397946aac0e46281a2271 Mon Sep 17 00:00:00 2001 From: dzaima Date: Fri, 26 Aug 2022 13:37:51 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2file.Size,=20make=20=E2=80=A2file.List?= =?UTF-8?q?=20decode=20UTF-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit invalid UTF-8 is a problem for the future --- docs/system.md | 2 +- src/builtins/sysfn.c | 7 +++++-- src/utils/file.c | 3 ++- src/utils/file.h | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/system.md b/docs/system.md index 56805eb5..6cf449bf 100644 --- a/docs/system.md +++ b/docs/system.md @@ -15,7 +15,7 @@ See [the BQN specification](https://mlochbaum.github.io/BQN/spec/system.html) fo | `•name` | | | `•wdpath` | | | `•Exit` | | -| `•file` | Fields: `path`, `At`, `List`, `Bytes`, `Chars`, `Lines`, `Type`, `Exists`, `Name`, `Parent`, `MapBytes`, `CreateDir`, `Rename`, `Remove`, `Created`, `Modified`, `Accessed` | +| `•file` | Fields: `path`, `At`, `List`, `Bytes`, `Chars`, `Lines`, `Type`, `Exists`, `Name`, `Parent`, `MapBytes`, `CreateDir`, `Rename`, `Remove`, `Created`, `Modified`, `Accessed`, `Size` | | `•FChars` | | | `•FBytes` | | | `•FLines` | | diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 018ec42e..0c62dc73 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -729,6 +729,7 @@ static NFnDesc* fTypeDesc; static NFnDesc* fCreatedDesc; static NFnDesc* fAccessedDesc; static NFnDesc* fModifiedDesc; +static NFnDesc* fSizeDesc; static NFnDesc* fExistsDesc; static NFnDesc* fListDesc; static NFnDesc* fMapBytesDesc; @@ -766,6 +767,7 @@ B ftype_c1(B d, B x) { B fcreated_c1 (B d, B x) { return path_info(path_rel(nfn_objU(d), x), 0); } B faccessed_c1(B d, B x) { return path_info(path_rel(nfn_objU(d), x), 1); } B fmodified_c1(B d, B x) { return path_info(path_rel(nfn_objU(d), x), 2); } +B fsize_c1 (B d, B x) { return path_info(path_rel(nfn_objU(d), x), 3); } B fexists_c1(B d, B x) { char ty = path_type(path_rel(nfn_objU(d), x)); @@ -1225,7 +1227,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(fCreated), F(fAccessed), F(fModified), F(fExists), inc(bi_fName), inc(bi_fParent), 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(fCreated), F(fAccessed), F(fModified), F(fSize), F(fExists), inc(bi_fName), inc(bi_fParent), F(fMapBytes), F(createdir), F(rename), F(remove)); #undef F } cr = incG(fileNS); @@ -1299,6 +1301,7 @@ void sysfn_init() { fCreatedDesc = registerNFn(m_c8vec_0("(file).Created"), fcreated_c1, c2_bad); fModifiedDesc= registerNFn(m_c8vec_0("(file).Modified"), fmodified_c1, c2_bad); fAccessedDesc= registerNFn(m_c8vec_0("(file).Accessed"), faccessed_c1, c2_bad); + fSizeDesc = registerNFn(m_c8vec_0("(file).Size"), fsize_c1, c2_bad); createdirDesc= registerNFn(m_c8vec_0("(file).CreateDir"), createdir_c1, c2_bad); renameDesc = registerNFn(m_c8vec_0("(file).Rename"), c1_bad, rename_c2); removeDesc = registerNFn(m_c8vec_0("(file).Remove"), remove_c1, c2_bad); @@ -1309,6 +1312,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","created","accessed","modified","exists","name","parent","mapbytes","createdir","rename","remove"); + file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","created","accessed","modified","size","exists","name","parent","mapbytes","createdir","rename","remove"); c(BMd1,bi_bitcast)->im = bitcast_im; } diff --git a/src/utils/file.c b/src/utils/file.c index 8bcaa37e..0f2a7ec6 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -226,7 +226,7 @@ B path_list(B path) { B res = emptySVec(); while ((c = readdir(d)) != NULL) { char* name = c->d_name; - if (name[0]=='.'? !(name[1]==0 || (name[1]=='.'&&name[2]==0)) : true) res = vec_addN(res, m_c8vec_0(name)); + if (name[0]=='.'? !(name[1]==0 || (name[1]=='.'&&name[2]==0)) : true) res = vec_addN(res, utf8Decode(name, strlen(name))); } closedir(d); dec(path); @@ -370,6 +370,7 @@ B path_info(B path, i32 mode) { if (mode==0) return get_timespec(s.st_ctim); if (mode==1) return get_timespec(s.st_atim); if (mode==2) return get_timespec(s.st_mtim); + if (mode==3) return m_f64(s.st_size); thrM("Unknown path_info mode"); } diff --git a/src/utils/file.h b/src/utils/file.h index 676c4df8..4ca24fd9 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -28,5 +28,5 @@ void file_wBytes(FILE* file, B name, B x); // doesn't consume B path_list(B path); // consumes char path_type(B path); // consumes; errors only if path isn't a string -B path_info(B path, i32 mode); // consumes; mode: 0:created 1:accessed 2:modified +B path_info(B path, i32 mode); // consumes; mode: 0:created 1:accessed 2:modified 3:size void cbqn_heapDump(void);