fifo support or whatever

This commit is contained in:
dzaima 2021-09-21 23:42:47 +03:00
parent 811fe16f93
commit fab1ffadf5
3 changed files with 28 additions and 21 deletions

View File

@ -150,7 +150,7 @@ int main(int argc, char* argv[]) {
B execRes; B execRes;
if (execStdin) { if (execStdin) {
execRes = gsc_exec_inline(fromUTF8a(stdin_allBytes()), m_str8l("(-)"), args); execRes = gsc_exec_inline(fromUTF8a(stream_bytes(stdin)), m_str8l("(-)"), args);
} else { } else {
execRes = bqn_execFile(src, args); execRes = bqn_execFile(src, args);
} }

View File

@ -25,13 +25,34 @@ static DIR* dir_open(B path) { // doesn't consume
return f; return f;
} }
I8Arr* stream_bytes(FILE* f) {
B r = emptyIVec();
u64 SZ = 8192;
TALLOC(i8, t, SZ);
while(true) {
u64 read = fread(t, 1, SZ, f);
if (read==0) break;
i8* ap; B a = m_i8arrv(&ap, read);
memcpy(ap, t, read);
r = vec_join(r, a);
}
TFREE(t);
return toI8Arr(r);
}
I8Arr* file_bytes(B path) { // consumes I8Arr* file_bytes(B path) { // consumes
FILE* f = file_open(path, "read", "r"); FILE* f = file_open(path, "read", "r");
fseek(f, 0, SEEK_END); int seekRes = fseek(f, 0, SEEK_END);
u64 len = ftell(f); I8Arr* src;
fseek(f, 0, SEEK_SET); if (seekRes==-1) {
I8Arr* src = m_arr(fsizeof(I8Arr,a,u8,len), t_i8arr, len); arr_shVec((Arr*)src); src = stream_bytes(f);
if (fread((char*)src->a, 1, len, f)!=len) thrF("Error reading file \"%R\"", path); } else {
i64 len = ftell(f);
fseek(f, 0, SEEK_SET);
src = m_arr(fsizeof(I8Arr,a,u8,len), t_i8arr, len); arr_shVec((Arr*)src);
if (fread((char*)src->a, 1, len, f)!=len) thrF("Error reading file \"%R\"", path);
}
dec(path); dec(path);
fclose(f); fclose(f);
return src; return src;
@ -65,20 +86,6 @@ B file_lines(B path) { // consumes
return harr_fv(r); return harr_fv(r);
} }
I8Arr* stdin_allBytes() {
B r = emptyIVec();
u64 SZ = 8192;
TALLOC(i8, t, SZ);
while(true) {
u64 read = fread(t, 1, SZ, stdin);
if (read==0) break;
i8* ap; B a = m_i8arrv(&ap, read);
memcpy(ap, t, read);
r = vec_join(r, a);
}
TFREE(t);
return toI8Arr(r);
}

View File

@ -10,7 +10,7 @@ I8Arr* file_bytes(B path); // consumes
B file_chars(B path); // consumes B file_chars(B path); // consumes
B file_lines(B path); // consumes B file_lines(B path); // consumes
I8Arr* stdin_allBytes(); I8Arr* stream_bytes();
void file_wChars(B path, B x); // consumes path void file_wChars(B path, B x); // consumes path
void file_wBytes(B path, B x); // consumes path void file_wBytes(B path, B x); // consumes path