•file.CreateDir

This commit is contained in:
dzaima 2022-05-09 22:57:37 +03:00
parent 5e67b87cf1
commit 63bd4a876e
3 changed files with 17 additions and 3 deletions

View File

@ -675,9 +675,14 @@ static NFnDesc* fTypeDesc;
static NFnDesc* fExistsDesc;
static NFnDesc* fListDesc;
static NFnDesc* fMapBytesDesc;
static NFnDesc* createdirDesc;
B list_c1(B d, B x) {
return path_list(path_rel(nfn_objU(d), x));
}
B createdir_c1(B d, B x) {
if (dir_create(path_rel(nfn_objU(d), x))) return m_i32(1);
thrM("(file).CreateDir: Failed to create directory");
}
B ftype_c1(B d, B x) {
char ty = path_type(path_rel(nfn_objU(d), x));
@ -1095,7 +1100,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));
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));
#undef F
}
cr = inc(fileNS);
@ -1163,12 +1168,13 @@ void sysfn_init() {
fBytesDesc = registerNFn(m_str8l("(file).Bytes"), fbytes_c1, fbytes_c2);
fListDesc = registerNFn(m_str8l("(file).List"), list_c1, c2_bad);
fTypeDesc = registerNFn(m_str8l("(file).Type"), ftype_c1, c2_bad);
createdirDesc = registerNFn(m_str8l("(file).CreateDir"), createdir_c1, c2_bad);
fMapBytesDesc = registerNFn(m_str8l("(file).MapBytes"), mapBytes_c1, c2_bad);
fExistsDesc = registerNFn(m_str8l("(file).Exists"), fexists_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","type","exists","name","mapbytes");
file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes","createdir");
c(BMd1,bi_bitcast)->im = bitcast_im;
}

View File

@ -302,10 +302,17 @@ B mmap_file(B path) {
void mmap_init() { }
#endif
#include <sys/types.h>
#include <sys/stat.h>
bool dir_create(B path) {
char* p = toCStr(path);
bool r = mkdir(p, S_IRWXU) == 0;
freeCStr(p);
dec(path);
return r;
}
char path_type(B path) {
char* p = toCStr(path);
struct stat path_stat;

View File

@ -14,6 +14,7 @@ B path_lines(B path); // consumes
I8Arr* stream_bytes(FILE* f);
B mmap_file(B path); // consumes
bool dir_create(B path); // consumes
void path_wChars(B path, B x); // consumes path
void path_wBytes(B path, B x); // consumes path