2021-02-18 14:13:25 +03:00
|
|
|
const support_system_file_fs = require('fs')
|
2020-07-06 18:58:02 +03:00
|
|
|
|
2021-04-12 18:57:09 +03:00
|
|
|
|
|
|
|
function support_system_file_fileErrno(){
|
2021-07-10 13:15:21 +03:00
|
|
|
const n = process.__lasterr===undefined?0:process.__lasterr.errno || 0
|
2021-04-12 18:57:09 +03:00
|
|
|
if (process.platform == 'win32') {
|
|
|
|
// TODO: Add the error codes for the other errors
|
|
|
|
switch(n) {
|
2021-07-10 13:15:21 +03:00
|
|
|
case -4058: return 2
|
|
|
|
case -4075: return 4
|
|
|
|
default: return -n
|
2021-04-12 18:57:09 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(n){
|
2021-07-10 13:15:21 +03:00
|
|
|
case -17: return 4
|
|
|
|
default: return -n
|
2021-04-12 18:57:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
// like `readLine` without the overhead of copying characters.
|
|
|
|
// returns int (success 0, failure -1) to align with the C counterpart.
|
|
|
|
function support_system_file_seekLine (file_ptr) {
|
|
|
|
const LF = 0x0a
|
|
|
|
const readBuf = Buffer.alloc(1)
|
|
|
|
let lineEnd = file_ptr.buffer.indexOf(LF)
|
2020-07-06 18:58:02 +03:00
|
|
|
while (lineEnd === -1) {
|
2021-02-18 14:13:25 +03:00
|
|
|
const bytesRead = support_system_file_fs.readSync(file_ptr.fd, readBuf, 0, 1, null)
|
2020-07-06 18:58:02 +03:00
|
|
|
if (bytesRead === 0) {
|
2021-02-18 14:13:25 +03:00
|
|
|
file_ptr.eof = true
|
2020-07-06 18:58:02 +03:00
|
|
|
file_ptr.buffer = Buffer.alloc(0)
|
2021-07-10 13:15:21 +03:00
|
|
|
return 0
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
2021-02-18 14:13:25 +03:00
|
|
|
file_ptr.buffer = Buffer.concat([file_ptr.buffer, readBuf.slice(0, bytesRead)])
|
|
|
|
lineEnd = file_ptr.buffer.indexOf(LF)
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
2021-02-18 14:13:25 +03:00
|
|
|
file_ptr.buffer = file_ptr.buffer.slice(lineEnd + 1)
|
2021-07-10 13:15:21 +03:00
|
|
|
return 0
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
function support_system_file_readLine (file_ptr) {
|
|
|
|
const LF = 0x0a
|
|
|
|
const readBuf = Buffer.alloc(1)
|
|
|
|
let lineEnd = file_ptr.buffer.indexOf(LF)
|
|
|
|
while (lineEnd === -1) {
|
|
|
|
const bytesRead = support_system_file_fs.readSync(file_ptr.fd, readBuf, 0, 1, null)
|
|
|
|
if (bytesRead === 0) {
|
|
|
|
file_ptr.eof = true
|
|
|
|
const line = file_ptr.buffer.toString('utf-8')
|
|
|
|
file_ptr.buffer = Buffer.alloc(0)
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
file_ptr.buffer = Buffer.concat([file_ptr.buffer, readBuf.slice(0, bytesRead)])
|
|
|
|
lineEnd = file_ptr.buffer.indexOf(LF)
|
|
|
|
}
|
|
|
|
const line = file_ptr.buffer.slice(0, lineEnd + 1).toString('utf-8')
|
|
|
|
file_ptr.buffer = file_ptr.buffer.slice(lineEnd + 1)
|
|
|
|
return line
|
|
|
|
}
|
2020-07-06 18:58:02 +03:00
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
function support_system_file_getStr () {
|
|
|
|
return support_system_file_readLine({ fd: 0, buffer: Buffer.alloc(0), name: '<stdin>', eof: false })
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
function support_system_file_openFile (n, m) {
|
|
|
|
try {
|
2021-04-12 18:57:09 +03:00
|
|
|
const fd = support_system_file_fs.openSync(n, m.replace('b', ''))
|
2021-02-18 14:13:25 +03:00
|
|
|
return { fd: fd, buffer: Buffer.alloc(0), name: n, eof: false }
|
|
|
|
} catch (e) {
|
|
|
|
process.__lasterr = e
|
|
|
|
return null
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:13:25 +03:00
|
|
|
function support_system_file_chmod (filename, mode) {
|
|
|
|
try {
|
2021-07-10 13:15:21 +03:00
|
|
|
support_system_file_fs.chmodSync(filename, mode)
|
|
|
|
return 0
|
2021-02-18 14:13:25 +03:00
|
|
|
} catch (e) {
|
|
|
|
process.__lasterr = e
|
2021-07-10 13:15:21 +03:00
|
|
|
return 1
|
2020-07-06 18:58:02 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-07 12:09:30 +03:00
|
|
|
|
|
|
|
function support_system_file_removeFile (filename) {
|
|
|
|
try {
|
|
|
|
support_system_file_fs.unlinkSync(filename)
|
|
|
|
return 0
|
|
|
|
} catch (e) {
|
|
|
|
process.__lasterr = e
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|