let File = {
read: function(path) {
let n = 0; let res = ''; let buf = 'xxxxxxxxxx'; let fp = File.fopen(path, 'r');
if (fp === null) return null;
while ((n = File.fread(buf, 1, buf.length, fp)) > 0) {
res += buf.slice(0, n);
}
File.fclose(fp);
return res;
},
remove: ffi('int remove(char *)'),
rename: ffi('int rename(char *, char *)'),
write: function(str, path, oMode) {
let fp = File.fopen(path, oMode || 'w');
if (fp === null) return 0;
let off = 0; let tot = str.length;
while (off < tot) {
let len = 5; if (off + len > tot) len = tot - off;
let n = File.fwrite(str.slice(off, off + len), 1, len, fp);
off += n;
}
File.fclose(fp);
return off;
},
fopen: ffi('void *fopen(char *, char *)'),
fclose: ffi('void fclose(void *)'),
fread: ffi('int fread(char *, int, int, void *)'),
fwrite: ffi('int fwrite(char *, int, int, void *)'),
};