tvdos files append functions

This commit is contained in:
minjaesong
2022-12-30 02:35:54 +09:00
parent 6afe0ee418
commit 34def419c1
3 changed files with 76 additions and 99 deletions

View File

@@ -55,7 +55,7 @@ for (let portNo = 1; portNo < 4; portNo++) {
sys.spin()
let devname = com.sendMessageGetBytes(portNo, "DEVTYP\x17").substring(0,4)
let driver = devnameToDriver[devname]
serial.println(`port: ${portNo}, devname: ${devname} ${com.sendMessageGetBytes(portNo, "DEVNAM\x17")}`)
// serial.println(`port: ${portNo}, devname: ${devname} ${com.sendMessageGetBytes(portNo, "DEVNAM\x17")}`)
if (driver) {
let dlet = currentDriveLetter[portNo]
_TVDOS.DRIVEFS[dlet] = driver
@@ -116,7 +116,7 @@ class TVDOSFileDescriptor {
this._path = p.substring(2) // detaches $:
this._driverID = driverID
this._driver = _TVDOS.DRV.FS[driverID]
serial.println(`TVDOSFileDescriptor input path: ${path0}, p = ${p}, driveLetter = ${this._driveLetter}, path = ${this._path}`)
// serial.println(`TVDOSFileDescriptor input path: ${path0}, p = ${p}, driveLetter = ${this._driveLetter}, path = ${this._path}`)
}
}
@@ -174,6 +174,20 @@ class TVDOSFileDescriptor {
swrite(string) {
this.driver.swrite(this, string)
}
/**
* @param ptr where the byes are (offsets from the base ptr can be coded as `baseptr + offset`)
* @param count how many bytes to append
*/
pappend(ptr, count) {
this.driver.pappend(this, ptr, count)
}
bappend(bytes) {
this.driver.bappend(this, bytes)
}
sappend(string) {
this.driver.sappend(this, string)
}
flush() {
this.driver.flush(this)
@@ -302,7 +316,7 @@ _TVDOS.DRV.FS.SERIAL.pread = (fd, ptr, count, offset) => {
if (response < 0 || response >= 128) {
throw Error("Reading a file failed with "+response)
}
dma.comToRam(port[0], offset, ptr, count)
dma.comToRam(port[0], offset || 0, ptr, count || 0)
}
_TVDOS.DRV.FS.SERIAL.sread = (fd) => {
if (129 == _TVDOS.DRV.FS.SERIAL._openr(fd)) throw Error(`No such file: ${fd.fullPath}`)
@@ -318,6 +332,14 @@ _TVDOS.DRV.FS.SERIAL.sread = (fd) => {
}
return com.pullMessage(port[0])
}
_TVDOS.DRV.FS.SERIAL.bread = (fd) => {
let str = _TVDOS.DRV.FS.SERIAL.sread(fd)
let bytes = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
bytes[i] = str.charCodeAt(i)
}
return bytes
}
_TVDOS.DRV.FS.SERIAL.pwrite = (fd, ptr, count, offset) => {
if (offset) throw Error(`Write offset not supported on Serial device such as disk drives`)
@@ -325,7 +347,6 @@ _TVDOS.DRV.FS.SERIAL.pwrite = (fd, ptr, count, offset) => {
let port = _TVDOS.DRV.FS.SERIAL._toPorts(fd.driveLetter)
dma.ramToCom(ptr, port[0], count)
}
_TVDOS.DRV.FS.SERIAL.swrite = (fd, string) => {
let rrrr = _TVDOS.DRV.FS.SERIAL._openw(fd); if (rrrr != 0) throw Error("Writing a file failed with "+rrrr)
@@ -342,18 +363,36 @@ _TVDOS.DRV.FS.SERIAL.swrite = (fd, string) => {
com.sendMessage(port[0], string)
_TVDOS.DRV.FS.SERIAL._flush(port[0]);_TVDOS.DRV.FS.SERIAL._close(port[0])
}
_TVDOS.DRV.FS.SERIAL.bread = (fd) => {
let str = _TVDOS.DRV.FS.SERIAL.sread(fd)
let bytes = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
bytes[i] = str.charCodeAt(i)
}
return bytes
}
_TVDOS.DRV.FS.SERIAL.bwrite = (fd, bytes) => { // pwrite replaces DMA.ramToCom
let string = String.fromCharCode.apply(null, bytes) // no spreading: has length limit
_TVDOS.DRV.FS.SERIAL.swrite(fd, string)
}
_TVDOS.DRV.FS.SERIAL.pappend = (fd, ptr, count) => {
let rrrr = _TVDOS.DRV.FS.SERIAL._opena(fd); if (rrrr != 0) throw Error("Appending to a file failed with "+rrrr)
let port = _TVDOS.DRV.FS.SERIAL._toPorts(fd.driveLetter)
dma.ramToCom(ptr, port[0], count)
}
_TVDOS.DRV.FS.SERIAL.bappend = (fd, bytes) => { // pwrite replaces DMA.ramToCom
let string = String.fromCharCode.apply(null, bytes) // no spreading: has length limit
_TVDOS.DRV.FS.SERIAL.sappend(fd, string)
}
_TVDOS.DRV.FS.SERIAL.sappend = (fd, string) => {
let rrrr = _TVDOS.DRV.FS.SERIAL._opena(fd); if (rrrr != 0) throw Error("Appending to a file failed with "+rrrr)
let port = _TVDOS.DRV.FS.SERIAL._toPorts(fd.driveLetter)
com.sendMessage(port[0], "WRITE"+string.length)
let response = com.getStatusCode(port[0])
if (135 == response) {
throw Error("File not opened")
}
if (response < 0 || response >= 128) {
throw Error("Appending to a file failed with "+response)
}
com.sendMessage(port[0], string)
_TVDOS.DRV.FS.SERIAL._flush(port[0]);_TVDOS.DRV.FS.SERIAL._close(port[0])
}
_TVDOS.DRV.FS.SERIAL.isDirectory = (fd) => {
if (129 == _TVDOS.DRV.FS.SERIAL._openr(fd)) return false // file not exists
@@ -626,7 +665,7 @@ _TVDOS.DRV.FS.NET.sread = (fd) => {
let port = _TVDOS.DRV.FS.SERIAL._toPorts(fd.driveLetter)
let url = fd.path.substring(fd.path.indexOf("\\")+1).replaceAll("\\","/")
serial.println("NETFILE GET " + url)
// serial.println("NETFILE GET " + url)
com.sendMessage(port[0], "GET " + url)
com.waitUntilReady(port[0])

View File

@@ -17,6 +17,10 @@ const welcome_text = (termWidth > 40) ? `${osName}, version ${_TVDOS.VERSION}`
const greetLeftPad = (termWidth - welcome_text.length - 6) >> 1
const greetRightPad = termWidth - greetLeftPad - welcome_text.length - 6
function debugprintln(t) {
if (DEBUG_PRINT) serial.println(t)
}
function makeHash() {
let e = "YBNDRFG8EJKMCPQXOTLVWIS2A345H769"
let m = e.length
@@ -314,29 +318,29 @@ shell.resolvePathInput = function(input) {
}
serial.println("command.js > resolvePathInput > sanitised input: "+pathstr)
debugprintln("command.js > resolvePathInput > sanitised input: "+pathstr)
let startsWithSlash = pathstr.startsWith('/')
let newPwd = []
serial.println("command.js > resolvePathInput > path starts with slash: "+startsWithSlash)
debugprintln("command.js > resolvePathInput > path starts with slash: "+startsWithSlash)
// split them into an array while filtering empty elements except for the root 'head'
let ipwd = (startsWithSlash ? [""] : shell_pwd).concat(pathstr.split("/").filter(function(it) { return (it.length > 0); }))
serial.println("command.js > resolvePathInput > ipwd = "+ipwd)
serial.println("command.js > resolvePathInput > newPwd = "+newPwd)
debugprintln("command.js > resolvePathInput > ipwd = "+ipwd)
debugprintln("command.js > resolvePathInput > newPwd = "+newPwd)
// process dots
ipwd.forEach(function(it) {
serial.println("command.js > resolvePathInput > ipwd.forEach > it = "+it)
debugprintln("command.js > resolvePathInput > ipwd.forEach > it = "+it)
if (it === ".." && newPwd[1] !== undefined) {
newPwd.pop()
}
else if (it !== ".." && it !== ".") {
newPwd.push(it)
}
serial.println("command.js > resolvePathInput > newPwd = "+newPwd)
debugprintln("command.js > resolvePathInput > newPwd = "+newPwd)
})
// construct new pathstr from pwd arr so it will be sanitised
@@ -372,7 +376,7 @@ shell.coreutils = {
return
}
let path = shell.resolvePathInput(args[1])
if (DEBUG_PRINT) serial.println("command.js > cd > pathstr = "+path.string)
debugprintln("command.js > cd > pathstr = "+path.string)
// check if path is valid
let file = files.open(path.full)
@@ -394,8 +398,8 @@ shell.coreutils = {
let sourceFile = files.open(path.full)
let destFile = files.open(pathd.full)
serial.println(`[cp] source path: ${path.full}`)
serial.println(`[cp] dest path: ${pathd.full}`)
debugprintln(`[cp] source path: ${path.full}`)
debugprintln(`[cp] dest path: ${pathd.full}`)
if (sourceFile.isDirectory || !sourceFile.exists) { printerrln(`${args[0].toUpperCase()} failed for '${sourceFile.fullPath}'`); return 1 } // if file is directory or failed to open, IO error code will be returned
if (destFile.isDirectory) { printerrln(`${args[0].toUpperCase()} failed for '${destFile.fullPath}'`); return 1 } // if file is directory or failed to open, IO error code will be returned
@@ -489,7 +493,7 @@ shell.coreutils = {
}
let path = shell.resolvePathInput(args[1])
let file = files.open(path.full)
if (DEBUG_PRINT) serial.println("command.js > mkdir > pathstr = "+path.full)
debugprintln("command.js > mkdir > pathstr = "+path.full)
// check if path is valid
if (!file.exists) {
@@ -596,7 +600,7 @@ shell.execute = function(line) {
// TODO : if operator is not undefined, swap built-in print functions with ones that 'prints' on pipes instead of stdout
if (op == '|') {
serial.println(`Statement #${c+1}: pushing anon pipe`)
debugprintln(`Statement #${c+1}: pushing anon pipe`)
shell.pushAnonPipe('')
print = shell.stdio.pipe.print
@@ -656,7 +660,7 @@ shell.execute = function(line) {
searchPath = trimStartRevSlash(search + cmd + pathExt[j])
if (DEBUG_PRINT) {
serial.println("[command.js > shell.execute] file search path: "+searchPath)
debugprintln("[command.js > shell.execute] file search path: "+searchPath)
}
searchFile = files.open(`${CURRENT_DRIVE}:\\${searchPath}`)
@@ -735,8 +739,8 @@ shell.execute = function(line) {
// destroy pipe if operator is not pipe
if (op != "|" && op != ">>" && op != ">") {
serial.println(`Statement #${c+1}: destroying pipe`)
serial.println(`its content was: ${shell.removePipe()}`)
debugprintln(`Statement #${c+1}: destroying pipe`)
debugprintln(`its content was: ${shell.removePipe()}`)
}

View File

@@ -26,77 +26,12 @@ if (!outfilename) {
let outfile = files.open(_G.shell.resolvePathInput(outfilename).full)
const filesystem = {};
filesystem._toPorts = (driveLetter) => {
if (driveLetter.toUpperCase === undefined) {
throw Error("'"+driveLetter+"' (type: "+typeof driveLetter+") is not a valid drive letter");
}
var port = _TVDOS.DRIVES[driveLetter.toUpperCase()];
if (port === undefined) {
throw Error("Drive letter '" + driveLetter.toUpperCase() + "' does not exist");
}
return port
}
filesystem._close = (portNo) => {
com.sendMessage(portNo, "CLOSE")
}
filesystem._flush = (portNo) => {
com.sendMessage(portNo, "FLUSH")
}
filesystem.open = (driveLetter, path, operationMode) => {
var port = filesystem._toPorts(driveLetter);
filesystem._flush(port[0]); filesystem._close(port[0]);
var mode = operationMode.toUpperCase();
if (mode != "R" && mode != "W" && mode != "A") {
throw Error("Unknown file opening mode: " + mode);
}
com.sendMessage(port[0], "OPEN"+mode+'"'+path+'",'+port[1]);
return com.getStatusCode(port[0]);
}
filesystem.getFileLen = (driveLetter) => {
var port = filesystem._toPorts(driveLetter);
com.sendMessage(port[0], "GETLEN");
var response = com.getStatusCode(port[0]);
if (135 == response) {
throw Error("File not opened");
}
if (response < 0 || response >= 128) {
throw Error("Reading a file failed with "+response);
}
return Number(com.pullMessage(port[0]));
}
filesystem.write = (driveLetter, string) => {
var port = filesystem._toPorts(driveLetter);
com.sendMessage(port[0], "WRITE"+string.length);
var response = com.getStatusCode(port[0]);
if (135 == response) {
throw Error("File not opened");
}
if (response < 0 || response >= 128) {
throw Error("Writing a file failed with "+response);
}
com.sendMessage(port[0], string);
filesystem._flush(port[0]); filesystem._close(port[0]);
}
filesystem.writeBytes = (driveLetter, bytes) => {
var string = String.fromCharCode.apply(null, bytes); // no spreading: has length limit
filesystem.write(driveLetter, string);
}
function appendToOutfile(bytes) {
filesystem.open("A", outfilename, "A")
filesystem.writeBytes("A", bytes)
outfile.bappend(bytes)
}
function appendToOutfilePtr(ptr, len) {
filesystem.open("A", outfilename, "A")
dma.ramToCom(ptr, 0, len)
outfile.pappend(ptr, len)
}
// write header to the file
@@ -113,14 +48,13 @@ let headerBytes = [
let ipfFun = (IPFMODE == 1) ? graphics.encodeIpf1 : (IPFMODE == 2) ? graphics.encodeIpf2 : 0
if (!ipfFun) throw Error("Unknown IPF mode "+IPFMODE)
filesystem.open("A", outfilename, "W")
filesystem.writeBytes("A", headerBytes)
outfile.bwrite(headerBytes)
for (let f = 1; f <= TOTAL_FRAMES; f++) {
let fname = PATHFUN(f)
filesystem.open("A", fname, "R")
let fileLen = filesystem.getFileLen("A")
dma.comToRam(0, 0, infile, fileLen)
let framefile = files.open(_G.shell.resolvePathInput(fname).full)
let fileLen = framefile.size
framefile.pread(infile, fileLen)
let [_1, _2, channels, _3] = graphics.decodeImageTo(infile, fileLen, imagearea)